My web application is written using Spring MVC + Hibernate.
I saw a lot of applications follow this (best) practice but I'm wondering why I would need a service layer.
Maybe it's useful for decoupling purpose: I can show a universal facade to the controllers and inject into the service HibernateDAO, GaeDAO, MyDAO, and so on.... But I could do that without the service, too: using an interface.
I also tought: validation. I'll make my Customer validation in the service but.... it's much more convenient to validate in Spring controller.
Help me understand the concept please :)
The service layer is there to provide logic to operate on the data sent to and from the DAO and the client. Very often these 2 pieces will be bundled together into the same module, and occasionally into the same code, but you'll still see them as distinct logical entities.
A Service Layer defines an application's boundary and its set of available operations from the perspective of interfacing client layers. It encapsulates the application's business logic, controlling transactions and coordinating responses in the implementation of its operations.
The service layer consists of a collection of Java classes that implement business logic (data retrieval, updates, deletions, and so on) through one or more high-level methods. In other words, the service layer controls the workflow.
A service layer is a layer in an application that facilitates communication between the controller and the persistence layer. Additionally, business logic is stored in the service layer. It includes validation logic in particular. The model state is used to communicate between the controller and service layers.
You don't need a service layer. However it helps you to
class Service { private DatabaseBarRepo barRepo; private DatabaseFooRepo fooRepo; @Transactional public void serviceRoutine() { barRepo.doStuff(); fooRepo.doStuff(); } }
Here we let two separate repositories take part in the same transaction. This is specific for databases albeit the principles are valid for other systems as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With