<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: JPA implementation patterns: Service Facades and Data Transfers Objects</title>
	<atom:link href="http://blog.xebia.com/2009/05/11/jpa-implementation-patterns-service-facades-and-data-transfers-objects/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.xebia.com/2009/05/11/jpa-implementation-patterns-service-facades-and-data-transfers-objects/</link>
	<description>Software development done right!</description>
	<lastBuildDate>Wed, 08 Feb 2012 14:41:38 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Nils</title>
		<link>http://blog.xebia.com/2009/05/11/jpa-implementation-patterns-service-facades-and-data-transfers-objects/#comment-107558</link>
		<dc:creator>Nils</dc:creator>
		<pubDate>Sun, 24 Jul 2011 18:28:22 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1692#comment-107558</guid>
		<description>I would also greatly benefit from an example implementation of this pattern! Would you be able to publish or send me one?

As a hobby project I am working on a Adobe Flex front-end with a Java back-end using JPA for persistence. The protocol I am using is remote objects (AMF) implemented with BlazeDS.

I started out with a service-facade and entity DAOs, but without any specific DTOs. The same POJOs, the domain objects, were passed in the service-facade as those used as DTOs passed to the Hibernate DAOs.

However, the latest two days I have been thinking whether this is a good approach or not. For example, say I have POJO Book with a unidirection ManyToOne relation with the POJO Category (i.e. each book may only be associated with one category, but the same category may be associated with many books). I see some alternatives:

Alternative 1:
I expose a method/operation addUpdateBook(Book book). In the implementation of this operation I add/update both the book and the referenced category. I mean, if the client submits a book having a category that doesn&#039;t exist from before, this would mean that the client implicitly may edit categories using the addUpdateBook service.
- pro: the client is working directly with the domain model!
- con: the entire category information will be sent when a new book is added even though a reference to the category would be sufficient

Alternative 2:
I expose a method/operation addUpdateBook(Book book,Long categoryId). In the implementation I retrieve the category for the given categoryId and replace the category given in the book POJO and then I persist the book. In other words, I ignore any category in the book object, I just look at the categoryId. This means that the client would need to use another operation in order to modify the category.
- pro: the client can still work on the domain model
- con: it is confusing for the client that the category of the book object will be ignored
- con: the entire category information of the book will be sent, even if the server never will read it
- pro: it may be more clear when a separate operation should be used for category modifications
- con: I need to retrieve the category before persisting the book. I guess this means some overhead.

Alternative 3:
I expose a method/operation addUpdateBook(BookDTO bookDto). The POJO BookDTO looks as the POJO Book, but instead of a field &quot;Category category&quot; it has a field &quot;Long categoryId&quot;. In the implementation I retrieve the Category for the given categoryId before I persist the Book.
- pro: not confusing for the client
- con(?): what should the method getBook(Long bookId) return? should it return only the BookDTO? Then it would be required to invoke also the operation getCategory(Long categoryId) in order to have &quot;the entire book information&quot;. Then the client would need to set together the different parts to a local domain representation of the book. Compared to alternative 1 this would be more complex on the client side?
- con: I need to retrieve the category before persisting the book. I guess this means some overhead.

I guess (!) alternative 3 is the way you would design the operations in a SOA context. However, for me, it is not that important to be loosely-coupled between the client and server. My initial focus is not to provide multiple client-platform support.

Please give me your comments!</description>
		<content:encoded><![CDATA[<p>I would also greatly benefit from an example implementation of this pattern! Would you be able to publish or send me one?</p>
<p>As a hobby project I am working on a Adobe Flex front-end with a Java back-end using JPA for persistence. The protocol I am using is remote objects (AMF) implemented with BlazeDS.</p>
<p>I started out with a service-facade and entity DAOs, but without any specific DTOs. The same POJOs, the domain objects, were passed in the service-facade as those used as DTOs passed to the Hibernate DAOs.</p>
<p>However, the latest two days I have been thinking whether this is a good approach or not. For example, say I have POJO Book with a unidirection ManyToOne relation with the POJO Category (i.e. each book may only be associated with one category, but the same category may be associated with many books). I see some alternatives:</p>
<p>Alternative 1:<br />
I expose a method/operation addUpdateBook(Book book). In the implementation of this operation I add/update both the book and the referenced category. I mean, if the client submits a book having a category that doesn&#8217;t exist from before, this would mean that the client implicitly may edit categories using the addUpdateBook service.<br />
- pro: the client is working directly with the domain model!<br />
- con: the entire category information will be sent when a new book is added even though a reference to the category would be sufficient</p>
<p>Alternative 2:<br />
I expose a method/operation addUpdateBook(Book book,Long categoryId). In the implementation I retrieve the category for the given categoryId and replace the category given in the book POJO and then I persist the book. In other words, I ignore any category in the book object, I just look at the categoryId. This means that the client would need to use another operation in order to modify the category.<br />
- pro: the client can still work on the domain model<br />
- con: it is confusing for the client that the category of the book object will be ignored<br />
- con: the entire category information of the book will be sent, even if the server never will read it<br />
- pro: it may be more clear when a separate operation should be used for category modifications<br />
- con: I need to retrieve the category before persisting the book. I guess this means some overhead.</p>
<p>Alternative 3:<br />
I expose a method/operation addUpdateBook(BookDTO bookDto). The POJO BookDTO looks as the POJO Book, but instead of a field &#8220;Category category&#8221; it has a field &#8220;Long categoryId&#8221;. In the implementation I retrieve the Category for the given categoryId before I persist the Book.<br />
- pro: not confusing for the client<br />
- con(?): what should the method getBook(Long bookId) return? should it return only the BookDTO? Then it would be required to invoke also the operation getCategory(Long categoryId) in order to have &#8220;the entire book information&#8221;. Then the client would need to set together the different parts to a local domain representation of the book. Compared to alternative 1 this would be more complex on the client side?<br />
- con: I need to retrieve the category before persisting the book. I guess this means some overhead.</p>
<p>I guess (!) alternative 3 is the way you would design the operations in a SOA context. However, for me, it is not that important to be loosely-coupled between the client and server. My initial focus is not to provide multiple client-platform support.</p>
<p>Please give me your comments!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Srinivas C</title>
		<link>http://blog.xebia.com/2009/05/11/jpa-implementation-patterns-service-facades-and-data-transfers-objects/#comment-106862</link>
		<dc:creator>Srinivas C</dc:creator>
		<pubDate>Tue, 12 Jul 2011 15:42:18 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1692#comment-106862</guid>
		<description>This really is an interesting article. Thanks.</description>
		<content:encoded><![CDATA[<p>This really is an interesting article. Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: juro</title>
		<link>http://blog.xebia.com/2009/05/11/jpa-implementation-patterns-service-facades-and-data-transfers-objects/#comment-100083</link>
		<dc:creator>juro</dc:creator>
		<pubDate>Wed, 02 Feb 2011 08:20:00 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1692#comment-100083</guid>
		<description>Could you send some link for an example app which takes advantage of service facade and DTOs? I can&#039;t find anything and it would help me a lot. Thanks</description>
		<content:encoded><![CDATA[<p>Could you send some link for an example app which takes advantage of service facade and DTOs? I can&#8217;t find anything and it would help me a lot. Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joeri Vandenberghe</title>
		<link>http://blog.xebia.com/2009/05/11/jpa-implementation-patterns-service-facades-and-data-transfers-objects/#comment-95447</link>
		<dc:creator>Joeri Vandenberghe</dc:creator>
		<pubDate>Tue, 29 Jun 2010 13:25:23 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1692#comment-95447</guid>
		<description>Hi,

I layered a bit more...
My Generic Facades use a persistence unit, and implement CRUD functionality.
Then I have Generic Managers who implement the service/domain logic with transactions, there I use the facade interfaces to compose my service/domain specific  methods.</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>I layered a bit more&#8230;<br />
My Generic Facades use a persistence unit, and implement CRUD functionality.<br />
Then I have Generic Managers who implement the service/domain logic with transactions, there I use the facade interfaces to compose my service/domain specific  methods.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JPA Implementation Patterns &#124; Upthrust</title>
		<link>http://blog.xebia.com/2009/05/11/jpa-implementation-patterns-service-facades-and-data-transfers-objects/#comment-94563</link>
		<dc:creator>JPA Implementation Patterns &#124; Upthrust</dc:creator>
		<pubDate>Mon, 12 Apr 2010 05:10:51 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1692#comment-94563</guid>
		<description>[...] Service Facades and Data Transfer Objects [...]</description>
		<content:encoded><![CDATA[<p>[...] Service Facades and Data Transfer Objects [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sigmund Lundgren</title>
		<link>http://blog.xebia.com/2009/05/11/jpa-implementation-patterns-service-facades-and-data-transfers-objects/#comment-94261</link>
		<dc:creator>Sigmund Lundgren</dc:creator>
		<pubDate>Mon, 08 Mar 2010 15:09:44 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1692#comment-94261</guid>
		<description>ServiceFacade con:

discipline needed, developers tend to implement service/domain logic in the facade. Too common</description>
		<content:encoded><![CDATA[<p>ServiceFacade con:</p>
<p>discipline needed, developers tend to implement service/domain logic in the facade. Too common</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Service Facades and Data Transfers Objects &#171; When IE meets SE&#8230;</title>
		<link>http://blog.xebia.com/2009/05/11/jpa-implementation-patterns-service-facades-and-data-transfers-objects/#comment-93763</link>
		<dc:creator>Service Facades and Data Transfers Objects &#171; When IE meets SE&#8230;</dc:creator>
		<pubDate>Thu, 07 Jan 2010 18:37:31 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1692#comment-93763</guid>
		<description>[...] 1. JPA implementation patterns: Service Facades and Data Transfers Objects [...]</description>
		<content:encoded><![CDATA[<p>[...] 1. JPA implementation patterns: Service Facades and Data Transfers Objects [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vincent Partington</title>
		<link>http://blog.xebia.com/2009/05/11/jpa-implementation-patterns-service-facades-and-data-transfers-objects/#comment-92997</link>
		<dc:creator>Vincent Partington</dc:creator>
		<pubDate>Sat, 31 Oct 2009 10:17:56 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1692#comment-92997</guid>
		<description>&lt;strong&gt;@Simon:&lt;/strong&gt; Your comment on Adam Bien&#039;s post is a good one. Although with all the attention untyped languages like Ruby and Groovy are getting lately, strong typing seems like an old fashioned notion. ;-)

I guess we now have three approaches on how to send data from server to client:
&lt;ol&gt;
&lt;li&gt;Use the actual domain objects. No translation, but not always technically possible (lazy loading!) and does not shield the client from changes in the domain model that are not significant to it.&lt;/li&gt;
&lt;li&gt;Use specific DTO&#039;s. Lots of boilerplate code but highest flexibility, both in adapting to technical issues and in shielding components from change.&lt;/li&gt;
&lt;li&gt;Use generic DTO&#039;s. Less boilerplate code but also less protection from the type system.&lt;/li&gt;
&lt;/ol&gt;

Which approach is best depends on a number of variables, two of which are:
&lt;ol&gt;
&lt;li&gt;How closely are the &quot;client&quot; and the &quot;server&quot; coupled &lt;em&gt;in the development cycle&lt;/em&gt;? Does every change in the serer immediately mean that the client has to change anyway? For example, a Spring WebMVC view and its controllers are very closely coupled while a Flex UI and a Java backend are probably less closely coupled.&lt;/li&gt;
&lt;li&gt;How closely are the &quot;client&quot; and the &quot;server&quot; coupled &lt;em&gt;technically&lt;/em&gt;? Once again, a Spring Web MVC view communicates with its controller using Java objects in the same VM, while a Flex UI gets it objects over an AMF channel.&lt;/li&gt;
&lt;/ol&gt;</description>
		<content:encoded><![CDATA[<p><strong>@Simon:</strong> Your comment on Adam Bien&#8217;s post is a good one. Although with all the attention untyped languages like Ruby and Groovy are getting lately, strong typing seems like an old fashioned notion. <img src='http://blog.xebia.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>I guess we now have three approaches on how to send data from server to client:</p>
<ol>
<li>Use the actual domain objects. No translation, but not always technically possible (lazy loading!) and does not shield the client from changes in the domain model that are not significant to it.</li>
<li>Use specific DTO&#8217;s. Lots of boilerplate code but highest flexibility, both in adapting to technical issues and in shielding components from change.</li>
<li>Use generic DTO&#8217;s. Less boilerplate code but also less protection from the type system.</li>
</ol>
<p>Which approach is best depends on a number of variables, two of which are:</p>
<ol>
<li>How closely are the &#8220;client&#8221; and the &#8220;server&#8221; coupled <em>in the development cycle</em>? Does every change in the serer immediately mean that the client has to change anyway? For example, a Spring WebMVC view and its controllers are very closely coupled while a Flex UI and a Java backend are probably less closely coupled.</li>
<li>How closely are the &#8220;client&#8221; and the &#8220;server&#8221; coupled <em>technically</em>? Once again, a Spring Web MVC view communicates with its controller using Java objects in the same VM, while a Flex UI gets it objects over an AMF channel.</li>
</ol>
]]></content:encoded>
	</item>
	<item>
		<title>By: Simon Massey</title>
		<link>http://blog.xebia.com/2009/05/11/jpa-implementation-patterns-service-facades-and-data-transfers-objects/#comment-92857</link>
		<dc:creator>Simon Massey</dc:creator>
		<pubDate>Mon, 19 Oct 2009 23:55:22 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1692#comment-92857</guid>
		<description>&lt;strong&gt;@Vincent:&lt;/strong&gt; The comments to Adam Bien&#039;s posts have a strong negative reaction. In added my own comment. I did put in a caveat that using maps as an impedance mismatch solution when taking to a weak typed client seems okay. In my comment to Adam&#039;s blogg I added an observation to the effect that peer code review cannot always be trusted to catch problems if it is too easy to do the wrong thing in order to cut some corners ;)</description>
		<content:encoded><![CDATA[<p><strong>@Vincent:</strong> The comments to Adam Bien&#8217;s posts have a strong negative reaction. In added my own comment. I did put in a caveat that using maps as an impedance mismatch solution when taking to a weak typed client seems okay. In my comment to Adam&#8217;s blogg I added an observation to the effect that peer code review cannot always be trusted to catch problems if it is too easy to do the wrong thing in order to cut some corners <img src='http://blog.xebia.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vincent Partington</title>
		<link>http://blog.xebia.com/2009/05/11/jpa-implementation-patterns-service-facades-and-data-transfers-objects/#comment-92558</link>
		<dc:creator>Vincent Partington</dc:creator>
		<pubDate>Sun, 27 Sep 2009 17:22:33 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1692#comment-92558</guid>
		<description>&lt;strong&gt;@Simon Massey:&lt;/strong&gt; You are right in saying that the &quot;alien&quot; nature of the client pushed us in the direction of DTO&#039;s. But that is not the whole story; for some parts of the system we actually sent the entities straight to the front end (Flex is not that alien!) but we&#039;ve run into the limitations of that approach again. This time is not actually Hibernate&#039;s lazy CGLIB proxies that are acting up but some CGLIB proxies that we&#039;ve built ourselves (why is a subject for another blog ;-)). Therefore we are now refactoring that piece of code that also use DTO&#039;s.

And that probably answers your question: in a number of cases not having DTO&#039;s works pretty well. This is especially the case for the data structures oriented model which tends to be used in CRUD-like applications. My recommendation is to look into Service Facades and DTO&#039;s when your interactions with the entities becomes more complex and the benefit of having an abstraction layer outweighs the added complexity of another architecture layer and the hassle of having to copy stuff in and out.

BTW, &lt;a href=&quot;http://www.adam-bien.com/&quot; rel=&quot;nofollow&quot;&gt;Adam Bien&lt;/a&gt; just blogged about a very nice way to build &lt;a href=&quot;http://www.adam-bien.com/roller/abien/entry/no_duplication_no_decoupling_the&quot; rel=&quot;nofollow&quot;&gt;Generic DTO&#039;s&lt;/a&gt;.</description>
		<content:encoded><![CDATA[<p><strong>@Simon Massey:</strong> You are right in saying that the &#8220;alien&#8221; nature of the client pushed us in the direction of DTO&#8217;s. But that is not the whole story; for some parts of the system we actually sent the entities straight to the front end (Flex is not that alien!) but we&#8217;ve run into the limitations of that approach again. This time is not actually Hibernate&#8217;s lazy CGLIB proxies that are acting up but some CGLIB proxies that we&#8217;ve built ourselves (why is a subject for another blog <img src='http://blog.xebia.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> ). Therefore we are now refactoring that piece of code that also use DTO&#8217;s.</p>
<p>And that probably answers your question: in a number of cases not having DTO&#8217;s works pretty well. This is especially the case for the data structures oriented model which tends to be used in CRUD-like applications. My recommendation is to look into Service Facades and DTO&#8217;s when your interactions with the entities becomes more complex and the benefit of having an abstraction layer outweighs the added complexity of another architecture layer and the hassle of having to copy stuff in and out.</p>
<p>BTW, <a href="http://www.adam-bien.com/" rel="nofollow">Adam Bien</a> just blogged about a very nice way to build <a href="http://www.adam-bien.com/roller/abien/entry/no_duplication_no_decoupling_the" rel="nofollow">Generic DTO&#8217;s</a>.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
<!-- This Quick Cache file was built for (  blog.xebia.com/2009/05/11/jpa-implementation-patterns-service-facades-and-data-transfers-objects/feed/ ) in 0.53308 seconds, on Feb 9th, 2012 at 10:12 pm UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on Feb 9th, 2012 at 11:12 pm UTC -->
