Personally I've settled on an architecture like this:
user interface (struts or swing actions) access service layer
service layer access dao's
dao's access the database (via hibernate).
My domain objects (the mapped, persistant ones) are passed around between layers.
The advantages I find:
i) Conceptually very clean. Sits well in my brain. Easy to explain to others.
ii) Clean delegation of responsibility: the DAO's know how to find, delete etc. your domain objects, and are application independent (and so can be
reused across applications).
The service layer contains the application specific, higher level logic. All transactions occur at this level (in my system at least). I use Spring to manage this declaratively - at this level you can call multiple DAO methods within one transaction easily. At this level you will get some simple service methods that straight delegate to the DAO, but for more complex application logic that calls multiple DAO methods, from multiple DAO's, this obviously is not the case.
iii) The GUI actions just call the needed services and know nothing about DAO's or how to persist or even if data is coming from a database. (Eg, I have one app that gets data from a local database and from a web service. To the application this differential data source is not apparent.)
iv) Very easy to reuse service logic with different GUI interfaces.
v) If distributed app needed, easy to change to a session ejb front end to the service layer.
I'd recommend looking into using Spring to construct and manage your service layer. It really leads to a very clean solution, reducing a lot of plumbing code (for transactions), and really clarified the overall architecure in my head when learning it.
Jonny
micho2001 wrote:
deyanp wrote:
Obviously the Service layer will delegate/use the DAO objects, but should the clients of the application (web UI, web service etc) call retrieve methods on the Service Layer classes only?
10x in advance,
Deyan
Uhm... I'm struggling with this one myself. Because we only program for web browsers, we just make the controller (Struts Actions) call DAO's and pass domain objects to the view.
My hunch would be that if you use a service layer it would mainly delegate calling DAO's and such to obtain info from the DB (as domain objects) and pass it to the view (JSP's).
In theory service layers have the potential to make your app easily portable to other presentation technologies (PDA's, etc...) or things like webservices and have application containers help you with stuff like declarative security, logging, etc...).
Any ideas or experiences would be appreciated.