Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC what is service component?

Could anyone please give some examples of possible service. I am going through the book, but cannot understand what can the service do? It provides processed data for modelAndView to controller, but what it looks like is it java bean connecting and retrieving results from database, what can it be?

like image 235
Tos Avatar asked Dec 13 '22 11:12

Tos


1 Answers

A service component is where all your DAOs come together and have the business logic. You can think of it this way.

  • DAO - should only load data from db. Nothing more.
  • Services - can use daos to load multiple objects and do some kind of business logic
  • controllers - use services to load objects. They should have nothing more than simple logic because complicated logics should really belong in the service. Reason for this is in the future when you want to reuse this logic, you can do so if its a in service but not if its in the controller.

Example:

  • BookDAO - Loads the book
  • BookService - loads the books for a person that is logged in

Finally, I'd like to quote the grails doc for a clean concise quote.

As well as the Web layer, Grails defines the notion of a service layer. The Grails team discourages the embedding of core application logic inside controllers, as it does not promote re-use and a clean separation of concerns.

like image 154
Amir Raminfar Avatar answered Feb 12 '23 18:02

Amir Raminfar