<?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; JVM</title>
	<atom:link href="http://sadekdrobi.com/category/jvm/feed/" rel="self" type="application/rss+xml" />
	<link>http://sadekdrobi.com</link>
	<description>Sadek Drobi</description>
	<lastBuildDate>Wed, 06 Jan 2010 23:11:59 +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>Yet another million dollar mistake?</title>
		<link>http://sadekdrobi.com/2009/05/04/yet-another-million-dollar-mistake/</link>
		<comments>http://sadekdrobi.com/2009/05/04/yet-another-million-dollar-mistake/#comments</comments>
		<pubDate>Mon, 04 May 2009 23:53:21 +0000</pubDate>
		<dc:creator>Sadache</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[JVM]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://sadekdrobi.com/2009/05/04/yet-another-million-dollar-mistake/</guid>
		<description><![CDATA[Martin Odersky:

In the generics design, there were a lot of very, very hard constraints. The strongest constraint, the most difficult to cope with, was that it had to be fully backwards compatible with ungenerified Java. The story was the collections library had just shipped with 1.2, and Sun was not prepared to ship a completely [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Martin Odersky</strong>:<br />
<blockquote>
<p>In the generics design, there were a lot of very, very hard constraints. The strongest constraint, the most difficult to cope with, was that it had to be fully backwards compatible with ungenerified Java. The story was the collections library had just shipped with 1.2, and Sun was not prepared to ship a completely new collections library just because generics came about. So instead it had to just work completely transparently.
<p>That&#8217;s why there were a number of fairly ugly things. You always had to have ungenerified types with generified types, the so called raw types. Also you couldn&#8217;t change what arrays were doing so you had unchecked warnings. Most importantly you couldn&#8217;t do a lot of the things you wanted to do with arrays, like generate an array with a type parameter T, an array of something where you didn&#8217;t know the type. [..]
<p>When Java first shipped, Bill Joy and James Gosling and the other members of the Java team thought that Java should have generics, only they didn&#8217;t have the time to do a good job designing it in. So because there would be no generics in Java, at least initially, they felt that arrays had to be covariant. That means an array of String is a subtype of array of Object, for example. The reason for that was they wanted to be able to write, say, a “generic” sort method that took an array of Object and a comparator and that would sort this array of Object. And then let you pass an array of String to it. <strong>It turns out that this thing is type unsound in general. That&#8217;s why you can get an array store exception in Java. And it actually also turns out that this very same thing blocks a decent implementation of generics for arrays. That&#8217;s why arrays in Java generics don&#8217;t work at all. You can&#8217;t have an array of list of string, it&#8217;s impossible. You&#8217;re forced to do the ugly raw type, just an array of list, forever. So it was sort of like an original sin. They did something very quickly and thought it was a quick hack. But it actually ruined every design decision later on.</strong></p>
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://sadekdrobi.com/2009/05/04/yet-another-million-dollar-mistake/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Ted Neward on Present and Past Languages</title>
		<link>http://sadekdrobi.com/2008/10/28/ted-neward-on-present-and-past-languages/</link>
		<comments>http://sadekdrobi.com/2008/10/28/ted-neward-on-present-and-past-languages/#comments</comments>
		<pubDate>Tue, 28 Oct 2008 13:28:58 +0000</pubDate>
		<dc:creator>Sadache</dc:creator>
				<category><![CDATA[Conferences]]></category>
		<category><![CDATA[DOTNET]]></category>
		<category><![CDATA[DSL]]></category>
		<category><![CDATA[F#]]></category>
		<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[InfoQ]]></category>
		<category><![CDATA[JVM]]></category>
		<category><![CDATA[LinQ]]></category>
		<category><![CDATA[Polyglot Programming]]></category>

		<guid isPermaLink="false">http://sadekdrobi.com/2008/10/28/ted-neward-on-present-and-past-languages/</guid>
		<description><![CDATA[This is an interview I did at QCon with Ted Neward. Talking to Ted was very interesting even though arguing with him turned to be not easy at all :)
]]></description>
			<content:encoded><![CDATA[<p>This is <a href="http://www.infoq.com/interviews/Languages-Ted-Neward">an interview I did at QCon with Ted Neward</a>. Talking to <a href="http://blogs.tedneward.com/">Ted</a> was very interesting even though arguing with him turned to be not easy at all :)</p>
]]></content:encoded>
			<wfw:commentRss>http://sadekdrobi.com/2008/10/28/ted-neward-on-present-and-past-languages/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
