Do you doubt with FluentNHibernate mapping?

1

I'm trying to create a scheme to set permissions on my system and these permissions are given to the user profile, where you can choose what each profile is allowed to do in each module. For this I have created some tables and I am in doubt how to do the mapping between them, mainly in the part of permissions.

In Java I do the mappings in the entities themselves and in NHibernate I am new so I still do not quite understand the concept of how to do the mappings. My question is between relating the Profile / Module and Permission entity. In case I created an entity only for Permission where it does the Profile relationship (HasOne) and Modulo (HasMany), is this correct?

Note: When defining the permissions for the profile, I want to display all the modules in a DataGridView with the options checkbox checkbox (insert, change, search, print) and thus define the permissions of each module in the permissoes table %.

What is the best way to do this?

Entities

Profile

public class Perfil {

        public virtual int id { set; get; }
        public virtual string descricao { set; get; }

        public Perfil() {
        }
    }

Modules

public class Modulo {

        public virtual int id { set; get; }
        public virtual string nome { set; get; }
        public virtual string descricao { set; get; }

        public Modulo() {
        }

        public Modulo(string nome, string descricao) {
            this.nome = nome;
            this.descricao = descricao;
        }

    }

Permissions

 public class Permissao {

    public virtual int id { set; get; }
    public virtual Perfil perfil { set; get; }
    public virtual IList<Modulo> modulos { set; get; }
    public virtual int inserir { set; get; }
    public virtual int alterar { set; get; }
    public virtual int pesquisar { set; get; }
    public virtual int imprimir { set; get; }

    public Permissao() {
        modulos = new List<Modulo>();
    }

}

Mappings

Profile

public class PerfilMap : ClassMap<Perfil>{

        public PerfilMap() {
            Table("perfis");
            Id(p => p.id).GeneratedBy.Identity();
            Map(p => p.descricao).Unique().Not.Nullable();

        }
    }

Module

public class ModuloMap : ClassMap<Modulo>{

        public ModuloMap() {
            Table("modulos");
            Id(m => m.id).GeneratedBy.Identity();
            Map(m => m.nome).Unique().Not.Nullable();
            Map(m => m.descricao).Not.Nullable();

        }
    }

Permission

public class PermissaoMap : ClassMap<Permissao>{

        public PermissaoMap() {
            Table("permissoes");
            Id(p => p.id).GeneratedBy.Identity();
            Map(p => p.inserir).CustomType<int>();
            Map(p => p.alterar).CustomType<int>();
            Map(p => p.pesquisar).CustomType<int>();
            Map(p => p.imprimir).CustomType<int>();
            HasOne(p => p.perfil);
            HasMany(p => p.modulos).LazyLoad();
        }

    }

model

    
asked by anonymous 09.03.2016 / 05:26

1 answer

1
  

This answer has no intention of solving the problem, but rather of pointing out faults and suggesting possible alternatives.

Possible issues / failures

Based on your clarification in the comments, your model is wrong with your database, because in your model Permissoes you have:

public virtual IList<Modulo> modulos { set; get; }

And in the database in the Permissoes table you have the relation field with Modelos as an integer ( INT ):

modelos_id : INT

How would you store a list of references to Modulo table in a column of type INT ?

Suggestion

If I were to model this case, I would make the Permissoes table a relationship between Perfil and Modulo specifying the permissions for this relation:

Permissoes
--------------------------------------------
id : INT | perfil_id: INT | modulo_id : INT | insert: INT (0 = false, 1 = true) | update: INT (0 = false, 1 = true) | search: INT (0 = false, 1 = true) | print: INT (0 = false, 1 = true)

Becoming very similar to what it was previously.

And in the template I would trade for:

public class Permissao {

    public virtual int id { set; get; }
    public virtual Perfil perfil { set; get; }
    public virtual Modulo modulo { set; get; }
    public virtual bool inserir { set; get; }
    public virtual bool alterar { set; get; }
    public virtual bool pesquisar { set; get; }
    public virtual bool imprimir { set; get; }

    public Permissao() {
    }

}

And the mapping something like this:

public class PermissaoMap : ClassMap<Permissao>{

    public PermissaoMap() {
        Table("permissoes");
        Id(p => p.id).GeneratedBy.Identity();
        Map(p => p.inserir);
        Map(p => p.alterar);
        Map(p => p.pesquisar);
        Map(p => p.imprimir);
        References(p => p.perfil);
        References(p => p.modulo);
    }

}

It would also create a unique index between columns perfil_id and modulo_id , making it not allowed to add more than one permission to Perfil related to Modulo .

To illustrate how to get permissions by Perfil we could have a query similar to this:

int idPerfil = 1;

// Em lambda expression
IList<Permissao> permissoesPerfilLambda = Session.QueryOver<Permissao>()
    .Where(t => t.perfil.id == idPerfil)
    .List();

// Em LINQ
IList<Permissao> permissoesPerfilLinq = (from permissao in Session.Query<Permissao>()
                                            where permissao.perfil.id == idPerfil
                                            select permissao).ToList();

// Em SQL nativo
SELECT * FROM permissoes p WHERE p.perfil_id == 1;
    
09.03.2016 / 18:33