Rakesh
Rakesh

Reputation: 1035

Why we use Service layer and DAO with JPA repository

Why we use Service layer and DAO with JPA repository. Is there any standard for using this architecture in spring mvc projects? I am confuse about this and I am new in spring mvc. Please help me. Thanks in advance.

Upvotes: 4

Views: 1228

Answers (2)

SpringLearner
SpringLearner

Reputation: 13844

I am using spring MVC and in my project I have 3 layers

  1. Controller
  2. Service
  3. DAO

Controller simply controls in/out of the project.In controller we do not write any business logics

We write all the business logic in service layer

In dao, we write the database logic like CRUD Operations

This is done as we do not wish to mix all the logic flows in a single class. It will increase burden and hard to debug

Upvotes: 1

Shyam
Shyam

Reputation: 6444

There are two reason to use Service layer:

1. Code Modularity:

Using Service layer we can separate the code into different layers like for ORM and Business logic.

2. Security

Service layer that has no relation to the DB, then is it more difficult to gain access to the DB from the client except through the service. If the DB cannot be accessed directly from the client (and there is no trivial DAO module acting as the service) then all an attacker who has taken over the client can do is attempt to hack the service layer as well before he gets all but the most sanitised access to your data.

Upvotes: 1

Related Questions