doubts generic class asp.net mvc

1

Good evening,

In my project I have an application layer where the class is structured as follows:

namespace ProjetoTreino.Aplicacao {
public class PessoaAplicacao 
{
    private readonly IRepositorio<Pessoa> repositorio;

    public PessoaAplicacao(IRepositorio<Pessoa> rep)
    {
        repositorio = rep;
    }

    public void Excluir(Pessoa entidade)
    {
        repositorio.Excluir(entidade);
    }

    public void Inserir(Pessoa entidade)
    {
        repositorio.Inserir(entidade);
    }

    public Pessoa ListarPorId(int id)
    {
        return repositorio.ListarPorId(id);
    }

    public IEnumerable<Pessoa> ListarTodos()
    {
        return repositorio.ListarTodos();
    }

    public void Update(Pessoa entidade)
    {
        repositorio.Update(entidade);
    }
} }

I wanted to know if it is really right to declare this property and this constructor in this way or if I should instead declare IRepository to get the Direct Repository since the Repository inherits from IRepository, and also if I could instead declare the property ja inherit from it as in the example:

public class PessoaAplicacao : Repositorio<Pessoa>
    
asked by anonymous 29.01.2018 / 03:12

1 answer

0
  

I wanted to know if it's really right to declare this property and this   constructor in this way or should I instead of declaring   IRepository get the Direct Repository since the Repository inherits from   IRepository.

You could instantiate directly the Repository. However, usually following the DIP standard, it is recommended to use this form that you exemplify.

It's still worth mentioning that there is probably a dependency injection component behind that that requires the interface to work properly.

  

and also if I could instead declare the property already inherit from it   as in the example:

No, in your example you are saying that PersonApplication has an IRepository implementation as a property, since there is no obligation for the application to persist data.

IfyoumakepublicclassPessoaAplicacao:Repositorio<Pessoa>,itsaysthatPersonApplicationisachildofRepository,soithasallthecharacteristicsofRepositoryandisresponsibleforpersistingthedata.Seethatthereisdifference.

The modeling was very simple and there could be problems, community feedback is always welcome.

    
29.01.2018 / 13:29