Duvida Interface asp.net mvc [closed]

0

Good evening, I have a doubt, I chose to work with generic interface and my doubt is: Does every entity need to have an interface inheriting from the generic interface? Ex:

public interface IPessoaRepositorio : IRepositorio<Pessoa>

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

And in my repository I created it like this:

public class PessoaRepositorio : Repositorio<Pessoa>, IPessoaRepositorio

Did you have to create it anyway or did you just inherit from Repository?

Thank you in advance.

    
asked by anonymous 29.01.2018 / 01:19

1 answer

1

You need to know to want to use the Interface,

Methods form the interface of the object 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 television on and off.

In its most common form, an interface is a group of methods related to empty bodies. The behavior of a Person on 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. it's like a contract.

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

In your doubt, you refer to the 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 IRepository knows that receiving a class of the type people.

Imagine a system that implements the same method as above for several class (Person, Center Cost, 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 in the interface.

For a standard you can use the generic interfaces with no 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 the 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 the IRepository interface, imagine that 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 make sense to implement a AdicionarFilho , so it would not be appropriate to create this method as a generic interface.

    
31.01.2018 / 13:01