简体   繁体   English

将枚举绑定到静态数据

[英]Binding enums to static data

I'm working on an application that has to manage permissions, and I've come up with a design I wanted some feedback on. 我正在开发一个必须管理权限的应用程序,我想出了一个我想要一些反馈的设计。 I'm not sure if this is exactly the right place to post it but I guess we'll see. 我不确定这是否是发布它的正确位置,但我想我们会看到。

A user in my application can be assigned permissions. 可以为我的应用程序中的用户分配权限。 They can have permissions on an individual level, or from being part of a group. 他们可以拥有个人级别的权限,也可以拥有组的权限。 Thus I have the following tables (I think the names are relatively self-explanatory but can add further information if needed): 因此,我有以下表格(我认为名称相对不言自明,但如果需要可以添加更多信息):

  • Users 用户
  • Permissions 权限
  • User_Permissions User_Permissions
  • Groups
  • User_Groups User_Groups
  • User_Group_Permissions User_Group_Permissions

The design of the permissions table is this: 权限表的设计是这样的:

  • id int (permission id, primary key) id int(权限id,主键)
  • name varchar(255) (name of the permission, such as "ADMINISTRATION") name varchar(255)(权限的名称,例如“ADMINISTRATION”)
  • description varchar(1000) (A short description of the permission) description varchar(1000)(权限的简短描述)
  • default_value tinyint (The default value to assign to users (Yes/No/Never)) default_value tinyint(分配给用户的默认值(是/否/从不))

However, when I check the permission in code, I didn't want to use a "Magic String" so I created an enum for the permissions in my permissions table, and then use it like so: 但是,当我检查代码中的权限时,我不想使用“魔术字符串”,所以我在权限表中为权限创建了一个枚举,然后像这样使用它:

public enum EPermission
{
    ADMINISTRATION = 1,
    LOGIN = 2
}

public bool HasPermission(EPermission permission)
{
    int permission_id = (int)permission;
    //look up the permission in the database based on permission_id
}

The ids in my permissions table match the number values I've assigned in my enums. 我的权限表中的ID与我在枚举中指定的数字值相匹配。 The application has no way of changing these. 应用程序无法更改这些。 The permissions in the permissions table are inserted with those ids as part of the installation and should not be changed. 权限表中的权限将作为安装的一部分与这些ID一起插入,不应更改。

Is this kind of coupling acceptable? 这种耦合是否可以接受? I'm not expecting the data to change, but it's always possible that it could, and I'm just wondering if there might be a better way to do things before I get too deep into this. 我并不期望数据会发生变化,但它总是可能会发生变化,而我只是想知道在我深入研究之前是否有更好的方法可以做到。

Yes it is "acceptable" and a fairly common practice. 是的,它是“可接受的”,是一种相当普遍的做法。

EDIT: If you wanted to really minimize the chance that your DB and the enum will become out of sync, you can reflect the enum to seed your table with data during deployment. 编辑:如果您想真正最小化数据库和枚举不同步的可能性,您可以反映枚举在部署期间使用数据为您的表种子化。

As you do not change the Permissions you can also get away from the permissions table and only use the enum. 由于您不更改权限,因此您也可以远离权限表并仅使用枚举。 You can then store the value of the enum as an int in the User_Permissions and User_Group_Permissions tables. 然后,您可以将枚举的值作为int存储在User_Permissions和User_Group_Permissions表中。

When retrieving the data from the database you can deserialize the value to the enum. 从数据库中检索数据时,可以将值反序列化为枚举。 This reduces the possibility that the data in your database gets corrupted and does not match with your enum anymore. 这样可以降低数据库中的数据损坏并且与枚举不匹配的可能性。

I suspect that it will not help long run. 我怀疑它不会有助于长期运行。 what If a single user can have one or more permissions. 什么如果单个用户可以拥有一个或多个权限。 Then you need to switch flag enum? 那你需要切换标志枚举吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM