Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it correct to access the DAO layer from the Controller layer? [closed]

In my applications I usually access the DAO layer in order to obtain/save or update objects to it's repository, and I use a Service layer to perform more complex operations.

So my question is this: Is it correct (according to best practices and design/architecture patterns) to access the DAO layer from the Controller layer, or should I bypass it through the Service layer? Thanks!

like image 585
Joaquín L. Robles Avatar asked May 25 '11 20:05

Joaquín L. Robles


People also ask

Which is used to pass the values between controller and DAO layer?

Answer: True The Model Map is used to pass the values between controller and DAO layer.

What is controller and DAO?

The Controller is a server side component (for web apps) that accepts requests from View clients, updates the Model appropriately, and sends results back to any and all View clients that need it. Service/DAO extends those layers to the server side.

What is DAO layer in spring boot?

DAO stands for data access object. Usually, the DAO class is responsible for two concepts: encapsulating the details of the persistence layer and providing a CRUD interface for a single entity.


2 Answers

In theory: within the context of the MVC architectural pattern, there is no clear distinction between a data access layer (DAO) and a service layer. The Service layer and the DAO layer could both be seen as the "Model" in MVC. A Service layer may well implement business logic, complex validations, etc. - yet it is still a layer for accessing your data! As long as you maintain a clean separation of concerns between your Model, View and Controller objects, it would be correct to access the DAO layer from a Controller object.

In practice: I have seen both scenarios. If you have a small application with a simple data model, it would make sense to use the DAO layer directly from Controllers. However, as business logic gets complicated, or if your model is shared by more than one application, it would make more sense to factor out Business Delegates and DAOs in order to re-use components, minimize impact when changes are made, increased flexibility between components, etc. This would be dictated by the technical architecture of the system in question.

like image 96
octy Avatar answered Oct 20 '22 20:10

octy


I think that if there is no need for ANY kind of processing from the Service layer, there is no problem to have the Controller layer to access the DAO directly. But it should really have at least some kind of processing to do, like server valitadion of input data before messing with the database.

like image 32
Caminada Avatar answered Oct 20 '22 20:10

Caminada