JPA implementation patterns: Service Facades and Data Transfers Objects
Join the DZone community and get the full member experience.Join For Free
if there is one thing that i learned when implementing jpa for the first time is that some of the "old school" enterprise application architecture patterns still apply, even though some people have proclaimed them to be no longer necessary:
- the dao has been declared dead because you might just as well invoke the entitymanager directly. it provides a nice enough interface and switching from jpa to a different persistence implementation is not something the dao abstraction would make much easier.
- dto's have been deemed superfluous because you can also use your domain objects directly in the presentation layer. this is made possible by a combination of the open entitymanager in view pattern, tag libraries to display your domain objects in jsp's and data-binding utilities to map http request parameters back to domain objects.
- and finally service facades also seem to have gone out of fashion. instead you can have the controller directly invoke the services it needs or, even simpler, directly contain the business logic.
the resulting application architecture will look something like this diagram of the spring web mvc application architecture :
so why bother with dao's, dto's and service facades?
i've already argued why the dao pattern is still relevant in jpa application architecture . a similar argument can be made for the dto and service facade patterns. while the dto-less and service facade-less architecture displayed above works quite well for straight forward web applications, it has two major drawbacks:
- if you wish to expose your application to other non-html clients (think of web services using soap , flex frontends using amf or ajax applications using json ), you will need a more clearly defined interface. that interface specifies which services get exposed to the client and what types are used as the input and output. service facades and dto's respectively help you define this interface.
- using domain objects directly in the presentation layer requires those domain objects to expose all their fields as public properties with getters and setters. if the fields are not exposed as such, the tag libraries won't be able to render them and the data binding code won't be able to set them. as allen holub has already argued before, getters and setters are evil . (btw, some people interpret holub's article as an excuse to make all fields public. the article is saying that public getters and setters are no better, but that does not mean your code should just have public fields. instead the article is advocating use of the tell, don't ask approach to oo.)
in fact the reason i found out why service facades and dto's are still useful, is that the application my team and i are developing has a flex frontend and a command-line interface that communicate to the core using amf and hessian respectively. the only html code our application produces loads the swf file for the flex frontend! we started out without dao's, dto's and service facades as one would do, but added them all to our architecture to make this work. and we got a well defined interface between the service layer and the presentation layer as an added bonus.
pros and cons of dto's
of course it would be silly to say you should always use dto's in your architecture. as always it depends. to allow you to make up your own mind, i will list a number of pros and cons of using dto's:
- con: dto's cause code duplication . this is especially the case when your dto's have exactly the same fields as your domain objects, and even more so when they both have getters and setters for those fields. but having dto's in your architecture allows you to get rid of the getters and setters in your domain objects.
- con: dto's require you to write boilerplate code to copy properties back and forth. some people have suggested using a java bean mapper framework such as dozer , apache commons beanutils , or the spring framework's beanutils class , but that requires you add getters and setters to your beans and we just decided we did not want to do that anymore!
- pro/con: dto's make it impossible to use entitymanger.merge to copy their state to your persistent objects. instead you'll have to apply the diy merge pattern as described in my blog on saving (detached) entities . of course forcing you to do something a particular way, and one that is not 100% satisfying, is not really an advantage. but at least the dto and the diy merge work well together.
- pro: dto's ensure you are not hit by unexpected lazy loading problems in your presentation layer. or in the case of remote invocations they protect you from lazy loading problems when serializing for transport or, even stranger, on the client.
- pro: the dto pattern forces you to think about the interface of your application. you can make your dto's richer than your plain domain objects, e.g. by adding security information to them. or you can group information from multiple domain objects into one dto to make the interface easier to use.
before we move on to discuss service facades, it might be worth to have a look at anirudh vyas's blog on common abuses of the dto pattern .
pros and cons of service facades
just like is the case with dto's, there are cases when service facades make a lot of sense and there are cases when they just add meaningless overhead. let's have a look at some pros and cons:
- con: service facades add an extra layer that does not do much apart from delegate to the actual service. this argument appeals especially to java ee developers that have bad memories of the ejb 1.0 based multi-tier architectures we set up in the beginning of this century.
- pro: service facades can (should!) be made responsible for mapping from dto's to domain objects and back. service facades are invoked with dto's as arguments, map them to domain objects, invoke the actual service (or services), map the result back to dto's and return those dto's to the client. actually, to make sure your service facades only adhere to single responsibility principle , you should factor the mapping logic out to separate dto2domapper and do2dtomapper classes.
- pro: service facades can function as the transaction boundary of your application, i.e. a transaction is started for the duration of a request to the service facade. instead of having to define the transaction attributes of all your services, you can assume that all service invocation arrive through the service facade. actually, setting your service facade to be the transaction boundary is obligatory when you want to invoke more than one service during the handling of one request or when you want the service facade to map lazily loaded domain objects to dto's. in that case you end with something akin to the open entitymanager in view pattern; a transaction is started that lasts from the moment the incoming dto's are translated to domain objects to the moment the resulting dto's are returned to the client. (btw, if you want to enforce that all services invocations go through the service facade you can set their transaction attribute to mandatory. if a transaction hasn't already been started when such a service is invoked, an exception is thrown)
- pro: the service facade pattern forces you to think about the interface of your application. instead of letting the client invoke all your services, you get to decide which ones to actually expose.
hmm, i can't seem to think of many disadvantages to the service facade pattern. apart from the "overhead" argument and that is a subjective thing. feel free to add more cons of this pattern to the comment section!
impact on application architecture
if we apply the dao, dto and service facade patterns, we end up with a jpa application architecture that looks like this:
when a request is made, the sequence of events goes something like this:
- a service client sends a request to the service facade. all objects sent are dto's.
- a transaction is started.
- the service facade invokes the dto2domapper to map the incoming dto's to domain objects. the dto2domapper may invoke one or more dao's to load domain objects from the database.
- the service facade invokes one or more services to perform actual business logic.
- the service facade passes the return values to the do2dtomapper and gets dto's back. the do2dtomapper may invoke one or more services or dao's to enrich the dto's.
- the transaction is committed. or rolled back in case an exception has occurred.
- the service facade passes the dto's to client.
- the service client receives the dto's.
in fact, if you take this diagram and replace "dto2domapper" with
"data binding", "do2dtomapper" with "view rendering", and "service
you get the original web mvc architecture we started out with. the big
difference is that the incoming "dto's" are request parameters while
the outgoing "dto's" are html:
that makes this blog come full circle, so now is a good time to wrap up. as you can see there is no clear-cut answer to whether or not to use dto's and service facades. it all depends on what you are trying to achieve with your application, for example:
- will it be a straight html application or will you want to expose it over different protocols?
- how tightly coupled do you want your clients and your services to be?
- do you want your domain objects to not have any getters and setters?
i am very interested to hear what you guys think about the validity of these patterns in modern java ee architecture. when would you apply then? or when not and why not? see you all at the next blog in which i will try and tackle the subject of how to handle inheritance in jpa.
Opinions expressed by DZone contributors are their own.