FluentNHibernate和枚举 [英] FluentNHibernate and Enums

查看:118
本文介绍了FluentNHibernate和枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Permissions的枚举。可以为用户分配权限,或者可以将权限分配给角色,用户可以获得角色。



用户和角色都具有如下属性: / p>

  public virtual IList< Permission>权限{get;组; 

我想使用一个枚举的权限,所以在我的代码中,我可以做一些像

  public static bool UserHasPermission(Permission.DeleteUser)

现在我在我的枚举中有大约50个不同的权限。如果我不必在数据库中填充镜像数据集,那将是很好的。我的枚举如下所示:

  public enum Permission 
{
//网站权限1-99
[StringValue(查看用户)]
ViewUser = 1,

[StringValue(添加用户)]
AddUser = 2,

[StringValue(编辑用户)]
EditUser = 3,

[StringValue(删除用户)]
DeleteUser = 4

...

}

目前,我有一张表PermissionsId(int)和PermissionName(varchar(50))的权限。



这是我角色角色的表格。用户端与权限相同:

  CREATE TABLE dbo.Roles 

RoleId int IDENTITY(2,1)NOT NULL,
RoleName varchar(50)NOT NULL,

CONSTRAINT PK_Roles PRIMARY KEY CLUSTERED(RoleId)


CREATE TABLE dbo.RolePermissions

RolePermissionId int IDENTITY(1,1)NOT NULL,
RoleId int NOT NULL,
PermissionId int NOT NULL,

CONSTRAINT PK_RolePermissions PRIMARY KEY CLUSTERED(RolePermissionId),
CONSTRAINT FK_RolePermissions_Roles FOREIGN KEY(RoleId)REFERENCES角色(RoleId),
CONSTRAINT U_RolePermissions UNIQUE(RoleId,PermissionId)

然后,我有一个权限,以防我需要它,但我只是不知道如何映射R中的id字段olePermissions或Permissions表返回到枚举。

  CREATE TABLE dbo.Permissions 

PermissionId int NOT NULL,
PermissionName varchar(50)NOT NULL,

CONSTRAINT PK_Permissions PRIMARY KEY CLUSTERED(PermissionId)




  1. 我可以将枚举映射到表吗?我应该甚至映射它,或者我应该只是取出Permissions表,并在UserPermissions和RolePermissions中离开PermissionId只是一个int并映射到枚举?

  2. 如果我保留Permissions表,是否有一种方法让nhibernate从枚举中的数据自动填充Permissions表中的数据?

现在,我没有映射到权限枚举,除了这样的东西:

  HasManyToMany(x => x.Permissions)
.WithParentKeyColumn(RoleId)
.WithChildKeyColumn(PermissionId)
.WithTableName(RolePermissions)
.LazyLoad()
.Cascade.All();

不幸的是,这会导致错误:


表中的一个关联
RolePermissions是指未映射的
类:
GotRoleplay.Core.Domain.Model.Permission


我在枚举中做错了什么?当枚举是对象上的值列表而不仅仅是一个值时,是否有一个标准的方法或最佳实践来使用fluentnhibernate?

解决方案

所以,在与其他开发人员谈话几天之后,在网上挖掘,并在谷歌上发布到FluentNHibernate组,我发现目前不完全支持枚举集合。如果有办法做我想要做的事情,那么这是没有记录或解决办法的。



所以,我回到绘图板,我在做什么真的想到了。基本上,我唯一的选择是使用一个类来获得我的权限。但是,我想在代码中使用我的枚举,那就是它打我的时候。 PermissionId字段可以从int转换为枚举,反之亦然。另外,使用一些在使用内置ASP.NET提供程序的枚举枚举之前做过的逻辑,我可以编写一个管理工具,根据我的枚举中的内容创建权限。



首先,我更名为我的枚举。

  public enum PermissionCode 
{
//网站权限1-99
[StringValue(查看用户)]
ViewUser = 1,

[StringValue(添加用户)]
AddUser = 2,

[StringValue(编辑用户)]
EditUser = 3,

[StringValue(删除用户)]
DeleteUser = 4,
}

然后,我创建了一个新的Permission类。这个类镜像数据库,只是一个int id和一个字符串名称。但是,我添加了一个属性将该ID转换为我的PermissionCode枚举。

  public class Permission 
{
public virtual int PermissionId {get;组; }
public virtual string PermissionName {get;组; }

public virtual PermissionCode PermissionCode
{
get
{
return(PermissionCode)PermissionId;
}
}
}

然后,在我的用户和角色对象,我参考权限,我仍然可以做一些匹配,如bool UserHasPermission(PermissionCode.DeleteUser);



然后在global.asax中,在Application_Start上,我将检查webconfig用于指示是否需要重建权限的标志。这样,只有当我需要时,才能启用重建,而无需获得进入并可能需要处理错误的权限。这意味着我只在我的枚举中保留了一个单一的权限列表,这是理想的。



认为,整体方法并不像我想要的那样光滑,但它的工作原理。有人有其他建议吗?


I have an enum called Permissions. A user can be assigned permissions, or permissions can be asigned to a role and the user can be given a role.

User and Role both have a property like this:

public virtual IList<Permission> Permissions { get; set; }

I want to use an enum for Permissions so in my code I can do things like

public static bool UserHasPermission(Permission.DeleteUser)

Right now I have about 50 different permissions in my enum. It would be nice if I didn't have to populate a mirror set of data in the database. My enum looks like this:

public enum Permission
    {
        //site permissions 1-99
        [StringValue("View Users")]
        ViewUser = 1,

        [StringValue("Add User")]
        AddUser = 2,

        [StringValue("Edit User")]
        EditUser = 3,

        [StringValue("Delete User")]
        DeleteUser = 4

        ...

}

Currently, I have a table for Permissions that is the PermissionId (int) and PermissionName (varchar (50)).

Here are my tables on the roles side. The user side is exactly the same as far as permissions go:

CREATE TABLE dbo.Roles
(
    RoleId                      int                 IDENTITY(2,1) NOT NULL,
    RoleName                    varchar (50)        NOT NULL,

    CONSTRAINT PK_Roles PRIMARY KEY CLUSTERED (RoleId)
)

CREATE TABLE dbo.RolePermissions
(
    RolePermissionId            int                 IDENTITY(1,1) NOT NULL,
    RoleId                      int                 NOT NULL,
    PermissionId                int                 NOT NULL,

    CONSTRAINT PK_RolePermissions PRIMARY KEY CLUSTERED (RolePermissionId),
    CONSTRAINT FK_RolePermissions_Roles FOREIGN KEY (RoleId) REFERENCES Roles(RoleId),
    CONSTRAINT U_RolePermissions UNIQUE(RoleId, PermissionId)
)

Then, I have a permissions tble in case I need it, but I just don't get how to map either the id field in RolePermissions or the Permissions table back to the enum.

CREATE TABLE dbo.Permissions
(
    PermissionId                    int                 NOT NULL,
    PermissionName                  varchar (50)        NOT NULL,

    CONSTRAINT PK_Permissions PRIMARY KEY CLUSTERED (PermissionId)
)

  1. Can I map the enum to the table?
  2. Should I even map it, or should I just take out the Permissions table and in UserPermissions and RolePermissions leave PermissionId just an int and map the into to the enum?
  3. If I keep the Permissions table, is there a way for nhibernate to autopopulate the data in the Permissions table from the data in the enum?

Right now, I have no mapping to the permission enum, other than something like this:

HasManyToMany(x => x.Permissions)
                 .WithParentKeyColumn("RoleId")
                 .WithChildKeyColumn("PermissionId")
                 .WithTableName("RolePermissions")
                 .LazyLoad()
                 .Cascade.All();

Unfortunately, this causes an error:

An association from the table RolePermissions refers to an unmapped class: GotRoleplay.Core.Domain.Model.Permission

What am I doing wrong with enums? Is there a standard way or best practice for using them with fluentnhibernate when the enum is a list of values on the object and not just a single value?

解决方案

So, after a few days of talking to other developers, digging around online, and posting to the FluentNHibernate group on google, I have discovered that collections of enums are currently not exactly supported. If there is a way to do what I want to do, well, it's undocumented or a workaround.

So, I went back to the drawing board with what I was doing and really thought about it. Basically, my only choice was to use a class for my permissions. But, I want to use my enum in code, and that's when it hit me. The PermissionId field can be converted from an int to the enum and vice versa. Also, using some logic I have done before when using an enum of roles with the built in ASP.NET provider, I can write an administrative tool that will build permissions based on what is in my enum.

First, I renamed my enum.

public enum PermissionCode
    {
        //site permissions 1-99
        [StringValue("View Users")]
        ViewUser = 1,

        [StringValue("Add User")]
        AddUser = 2,

        [StringValue("Edit User")]
        EditUser = 3,

        [StringValue("Delete User")]
        DeleteUser = 4,
    }

Then, I created a new Permission class. This class mirrors the database and is simply an int Id and a string name. However, I added one property to convert that id into my PermissionCode enum.

public class Permission
    {
        public virtual int PermissionId { get; set; }
        public virtual string PermissionName { get; set; }

        public virtual PermissionCode PermissionCode 
        {
            get
            {
                return (PermissionCode)PermissionId;
            }
        }
    }

Then, in my User and Role objects, I reference Permission, and I can still do matchups like bool UserHasPermission(PermissionCode.DeleteUser);

Then in global.asax, on Application_Start I will check the webconfig for a flag that will indicate if the permissions need to be rebuilt. This way I can enable the rebuild only when I need to without having to have a permission to get in and possibly needing to deal with an error. This means I only maintain a single list of permissions in my enum, which is ideal.

Granted, the overall approach isn't as slick as I wanted, but it works. Does anyone have any other suggestions?

这篇关于FluentNHibernate和枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆