Spring data Jpa implement generic methods

Asked

Viewed 313 times

3

I have 3 services with these methods in common create(),deleteById(),findAll(),getById() and update().

@Service
public class AutorService {create(),deleteById(),findAll(),getById(),update(), etc...}

@Service
public class GrupoService {create(),deleteById(),findAll(),getById(),update(), etc...}

@Service
public class GeneroService {create(),deleteById(),findAll(),getById(),update(), etc...}

I intend to transform them into an interface and create the implementation with the business Logic in them.

Would it be a good practice to create an interface with these common methods to then extend them? Because every time I have to keep creating these methods.

Example:

Genericservice with the methods in common

public interface GenericService<T, I extends Serializable> {

    List<T> findAll();

    T getById(Long id);

    T create(T entity);

    T update(T entity);

    void deleteById(Long id);
}

Autorservice extend Genericservice and has its findByName method()

public interface AutorService extends GenericService<AutorEntity,Long>{

    public AutorEntity findByNome(String nome);
}

Autorserviceimpl implements Genericservice with the Autorservice method

@Service
public class AutorServiceImpl implements AutorService {/*.....*/}

2 answers

1

Surely proper use of interfaces is good practice, they will ensure that a standard is maintained throughout the project because they force the implementation of the methods defined in it. Another advantage I like very much in applying them is the documentation of the code, for example:

Let’s say you have a company where there are 3 categories of function, the salary commission for each function requires a different calculation formula, in this case I use an interface, and the documentation of it will reflect on the methods generated from the implementation of the same.

public interface Funcionario {

    /**
     * Retorna o valor da comissão recebida por determinado empregado.
     * @param taxa
     */
    BigDecimal calcularComissao(BigDecimal taxa);

}

All classes that implement this interface will receive the corresponding documentation in the generated methods. Vc ensures that documentation will also be standardized.

1


Generally, all your services will have the same crud methods, such as, save, delete, find, findAll, etc. I recommend that you create a generic interface by passing the entity type as parameter. This interface must contain the signature of these methods, and in its service(Interface) you inherit this generic interface.

For your implementation, you can create a generic abstract class that implements your generic interface and overrides the methods, and in your service(Implementation) you will inherit from this class.

Another good practice is in your abstract class, you create methods like, preSave and postSave, so anyone who wants to apply some business rule, can implement the method and write your rule without screwing up the code.

  • Hello @Felix Welcome to ptSO, Thank you for your time to contribute a response. [tour]

Browser other questions tagged

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