Fluent Nhibernate with multiple tables

Asked

Viewed 533 times

3

I’m using the Fluent NHibernate to assist the ORM (replacing the manually made . hbm files) with a single class:

User class:

public class Usuario
{
    public virtual int Id { get; set; }
    public virtual string Nome { get; set; }
    public virtual string Login { get; set; }
    public virtual string Senha { get; set; }
    public virtual char Status { get; set; }

}

Mapping of the Class:

public class UsuarioMap : ClassMap<Usuario>
{
    public UsuarioMap()
    {
        Id(c => c.Id);
        Map(c => c.Nome);
        Map(c => c.Login);
        Map(c => c.Senha);
        Map(c => c.Status);
        Table("usuarios");
    }
}

Addition of mapping to Fluent NHibernate

var configMap = Fluently.Configure().Database(configDB).Mappings(c => c.FluentMappings.AddFromAssemblyOf<Mapeamento.UsuarioMap>());

Question: Now I need to add another class (Address, the User instance will have an Address property) and a Vendor class (which is not linked to any other) to Mapping, what this mapping would look like ?

New User Class:

public class Usuario
{
    public virtual int Id { get; set; }
    public virtual string Nome { get; set; }
    public virtual string Login { get; set; }
    public virtual string Senha { get; set; }
    public virtual char Status { get; set; }
    public virtual Endereco EnderecoResidencial { get; set; }
}

Address Class:

public class Endereco
{
    public virtual string Rua { get; set; }
    public virtual string Quadra { get; set; }
    public virtual string Lote { get; set; }
    public virtual string Bairro { get; set; }
    public virtual string CEP { get; set; }
    public virtual string Complemento { get; set; }
    public virtual string PontoReferencia { get; set; }
}

Supplier class:

public class Fornecedor
{
    public virtual string CNPJ { get; set; }
    public virtual string NomeFantasia { get; set; }
    public virtual string RazaoSocial { get; set; }
    public virtual string Descricao { get; set; }
}

1 answer

3


Supposing your table of usuario has a FK for the table endereco, you would have something like this:

public class UsuarioMap : ClassMap<Usuario>
{
    public UsuarioMap()
    {
        Id(c => c.Id);
        Map(c => c.Nome);
        Map(c => c.Login);
        Map(c => c.Senha);
        Map(c => c.Status);
        References(c => c.EnderecoResidencial).Column("endereco_id");
        Table("usuarios");
    }
}

Your class endereco must have an ID:

public class Endereco
{
    public virtual int Id { get; set; }
    public virtual char Rua { get; set; }
    public virtual char Quadra { get; set; }
    public virtual char Lote { get; set; }
    public virtual char Bairro { get; set; }
    public virtual char CEP { get; set; }
    public virtual char Complemento { get; set; }
    public virtual char PontoReferencia { get; set; }
}

And its mapping of endereco:

public class EnderecoMap : ClassMap<Endereco>
{
    public EnderecoMap()
    {
        Id(c => c.Id);
        Map(c => c.Rua);
        Map(c => c.Quadra);
        Map(c => c.Lote);
        Map(c => c.Bairro);
        Map(c => c.CEP);
        Map(c => c.Complemento);
        Map(c => c.PontoReferencia); 
        Table("enderecos");
    }
}

I recommend using the LazyLoad() in the mapping of the entity usuario, in order to prevent any time you make a select of one or more usuario, come all Addressee entity. If you want, I edit the response with an example of LazyLoad().

EDIT:

Like the Fornecedor has no relation to any class, would be only "one more" in the Assembly of the Mapeamento.UsuarioMap:

public class FornecedorMap : ClassMap<Fornecedor>
{
    public FornecedorMap()
    {
        Map(c => c.CNPJ);
        Map(c => c.NomeFantasia);
        Map(c => c.RazaoSocial);
        Map(c => c.Descricao);
        Table("fornecedor");
    }
}

EDIT 2:

EXAMPLE OF USE WITH LazyLoad(): On all the maps you want to use LazyLoad(), just add it as example below.

public class UsuarioMap : ClassMap<Usuario>
{
    public UsuarioMap()
    {
        Table("usuarios");
        LazyLoad();
        Id(c => c.Id);
        Map(c => c.Nome);
        Map(c => c.Login);
        Map(c => c.Senha);
        Map(c => c.Status);
        References(c => c.EnderecoResidencial).Column("endereco_id");
    }
}

In this case, when you query a user list, for example:

using (var session = NHibernateSession.OpenSession())
{    
    var listaUsuarios = session.Query<Usuario>();
}

You will have the propriedades of user filled in query and ONLY the entity foreign key Endereco. In case you NEED the entity TODA, just give a Fetch in the entity:

using (var session = NHibernateSession.OpenSession())
{    
     var listaUsuarios = session.Query<Usuario>().Fetch(x=>x.EnderecoResidencial);
}

There besides the properties, you will also have the whole entity you are giving fetch.

  • Marllonnasser would look like: var configMap = Fluently.Configure(). Database(configDB). Mappings(c => c.FluentMappings.Addfromassemblyof<Mapping.Usuariomap>()); together with the third and isolated Supplier class ?

  • Fornecedor would it have any relation with the other classes? If not, it’s just another class to be mapped by Fluent.. In other words, you don’t have to do anything if this class is mapped in the same assemby Mapeamento.UsuarioMap

  • Marllonnasser created the mappings in different files

  • I edited the answer, Ricardo, that’s it?

  • Marllonnasser It would be but I wanted to see how it would look: var configMap = Fluently.Configure().Database(configDB).Mappings(c => c.FluentMappings.AddFromAssemblyOf<Mapeamento.UsuarioMap>()); with the three classes (Address related to User and Supplier that has no relation to any)

  • Ahh.. does not need to modify anything... the Luent itself understands that it will look at all classes that derive from the class ClassMap of the same "project"...

  • OK Marllonnasser then because I had to add this User map?

  • Marllonnasser could also insert the example with Lazyload?

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.