FluentNHibernate and Enums

I have an enum called Permissions. Permissions may be assigned to a user, or roles may be assigned permissions, and a role may be granted to a user.

The user and role have a property similar to this:

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

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

public static bool UserHasPermission(Permission.DeleteUser)

Now I have about 50 different permissions in my enumeration. It would be nice if I didn't have to populate the mirrors dataset in the database. My listing is as follows:

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

        ...

}

I currently have a permissions table which is PermissionId (int) and PermissionName (varchar (50)).

Here are my role side tables. The user side is exactly the same as the permissions:

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)
)

tble , , , id RolePermissions, Permissions .

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

    CONSTRAINT PK_Permissions PRIMARY KEY CLUSTERED (PermissionId)
)
  • ?
  • , Permissions, UserPermissions RolePermissions PermissionId int enum?
  • Permissions, , nhibernate Permissions ?

, , - :

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

, :

RolePermissions unmapped : GotRoleplay.Core.Domain.Model.Permission

? fluentnhibernate, , ?

+3
2

, , FluentNHibernate google, , . , , , .

, , , . , , . , enum , , . PermissionId int . , , , ASP.NET, , , .

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,
    }

Permission. int . , id PermissionCode.

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

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

, User and Role, Permission, , bool UserHasPermission (PermissionCode.DeleteUser);

global.asax, Application_Start, webconfig , , . , , , , , . , , .

, , , . - ?

0

, , enum ? , , :

public class Toy {
    public virtual int Id { get; private set; }
    public virtual string Name { get; set; }
    public virtual ToyCondition Condition { get; set; }
}

public enum ToyCondition {
    New,
    Used
}

public class ToyMap : ClassMap<Toy> {
    public ToyMap() {
        Id(x => x.Id);
        Map(x => x.Name);
        Map(x => x.Condition).CustomTypeIs(typeof(ToyCondition));
    }
}

, Condition, , ToyCondition.

Toy newToy = new Toy();
newToy.Condition = ToyCondition.New;
toyRepository.Save(newToy);

Condition int. , . , , .

: , , , - , . , .: (

0

Source: https://habr.com/ru/post/1710213/


All Articles