Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I really need a service layer?

My web application is written using Spring MVC + Hibernate.

  • My model is "Customer" entity POJO.
  • I have got a DAO object "CustomerDAO", its method "saveCustomer(c)" contains the code interacting with Hibernate;
  • Then I created a "CustomerService with a "saveCustomer(c)" method who simply pass the customer object to the dao for saving;
  • Finally there are "CustomerController" and customer.jsp, who are responsible for the view layer, the jsp's form fields are bound to a Customer object on the controller side. The controller calls the service.

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 :)

like image 863
Fabio B. Avatar asked Mar 09 '12 11:03

Fabio B.


People also ask

Why do we need a service layer?

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.

What should go in the service layer?

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.

What is the purpose of service layer in Java?

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.

What happens in service layer?

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.


1 Answers

You don't need a service layer. However it helps you to

  • Decouple your components
  • You can enforce specific business rules in your service layer which should be agnostic to your repository
  • Let a service facade one or more repositories. Let's consider the following sample
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.

like image 120
Johan Sjöberg Avatar answered Sep 23 '22 19:09

Johan Sjöberg