<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>let x=x &#187; patterns</title>
	<atom:link href="http://www.crazymcphee.net/x/tag/patterns/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.crazymcphee.net/x</link>
	<description>programming idiom and methodology</description>
	<lastBuildDate>Tue, 13 Jul 2010 21:56:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>&#8216;Disappeared&#8217; DAO layers</title>
		<link>http://www.crazymcphee.net/x/2009/02/06/disappeared-dao-layers/</link>
		<comments>http://www.crazymcphee.net/x/2009/02/06/disappeared-dao-layers/#comments</comments>
		<pubDate>Fri, 06 Feb 2009 07:01:18 +0000</pubDate>
		<dc:creator>Scot Mcphee</dc:creator>
				<category><![CDATA[infrastructure and frameworks]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jpa]]></category>
		<category><![CDATA[patterns]]></category>
		<category><![CDATA[persistence]]></category>
		<category><![CDATA[test driven design]]></category>

		<guid isPermaLink="false">http://www.crazymcphee.net/x/?p=139</guid>
		<description><![CDATA[Adam Bien still wants to believe that a JPA layer can directly replace a formal &#8216;DAO layer&#8217;. And I still disagree. I might agree that for simple enough systems, it could end up being that in fact the service layers end up with PersistenceManager logic directly in their methods. But then I might say a [...]]]></description>
			<content:encoded><![CDATA[<p>Adam Bien still wants to believe that a <a href="http://www.adam-bien.com/roller/abien/entry/daos_aren_t_dead_but" target="_blank">JPA layer can directly replace a formal &#8216;DAO layer&#8217;</a>. And I still disagree.</p>
<p>I might agree that for simple enough systems, it could end up being that in fact the service layers end up with PersistenceManager logic directly in their methods. But then I might say a language and framework like Ruby on Rails could suit your application better in that case. For any sufficiently complex Java-based enterprise system, a POJO-style data persistence layer naturally evolves, in order to reduce as far as possible the responsibilities the service layer must carry out.</p>
<p>As mentioned by commentators on his blog, the discipline of test-driven design is usually  a strong driver of having a separated set of components that deal with persistence and other data concerns. Not just from the issue over mocking the PersistenceManager, which is certainly a strong driver, but also from design principles.</p>
<p>Service layers marshall requests from clients, and decompose them to call various component layers. Data access layers typically form part of these component layers. A TDD discipline means these data access layers usually end up with more than just CRUD operations.</p>
<p>Furthermore, as a matter of discipline, in any sufficiently complex service layer I prefer not to work directly with database abstractions (i.e. entities that are modelled on database tables and their relationships) but on first-class objects designed to fit directly with the modelled domain. I want those objects to have rich behaviour &#8211; the service layer is actually part of their behaviour. I want to start the design of the system by directly modelling those actions, e.g.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">passenger.<span style="color: #006633;">at</span><span style="color: #009900;">&#40;</span>origin<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
passenger.<span style="color: #006633;">orderJourney</span><span style="color: #009900;">&#40;</span>dispatch, destination<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Vehicle vehicle <span style="color: #339933;">=</span> dispatch.<span style="color: #006633;">sendVehicle</span><span style="color: #009900;">&#40;</span>origin<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
vehicle.<span style="color: #006633;">pickUp</span><span style="color: #009900;">&#40;</span>passenger<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
vehicle.<span style="color: #006633;">arrives</span><span style="color: #009900;">&#40;</span>destination<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">dropsOff</span><span style="color: #009900;">&#40;</span>passenger<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
vehicle.<span style="color: #006633;">notify</span><span style="color: #009900;">&#40;</span>dispatch<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I do not want these objects to know about database semantics. That is a responsibility of a separated persistence layer, which we may for convienience sake, called the DAO. Even if internally we end up with;</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Passenger <span style="color: #009900;">&#123;</span>
  at<span style="color: #009900;">&#40;</span>Location origin<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">origin</span><span style="color: #339933;">=</span>origin<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  call<span style="color: #009900;">&#40;</span>DispatchCentre dispatch, Location destination<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    dispatch.<span style="color: #006633;">passengerOrder</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, destination<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>DispatchCentre at this level, is a service, but we will model it as part of the domain of passengers, locations, and vehicles. Behind that service, it will have a database layer with the semantics as to how it saves and loads the data concerning passengers, locations, and vehicles. I don&#8217;t necessarily want the client objects (e.g. the web action) to have to deal with JPA annotated objects. They just deal with objects as much as possible. And I don&#8217;t see there&#8217;s anything served with developing possibly complex persistence logic directly in the DispatchCentre. In fact, taking a top-down approach, I know pretty much I won&#8217;t do that.</p>
<p>Only after I&#8217;ve done this initial part of the modelling, will I be concerned as to which of these components can be directly JPA or Hibernate annotated, which are Spring-injected services, which are Session EJBs, and the like. I think starting your modelling from the point of view of having a persistence framework like JPA or Session EJBs or any other &#8216;archtectural artifact&#8217; leads to an unnatural design.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.crazymcphee.net/x/2009/02/06/disappeared-dao-layers/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Does JPA kill the DAO Pattern?</title>
		<link>http://www.crazymcphee.net/x/2009/01/16/does-jpa-kill-the-dao-pattern/</link>
		<comments>http://www.crazymcphee.net/x/2009/01/16/does-jpa-kill-the-dao-pattern/#comments</comments>
		<pubDate>Fri, 16 Jan 2009 12:30:41 +0000</pubDate>
		<dc:creator>Scot Mcphee</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[ejb]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[jpa]]></category>
		<category><![CDATA[patterns]]></category>
		<category><![CDATA[persistence]]></category>

		<guid isPermaLink="false">http://www.crazymcphee.net/x/?p=44</guid>
		<description><![CDATA[Oliver Gierke definitely thinks not. And I concur with him. A couple of years ago I got into an running battle with a developer at my old workplace as to whether the JPA architecture abstracted away enough of the persistence to justify eliminating the database layer. My answer was, and still is, certainly not! The [...]]]></description>
			<content:encoded><![CDATA[<p>Oliver Gierke <a href="http://www.olivergierke.de/wordpress/2009/01/se-radio-episode-121-or-mappers/">definitely thinks not</a>. And I concur with him.</p>
<p>A couple of years ago I got into an running battle with a developer at my old workplace as to whether the JPA architecture abstracted away enough of the persistence to justify eliminating the database layer. My answer was, and still is, <em>certainly not!</em></p>
<p>The simple answer to this question is another question &#8211; <em>What if you wanted to replace JPA?</em> The users of the data layer, lets call these objects the SessionLayer, need not, and should not know what sort of persistence layer you&#8217;re using. JPA does manage to abstract away some annoyances with choosing a specific transactional persistence technology just as Hibernate or Toplink. But what if you need to replace database access with a call to a web service? Convert the entire application to use Spring transactions? Send a non-transactional JMS message? Or just write the data in a <em>flat file</em>? </p>
<p>If you have an entirely separate data access layer, which encapsulates your data persistence methods from its users, there&#8217;s only one place to change. It&#8217;s clients need not ever know you made that change. You have <em>isolated the responsibility</em>. JPA, unencapsulated, encourages users of the data layer to consider the method of persistence. Use JPA underneath your data layer.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.crazymcphee.net/x/2009/01/16/does-jpa-kill-the-dao-pattern/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>In which magic method naming is considered to be harmful</title>
		<link>http://www.crazymcphee.net/x/2009/01/16/inwhichmagicmethodnamingconsideredharmful/</link>
		<comments>http://www.crazymcphee.net/x/2009/01/16/inwhichmagicmethodnamingconsideredharmful/#comments</comments>
		<pubDate>Fri, 16 Jan 2009 06:05:41 +0000</pubDate>
		<dc:creator>Scot Mcphee</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[ejb]]></category>
		<category><![CDATA[patterns]]></category>
		<category><![CDATA[refactor]]></category>

		<guid isPermaLink="false">http://www.crazymcphee.net/x/?p=30</guid>
		<description><![CDATA[AKA EJB &#60;= 2.x Entities are a stupid idea stupidly implemented. A blast from the past. What idiot decided this was a good idea? public interface SomeEntity &#123; String getType&#40;&#41;; String getName&#40;&#41;; &#125; &#160; public interface SomeEntityHome extends EntityHome &#123; SomeEntity findBySomeData&#40;String name, String type&#41;; &#125; &#160; public class SomeEntityEjb implements SomeEntity, EntityBean &#123; public SomeEntity [...]]]></description>
			<content:encoded><![CDATA[<h3>AKA EJB &lt;= 2.<em>x</em> Entities are a stupid idea stupidly implemented.</h3>
<p>A blast from the past. What <em>idiot</em> decided this was a good idea?</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> SomeEntity <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">String</span> getType<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">String</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> SomeEntityHome <span style="color: #000000; font-weight: bold;">extends</span> EntityHome <span style="color: #009900;">&#123;</span>
    SomeEntity findBySomeData<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name, <span style="color: #003399;">String</span> type<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SomeEntityEjb <span style="color: #000000; font-weight: bold;">implements</span> SomeEntity, EntityBean <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> SomeEntity ejbFindBySomeData<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name, <span style="color: #003399;">String</span> type<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// some cogent implementation here</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getType<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">type</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">name</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Let&#8217;s refactor SomeEntityEjb.java shall we? Change that ejbFind method. Then spend the next half hour figuring out what you did wrong &#8211; Jboss doesn&#8217;t deploy the EJBs! Grep the code-base for &#8220;ejbFindBySomeData&#8221; &#8230; there&#8217;s got to be a magic specification of that method <em>somewhere</em> in the code. Nothing found?!  Hmm it&#8217;s been years since I&#8217;ve dealt with &lt; 3.0 Entity Beans. Struggle to recall the EJB specification .. what are the magical matching methods in the Home interface? <em>Oh yeah, that&#8217;s right!</em>  EntityHome has <span style="text-decoration: underline;">findXXX</span>, EntityBean has <span style="text-decoration: underline;">ejbFindXXX</span>. <em>Why, in god&#8217;s name, why?</em></p>
<p>No wonder people got dead-set against EJBs (meaning Entity EJBs) back in 2001 or 2002. Ahhh give me Spring and Hibernate anyday. And remember -<em> parallel magic method naming is a stupid idea - </em>especially in a statically typed language that is supposed to let the compiler do the hard work for you. Jesus kill the people who invented this. Please.</p>
<p>I wish I had the scope just to remove all these Entities &#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.crazymcphee.net/x/2009/01/16/inwhichmagicmethodnamingconsideredharmful/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>log.debug()</title>
		<link>http://www.crazymcphee.net/x/2009/01/14/logdebug/</link>
		<comments>http://www.crazymcphee.net/x/2009/01/14/logdebug/#comments</comments>
		<pubDate>Wed, 14 Jan 2009 06:39:16 +0000</pubDate>
		<dc:creator>Scot Mcphee</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[patterns]]></category>
		<category><![CDATA[refactor]]></category>

		<guid isPermaLink="false">http://www.crazymcphee.net/x/?p=17</guid>
		<description><![CDATA[So everybody knows printing debugging statements to the log file can take considerable amounts of time, right? And in order to decrease useless time logging debug statements that won&#8217;t appear in the log, you can use isDebugEnabled() to find out if you&#8217;re wasting your time, right? So you end up with; if &#40;log.isDebugEnabled&#40;&#41;&#41; &#123;    [...]]]></description>
			<content:encoded><![CDATA[<p>So everybody knows printing debugging statements to the log file can take considerable amounts of time, right? And in order to decrease useless time logging debug statements that won&#8217;t appear in the log, you can use isDebugEnabled() to find out if you&#8217;re wasting your time, right? So you end up with;</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>log.<span style="color: #006633;">isDebugEnabled</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    log.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;hello world&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>1000 times in your code &#8211; everywhere you want a debug statement.</p>
<p>But let&#8217;s say you don&#8217;t want to rely directly on logging framework. So in your infinite wisdom you write a logging <em>Decorator</em>, which decorates the Log4J Logger class.</p>
<p>I mean, now we can rid of all those 1000 <em>if statements</em>, right, and replace it with <em>just one</em>.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MySpecialLogger <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> Logger log <span style="color: #339933;">=</span> Logger.<span style="color: #006633;">logger</span><span style="color: #009900;">&#40;</span>MySpecialLogger.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> debug<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> message<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>log.<span style="color: #006633;">isDebugEnabled</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            log.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>But no &#8211; not if you are scared to refactor your code. You might end up with;</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>mylog.<span style="color: #006633;">isDebugEnabled</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    mylog.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;hello world&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">// do absolutely nothing else</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>mylog.<span style="color: #006633;">isDebugEnabled</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    mylog.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;goodbye cruel world&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Hundreds, if not thousands, of times in your code. And some other programmer can clean it up.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.crazymcphee.net/x/2009/01/14/logdebug/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Dependency Injection: &#8220;Don&#8217;t look for things!&#8221;</title>
		<link>http://www.crazymcphee.net/x/2009/01/13/dependency-injection-dont-look-for-things/</link>
		<comments>http://www.crazymcphee.net/x/2009/01/13/dependency-injection-dont-look-for-things/#comments</comments>
		<pubDate>Tue, 13 Jan 2009 11:49:48 +0000</pubDate>
		<dc:creator>Scot Mcphee</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[composition]]></category>
		<category><![CDATA[oo]]></category>
		<category><![CDATA[patterns]]></category>

		<guid isPermaLink="false">http://www.crazymcphee.net/x/?p=6</guid>
		<description><![CDATA[Here&#8217;s a piece of advice I definitely can take to heart. From Google testing blog: *Clean Code Talk Series* *Topic: Don&#8217;t Look For Things!* http://googletesting.blogspot.com/2008/11/clean-code-talks-dependency-injection.html Recently I&#8217;ve been doing maintenance work on a system that has a lot of custom-rolled code to do stuff like, look up EJB Home and Remote references. Even though parts [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a piece of advice I definitely can take to heart.</p>
<p><strong><ins>From Google testing blog:</ins></strong></p>
<p>*Clean Code Talk Series*<br />
*Topic: Don&#8217;t Look For Things!*</p>
<p><span class="nobr"><a rel="nofollow" href="http://googletesting.blogspot.com/2008/11/clean-code-talks-dependency-injection.html">http://googletesting.blogspot.com/2008/11/clean-code-talks-dependency-injection.html<sup><img class="rendericon" src="http://modular.autonomous.org/confluence/images/icons/linkext7.gif" border="0" alt="" width="7" height="7" align="absmiddle" /></sup></a></span></p>
<p><span class="nobr">Recently I&#8217;ve been doing maintenance work on a system that has a lot of custom-rolled code to do stuff like, look up EJB Home and Remote references. Even though parts of the system are five years old, even then there were a bunch of accepted patterns for getting this sort of work done. However there&#8217;s lots of motion, but very little movement. Even mis-named patterns in places (Delegates that aren&#8217;t, and so on).</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.crazymcphee.net/x/2009/01/13/dependency-injection-dont-look-for-things/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
