3
People in my projects .Net MVC
use the structure below. My projects, are with Entity Framework
.
The briefcase Entidades
, are the classes I use in DbContext
.
Detailing the problem
I have the class Cliente
, that has ForeignKey
for the class Pessoa
.
Not to have to DbContext.Cliente.Include("Pessoa")
, created the PessoaDTO
which is a class with some class fields Pessoa
and how PessoaDTO
has a Interface
I can use it in other classes like Filial
and the like.
And this way, I can ride a ViewPartial
, to display the person’s data in a "generic" and several Views
.
Doubts
- There is something more intelligent and performative for the above structure?
- Use
DTOs
they may have methods?
Example of use
public ActionResult Detalhar(int id)
{
using (var db = new Conexao())
{
var registro = db.Cliente.Find(id);
registro.DTO();
return View(registro);
}
}
Sources of classes
Classe Pessoa
public class Pessoa : IEntidadeBase, IFixo
{
[Key]
public int PessoaID { get; set; }
[Obrigatorio]
[Display(Name = "Tipo de Pessoa")]
public int TipoPessoaID { get; set; }
[Obrigatorio]
[Display(Name = "Sexo")]
public int SexoID { get; set; }
[Display(Name = "CPF/CNPJ")]
public string CNPJCPF { get; set; }
[Display(Name = "Identificação Interna / Matrícula")]
public string IdentificacaoInterna { get; set; }
[Obrigatorio]
[Display(Name = "Nome/Nome Fantasia")]
public string Nome { get; set; }
Client class
public class Cliente : IEntidadeBase, IFixo, IPessoaDTO
{
[Key]
public int ClienteID { get; set; }
public int PessoaID { get; set; }
[ForeignKey("PessoaID")]
public virtual Pessoa Pessoa { get; set; }
[NotMapped]
public PessoaDTO PessoaDTO { get; set; }
public void DTO()
{
if (Pessoa != null)
{
PessoaDTO = new PessoaDTO();
PessoaDTO.Popular(Pessoa);
}
}
}
Classe Pessoadto
public class PessoaDTO
{
public string Nome { get; set; }
[Display(Name = "CNPJ/CPF")]
public string CNPJCPF { get; set; }
[Display(Name = "Tipo de Pessoa")]
public string TipoDePessoa { get; set; }
public string Sexo { get; set; }
public string Rotulo { get; set; }
public void Popular(Pessoa registro)
{
Nome = registro.Nome;
CNPJCPF = registro.CNPJCPF;
TipoDePessoa = registro.TipoPessoaID == 1 ? "Física" : registro.TipoPessoaID == 2 ? "Juridica" : String.Empty;
Sexo = registro.SexoID == 1 ? "Masculino" : registro.SexoID == 2 ? "Feminino" : "Não aplicável";
Rotulo = String.Format("{0} [{1}]", Nome, CNPJCPF);
}
}
I don’t know if it’s the intention, but you want to use the DDD or "Domain", it’s just the same name, because if you’re considering the DDD, you couldn’t associate your entities with Dtos, maintain infrastructure dependency, have [KEY] annotations etc in the entities, or even access the dbcontext directly from the controller since you have the repositories, but if it really makes a difference to you, you could create DTOS, MODELS VIEW or DYNAMICS and use the micro ORM to make the custom querys for the output you want. Patterns in this case would be partially CQRS.
– Rafael