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.