Spring Rest - Use of Various Services and Repositories

Asked

Viewed 53 times

3

I have a question about how best to implement some services with the Spring Rest, but I couldn’t find the material for this case:

Let’s say I have a launch service (Lancamentoservice). This service makes the launch, updates the balance and charges a fee. Soon, I have injected the repositories Lancamentorepository, Saldorepository and Tarifarepository.

In addition to this service, it would also have services to serve, transfer, etc., and all of these would need to make a launch, in addition to other things. To do this I’m injecting the Lancamentoservice within each of these services.

However, even if I call a service on Saqueservice, for example, that would not need to make launches, the LancamentoService is "loaded", along with all its repositories.

Is that right? If someone has a better practice or some reference material I really appreciate it.

1 answer

3


Early in the application, Spring already sweeps its classes in search of Beans to be managed by him. This means that even in classes in which your program has not even passed yet, the Beans are already, in theory, instantiated and managed, ready for use. In other words, any bean injected into your class, even if not in use, will be previously instantiated. It is a small price to pay for the enormous ease that the inversion of control provides in the development.

As for its architecture, I would create a service for each launch action (TarifaService, SaldoService etc.), inject into each of them their respective Repository and finally in the class LancamentoService, would inject these services created. This facilitates you to centralize business rules that eventually need to be applied to the tariff, for example, and also conforms to best practices, which say business rules should be handled in the service layer and not in the DAO layer (repositories).

  • I understood, so I was not far from ideal. Taking advantage then: the use of a generic Repository would bring some performance benefit?

  • 1

    Is not Repository generic. Today, following your example, you inject directly into the class LancamentoService the repositories of the actions a release may have. That’s not a good idea. The best is that each of these repositories have your own service (i.e., for example, you would create a SaldoService and within it would inject the SaldoRepository, create a public method, for example: calculate Aldo(), and in the class LancamentoService, you inject that SaldoService (and not the SaldoRepository, as you mentioned in your example) newly created and calls the method calcularSaldo().

  • Beauty. Thank you very much!

Browser other questions tagged

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