简体   繁体   English

使用Enum Flags如何?

[英]Using Enum Flags How to?

Currently I have 4 permissions in my website 目前我的网站有4个权限

Edit
Delete
Add
View

Everyone gets view permissions when someone subscribes to someone else. 当有人订阅其他人时,每个人都获得查看权限。

Currently in I store each as a column in my database as a bit value. 目前我将每个作为一个列存储在我的数据库中作为位值。

I am thinking maybe I should use enum with flags but I have some questions. 我想也许我应该使用enum with flags但我有一些问题。

  1. The user will be able to choose the users permissions(eveyone gets view you cannot take that away). 用户将能够选择用户权限(eveyone获取视图,你不能拿走它)。 Right now it is easy as I use a checkbox and then save the bool value to the db. 现在,我很容易使用复选框,然后将bool值保存到db。

My questions is what is the best way to convert checkboxes to the enum type? 我的问题是将复选框转换为枚举类型的最佳方法是什么? So if they choose add permission I can map that to the right enum type. 因此,如果他们选择添加权限,我可以将其映射到正确的枚举类型。

Wondering if there is a better way then 想知道是否有更好的方法

PermissionTypes t;
if(add == true)
{
   t = PermissionTypes.Add
}

if(edit == true)
{
   t += PermissionTypes.Edit // not sure if you even can do this.
}

I know with enum flags you can use the pipe and do something like this 我知道使用枚举标志你可以使用管道并做这样的事情

PermissionTypes t = PermissionTypes.Add | PermissionTypes.Edit

I am wondering is it good practice to have another type in my enum like 我想知道在我的枚举中有另一种类型的好习惯

AddEdit = Add | Edit

I am thinking that can result in many combinations(imagine if I had like 10 permissions or something like that). 我认为这可以导致许多组合(想象一下,如果我有10个权限或类似的东西)。

Enums are simply integers values. 枚举只是整数值。 By settings your enum values to different power of 2, you can easily have any combination you need: 通过将枚举值设置为2的不同幂,您可以轻松地获得所需的任何组合:

enum ePermissions
{

   None = 0,
   View = 1,
   Edit = 2,
   Delete = 4,
   Add = 8,
   All = 15
}

ePermissions t;
t = (ePermissions)((add?ePermissions.Add:ePermissions.None) | (delete?ePermissions.Delete:ePermissions.None) | ...);

if you want to know if some user has the Add permission just do as follow: 如果您想知道某些用户是否具有添加权限,请执行以下操作:

canAdd = (currentPermission & ePermissions.Add) != 0;

On converting set of bool values to enum - for small set if would be ok. bool值的集合转换为枚举 - 对于小集合, if可以的话。 You can also use ?: operator to do the same in smaller number of lines: 您也可以使用?:运算符在较少的行中执行相同的操作:

[Flags]
enum Permissions 
{ 
   None = 0, 
   View = 1, 
   Edit = 2, 
   Delete = 4, 
   Add = 8, 
}

var permissions = 
  (add ? Permissions.Add : Permissions.None) |
  (edit ? Permissions.Edit : Permissions.None) |
  (delete ? Permissions.Delete : Permissions.None);

If you have large number of values some sort of mapping of Control to Enum may be useful. 如果您有大量值,则控件与枚举的某种映射可能很有用。

On AddEdit - this particular on is probably not useful. AddEdit - 这个特殊的东西可能AddEdit You can as easily write Add | Edit 您可以轻松编写Add | Edit Add | Edit . Add | Edit Some menaingful combinations of permissions (or falgs) may be named specifically if code will use such combination in meanigfull way. 如果代码将以有意义的方式使用这种组合,则可以具体命名一些具有权限的权限组合(或falgs)。 Ie CanModify = Add | View CanModify = Add | View CanModify = Add | View . CanModify = Add | View

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

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