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.
If not even you can explain why this interface should exist, it is a strong sign that it should not exist.
– Fabri Damazio
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.
– Fabri Damazio