4

I am a java web application developer.

And I found I am coufused with the layers in the server side.

I do not mean the MVC(model/view/control) but the dao/service layers.

Dao layers is used to conntect the database.

Why the service layer?

Since we use the spring mvc framework now,I used to process the login in the control including call the dao to fetch data. Is the service layer necessary?

hguser
  • 35,079
  • 54
  • 159
  • 293

4 Answers4

3

There are several reasons for the service layer, but for me there are a few primary advantages:

  1. Allows declarative transaction control on service methods, which may consist of aggregated non-transactional DAO calls.
  2. Clear separation of business layer logic from mere DB access.
  3. Allows easier testing at higher levels by encapsulating the above into easily-replaceable chunks.

Is the service layer necessary? Of course not--but technically no layers are necessary; everything could be wrapped up in a JSP page. It's a matter of granularity, control, and separation of concerns.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
2

Service layers know about transactions. They map to use cases and units of work. They have a chance to be useful even if your web UI goes away, because they're the basis for a service-oriented architecture.

I would say that the only time I'd dispense with a layer would be for read-only access to data. In that case I'd be more likely to dispose of the DAO than the service.

duffymo
  • 305,152
  • 44
  • 369
  • 561
2

DAO Layer- handles only the DB stuff, basic CRUD and some other find queries, It handles DB transactions

Service Layer - Actually depends on DAO layer to perform business specific logic, It handles business transactions

The advantage to differentiate them is you could plug service layer with any other front end, also you would have chance to plug/change DAO layer if the raw storage changes

Also See

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
1

The service layer is typically used as an integration layer. It prevents the 'muddying' of the DAO layer with features that are not related to it. For example:

  • Security
  • Business logic and validations
  • External system integration

Your service layer is also where you would implement various 'views' of the system, mixing and matching from lower layers to create new functionality.

Perception
  • 79,279
  • 19
  • 185
  • 195