设计级别问题:多个枚举或在 UI 代码中循环枚举以做出可见性决策?

发布于 2024-08-13 06:12:53 字数 1188 浏览 4 评论 0原文

我们就 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帥小哥 2024-08-20 06:12:53

我会稍微改变一下并使用简单的位掩码来获取权限:

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

然后每个用户类型都会获得其权限级别集(每个可用权限加在一起):

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

然后当您在 UI 中填充下拉列表时,位掩码就会出现:

comboboxTeamTypes.Items.AddRange(
    Enum.GetValues(typeof(TeamType))
        .Where(v => myLevel & v == v)
        .Select(v => Enum.GetName(typeof(TeamType), v));

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));
策马西风 2024-08-20 06:12:53

你的编程语言是什么?在 Java 中,我会告诉您创建如下所示的枚举类,但显然这不是 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;
    }
}

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文