Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Domain, DAO and Service layers

I need to learn the difference between the type of methods (in term of business logic) that should be inside the Domain, DAO and Service layers objects.

For example, if I am building a small web application to create, edit and delete customers data, as far as I understand inside Domain layer object I should add methods that Get/Set Customers object properties, for example (getName, getDOB, setAddress, setPhone...etc).

Now what I am trying to learn is what methods shall I put in DAO and Service layers objects.

Thanks in advance for your time and efforts.

like image 221
MChan Avatar asked May 31 '13 17:05

MChan


People also ask

What is difference between DAO and service?

Service is the utility that defines the business logic of the application. DAO or Data Access Object is used to interact with the database directly.

What is the difference between DAO and repository?

DAO is an abstraction of data persistence. However, a repository is an abstraction of a collection of objects. DAO is a lower-level concept, closer to the storage systems. However, Repository is a higher-level concept, closer to the Domain objects.

What is service layer and DAO layer?

Generally the DAO layer should be as light as possible and should exist solely to provide a connection to the DB, sometimes abstracted so different DB backends can be used together. The service layer is there to provide logic to operate on the data sent to and from the DAO and the client.


2 Answers

Speaking generally (not Hibernate or Spring specific):

The DAO layer contains queries and updates to save your domain layer into your datastore (usually a relational DB but doesn't have to be). Use interfaces to abstract your DAO away from the actual datastore. It doesn't happen often, but sometimes you want to change datastores (or use mocks to test your logic), and interfaces make that easier. This would have methods like "save", "getById", etc.

The Service layer typically contains your business logic and orchestrates the interaction between the domain layer and the DAOs. It would have whatever methods make sense for your particular domain, like "verifyBalance", or "calculateTotalMileage".

like image 82
lreeder Avatar answered Oct 09 '22 14:10

lreeder


DAO: "wrapper" methods for "wrapping" JPA or JDBC or SQL or noSQL calls or whatever for accessing DB systems.

Domain: Business logic calls correlated to a single type of entities (domain objects).

Service: Business logic calls correlated to a group of type of entities or to a group of several entities of the same type.

(I'm not sure about English, sorry.......)

It means: Service layer is "bigger" than Domain layer, is often close to front-end, often calls or uses several domain objects.

Domain objects encapsulate most stuff for one part of the domain (that's why they are called D.O.)

DAO is just sth technical, sometimes needed, sometimes not. When real domain objects are used, then often "repositories" are used to hide access to database systems, or adding special db functionality or whatever.

front-end --> service method 1 --> d.o. A of type X, d.o. B of type X, List

like image 8
xtraclass Avatar answered Oct 09 '22 13:10

xtraclass