tags:

views:

95

answers:

2

Hi!, we have a debate about a BEST PRACTISE from a .NET architecture DESIGN POINT OF VIEW:

Task: How to manage role based visibility in UI with enum?

For example: I want to show all team types [a,b,c,d,e] to administrator but only team types [a,b,c] to normal user.

First I have enum which includes all team types:

public enum TeamType { a, b, c, d, e }

I use it like this:

if (IsAdminitrator()) 
    comboboxTeamtypes.Items.AddRange(Enum.GetNames(typeof(TeamType)));

Now I should decide how to implement else clause. Because enums can't be inherited I have two alternative approaches:

1) Should I introduce an other role specific enum:

public enum TeamTypeOthers {a, b, c }

and then I would have:

else
    comboboxTeamtypes.Items.AddRange(Enum.GetNames(typeof(TeamTypeOthers)));

2) Or should I forget creating any role specific enum TeamTypeOthers and just loop the original TeamType enum values in UI-code:

else
{
    foreach (TeamType teamtype in Enum.GetValues(typeof(TeamType)))
    {
        if (asdf == TeamType.a)
            comboboxTeamtypes.Items.Add(teamtype);
        else if (asdf == TeamType.b)
            comboboxTeamtypes.Items.Add(teamtype);
        if (asdf == TeamType.c)
            comboboxTeamtypes.Items.Add(teamtype);
    }
}

I think the first solution is nice and clean (though repetitive which is not so nice). But now I also make decision about the use of enum in deeper architecture than in solution 2 which is probably bad and supports the use of solution 2. To me solution 2 is ugly and messy because I don't like loop cultivation around the code.

A: 

What is your programming language? In Java, I would tell you to create enum class like below, but obviously this isn't Java.

public enum TeamType {
    A(false), B(false), C(false), D(true), E(true);
    private final boolean isAdmin;
    public TeamType(boolean isAdmin) {
        this.isAdmin = isAdmin;
    }
    public boolean isAdmin() {
        return this.isAdmin;
    }
}
FelixM
+1  A: 

I would change things a little and use simple bitmasking for permissions:

public enum TeamType
{
    a = 1, // or 00001
    b = 2, // or 00010
    c = 4, // or 00100
    d = 8, // or 01000
    e = 16 // or 10000
}

Then each usertype gets its permission level set (each available permission added together):

int administratorLevel = 32; // 11111
int userLevel = 7; // 00111

And then the bitmasking comes in when you populate your dropdown in the UI:

comboboxTeamTypes.Items.AddRange(
    Enum.GetValues(typeof(TeamType))
        .Where(v => myLevel & v == v)
        .Select(v => Enum.GetName(typeof(TeamType), v));
Justin Niessner
Thak you, nice approach. Maybe I expressed my point a little vaguely. The question is more like is using repetitive enums not so good coding when you can use one single enum and make decision how to use it in UI code. It means that there is more code in UI but in deeper level there is more generic or clear solution that doesn't know about it's use. If I had a class I could use inheritance to solve this kind of problem...