设计级别问题:多个枚举或在 UI 代码中循环枚举以做出可见性决策?
我们就 .NET 架构设计观点的最佳实践进行了辩论:
任务:如何使用枚举来管理 UI 中基于角色的可见性?
例如:我想向管理员显示所有团队类型 [a,b,c,d,e],但仅向普通用户显示团队类型 [a,b,c]。
首先,我有包含所有团队类型的枚举:
public enum TeamType { a, b, c, d, e }
我像这样使用它:
if (IsAdminitrator())
comboboxTeamtypes.Items.AddRange(Enum.GetNames(typeof(TeamType)));
现在我应该决定如何实现 else 子句。因为枚举不能被继承,所以我有两种替代方法:
1)我应该引入其他特定于角色的枚举:
public enum TeamTypeOthers {a, b, c }
然后我会:
else
comboboxTeamtypes.Items.AddRange(Enum.GetNames(typeof(TeamTypeOthers)));
2)或者我应该忘记创建任何特定于角色的枚举 TeamTypeOthers 并只循环原始的 TeamType 枚举值在 UI 代码中:
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);
}
}
我认为第一个解决方案很好而且很干净(虽然重复,但不太好)。但现在我也决定在比解决方案 2 更深的架构中使用枚举,这可能很糟糕,并且支持解决方案 2 的使用。对我来说,解决方案 2 丑陋且混乱,因为我不喜欢围绕代码进行循环培养。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会稍微改变一下并使用简单的位掩码来获取权限:
然后每个用户类型都会获得其权限级别集(每个可用权限加在一起):
然后当您在 UI 中填充下拉列表时,位掩码就会出现:
I would change things a little and use simple bitmasking for permissions:
Then each usertype gets its permission level set (each available permission added together):
And then the bitmasking comes in when you populate your dropdown in the UI:
你的编程语言是什么?在 Java 中,我会告诉您创建如下所示的枚举类,但显然这不是 Java。
What is your programming language? In Java, I would tell you to create enum class like below, but obviously this isn't Java.