8
votes

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.

2

2 Answers

9
votes

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".

8
votes

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