12
Good use the following design pattern:
Where the Interface will communicate only with the application and this will communicate with the Repository. Thus the interface will have no restrictions and no knowledge of how the communication occurs with the database. In my case I use the Entity Framework, but in this scheme I can easily be using other communication methods.
The Application has two classes for each Domain class:
public class CartasAplicacao
{
private readonly IRepositorio<Cartas> repositorio;
public CartasAplicacao(IRepositorio<Cartas> repo)
{
repositorio = repo;
}
public void Salvar(Cartas carta)
{
repositorio.Salvar(carta);
}
public void Excluir(Cartas carta)
{
repositorio.Excluir(carta);
}
public IEnumerable<Cartas> ListarTodos()
{
return repositorio.ListarTodos();
}
public Cartas ListarPorId(string id)
{
return repositorio.ListarPorId(id);
}
}
And:
public class CartasAplicacaoConstrutor
{
public static CartasAplicacao CartasAplicacaoEF()
{
return new CartasAplicacao(new CartasRepositorioEF());
}
}
In Repositorioef I do the following:
public DbSet<SBE_ST_BannerRotativo> BannerRotativo { get; set; }
public DbSet<Cartas> Cartas{ get; set; }
public Contexto()
: base("BancoDados")
{
Database.SetInitializer<Contexto>(null);
}
And:
public class CartasRepositorioEF: IRepositorio<Cartas>
{
private readonly Contexto contexto;
public CorpoDocenteRepositorioEF()
{
contexto = new Contexto();
}
public void Salvar(Cartas entidade)
{
if (entidade.Id > 0)
{
var cartaAlterar = contexto.Cartas.First(x => x.Id == entidade.Id);
cartaAlterar.Descricao = entidade.Descricao;
cartaAlterar.Imagem = entidade.Imagem;
cartaAlterar.Nome = entidade.Nome;
}
else
{
contexto.CorpoDocente.Add(entidade);
}
contexto.SaveChanges();
}
public void Excluir(Cartas entidade)
{
var cartaAlterar = contexto.Cartas.First(x => x.Id == entidade.Id);
contexto.Set<Cartas>().Remove(cartaAlterar );
contexto.SaveChanges();
}
public IQueryable<Cartas> ListarTodos()
{
return contexto.Cartas;
}
public Cartas ListarPorId(int id)
{
return contexto.Cartas.FirstOrDefault(x => x.Id == id);
}
}
Then to use in the Interface:
var bdcarta = CartasAplicacaoConstrutor.CartasAplicacaoEF();
bdcarta.Salvar(carta);
My questions are about what the pros and cons about this design model are. If there are other patterns that are "better" than this.
I believe that the tags [software-engineering] or [software-architecture] are more suitable than [design-Pattern]
– gmsantos
@gmsantos, edited, thank you!
– Diego Zanardo