Doubt Interface Asp.net mvc

Asked

Viewed 153 times

-1

Good night, I have a doubt, I chose to work with Generica interface and my doubt is: Every entity needs to have an interface inheriting from the Generica interface ? Ex:

public interface IPessoaRepositorio : IRepositorio<Pessoa>

Or I create this person interface only if you have something unique from that entity to create ?

and in my repository I created so:

public class PessoaRepositorio : Repositorio<Pessoa>, IPessoaRepositorio

I needed to create anyway or just inherit only from Repositorio ?

Thanks in advance.

  • 2

    If not even you can explain why this interface should exist, it is a strong sign that it should not exist.

  • 2

    I’m sorry if I seemed inelegant, but that’s the idea. If you can’t see why to create then you don’t have to create.

1 answer

1


You need to know to want to server the Interface,

The methods form the object’s interface with the outside world; the buttons on the front of your television set, for example, are the interface between you and the electrical wiring on the other side of the plastic box. You press the "power" button to turn the TV on and off.

In its most common form, an interface is a group of methods related to empty bodies. The behavior of a Person in a system, if specified as an interface, may appear as follows:

public interface IRepositorioPessoa
{
    void Salvar(Pessoa pessoa); 
    //.....  Todo ....     
}

A class that inherits this interface will have to implement its Save method. is like a contract.

In the DDD architecture interfaces are mainly used for decoupling since you can use an interface is not needing to know who to implement it or how it is implemented.

In your doubt, you refer to generic interface, they are used when you have a pattern by the class that inherits it.

For example;

public interface IRepositorioPessoa
{
    void Salvar(Pessoa pessoa); 
    void Editar(Pessoa pessoa);
    void Excluir(int id);
    Pessoa Buscar(int id);  
}

public Class Pessoa : IRepositorioPessoa
{
   public void Salvar(Pessoa pessoa)
   {
      // Todo
   }
}

Here the class that implements the Irepositoriopessoa knows that to receive a personal class.

Imagine a system that implements the same method above for several classes (Person, Cost Center, Account ...), you could create a generic interface that has these methods not needing to duplicate creating other interfaces.

public interface IRepositorio<T> where T : class
{
    void Salvar(T entidade);
    void Editar(T entidade);
    void Excluir(int id);
    T Buscar(int id);
}

public Class Pessoa : IRepositorio<Pessoa>
{
  public void Salvar(Pessoa pessoa)
   {
      // Todo
   }
}

public Class CentroCusto : IRepositorio<CentroCusto>
{
  public void Salvar(CentroCusto centroCusto)
   {
      // Todo
   }
}

Here you need to tell which class is implemented the interface.

for a pattern you can use generic interfaces without problem, Now imagine a scenario where your interface has specific methods of an object a generic interface would not be a good one, because all classes that inherit will have to implement a particular method of an object making no sense to them.

In this case using a proper interface for the object would be more appropriate.

For example; Let’s go back to Irepositoriopessoa interface, imagine you need a new method AdicionarFilho(Pessoa pessoa)

public interface IRepositorioPessoa
{
    void Salvar(Pessoa pessoa); 
    void Editar(Pessoa pessoa);
    void Excluir(int id);
    Pessoa Buscar(int id);  
    void AdicionarFilho(Pessoa pessoa);
}

The Centrocusto class would not have made sense to implement a AdicionarFilho, so it would not be appropriate to create such a generic interface.

  • I get it, but in that case I couldn’t implement the Direct Adder method in Repositoriopessoa and this repository person inherit from the Irepositorio "Generico" to implement the common methods, instead of having to create an Irepositoriopessoa only with the void method Addirfilho(Person person) and the repositorioPessoa inherit from the Irepositorio person who ultimately inherits from Irepositorio, could not understand me...

  • @Desalex. Yes ... you can create the Irepositoriopessoa only with the Add-Info void method (Person person) and inherit the generic Irepositorio. and finally implement everything in the repositoriopessoa

  • I don’t think you understand, I told you to implement the Add Resource method on the Repository ?

  • Yes, you can, but you would lose the power of decoupling.

Browser other questions tagged

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