6

I'm building a web application that primarily constitutes of CRUD operations of data from back end/database. There are instances where in I have to write business logic(I'm sure we will have more business logic built as we go deeper in to development). Currently for each UI screen I'm creating I create a model class,Service class, DAO class, a controller(it's servlet essentially) and bunch of jsp pages. In most cases the service class just calls the methods from DAO to pass in model objects. Essentially we use model classes to map data from UI screens. Hence the controller will have the model objects populated when a form is submitted. I have started using service classes to keep a separation layer from web layer to DAO layer. But at times I feel that the service class is just adding unnecessary level of API calls, I would think that I could just inject the DAO in to Controller and complete the task faster. I want to use the service class only when there is additional business logic to be performed. If you have to design an application what factors do you consider using controller->DAO vs controller->Service->DAO control flow?

Srini Kandula
  • 981
  • 2
  • 18
  • 47
  • 1
    I came into an already existing project when I started my current job. I have been here for a little over a year. Honestly, I just realized that there is supposed to be a separate layer for injecting DAO. The project at work just injects them into the Controllers. Now that I know this, and have ready about the benefit of atomicity, I think having a service layer would have been good since we do have multiple interacting entities throughout the application. – theblang Sep 17 '13 at 19:42

4 Answers4

11

DAOs are more granular and deal with one specific entity. Services provide macro level functionalities and can end up using more than one DAO. Typically, Services are used for defining transaction boundaries to gain atomicity. In other words, if you end up updating multiple tables using multiple DAOs, defining transaction boundary at service will help in either committing or rollbacking all the changes done to DB.

In your design, since you are primarily doing CRUD for various entities, it may seem that services are not adding much value. However, think of web-based front end as one way of updating data. Usage of services will allow you to expose same capabilities as a web-service later to other forms of client like third party integrators, etc.

So, in summary, your design seems to be in line with conventional practices. If you feel that you can combine multiple services into one based on some common theme such that it can reduce the overhead of code, then, you should go ahead and do it. At the end of day, ultimate goal is to create maintainable code which no one is afraid to change when need arises.

Wand Maker
  • 18,476
  • 8
  • 53
  • 87
  • 1
    "Usage of services" you don;t need a service class to return a simple list/perform crud or expose as a restful-api. You need a serpate service classes when manipulating multiple interacting entities. – NimChimpsky Apr 26 '13 at 12:00
  • You'll want services when providing for external apps too. Imagine you have a web service that provides data for an Android app. You can create a Service layer which will obtain the data and return DTO (Data Transfer Objects) for an Adapter layer which handles REST requests and transforms those DTOs in JSON or XML to send to the Android app. – dgimenes Nov 27 '13 at 20:19
1

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

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

and they are using the same class as service and repository as in 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 in case you are going to use spring-data CRUDRepository or JPARepository then your DAO will be Interface and you have to make service layer to handle your code

Bassem Reda Zohdy
  • 12,662
  • 3
  • 33
  • 39
0

I'd reference my answer here

The long and short of it is the advantage of using a Service layer is it gives you room to move in the future if you want to do anything with Spring Security and roles etc. It allows you to handle transactions more atomically and Spring itself has really nice annotations for this.

Community
  • 1
  • 1
David
  • 19,577
  • 28
  • 108
  • 128
  • Then you have to go through the pains of unpicking your DAO concerns from your controllers. If you have your controllers heavily linked to your DAOs this is more work in the future if you haven't already built a service layer. – David Apr 26 '13 at 13:15
  • 1
    Because the service should provide separation of concerns http://en.wikipedia.org/wiki/Separation_of_concerns. It would be poor practice to ignore that service facade and go straight to the DAO from the controller if you introduced a service layer. Although initially you would break the DRY rule by not needing a service layer initially, the fact it'd SoC would be more of a pain in the future for any refactoring. – David Apr 26 '13 at 15:05
  • I'm just going to leave this here http://stackoverflow.com/questions/3688664/simple-spring-app-why-use-service-layer#answer-3688779 as it basically sums up my reasoning of why having a service layer for an application that may grow in complexity is a good idea. – David Apr 26 '13 at 16:53
0

Use a service class when dealing with more than one aggregate root.

Inject repositories (aka a dao that returns a collection) or dao's directly into controller, no need for an extra layer/class to do a basic get.

Only use service classes where necessary, otherwise you have twice as much code as required.

You can make repository generic, and annoatoate with @Transactional(propagation = Propagation.REQUIRED) which enforces a transaction is present, but won't create a new one if already present. So if you later use multple repositoes in one service class method, you will only have the one transaction.

Community
  • 1
  • 1
NimChimpsky
  • 46,453
  • 60
  • 198
  • 311