<?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></description>
	<lastBuildDate>Thu, 11 Mar 2010 16:23:01 +0100</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Sigmund Lundgren</title>
		<link>http://blog.xebia.com/2009/05/11/jpa-implementation-patterns-service-facades-and-data-transfers-objects/comment-page-1/#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-page-1/#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-page-1/#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-page-1/#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-page-1/#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>
	<item>
		<title>By: Simon Massey</title>
		<link>http://blog.xebia.com/2009/05/11/jpa-implementation-patterns-service-facades-and-data-transfers-objects/comment-page-1/#comment-92518</link>
		<dc:creator>Simon Massey</dc:creator>
		<pubDate>Wed, 23 Sep 2009 11:25:22 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1692#comment-92518</guid>
		<description>&lt;strong&gt;@Vincent&lt;/strong&gt; (said over on his previous blog post comments): If I understand [Simon Massey] correctly, your suggestion is a mix between the two approaches. There is a Service Facade but instead of using DTO’s you have the Service Facade make sure all domain objects are correctly initialized so that you can just return them as-is.

Yes. I just dusted off an re-read the first few chapters of Chris Richardson&#039;s POJOs In Action and found that the approach that I was trying to articulate is strongly influenced by reading his book back in 2006 (written pre-JPA). To summarize his writings: 

* His Entities have state and behavior. They are part of the domain model. 
* His Services are purely focused on manipulating entities to fulfill use case logic. They are part of the domain model. He says that where possible they delegate logic to the entities. 
* The façades return detached entities as the DTOs (&quot;POJO façades that use detached domain objects as DTOs&quot;). They are not part of his domain model. They worry about the plumbing (loading enough data to render on screen and merging detached entities). He is explicit about not copying to DTOs with his case study of a CRUD orientated app. 
* He has &#039;repositories&#039; which can be placed today by your DAOs (but I would call them Managers or Repositories to avoid confusion with old-school jdbc backed result set mappers with no lazy-loading).  

@Erik rightly points out Chapter 6 of Clean Code which says that we should be very aware of whether we are going for a data structures orientated model or a fully encapsulated object model that shields its internal fields (which we would fire DTOs into or Commands at and hide the internal fields of any entities from the client). The Chris Richardson book and my last three large scale systems fit into the data structures end of the entity spectrum. The services encapsulate the concerns of locking and workflow for the transactional use cases (in the business sense of placing an order as opposed to browsing through data) which have high complexity. Data structure like entities are exposed to the UI don&#039;t need public setters on fields to protected their class invariants. 

So my question to is what is the nature of the application for which the DTOs model is recommended in this blog? Is it a online rating engine which needs to follow a command like pattern to hide its internal fields (as recommended by Clean Code chapter 6) or is it a largely crud like app which could fit a data structures model? I feel that the type of logic expose to the client makes a significant impact on the suitability of either the exposed domain model or DTO design. Also whether the client is trusted code or not can have a big impact. If your using a plug-in on the client for UI presentation then you have different concerns about encapsulation of the domain model behind DTOs than with a Swing app or a ZK RIA webapp.</description>
		<content:encoded><![CDATA[<p><strong>@Vincent</strong> (said over on his previous blog post comments): If I understand [Simon Massey] correctly, your suggestion is a mix between the two approaches. There is a Service Facade but instead of using DTO’s you have the Service Facade make sure all domain objects are correctly initialized so that you can just return them as-is.</p>
<p>Yes. I just dusted off an re-read the first few chapters of Chris Richardson&#8217;s POJOs In Action and found that the approach that I was trying to articulate is strongly influenced by reading his book back in 2006 (written pre-JPA). To summarize his writings: </p>
<p>* His Entities have state and behavior. They are part of the domain model.<br />
* His Services are purely focused on manipulating entities to fulfill use case logic. They are part of the domain model. He says that where possible they delegate logic to the entities.<br />
* The façades return detached entities as the DTOs (&#8221;POJO façades that use detached domain objects as DTOs&#8221;). They are not part of his domain model. They worry about the plumbing (loading enough data to render on screen and merging detached entities). He is explicit about not copying to DTOs with his case study of a CRUD orientated app.<br />
* He has &#8216;repositories&#8217; which can be placed today by your DAOs (but I would call them Managers or Repositories to avoid confusion with old-school jdbc backed result set mappers with no lazy-loading).  </p>
<p>@Erik rightly points out Chapter 6 of Clean Code which says that we should be very aware of whether we are going for a data structures orientated model or a fully encapsulated object model that shields its internal fields (which we would fire DTOs into or Commands at and hide the internal fields of any entities from the client). The Chris Richardson book and my last three large scale systems fit into the data structures end of the entity spectrum. The services encapsulate the concerns of locking and workflow for the transactional use cases (in the business sense of placing an order as opposed to browsing through data) which have high complexity. Data structure like entities are exposed to the UI don&#8217;t need public setters on fields to protected their class invariants. </p>
<p>So my question to is what is the nature of the application for which the DTOs model is recommended in this blog? Is it a online rating engine which needs to follow a command like pattern to hide its internal fields (as recommended by Clean Code chapter 6) or is it a largely crud like app which could fit a data structures model? I feel that the type of logic expose to the client makes a significant impact on the suitability of either the exposed domain model or DTO design. Also whether the client is trusted code or not can have a big impact. If your using a plug-in on the client for UI presentation then you have different concerns about encapsulation of the domain model behind DTOs than with a Swing app or a ZK RIA webapp.</p>
]]></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-page-1/#comment-92513</link>
		<dc:creator>Simon Massey</dc:creator>
		<pubDate>Tue, 22 Sep 2009 18:24:59 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1692#comment-92513</guid>
		<description>I feel that it is the &quot;alien nature&quot; of the your client tier which makes DTOs a preferable solution for you. To explain what I mean we use of the ZK RIA framework as our client and that is a good fit with the Exposed Domain Model pattern. With that framework you keep detached entities as the application model bound to the desktop for re-use with multiple AJAX requests. The &quot;commands&quot; to manipulate them are directly modeled as Java UI Events that are fired into your UI controllers. The UI controllers then manipulated your entities and pass them to the service façade to have them merged and manipulated by application services. Only UI mapped entity fields are exposed to the browser so you don&#039;t have to have the security concerns. I wrote an article with some sample code (with no façade) here:

http://java.dzone.com/articles/using-desktop-model-view

With such an Exposed Domain Model approach you can either go with Open Session In View or you have to figure out how scope the transaction to a narrower scope. For scalability we prefer to scope the database transaction to a business façade. More or less the façade encapsulates the use case for the UI controller and needs to ensure that enough of the entities are loaded for rendering. The façade can re-attach the detached entities, and use queries to load more data, and merge the detached entities. The UI controller is responsible for replacing the merged entity returned from the façade into the UI model mapped to the browser screen. 

If we wanted to add a new client, say some soap over jms remote system, then we would also a façade. That would load data from the xml payload (which can be considered as an xml-ised DTO), load the referenced domain entities, map the xml-dto to a new/updated entity, perform validation of the &#039;command&#039; represented in the message, all within a transaction. The façade would re-use internal application services just like the façades for our &#039;native&#039; AJAX front-end. 
 
I must say I was keen to ditch the façades a while ago as they give a procedural &quot;transaction script pattern&quot; smell that plagued early EJB. Certainly in a small app they look like over-kill. Yet they perform a clear responsibility, particularly if we let then go straight to the DOAs occasionally as well as letting them use generalized internal services which encapsulate more complex business operations. An alternative pattern with a small app is to use something like a spring transaction template directly within the UI controller to &#039;in-line&#039; the façade logic into the AJAX UI controller whilst minimizing the scope of the JPA session to the transaction template call-back.</description>
		<content:encoded><![CDATA[<p>I feel that it is the &#8220;alien nature&#8221; of the your client tier which makes DTOs a preferable solution for you. To explain what I mean we use of the ZK RIA framework as our client and that is a good fit with the Exposed Domain Model pattern. With that framework you keep detached entities as the application model bound to the desktop for re-use with multiple AJAX requests. The &#8220;commands&#8221; to manipulate them are directly modeled as Java UI Events that are fired into your UI controllers. The UI controllers then manipulated your entities and pass them to the service façade to have them merged and manipulated by application services. Only UI mapped entity fields are exposed to the browser so you don&#8217;t have to have the security concerns. I wrote an article with some sample code (with no façade) here:</p>
<p><a href="http://java.dzone.com/articles/using-desktop-model-view" rel="nofollow">http://java.dzone.com/articles/using-desktop-model-view</a></p>
<p>With such an Exposed Domain Model approach you can either go with Open Session In View or you have to figure out how scope the transaction to a narrower scope. For scalability we prefer to scope the database transaction to a business façade. More or less the façade encapsulates the use case for the UI controller and needs to ensure that enough of the entities are loaded for rendering. The façade can re-attach the detached entities, and use queries to load more data, and merge the detached entities. The UI controller is responsible for replacing the merged entity returned from the façade into the UI model mapped to the browser screen. </p>
<p>If we wanted to add a new client, say some soap over jms remote system, then we would also a façade. That would load data from the xml payload (which can be considered as an xml-ised DTO), load the referenced domain entities, map the xml-dto to a new/updated entity, perform validation of the &#8216;command&#8217; represented in the message, all within a transaction. The façade would re-use internal application services just like the façades for our &#8216;native&#8217; AJAX front-end. </p>
<p>I must say I was keen to ditch the façades a while ago as they give a procedural &#8220;transaction script pattern&#8221; smell that plagued early EJB. Certainly in a small app they look like over-kill. Yet they perform a clear responsibility, particularly if we let then go straight to the DOAs occasionally as well as letting them use generalized internal services which encapsulate more complex business operations. An alternative pattern with a small app is to use something like a spring transaction template directly within the UI controller to &#8216;in-line&#8217; the façade logic into the AJAX UI controller whilst minimizing the scope of the JPA session to the transaction template call-back.</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-page-1/#comment-92195</link>
		<dc:creator>Vincent Partington</dc:creator>
		<pubDate>Tue, 28 Jul 2009 12:30:56 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1692#comment-92195</guid>
		<description>&lt;strong&gt;@Varun Mehta:&lt;/strong&gt; Yeah! Must be true then. ;-)</description>
		<content:encoded><![CDATA[<p><strong>@Varun Mehta:</strong> Yeah! Must be true then. <img src='http://blog.xebia.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Varun Mehta</title>
		<link>http://blog.xebia.com/2009/05/11/jpa-implementation-patterns-service-facades-and-data-transfers-objects/comment-page-1/#comment-92191</link>
		<dc:creator>Varun Mehta</dc:creator>
		<pubDate>Mon, 27 Jul 2009 21:50:05 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1692#comment-92191</guid>
		<description>Dude, looks like you expressed the same thoughts I did a few months back. 

http://damroo.blogspot.com/2009/02/2-dto-or-not-2-dto.html</description>
		<content:encoded><![CDATA[<p>Dude, looks like you expressed the same thoughts I did a few months back. </p>
<p><a href="http://damroo.blogspot.com/2009/02/2-dto-or-not-2-dto.html" rel="nofollow">http://damroo.blogspot.com/2009/02/2-dto-or-not-2-dto.html</a></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-page-1/#comment-92128</link>
		<dc:creator>Vincent Partington</dc:creator>
		<pubDate>Mon, 20 Jul 2009 07:12:48 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1692#comment-92128</guid>
		<description>&lt;strong&gt;@Alex Omilian:&lt;/strong&gt; Good point. I guess this really is one of those &quot;it depends&quot; items. ;-)  My main point is that one might be lured into not bothering with a Service Facade because of the features offered by modern web frameworks without weighing the pros and cons of that decision.

Of course, deciding to not have a Service Facade while being mindful of the fact that you might need it later but that some cost will be associated with the refactoring needed at that time is a very good approach.
</description>
		<content:encoded><![CDATA[<p><strong>@Alex Omilian:</strong> Good point. I guess this really is one of those &#8220;it depends&#8221; items. <img src='http://blog.xebia.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />   My main point is that one might be lured into not bothering with a Service Facade because of the features offered by modern web frameworks without weighing the pros and cons of that decision.</p>
<p>Of course, deciding to not have a Service Facade while being mindful of the fact that you might need it later but that some cost will be associated with the refactoring needed at that time is a very good approach.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
