8

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.

MChan
  • 6,842
  • 27
  • 83
  • 132

2 Answers2

9

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

lreeder
  • 12,047
  • 2
  • 56
  • 65
  • Can you please provide me with examples of methods under DAO layer? As far as I understood from you under Service layer objects shall have methods like verifyBalance, calculateTotalMileage which as far as I can see are all calculation methods – MChan May 31 '13 at 17:47
  • @MChan - the DAO is mostly concerned with updating, creating, removing , and retrieving data from the database. In the case of a non-object datastore, it would also do a mapping from the persisted data (rows and columns in a RDB) to an object, and from an object to the persisted data. Method nanmes can be whatever you want, but usually are something like "save", "delete", "query","update". The service layer doesn't have to have calculation methods. Those methods would be anything your business needs to determine or do. – lreeder Jun 01 '13 at 02:32
8

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

xtraclass
  • 445
  • 3
  • 7
  • if I am using Hibernate, then what is the use of DAO object methods? I mean how they can help me in data access? – MChan May 31 '13 at 18:09