<?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>Sadek Drobi's Blog &#187; J2EE</title>
	<atom:link href="http://sadekdrobi.com/category/j2ee/feed/" rel="self" type="application/rss+xml" />
	<link>http://sadekdrobi.com</link>
	<description>Sadek Drobi</description>
	<lastBuildDate>Tue, 08 Mar 2011 22:56:51 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>DCI in Real World: Domain Context and Interaction with Scala in a Real World Project</title>
		<link>http://sadekdrobi.com/2009/06/10/dci-in-real-world-domain-context-and-interaction-with-scala-in-a-real-world-project/</link>
		<comments>http://sadekdrobi.com/2009/06/10/dci-in-real-world-domain-context-and-interaction-with-scala-in-a-real-world-project/#comments</comments>
		<pubDate>Wed, 10 Jun 2009 22:40:09 +0000</pubDate>
		<dc:creator>Sadache</dc:creator>
				<category><![CDATA[Agile Programming]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Coplien]]></category>
		<category><![CDATA[DCI]]></category>
		<category><![CDATA[Delivering Value]]></category>
		<category><![CDATA[Domain Driven Design]]></category>
		<category><![CDATA[Domain-Context-Interaction]]></category>
		<category><![CDATA[J2EE]]></category>
		<category><![CDATA[JVM]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Multi-Paradigm Design]]></category>
		<category><![CDATA[Multi-languages projects]]></category>
		<category><![CDATA[Polyglot Programming]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[back-to-oop]]></category>

		<guid isPermaLink="false">http://sadekdrobi.com/2009/06/10/dci-in-real-world-domain-context-and-interaction-with-scala-in-a-real-world-project/</guid>
		<description><![CDATA[Those that follow my twitter @sadache , me @infoQ or my blog have certainly already noticed that I am quite interested in Scala on languages&#8217; axis and in Domain Context Interaction DCI pattern on architecture axis. I always search new ways for delivering quality code which is modular and concise. Modularity offers the opportunity to [...]]]></description>
			<content:encoded><![CDATA[<p>Those that follow <a href="http://twitter.com/sadache">my twitter @sadache</a> , <a href="http://www.infoq.com/author/sadek-drobi">me @infoQ</a> or my blog have certainly already noticed that I am quite interested in Scala on languages&#8217; axis and in <a href="http://www.artima.com/articles/dci_vision.html">Domain Context Interaction DCI</a> pattern on architecture axis. I always search new ways for delivering quality code which is modular and concise. Modularity offers the opportunity to think about the problem in parts, which is typical of the way brains work, whereas conciseness makes use of imaginary system (reading code blocks like images). </p>
<p>Recently, I&#8217;ve been working on a Web Api system where, thanks to support of <a href="http://twitter.com/morlhon">@jeanlaurent</a> <a href="http://morlhon.net/blog/">http://morlhon.net/blog/</a>, I used Scala applying DCI architecture in a real world project. This post is about reporting benefits of using this approach. Other posts will follow that will be more focused on the use of Scala and Functional Programming in that project. Code included is a bit simplified and parts of the system that are not of interest are omitted.</p>
<p><span id="more-638"></span>
<p>The concerned part of the system is an API for logging a Web API usage and using it for showing interesting statistics to users and for implementing a Web 2.0 business model authorization.</p>
<p>The story starts with an API key passed as a parameter in the requested URI something like <em>product/1234?api_key=AFFSEFFVFE2344</em></p>
<p>I create an object that represents the API user containing the key: (did I tell I like one liners?)</p>
<p><a href="http://sadekdrobi.com/wp-content/uploads/2009/06/image.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://sadekdrobi.com/wp-content/uploads/2009/06/image-thumb.png" width="260" height="40"></a>&nbsp; </p>
<p>This is a class of an object that holds the apiKey, now I need a method that I will call to log the use of product/123 for user represented by the apiKey AFFSEFFVFE2344. What could be quite convenient and OOP-iesh is to call a <em>record</em> method on the apiKey instance, something like <em>apiUser.record(&#8230;) .</em> The problem with this, as explained in the original DCI article, is that adding these methods to the domain objects adds noise in other contexts for which recording is not relevant such as authorization and statistics, as we will see later. I would rather use here the DCI&#8217;s <em>role</em> concept (or <em>context</em>). Doing so I would say <em>(apiUser in recording role).record(&#8230;) . </em>This pseudo code tends to be very close to mixins implemented in Scala language and will look like following in real world Scala code:</p>
<p><a href="http://sadekdrobi.com/wp-content/uploads/2009/06/image1.png"></a><a href="http://sadekdrobi.com/wp-content/uploads/2009/06/image2.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://sadekdrobi.com/wp-content/uploads/2009/06/image-thumb1.png" width="617" height="57"></a>&nbsp; </p>
<p>Here the ApiUser object doesn&#8217;t contain a record method by default and so it can be shared in different contexts. In other words we kept our domain objects pure yet not scarifying OOP convenience. Now the <em>Recorder</em> is a trait that minds only business in the context of recording. The definition looks like the following:</p>
<p><a href="http://sadekdrobi.com/wp-content/uploads/2009/06/image3.png"></a><a href="http://sadekdrobi.com/wp-content/uploads/2009/06/image4.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://sadekdrobi.com/wp-content/uploads/2009/06/image-thumb2.png" width="281" height="108"></a>&nbsp; </p>
<p>Another view of the system is the end user&#8217;s statistics. Statistics should be represented in a meaningful way to the user and the user can&#8217;t care less about how the recording is done. So, it is quite desirable not to have the noise of recording when talking about the statistics view context. Here is how the call looks like in our example in Scala:</p>
<p><a href="http://sadekdrobi.com/wp-content/uploads/2009/06/image5.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://sadekdrobi.com/wp-content/uploads/2009/06/image-thumb3.png" width="667" height="63"></a> </p>
<p>Again what we are doing here looks merely like saying: I want to use the ApiUser in a statistics context and I choose to call method getRequestsPerMonthForLast12Months. I guess the convenience brought by using this approach is obvious. This is how the Statistics trait looks like:</p>
<p><a href="http://sadekdrobi.com/wp-content/uploads/2009/06/image6.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://sadekdrobi.com/wp-content/uploads/2009/06/image-thumb4.png" width="647" height="150"></a> </p>
<p>Note that as in the Recording trait, the <em>ApiUser</em> object is not passed as an explicit parameter but rather as an implicit receiver parameter (something like <em>this</em> for an object). To access it we use <em>Self</em> that refers to it<em>.</em></p>
<p>The third view of the system is what allows us to provide an authorization mechanism based on user&#8217;s activities for a business plan they choose (be it a quota per day, per month or other interesting combinations). It could be quite convenient to have the call looking like <em>apiUser.isAuthorized . </em>We get pretty close with DCI without polluting our domain object with a specific context logic:</p>
<p><a href="http://sadekdrobi.com/wp-content/uploads/2009/06/image7.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://sadekdrobi.com/wp-content/uploads/2009/06/image-thumb5.png" width="540" height="66"></a> </p>
<p>Like the previous, the Authorizer trait looks straightforward :</p>
<p><a href="http://sadekdrobi.com/wp-content/uploads/2009/06/image8.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://sadekdrobi.com/wp-content/uploads/2009/06/image-thumb6.png" width="454" height="207"></a> </p>
<p>Now that we looked at the three views of our system introducing three examples of &#8220;<em>Context</em>&#8221; from DCI pattern we can go beyond that to discover how we use nested <em>contexts</em> (or <em>roles</em>). </p>
<p>Looking at the Authorizer trait, the ApiUser referred to by <em>Self,</em> doesn&#8217;t tell us much about<em> </em>how many requests the user did today or this month. And for authorizing user one needs a richer ApiUser that can give the necessary information that live themselves in a file or a database. But once again, we don&#8217;t want to pollute our domain object with Data Access code that varies on a different axis than the domain object itself.&nbsp; Introducing a nested context/role that adds the Data Access logic would pretty convenient here:</p>
<p><a href="http://sadekdrobi.com/wp-content/uploads/2009/06/image9.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://sadekdrobi.com/wp-content/uploads/2009/06/image-thumb7.png" width="473" height="56"></a> </p>
<p>Here <em>Self </em>is a richer ApiUser that carries Requests Data Access logic defined in the RequestsDataAccessor trait:</p>
<p><a href="http://sadekdrobi.com/wp-content/uploads/2009/06/image10.png"></a><a href="http://sadekdrobi.com/wp-content/uploads/2009/06/image11.png"></a><a href="http://sadekdrobi.com/wp-content/uploads/2009/06/image12.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://sadekdrobi.com/wp-content/uploads/2009/06/image-thumb8.png" width="848" height="193"></a>&nbsp;&nbsp; </p>
<p>And the implementation of the Authorizer trait defined in terms of <em>ApiUser with RequestsDataAccessor</em>:</p>
<p><a href="http://sadekdrobi.com/wp-content/uploads/2009/06/image13.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://sadekdrobi.com/wp-content/uploads/2009/06/image-thumb9.png" width="468" height="373"></a> </p>
<p>In the same way we implement the <em>Statistics</em> and <em>Recorder</em> traits using <em>ApiUser with RequestsDataAccessor:</em></p>
<p><a href="http://sadekdrobi.com/wp-content/uploads/2009/06/image14.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://sadekdrobi.com/wp-content/uploads/2009/06/image-thumb10.png" width="451" height="148"></a> </p>
<p><a href="http://sadekdrobi.com/wp-content/uploads/2009/06/image15.png"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://sadekdrobi.com/wp-content/uploads/2009/06/image-thumb11.png" width="581" height="295"></a> </p>
<p>If you are not familiar with generics ( getRequestsPer[Day] ) you can ignore them since they are not central to the subject of this post and simply use their readability to understand the purpose of the method. The use of generics in this project will be the subject of a future post.</p>
<h3>Encapsulation Test!</h3>
<p>Now in the first version of the system, the ApiKey was passed in clear in the service URI. This is not very secured as it is quite easy to get the key and use it for using the API. We might prefer to encrypt the key in some way. This fortunately changes nothing for any of our traits implementations since the decryption of the key is the responsiblity of the ApiUSer object and all what our traits need is to get the concerned ApiKey from that object which can be easily done with pattern matching. Same thing if we want to replace database logging with file system in RequestsDataAccessor. Modularity and separation of concerns are apparent in this DCI architecture implementation.</p>
<p>&nbsp;</p>
<h3>Conclusion</h3>
<p>This is a refreshing approach to OOP. The <strong>Domain Context Interaction</strong> pattern allowed us to restore interesting properties of real OOP approach yet not sacrificing modularity and separation of concerns. This simple view of &#8220;an object in several contexts&#8221; helps quite much in thinking explicitly about and understanding context boundaries. Note that an object(instance) is not defined by a single class but each instance curries the necessary context. This is essential to OOP which is NOT COP (for Class Oriented Programming ;) )</p>
<p>&nbsp;</p>
<p>Feel free to ask questions and/or comment about this implementation. Refer to original papers for more in-depth explanation of the pattern. </p>
<p>Thanks to JetBrains&#8217; IDEA and the Scala plug-in that helped me programming in Scala.</p>
<p>&nbsp;</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:7cedd753-901b-4d50-b76a-a7c30a681f4e" class="wlWriterSmartContent">Mots clés Technorati : <a href="http://technorati.com/tags/DCI" rel="tag">DCI</a>,<a href="http://technorati.com/tags/Coplien" rel="tag">Coplien</a>,<a href="http://technorati.com/tags/OOP" rel="tag">OOP</a>,<a href="http://technorati.com/tags/Scala" rel="tag">Scala</a>,<a href="http://technorati.com/tags/J2EE" rel="tag">J2EE</a>,<a href="http://technorati.com/tags/Domain-Context-Interaction" rel="tag">Domain-Context-Interaction</a>,<a href="http://technorati.com/tags/Architecture" rel="tag">Architecture</a>,<a href="http://technorati.com/tags/Multi-Paradigm-Design" rel="tag">Multi-Paradigm-Design</a></div>
]]></content:encoded>
			<wfw:commentRss>http://sadekdrobi.com/2009/06/10/dci-in-real-world-domain-context-and-interaction-with-scala-in-a-real-world-project/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Who told Java Checked Exceptions were a Bad Idea?</title>
		<link>http://sadekdrobi.com/2009/01/04/who-told-java-checked-exceptions-were-a-bad-idea/</link>
		<comments>http://sadekdrobi.com/2009/01/04/who-told-java-checked-exceptions-were-a-bad-idea/#comments</comments>
		<pubDate>Sun, 04 Jan 2009 00:50:22 +0000</pubDate>
		<dc:creator>Sadache</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[DOTNET]]></category>
		<category><![CDATA[Decipline]]></category>
		<category><![CDATA[J2EE]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[ORM]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Spring Framework]]></category>

		<guid isPermaLink="false">http://sadekdrobi.com/2009/01/04/who-told-java-checked-exceptions-were-a-bad-idea/</guid>
		<description><![CDATA[
One of the few things that lack seriously, IMO, in C# is the power of Java checked exceptions. In Java world? things happen weirdly. Maybe it is a result of crowd intelligence and a bad side effect of open source. Often, people in J2EE world tend to believe the opposite. Anyway you see the effect [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://sadekdrobi.com/wp-content/uploads/2009/01/dsc-02851.jpg"><img style="border-right: 0px; border-top: 0px; margin: 0px 15px 10px 0px; border-left: 0px; border-bottom: 0px" height="315" alt="DSC_0285" src="http://sadekdrobi.com/wp-content/uploads/2009/01/dsc-0285-thumb1.jpg" width="459" align="left" border="0"></a>
<p>One of the few things that lack seriously, IMO, in C# is the power of Java checked exceptions. In Java world? things happen weirdly. Maybe it is a result of <a href="http://en.wikipedia.org/wiki/The_Wisdom_of_Crowds#Failures_of_crowd_intelligence">crowd intelligence</a> and a bad side effect of open source. Often, people in J2EE world tend to believe the opposite. Anyway you see the effect in most preferred frameworks (such as today&#8217;s popular ORMs and Containers) of wrapping checked Java exceptions and rethrowing them as runtime ones. </p>
<p><span id="more-569"></span></p>
<p>I guess offering runtime exceptions together with compile time ones did a hole in the Java type system, but this hole was left to discipline, in a way of giving the tools and letting smart enterprise programmers decide for the right tool for the job. Things turned to happen differently in the enterprise world.&nbsp; Programmers abused of this freedom of choice and mainstream frameworks designers decided to dismiss and bury one of the most interesting features of the Java type system.</p>
<p>Why do I like Java checked exceptions? Well simply because it saves me from surprises. And I tend not to like surprises much!</p>
<p>The funny thing is that, in my experience, it is mostly architects that dismiss Java checked exceptions whereas Compile time exceptions are a very good asset for cleaner and bug free architecture.</p>
<p>I just take a look at a &#8220;standard&#8221; DAO method that gets an entity</p>
<p>Customer getCustomer(CustomerId id);</p>
<p>This method lies! what if there is no connection? what if the Id is incorrect? The signature of the methods gives quite a promise that under all circumstances it will returns a Customer. Now for any architecture this is bad. It used to be better before some popular frameworks decided to completely omit it! Before? well I used to control my architecture better!</p>
<p>Before these &#8220;good practices&#8221; become mainstream, imposed in mainstream frameworks, It used to be often more predictable the return type a method has. For my example, the signature would tell me that the method will either return a customer OR throws a &#8220;Dao Exception&#8221;.</p>
<p>Customer getCustomer(CustomerId id) throws DaoException;</p>
<p>No surprise. Now when I use this method, I will certainly need to handle the case when the method will return something else than a Customer. Now of course, a Java &#8220;enterprise developer&#8221; could do one of to things:</p>
<p>1- Out of laziness or incompetence, keep rethrowing data access exceptions up until you&#8217;ll have to deal with it up in the GUI logic (where handling an infrastructure error does not often belong)</p>
<p>This often sounds for me the main argument for dismissing Java Compile Time Exceptions claiming that it is a &#8220;code boilerplate&#8221;. The thing is that you&#8217;ll have to deal with these exceptions once even when you turn them into runtime. The fact of wrapping them into runtime does not eliminate them (even if I wish it did). But having them as compile time will make easy to spot where a &#8220;bad&#8221; developer did not handle the exception correctly simply by following the signature and type system. Then the solution will be to wrap them into more business friendly, context meaningful Compile Time Exceptions that need to be handled. This way the contract communicated better the result of executing a piece of logic.</p>
<p>This turns to be as useful for &#8220;void&#8221; return type methods. With Compile Time Exception a method can simply communicate the fact that it will either return &#8220;void&#8221; or will throw an exception to be handled.</p>
<p>2- Out of laziness or incompetence, frustrated developers will start &#8220;catch&#8221;ing checked exception without rethrowing them.</p>
<p>This one is a result of the hole in the type system that is left intentionally to leave some place for flexibility relying on discipline. Nevertheless, and before the big favor of generous frameworks I mentioned earlier, this turned out to be an easy problem to spot. Most IDEs and code analyses tools can find easily empty catch clauses and warn me about them.</p>
<p>&nbsp;</p>
<p>I often get surprised of the limited use of Java language wealth in enterprise J2EE applications!</p>
<p>Good thing: C# and .Net are introducing checked exceptions together with other DbC stuff (like non null objects) in the next release. Sad thing: Java has Compile Time Exceptions since version 1, open source chose to unvalue them!</p>
]]></content:encoded>
			<wfw:commentRss>http://sadekdrobi.com/2009/01/04/who-told-java-checked-exceptions-were-a-bad-idea/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss>

