Using services and DAO in spring mvc controller

I am creating a web application that mainly consists of CRUD operations on data from the back end / database. There are cases when I have to write business logic (I am sure that we will have more business logic built as we go deeper into development). Currently, for each UI screen I create, I create a model class, a service class, a DAO class, a controller (this is essentially a servlet), and a bunch of jsp pages. In most cases, the service class simply calls methods from the DAO to pass in the model objects. Essentially, we use model classes to map data from user interface screens. Therefore, the controller will have model objects filled in when the form is submitted. I started using service classes to preserve the separation layer from the web tier to the DAO tier. But sometimes I feel like the service class just adds an unnecessary level of API calls, I think that I could just enter the DAO into the controller and complete the task faster. I want to use the service class only when additional business logic is required. If you need to develop an application, what factors do you consider using the controller -> DAO vs control-> Service-> DAO control flow?

+6
source share
4 answers

DAOs are more detailed and deal with one specific object. Services provide functionality at the macro level and can end up using more than one DAO. As a rule, Services are used to determine the boundaries of transactions to obtain atomicity. In other words, if you are completing the update of multiple tables using multiple DAOs, defining the transaction boundary during maintenance will help either commit or roll back all changes made to the DB.

In your design, since you mainly run CRUD for different objects, it might seem that services do not add much value. However, think of the web interface as one way to update data. Using services will allow you to later provide the same features as a web service to other forms of the client, such as third-party integrators, etc.

So overall, your design seems to be in line with normal practice. If you feel that you can combine several services into one, based on some common theme, so that this can reduce the overhead of the code, then you should go ahead and do it. At the end of the day, the ultimate goal is to create supported code that no one is afraid to change when the need arises.

+11
source

In Pro-Spring -3, they mentioned the line below for a controller with JPA2

Once the EntityManagerFactory had been properly configured, injecting it into your service layer classes is very simple. 

and they use the same class as the service and repository, as shown below:

 package com.apress.prospring3.ch10.service.jpa; // Import statements omitted @Service("jpaContactService") @Repository @Transactional public class ContactServiceImpl implements ContactService { private Log log = LogFactory.getLog(ContactServiceImpl.class); @PersistenceContext private EntityManager em; // Other code omitted } 

but if you are going to use spring -data CRUDRepository or JPARepository, then your DAO will be an interface and you have to make a service layer process your code

+1
source

I would name my answer here

The long and short of them - the advantage of using a service level - this gives you the opportunity to move on in the future if you want to do something with Spring Security and roles, etc. This allows transactions to be processed more atomically and Spring itself has really wonderful annotations for this.

0
source

Use a service class when working with more than one aggregated root .

Bring in the repositories (aka dao that returns the collection) or dao directly to the controller, there is no need for an extra layer / class to make a basic get.

Use only service classes if necessary, otherwise you will have twice as much code.

You can create a shared repository, and annoatoate with @Transactional(propagation = Propagation.REQUIRED) that provides the transaction is present, but will not create a new one if it is already present. Therefore, if you later use multi-user repositories in one class of service method, you will only have one transaction.

0
source

Source: https://habr.com/ru/post/943691/


All Articles