Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of the service layer

Am I correct in thinking that the purpose of a service layer includes the following?

  • thinning out of domain models (i.e. movement of certain functions like in caching, instantiation)
  • reduction in dependencies from domain models
  • API minimisation
like image 387
Ben Aston Avatar asked Feb 19 '10 10:02

Ben Aston


People also ask

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 is the use of service layer in spring?

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.

What happens in service layer?

A service layer is an additional layer in an ASP.NET MVC application that mediates communication between a controller and repository layer. The service layer contains business logic. In particular, it contains validation logic. For example, the product service layer in Listing 3 has a CreateProduct() method.

Is a service layer necessary?

You don't always need a service layer. Especially if your APIs are just simple CRUD operations, for example, with no real logic required or calculations. However, if you have an API which performs some logic before querying your repository then this should be in a separate service class.


2 Answers

Traditionally (when not using Domain Driven Design) the service layer, or 'business layer' as it is also called, is where you code all the business logic for your application. So, for example in an application that deals with giving out bank loans, the service layer is where the code goes that decides whether or not a certain loan should be given. Obviously, the service layer would require some information about the applicant of the loan to be able to make a decision on her credibility. To retrieve this information, the business layer calls the 'data' or 'repository' layer, which deals with extracting and storing information into the database.

The service layer does not deal with matters like persistence or other infrastructural concerns.

If your design is domain-driven, anthares' answer is true.

like image 106
Hans Westerbeek Avatar answered Oct 21 '22 08:10

Hans Westerbeek


In terms of domain-driven design for example the domain service layer is used for operations that cannot be defined in the context of you Domain objects. For example if you have an object CreditCard, a suitable operation in your service layer would be Issue a new Credit Card.

In bigger application is used the pattern Anemic Domain Models, where the domain objects are used only as data containers and the whole business logic is in your domain service layer (this is sometimes refered as anti-pattern, but may be very useful in big solution, with adding another abstraction layer).

In difference solution architectures and patterns, service layer may have difference purpose, though.

like image 31
anthares Avatar answered Oct 21 '22 07:10

anthares