doubts generic class Asp.net mvc

Asked

Viewed 38 times

1

Good night,

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 this way or if I should declare Irepositorio to take the direct Repositorio since the Repositorio inherits from Irepositorio, And also whether I could instead declare the property already inherited from it as in the example:

public class PessoaAplicacao : Repositorio<Pessoa>

1 answer

0

I wonder if it’s really right to declare this property and this builder this way or if I should instead declare Irepositorio get the Repositorio straight since the Repositorio inherits from Irepositorio.

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

It is still worth mentioning that it is likely that there is a dependency injection component behind it that needs the interface to work properly.

and also whether I could instead declare the property already inherited from it as in the example:

No, in your example you are saying that Personal Application has an implementation of Irepositorio as a property, since it is not an obligation of the application to persist data. inserir a descrição da imagem aqui

Should you do public class PessoaAplicacao : Repositorio<Pessoa>, is saying that Application is the son of Repositorio, so it has all the characteristics of Repositorio and becomes responsible for persisting the data. See that there’s a difference. inserir a descrição da imagem aqui

The modeling became very simple and there may be problems, feedback from the community is always welcome.

  • It would not be the case to inherit the interface?

  • I don’t understand, inherit the interface?

  • but instead of me declaring a private property readonly Irepositorio<Person> repositorio; thus, seeking from Irepositorio, could not directly refer to Repositorio, instead of the interface ?

  • The reference is Irepositorio, but actually behind there is a component that will be doing a new Repositorio<Person>(). he will not be "seeking" from Irepositorio.

Browser other questions tagged

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