<?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; Multi-Paradigm Design</title>
	<atom:link href="http://sadekdrobi.com/category/multi-paradigm-design/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>A monad in C# for simplifying WPF multi-threading for a more responsive GUI</title>
		<link>http://sadekdrobi.com/2009/06/26/a-monad-in-c-for-simplifying-wpf-multi-threading-for-a-more-responsive-gui/</link>
		<comments>http://sadekdrobi.com/2009/06/26/a-monad-in-c-for-simplifying-wpf-multi-threading-for-a-more-responsive-gui/#comments</comments>
		<pubDate>Fri, 26 Jun 2009 19:13:40 +0000</pubDate>
		<dc:creator>Sadache</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[DOTNET]]></category>
		<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[LinQ]]></category>
		<category><![CDATA[MVP]]></category>
		<category><![CDATA[Multi-Paradigm Design]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[Monads]]></category>
		<category><![CDATA[Thread]]></category>

		<guid isPermaLink="false">http://sadekdrobi.com/?p=563</guid>
		<description><![CDATA[
Code included here is over simplified for clarity, I hosted a better implementation code on CodePlex. These  modifications change strictly nothing for the client code and are only an implementation detail. I use a continuation rather than a delay, and I chose to design a custom continuation class rather than using a delegate because [...]]]></description>
			<content:encoded><![CDATA[<div style="font-family: courier new; background: white; color: gray; font-size: 10pt">
<p>Code included here is over simplified for clarity, I hosted a better implementation code on <a href="http://viewmonad.codeplex.com/sourcecontrol/changeset/view/22890?projectName=viewmonad#447750">CodePlex</a>. These  modifications change strictly nothing for the client code and are only an implementation detail. I use a continuation rather than a delay, and I chose to design a custom continuation class rather than using a delegate because of a type system limitations.</p>
</div>
<p><a href="http://sadekdrobi.com/wp-content/uploads/2008/12/dsc-2468.jpg"><img style="border-right-width: 0px; margin: 0px 15px 0px 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="DSC_2468" align="left" src="http://sadekdrobi.com/wp-content/uploads/2008/12/dsc-2468-thumb-196x300.jpg" width="279" height="426"></a>Most GUI frameworks, including Silverlight and WPF, are shipped with a fundamental problem: long use of the main thread causes the Window to blackout, and using different threads requires you to get your hands dirty with the Dispatcher stuff and freezable objects. Worse, you wont learn the necessity to do so until you get a surprise of &#8220;The calling thread cannot access this object because a different thread owns it.&#8221; exception when all what you were doing is to use available methods on an object that seemed you have access to, at lease it seemed until runtime! This post illustrates a solution based on Monads abstraction and LinQ syntax.<br />
<span id="more-563"></span></p>
<p>This is a problem you get often when applying Model View Presenter pattern [MVP]. There, your view (which is a WPF control) implements a contract IView that the presenter in its turn will use to extract values and then make operations on the view.</p>
<p>&nbsp;</p>
<p><em>interface ISayHello { string GetName(); Unit SayHello(string name); } </em></p>
<p><em>class MyLoginControl:Control,<strong>ISayHello </strong>{</em></p>
<p><em>//this interface gets implemented by the corresponding control as texboxes and a text area&#8230;</em></p>
<p><em>}</em></p>
<p>The problem appears when for doing any realistic responsive application, the presenter (representing business operations if you&#8217;d like) will have to run on a background thread to leave the main thread free for graphics rendering.</p>
<p>While working on a background thread, the presenter needs to access the view (having a reference to it through a contract) and there something wrong happen <strong>&#8220;The calling thread cannot access this object because a different thread owns it.&#8221;</strong> .&nbsp; </p>
<p>The problem here is simply that the view is giving quite a promise that it simply can not satisfy which is implementing the IView contract.</p>
<p>The view cannot satisfy the <em><strong>ISayHello </strong></em>contract under all circumstances, and not even under a commonly desirable condition (the presenter or business code running on another thread). This fact is simply not communicated through the type.</p>
<p>The solution I suggest to this problem that I implemented and employed in a production real world project is based on the LinQ syntax added to C# last year. In the solution I use two things: an extension method and a special type.</p>
<p>The type that I use is the monadic type (thanks to Wesdyer for his enlightening posts): <em><strong>View&lt;T&gt;</strong></em></p>
<p>So in my case my type will be View&lt;IView&gt; which means that what I am offering here is a a type that acts under some special circumstances as the Contract <em><strong>ISayHello</strong></em> . If I want for example to extract the name from the WPF control, I need to do something with the <strong><em><em><strong>View&lt;ISayHello&gt;</strong></em>.</em></strong>&nbsp;</p>
<h2>Using the View&lt;T&gt; Monad:</h2>
<p>And here comes the LinQ syntax for help. Having a reference to <em><strong>View&lt;ISayHello&gt;</strong></em>, the only way with which I can access the desired value or methods is using <strong>Linq</strong>:</p>
<p>Having the view contract: </p>
<p><!--<br />
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 interface\cf0  \cf4 ISayHello\par ??\cf0     \{\par ??        \cf4 Unit\cf0  SayHello(\cf1 string\cf0  Name);\par ??        \cf1 string\cf0  GetName();\par ??    \}\par ??    }<br />
--></p>
<div style="font-family: courier new; background: white; color: black; font-size: 10pt">
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span><span style="color: blue">public</span> <span style="color: blue">interface</span> <span style="color: #2b91af">ISayHello</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Unit</span> SayHello(<span style="color: blue">string</span> Name);</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">string</span> GetName();</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp; } <a href="$image[43].png"></a>&nbsp;<a href="$image[22].png"></a> </p>
</div>
<p>I can extract a view monad that I can pass to the presenter as <em><strong>View&lt;ISayHello&gt;</strong></em>&nbsp;</p>
<p><!--<br />
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red43\green145\blue175;\red255\green255\blue255;\red0\green0\blue0;\red0\green0\blue255;}??\fs20 \cf1 View\cf0 &lt;\cf1 ISayHello\cf0 &gt; view = \cf4 this\cf0 .AsView&lt;\cf1 Window1\cf0 , \cf1 ISayHello\cf0 &gt;();}<br />
--></p>
<div style="font-family: courier new; background: white; color: black; font-size: 10pt">
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span><span style="color: #2b91af">View</span>&lt;<span style="color: #2b91af">ISayHello</span>&gt; view = <span style="color: blue">this</span>.AsView&lt;<span style="color: #2b91af">Window1</span>, <span style="color: #2b91af">ISayHello</span>&gt;(); </p>
</div>
<p>note the type <em><strong>View&lt;ISayHello&gt;</strong></em> which is somehow useless without the LinQ syntax:</p>
<p><!--<br />
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;}??\fs20 (\cf3 from\cf0  v \cf3 in\cf0  view\par ??                \cf3 let\cf0  name = v.GetName()\par ??                \cf3 select\cf0  v.SayHello(name)).Do();}<br />
--></p>
<div style="font-family: courier new; background: white; color: black; font-size: 10pt">
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>(<span style="color: blue">from</span> v <span style="color: blue">in</span> view</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;<span style="color: blue">let</span> name = v.GetName()</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;<span style="color: blue">select</span> v.SayHello(name)).Do();</p>
</div>
<p>and you can also use several contracts in the same expression:</p>
<p><!--<br />
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;}??\fs20 \cf1 from\cf0  v \cf1 in\cf0  view1\par ??                \cf1 from\cf0  v2 \cf1 in\cf0  view2}<br />
--></p>
<div style="font-family: courier new; background: white; color: black; font-size: 10pt">
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span><span style="color: blue">from</span> v <span style="color: blue">in</span> view1</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span><span style="color: blue">from</span> v2 <span style="color: blue">in</span> view2</p>
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px">&nbsp;</p>
</div>
<h2>Implementation of the View&lt;T&gt; Monad:</h2>
<p>&nbsp;</p>
<p>View&lt;T&gt; is not really special. It is just a Delay&lt;T&gt; which is a Func&lt;T&gt;. And the Select implementation is the same one for functions and is not special at all: </p>
<p><!--<br />
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 delegate\cf0  R \cf4 View\cf0 &lt;R&gt;();}<br />
--></p>
<div style="font-family: courier new; background: white; color: black; font-size: 10pt">
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span><span style="color: blue">public</span> <span style="color: blue">delegate</span> R <span style="color: #2b91af">View</span>&lt;R&gt;();</p>
</div>
<p><!--<br />
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 enum\cf0  \cf4 Unit\par ??\cf0     \{\par ??        Unit\par ??    \}\par ??    \cf1 public\cf0  \cf1 static\cf0  \cf1 class\cf0  \cf4 WPFMonadExtensions\par ??\cf0     \{\par ??        \cf1 public\cf0  \cf1 static\cf0  \cf4 View\cf0 &lt;U&gt; SelectMany&lt;T, U&gt;(\cf1 this\cf0  \cf4 View\cf0 &lt;T&gt; m, \cf4 Func\cf0 &lt;T, \cf4 View\cf0 &lt;U&gt;&gt; k)\par ??        \{\par ??            \cf1 return\cf0  () =&gt; k(m())();\par ??        \}\par ??        \cf1 public\cf0  \cf1 static\cf0  \cf4 View\cf0 &lt;U&gt; Select&lt;T, U&gt;(\cf1 this\cf0  \cf4 View\cf0 &lt;T&gt; m, \cf4 Func\cf0 &lt;T, U&gt; selector)\par ??        \{\par ??            \cf1 return\cf0  () =&gt; selector(m());\par ??        \}\par ??        \cf1 public\cf0  \cf1 static\cf0  \cf4 View\cf0 &lt;V&gt; SelectMany&lt;T, U, V&gt;(\cf1 this\cf0  \cf4 View\cf0 &lt;T&gt; source, \cf4 Func\cf0 &lt;T, \cf4 View\cf0 &lt;U&gt;&gt; kSelector, \cf4 Func\cf0 &lt;T, U, V&gt; resultSelector)\par ??        \{\par ??            \cf1 return\cf0  () =&gt;\par ??                       \{\par ??                           \cf1 var\cf0  t = source();\par ??                           \cf1 var\cf0  u = kSelector(t)();\par ??                           \cf1 return\cf0  resultSelector(t, u);\par ??                       \};\par ??        \}\par ??        \cf1 public\cf0  \cf1 static\cf0  \cf4 Unit\cf0  Do(\cf1 this\cf0  \cf4 View\cf0 &lt;\cf4 Unit\cf0 &gt; k)\par ??        \{\par ??            \cf1 return\cf0  k();\par ??        \}}<br />
--></p>
<div style="font-family: courier new; background: white; color: black; font-size: 10pt">
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span><span style="color: blue">public</span> <span style="color: blue">enum</span> <span style="color: #2b91af">Unit</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Unit</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">static</span> <span style="color: blue">class</span> <span style="color: #2b91af">WPFMonadExtensions</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">static</span> <span style="color: #2b91af">View</span>&lt;U&gt; SelectMany&lt;T, U&gt;(<span style="color: blue">this</span> <span style="color: #2b91af">View</span>&lt;T&gt; m, <span style="color: #2b91af">Func</span>&lt;T, <span style="color: #2b91af">View</span>&lt;U&gt;&gt; k)</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> () =&gt; k(m())();</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">static</span> <span style="color: #2b91af">View</span>&lt;U&gt; Select&lt;T, U&gt;(<span style="color: blue">this</span> <span style="color: #2b91af">View</span>&lt;T&gt; m, <span style="color: #2b91af">Func</span>&lt;T, U&gt; selector)</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> () =&gt; selector(m());</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">static</span> <span style="color: #2b91af">View</span>&lt;V&gt; SelectMany&lt;T, U, V&gt;(<span style="color: blue">this</span> <span style="color: #2b91af">View</span>&lt;T&gt; source, <span style="color: #2b91af">Func</span>&lt;T, <span style="color: #2b91af">View</span>&lt;U&gt;&gt; kSelector, <span style="color: #2b91af">Func</span>&lt;T, U, V&gt; resultSelector)</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> () =&gt;</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> t = source();</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> u = kSelector(t)();</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> resultSelector(t, u);</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; };</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">static</span> <span style="color: #2b91af">Unit</span> Do(<span style="color: blue">this</span> <span style="color: #2b91af">View</span>&lt;<span style="color: #2b91af">Unit</span>&gt; k)</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> k();</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
</div>
<p>The fact that I am defining a new delegate type here (View&lt;T&gt;) is because we don&#8217;t have type synonyms is C#. And because of this I had to reimplement all the Select methods for this type. Of course all of that Monad plumping code is invisible and all the user needs to do is use the LinQ syntax.</p>
<p>The only specific part about the View monad, is the way you extract it. For Wpf for example the .AsView implementation looks like: </p>
<p>&nbsp;</p>
<p><!--<br />
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 static\cf0  \cf4 View\cf0 &lt;TView&gt; AsView&lt;TWPF, TView&gt;(\cf1 this\cf0  TWPF value) \cf1 where\cf0  TWPF : \cf4 UIElement\cf0 , TView\par ??        \{\par ??            \cf1 return\cf0  value.ToWpfMonad&lt;TWPF, TView&gt;();\par ??        \}\par ??\par ??        \cf1 public\cf0  \cf1 static\cf0  \cf4 View\cf0 &lt;Answer&gt; ToWpfMonad&lt;T, Answer&gt;(\cf1 this\cf0  T value)\par ??           \cf1 where\cf0  T : \cf4 UIElement\cf0 , Answer\par ??        \{\par ??            \cf1 return\cf0  () =&gt;\par ??            \{\par ??                Answer a = \cf1 default\cf0 (Answer);\par ??                value.Dispatcher.Invoke(\cf4 DispatcherPriority\cf0 .Normal, (\cf4 EventHandler\cf0 )((sender, e) =&gt;\par ??                \{\par ??                    a = value;\par ??                    \cf1 if\cf0  (a \cf1 is\cf0  \cf4 Freezable\cf0 )\par ??                    \{\par ??                        \cf1 var\cf0  result = ((\cf4 Freezable\cf0 )(\cf1 object\cf0 )a).Clone();\par ??                        a = (Answer)(\cf1 object\cf0 )result;\par ??                        result.Freeze();\par ??                    \}\par ??                \}), \cf1 null\cf0 , \cf1 null\cf0 );\par ??\par ??                \cf1 return\cf0  a;\par ??            \};\par ??        \}}<br />
--></p>
<div style="font-family: courier new; background: white; color: black; font-size: 10pt">
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span><span style="color: blue">public</span> <span style="color: blue">static</span> <span style="color: #2b91af">View</span>&lt;TView&gt; AsView&lt;TWPF, TView&gt;(<span style="color: blue">this</span> TWPF value) <span style="color: blue">where</span> TWPF : <span style="color: #2b91af">UIElement</span>, TView</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> value.ToWpfMonad&lt;TWPF, TView&gt;();</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">static</span> <span style="color: #2b91af">View</span>&lt;Answer&gt; ToWpfMonad&lt;T, Answer&gt;(<span style="color: blue">this</span> T value)</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">where</span> T : <span style="color: #2b91af">UIElement</span>, Answer</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> () =&gt;</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Answer a = <span style="color: blue">default</span>(Answer);</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value.Dispatcher.Invoke(<span style="color: #2b91af">DispatcherPriority</span>.Normal, (<span style="color: #2b91af">EventHandler</span>)((sender, e) =&gt;</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a = value;</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (a <span style="color: blue">is</span> <span style="color: #2b91af">Freezable</span>)</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> result = ((<span style="color: #2b91af">Freezable</span>)(<span style="color: blue">object</span>)a).Clone();</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a = (Answer)(<span style="color: blue">object</span>)result;</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result.Freeze();</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }), <span style="color: blue">null</span>, <span style="color: blue">null</span>);</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> a;</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; };</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
</div>
<p>Which merely tells about how to execute the delay when applied.</p>
<p>This is the code responsible for calling on WPF windows using dispatcher and other plumping details. Again code here is simplified for clarity. In the same way one can implement an AsView extension method for Silverlight with no need to change the Select implementation. View&lt;T&gt; is a generic monad and contain nothing specific to GUI technology.</p>
<p>&nbsp;</p>
<h2>Conclusion</h2>
<p>The Monad generalization provides a good solution for WPF/Silverlight thread problem. The solution is barely about communicating through the type system the fact that WPF/Silverlight <em>controls</em> are special, and using LinQ expression to operate on them leaving the plumping (Dispatcher.Invoke, Freezable) to the monad library implementers. Also this frees the caller from thread logic that is specific to the implementation technology. </p>
<p>In my project I didn&#8217;t work much on the freezing/unfreezing of Wpf controls (copy them and extract them to other threads) as it actually wasn&#8217;t necessary for my project. However, I think that a proper implementation of .AsView for WPF/Silverlight that manages Freezable and nested Freezable objects copying would be very interesting and would complete the API.</p>
<p>PS: I decided to finish this draft quickly as I am not having enough time to finish properly. Please tolerate typos and don&#8217;t hesitate to ask questions.</p>
<p/>
<div style="font-family: courier new; background: white; color: red; font-size: 10pt">The running example with a better implementation that is rather based on continuations is hosted on <a href="http://viewmonad.codeplex.com/sourcecontrol/changeset/view/22890?projectName=viewmonad#447750">CodePlex</a></div>
]]></content:encoded>
			<wfw:commentRss>http://sadekdrobi.com/2009/06/26/a-monad-in-c-for-simplifying-wpf-multi-threading-for-a-more-responsive-gui/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<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>I am speaking in Erlang Factory London 2009</title>
		<link>http://sadekdrobi.com/2009/05/28/i-am-speaking-in-erlang-factory-london-2009/</link>
		<comments>http://sadekdrobi.com/2009/05/28/i-am-speaking-in-erlang-factory-london-2009/#comments</comments>
		<pubDate>Thu, 28 May 2009 22:43:34 +0000</pubDate>
		<dc:creator>Sadache</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Conferences]]></category>
		<category><![CDATA[Erlang]]></category>
		<category><![CDATA[F#]]></category>
		<category><![CDATA[Multi-Paradigm Design]]></category>

		<guid isPermaLink="false">http://sadekdrobi.com/2009/05/28/i-am-speaking-in-erlang-factory-london-2009/</guid>
		<description><![CDATA[

]]></description>
			<content:encoded><![CDATA[</p>
<p><a href="http://www.erlang-factory.com/conference/London2009"><img border="0" alt="" src="http://www.erlang-factory.com/images/lon-speak.gif" width="100%" height="327"></a></p>
]]></content:encoded>
			<wfw:commentRss>http://sadekdrobi.com/2009/05/28/i-am-speaking-in-erlang-factory-london-2009/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Multimethod in Clojure: Should I call it first class Pattern Matching? or Pattern Matching in disguise?</title>
		<link>http://sadekdrobi.com/2009/05/27/multimethod-in-clojure-should-i-call-it-first-class-pattern-matching-or-pattern-matching-in-disguise/</link>
		<comments>http://sadekdrobi.com/2009/05/27/multimethod-in-clojure-should-i-call-it-first-class-pattern-matching-or-pattern-matching-in-disguise/#comments</comments>
		<pubDate>Wed, 27 May 2009 21:25:24 +0000</pubDate>
		<dc:creator>Sadache</dc:creator>
				<category><![CDATA[Multi-Paradigm Design]]></category>
		<category><![CDATA[Multi-languages projects]]></category>
		<category><![CDATA[Polyglot Programming]]></category>
		<category><![CDATA[clojure]]></category>

		<guid isPermaLink="false">http://sadekdrobi.com/2009/05/27/multimethod-in-clojure-should-i-call-it-first-class-pattern-matching-or-pattern-matching-in-disguise/</guid>
		<description><![CDATA[from Rich Hickey on Clojure&#8217;s Features and Implementation
Clojure multimethods are just another level of that same logic, in fact they are a realization of the last sentence I just said. They are dispatch based upon an arbitrary function of the arguments. You define a multimethod and you say &#8220;Here is a function of the arguments [...]]]></description>
			<content:encoded><![CDATA[<p>from <a href="http://www.infoq.com/interviews/hickey-clojure">Rich Hickey on Clojure&#8217;s Features and Implementation</a></p>
<blockquote><p>Clojure multimethods are just another level of that same logic, in fact they are a realization of the last sentence I just said. They are dispatch based upon an arbitrary function of the arguments. You define a multimethod and you say &#8220;Here is a function of the arguments I&#8217;d like you to use&#8221; You could look at the first argument, you could look at the 5th, you could look at all of them, you could look inside them, some member of an argument, it could look at the types or not or the values. Now, you could look at relationships between arguments, you have dispatch based upon an arbitrary function of the arguments and you have a vastly wider set of polymorphic possibilities than you had before and it&#8217;s quite powerful. In particular, it allows you to do Runtime dispatch on Runtime attributes. You don&#8217;t usually represent something like being hungry as part of something&#8217;s type, it&#8217;s some attribute that it acquires while the program is running or being outdated or things like that. Now you can access those things and you can do things polymorphically based upon that and take a lot of switch statements out of your code.</p>
</blockquote>
<p>Should I call it first class Pattern Matching? or Pattern Matching in disguise?</p>
]]></content:encoded>
			<wfw:commentRss>http://sadekdrobi.com/2009/05/27/multimethod-in-clojure-should-i-call-it-first-class-pattern-matching-or-pattern-matching-in-disguise/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Data, Context and Interaction : a new architectural approach by James O. Coplien and Trygve Reenskaug</title>
		<link>http://sadekdrobi.com/2009/05/08/data-context-and-interaction-a-new-architectural-approach-by-james-o-coplien-and-trygve-reenskaug/</link>
		<comments>http://sadekdrobi.com/2009/05/08/data-context-and-interaction-a-new-architectural-approach-by-james-o-coplien-and-trygve-reenskaug/#comments</comments>
		<pubDate>Fri, 08 May 2009 14:48:01 +0000</pubDate>
		<dc:creator>Sadache</dc:creator>
				<category><![CDATA[Agile Programming]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Model Mismatch]]></category>
		<category><![CDATA[Multi-Paradigm Design]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://sadekdrobi.com/2009/05/08/data-context-and-interaction-a-new-architectural-approach-by-james-o-coplien-and-trygve-reenskaug/</guid>
		<description><![CDATA[James O. Coplien and Trygve Reenskaug have recently introduced a new architectural approach to OOP based on Data, Context and Interaction pattern. It should allow capturing user mental model in terms of behavioral requirements, something that classic OOP fails to do. The article, that triggered many reactions and critics, provides insights into DCI using concrete [...]]]></description>
			<content:encoded><![CDATA[<p>James O. Coplien and Trygve Reenskaug have recently introduced a new architectural approach to OOP based on Data, Context and Interaction pattern. It should allow capturing user mental model in terms of behavioral requirements, something that classic OOP fails to do. The article, that triggered many reactions and critics, provides insights into DCI using concrete examples to show its advantages.</p>
<p><span id="more-606"></span>
<p><a href="http://www.infoq.com/news/2009/05/dci-coplien-reenskau">http://www.infoq.com/news/2009/05/dci-coplien-reenskau</a></p>
]]></content:encoded>
			<wfw:commentRss>http://sadekdrobi.com/2009/05/08/data-context-and-interaction-a-new-architectural-approach-by-james-o-coplien-and-trygve-reenskaug/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I am speaking at QCon</title>
		<link>http://sadekdrobi.com/2009/02/18/i-am-speaking-at-qcon/</link>
		<comments>http://sadekdrobi.com/2009/02/18/i-am-speaking-at-qcon/#comments</comments>
		<pubDate>Wed, 18 Feb 2009 18:00:06 +0000</pubDate>
		<dc:creator>Sadache</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Conferences]]></category>
		<category><![CDATA[F#]]></category>
		<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[InfoQ]]></category>
		<category><![CDATA[LinQ]]></category>
		<category><![CDATA[Multi-Paradigm Design]]></category>
		<category><![CDATA[QCon]]></category>

		<guid isPermaLink="false">http://sadekdrobi.com/2009/02/18/i-am-speaking-at-qcon/</guid>
		<description><![CDATA[
 
&#160;

Programming with a Mainstream Language
Track: Functional and Concurrent Programming Languages Applied
Time: Thursday 17:15 &#8211; 18:15
Location: Abbey Room
Abstract:
Using functional programming (FP) in enterprise software development is often quite a challenge. In this presentation, Sadek Drobi will talk about his experience of applying functional programming principles on a real-world project in relation with existing non-functional frameworks.
In [...]]]></description>
			<content:encoded><![CDATA[</p>
<p><a href="http://qconlondon.com/london-2009/presentation/Functional+Programming+with+a+Mainstream+Language"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="download (1)" src="http://sadekdrobi.com/wp-content/uploads/2009/02/download-1.jpg" width="547" height="196"></a> </p>
<p>&nbsp;</p>
<p><span id="more-590"></span></p>
<p><strong><a href="http://qconlondon.com/london-2009/presentation/Functional+Programming+with+a+Mainstream+Language">Programming with a Mainstream Language</a></strong></p>
<p><strong>Track</strong>: <a href="http://qconlondon.com/london-2009/tracks/show_track.jsp?trackOID=225">Functional and Concurrent Programming Languages Applied</a>
<p><strong>Time</strong>: Thursday 17:15 &#8211; 18:15
<p><strong>Location</strong>: Abbey Room
<p><strong>Abstract</strong>:
<p>Using functional programming (FP) in enterprise software development is often quite a challenge. In this presentation, Sadek Drobi will talk about his experience of applying functional programming principles on a real-world project in relation with existing non-functional frameworks.
<p>In the year 2008 and just on the release of C# 3.0 and Linq, which is basically several FP concepts implemented on C# language, Sadek Drobi worked as a tech lead on a project where functional programming approach was used to meet performance requirements and to achieve desired response time. Facing similar issues, a mainstream architect would strive to instantiate less objects and to use mutation for optimization. Considering the big amount of data that had to be processed on each request, Sadek chose to do it otherwise: no mutation, used memorization, laziness, recursion, functions, curry, monads, list comprehensions and, then, parallelization to yield an almost purely functional core domain model.
<p>Sadek Drobi will talk more about this experience and elaborate on the good and the bad of going almost extreme through a functional programming approach from different perspectives.
<p>Participants don&#8217;t need to be functional programming experts even if having some knowledge about LinQ will spare you too much hard thinking during the session. Each new concept will be briefly introduced and the structure of the talk will be as non-sequential as possible so that its parts can be understood and analyzed separately.</p>
]]></content:encoded>
			<wfw:commentRss>http://sadekdrobi.com/2009/02/18/i-am-speaking-at-qcon/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Programming Languages: 2008 Review and Prospects for 2009</title>
		<link>http://sadekdrobi.com/2009/01/10/programming-languages-2008-review-and-prospects-for-2009/</link>
		<comments>http://sadekdrobi.com/2009/01/10/programming-languages-2008-review-and-prospects-for-2009/#comments</comments>
		<pubDate>Sat, 10 Jan 2009 00:34:23 +0000</pubDate>
		<dc:creator>Sadache</dc:creator>
				<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[InfoQ]]></category>
		<category><![CDATA[Multi-Paradigm Design]]></category>
		<category><![CDATA[Polyglot Programming]]></category>

		<guid isPermaLink="false">http://sadekdrobi.com/?p=580</guid>
		<description><![CDATA[In the beginning of last year, Ehud Lamm launched on Lamba the Ultimate a thread about programming languages predictions for 2008. Several subjects popped up: concurrency, functional programming, future of Java, Ruby, C++, and many others&#8230; What really happened in 2008 and what are the prospects for 2009? Bloggers have addressed these questions on demand [...]]]></description>
			<content:encoded><![CDATA[<p>In the beginning of last year, Ehud Lamm launched on Lamba the Ultimate a thread about programming languages predictions for 2008. Several subjects popped up: concurrency, functional programming, future of Java, Ruby, C++, and many others&#8230; What really happened in 2008 and what are the prospects for 2009? Bloggers have addressed these questions on demand of James Iry, echoing at last year thread.</p>
<p><span id="more-580"></span></p>
<p>originally posted on <a href="http://www.infoq.com/news/2009/01/pl-predictions-2009">http://www.infoq.com/news/2009/01/pl-predictions-2009</a></p>
<p>In the beginning of the last year, Ehud Lamm launched a thread on Lamba the Ultimate inviting other bloggers to submit their <a href="http://lambda-the-ultimate.org/node/2600">predictions for 2008 in fields relative to programming languages</a> (PL).</p>
<p>Concurrency was one of the first topics to be defined as an upcoming theme of the year even though it was argued that it &#8220;won&#8217;t go anywhere because the current paradigms [&#8230;] and architectures [&#8230;] can&#8217;t deal with it very well&#8221;. Many equally ambivalent predictions concerned functional programming languages. Haskel was supposed to &#8220;rock the blogosphere&#8221; without being widely used as such, but rather becoming an inspiration for new features in more mainstream programming languages. More generally speaking, some expected that &#8220;no functional language [would] become significantly popular&#8221;, while several other bloggers had far more optimistic prognoses for F# and Scala that were to &#8220;enjoy large uptakes&#8221;, at least through the development of &quot;multi-language&quot; projects&#8220; in some combination of F#/C# or Scala/Java&#8221;.&#160; As far as other languages were concerned, Java was supposed to become &#8220;more entrenched&#8221;. And the same was to happen to Ruby that would be undermined by the downtrend of Rails. On the contrary, C++ with its new ISO standard for 2009 was expected to &#8220;become the language-of-the-year in 2008&#8221; and Javascript to gain more momentum.</p>
<p>As an echo to this tread, James Iry asked bloggers at the end of 2008 to share their opinion on &#8220;<a href="http://lambda-the-ultimate.org/node/3144">what was noteworthy about 2008 as far as programming languages were concerned</a>&#8221;.</p>
<p>Trying to assess the validity of last year predictions, Key Schluehr believes that concurrency was far from being the theme of the year. And, if there were one, it was, in his opinion, &#8220;cloud computing&#8221;, even though he considers that &#8220;this had little to with computing at all&#8221;. He also asserts that, just like Morris Johns expected, no functional language became significantly popular, which many other bloggers disagree with. <b></b></p>
<p>James Iry argues indeed that even though no functional language is at Java or even Ruby level of popularity, &#8220;the fact that languages like these popped up so brightly on the mainstream radar last year is not just significant, but huge.&#8221; Eli Ford highlights that &#8220;F# got its own CTP in September, and is now slated for a supported release as part of Visual Studio 2010, alongside C# and VB&#8221;. And Sean McDirmid argues that &#8220;as far as specific languages go 2008 was a good year for Scala&#8221;. &#8220;Clojure&#8221;, that was not at all mentioned in the last year discussion, is considered to be the discovery of the year by Chris Rathman who believes that it is a good example of how &#8220;to go about integrating existing concepts into a programming language&#8221;.</p>
<p>Along with Scala, Sean McDirmid mentions Objective C &#8220;as the real hot language of 2008 thanks to the iPhone SDK&#8221; and believes that 2008 was also the year of C vengeance with various forms of it being &#8220;used to program GPU hardware (HLSL, CUDA, OpenCL&#8230;)&#8221;.</p>
<p>On the other hand, several bloggers highlight that last year was not that good for Java. Sean McDirmid asserts that &#8220;JavaFX was late and didn&#8217;t make the splash it needed to make&#8221;. If it is true that bloggers express some concerns about Java&#8217;s future, Daniel Weinreb stresses that &#8220;it&#8217;s used in so many places now that we&#8217;re very unlikely to see it disappear&#8221; and, according to James Iry, &#8220;it is still and will remain for quite awhile one of the a handful of &quot;safe&quot; choices for an IT manager&#8221;. Others question however the capacity of Sun to survive the current crisis and speculate about the future of JVM expecting IBM or Google to step in.</p>
<p>The discussion moves from 2008 to 2009 and a number of new predictions are made. In the field of functional programming, James Iry expects great things from both Clojure and Scala teams while Falcon argues that &#8220;2009 will be a year of clojure rather than scala&#8221; and expects F# to be finally brought &#8220;to the attention of mainstream .NET developers.&#8221; Ross Smith, however, reiterates the prediction of last year that functional programming will enter the mainstream rather &#8220;by being incorporated into pre-existing procedural and OO languages&#8221;. He also believes that &#8220;the new C++ standard will become official&#8221;, &#8220;concurrency, including GPGPU applications, will continue its rise in importance&#8221;, &#8220;Python will start to bleed users specifically because of [its] lack of good support for concurrency&#8221; while &#8220;JavaScript&#8217;s star will continue to rise&#8221;.</p>
<p>Xscott goes along the same lines about JavaScript predicting that it &#8220;will eventually become the popular server and application scripting language &#8211; mostly because of it&#8217;s various JIT compiling implementations&#8221;, whereas Kay Schluehr believes that it &#8220;will not expand on its niche&#8221;. On the other hand, he thinks that &#8220;one of the great-future-of-programming hopes like Perl 6, Rubinius or PyPy [will] finally [reach] a state where programmers other than core developers start to show interest.&#8221; </p>
<p>Kaveh Shahbazian believes that &#8220;one thing [that] is going to happen [in 2009] is finding new approaches to employ scripting&#8221; and names Lua as a successful example. And, last but not least, Sean McDirmid predicts that &#8220;no progress will be made on the static/dynamic debate.&#8221; :)</p>
]]></content:encoded>
			<wfw:commentRss>http://sadekdrobi.com/2009/01/10/programming-languages-2008-review-and-prospects-for-2009/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>You&#8217;ve got 100 pages to convince me of your shiny language!</title>
		<link>http://sadekdrobi.com/2009/01/05/youve-got-100-pages-to-convince-me-in-your-shiny-language/</link>
		<comments>http://sadekdrobi.com/2009/01/05/youve-got-100-pages-to-convince-me-in-your-shiny-language/#comments</comments>
		<pubDate>Mon, 05 Jan 2009 20:42:37 +0000</pubDate>
		<dc:creator>Sadache</dc:creator>
				<category><![CDATA[Agile Programming]]></category>
		<category><![CDATA[Delivering Value]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Lua]]></category>
		<category><![CDATA[Methodologies]]></category>
		<category><![CDATA[Multi-Paradigm Design]]></category>
		<category><![CDATA[Multi-languages projects]]></category>
		<category><![CDATA[Paradigm Oriented Programming Language]]></category>
		<category><![CDATA[Polyglot Programming]]></category>
		<category><![CDATA[Useability]]></category>

		<guid isPermaLink="false">http://sadekdrobi.com/2009/01/05/youve-got-100-pages-to-convince-me-in-your-shiny-language/</guid>
		<description><![CDATA[ In the rapidly spanning world of programming languages, I find myself buying and reading a lot of books about new and old programming languages. There are a few interesting concepts in each language, and if you think about employing more than one language in your projects then you better know about the existence of [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://sadekdrobi.com/wp-content/uploads/2009/01/dsc-01501.jpg"><img style="border-right: 0px; border-top: 0px; margin: 0px 10px 5px 0px; border-left: 0px; border-bottom: 0px" height="322" alt="DSC_0150-" src="http://sadekdrobi.com/wp-content/uploads/2009/01/dsc-0150-thumb1.jpg" width="470" align="left" border="0"></a> In the rapidly spanning world of programming languages, I find myself buying and reading a lot of books about new and old programming languages. There are a few interesting concepts in each language, and if you think about employing more than one language in your projects then you better know about the existence of these concepts (see <a href="http://www.infoq.com/articles/paradigm-based-polyglot-prog">Paradigm based Polyglot Programming</a>).
<p>One thing that annoys me though about most programming language books is how raw they often are.</p>
<p><span id="more-575"></span></p>
<p>&nbsp; Probably constrained by time, authors and book teams seem to invest less on editorial stuff and teaching methodology and more on explaining details of technical and behind the scenes stuff. This often results in more manual-like books where you have a great wealth of material that you are not able to exploit anyway because of, well, lack of time. I am not sure I can invest time in reading 500+ manual of a new, or at least non mainstream, language that I might decide not to use by the end.&nbsp;
<p>I am not saying here that technical and behind the scene stuff does not matter, quite the opposite. I just think that manual, important it is, should not be the introduction to the programming language. A rather 100 to 200 pages brief book that is carefully edited to introduce paradigm, concepts, and strengths of the programming language is a more attractive choice , accompanied with a wordy but well organized reference book with examples and more in-depth explanation of language features. I guess this is a more pragmatic approach to programming language learning in the presence of WWW and Google search.
<p>PS: I personally find <a href="http://www.amazon.com/Haskell-School-Expression-Functional-Programming/dp/0521644089/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1231187744&amp;sr=8-1">Paul Hudak&#8217;s Haskell book</a> a very good example of an enjoyable, brief, nicely and carefully edited programming language book. Also <a href="http://www.amazon.com/Programming-Lua-Second-Roberto-Ierusalimschy/dp/8590379825/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1231187997&amp;sr=1-1">LUA&#8217;s programming book</a> is nicely done.</p>
]]></content:encoded>
			<wfw:commentRss>http://sadekdrobi.com/2009/01/05/youve-got-100-pages-to-convince-me-in-your-shiny-language/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RefX:: ORMs, Relational Data, Mismatch, LinQ and DSLs</title>
		<link>http://sadekdrobi.com/2008/11/10/refx-orms-relational-data-mismatch-linq-and-dsls/</link>
		<comments>http://sadekdrobi.com/2008/11/10/refx-orms-relational-data-mismatch-linq-and-dsls/#comments</comments>
		<pubDate>Mon, 10 Nov 2008 02:42:22 +0000</pubDate>
		<dc:creator>Sadache</dc:creator>
				<category><![CDATA[Agile in the Enterprise]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[D90]]></category>
		<category><![CDATA[DOTNET]]></category>
		<category><![CDATA[DSL]]></category>
		<category><![CDATA[Domain Driven Design]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[LinQ]]></category>
		<category><![CDATA[Mapping]]></category>
		<category><![CDATA[Model Mismatch]]></category>
		<category><![CDATA[Multi-Paradigm Design]]></category>
		<category><![CDATA[ORM]]></category>
		<category><![CDATA[Polyglot Programming]]></category>
		<category><![CDATA[Productivity]]></category>

		<guid isPermaLink="false">http://sadekdrobi.com/2008/11/10/refx-orms-relational-data-mismatch-linq-and-dsls/</guid>
		<description><![CDATA[   
Having worked with several Object-Relational mapping frameworks in the last few years, I got to a point where I couldn&#8217;t justify their complexity in my project. We often talk about the mismatch between the database and the object worlds, and that is where ORMs are often stated and referenced for &#8220;bridging the [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://sadekdrobi.com/wp-content/uploads/2008/11/raw000781.jpg"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="361" alt="Raw00078" src="http://sadekdrobi.com/wp-content/uploads/2008/11/raw00078-thumb1.jpg" width="534" border="0"></a> <a href="http://sadekdrobi.com/wp-content/uploads/2008/11/raw001701.jpg"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="379" alt="Raw00170" src="http://sadekdrobi.com/wp-content/uploads/2008/11/raw00170-thumb1.jpg" width="258" border="0"></a> <a href="http://sadekdrobi.com/wp-content/uploads/2008/11/dsc-01911.jpg"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="378" alt="DSC_0191" src="http://sadekdrobi.com/wp-content/uploads/2008/11/dsc-0191-thumb1.jpg" width="257" border="0"></a> </p>
<p>Having worked with several Object-Relational mapping frameworks in the last few years, I got to a point where I couldn&#8217;t justify their complexity in my project. We often talk about the mismatch between the database and the object worlds, and that is where ORMs are often stated and referenced for &#8220;bridging the gap&#8221;!</p>
<p>Well I prefer to call it lifting the gap, or highering the gap, to have it now between DAOs and the rest of the code than having it between database and code.But I wouldn&#8217;t call this in any way reducing the gap.</p>
<p><span id="more-538"></span></p>
<p>First thing to mention here, is that data structures are not evil. We often deal with XML data structure but we absolutely refuse to deal with relational data structures. The problem might be related to time, XML came with a whole arsenal of managed APIs that represent its model in a way that makes it more comfortable for you to manipulate these documents. All together with transformation APIs that made it a lot easier to fill your object model out of an XML. Relational data structure on the other hand did not have the luxury of standard APIs and representation models in mainstream languages, and that made dealing with a database request result so arbitrary (Datasets existed in .Net but they have been cursed heavily and has been forced out of the cool toolbox, do not know yet why!) .&nbsp; </p>
<p>In some way, I guess that the &#8220;mismatch&#8221; between Relational data structures and objects exists much similarly between XML and objects, however we didn&#8217;t think for so long of a doing a mapping between XML and object OXM? (no we are starting to do so, writing some XML to map XML to objects!).</p>
<p>With the experience I had with mapping technologies, I can say that they do not solve the problem for me. They merely add yet another useless level of abstraction where your domain types look nothing like relational data structures, but yet nothing like your target domain model! They have gone a half way, and now its your turn to poison your application with a lot of absurd ORM code trying to stretch this bridge to reduce the gap&#8230; </p>
<p>Geeks love talking about intrusivty of an ORMs and compare level of intrusivty in different terms. Intrusivty is much more than the lack of an interface or methods to implement in your domain objects. I guess I am ok to deal with that kind of intrusivity. However all ORMs I tried do not give me the opportunity to express my domain model in the way I would like to. I have to deal with notions of Lazy Loading, Attached/Detached entities, Entity sets, ids, back fields and a lot of other things that I don&#8217;t want to appear in my model and that make the resulting code so ugly and complex that I don&#8217;t wish to visit soon.</p>
<p>As Bob Martin said before, we are missing the opportunity to do oop, I would enforce and say we are missing the opportunity to model at all. And I guess we have gone too far with ORMs and it is time to go back to the basics, in the same way we have done after the EJB great abstraction.</p>
<p>It is very contradictory, while we are so excited about DSLs and the goodness they can potentially bring to enterprise development that results in a declarative semanticful style of programming, we can&#8217;t help but to engage all of our means in trying to bury SQL (and effective, semanticful, declarative powerful domain specific language) behind a dumb API to &#8220;abstract database access&#8221;! I guess we got to chose one strategy, either we keep SQL and we&nbsp; keep marketing DSLs or we hope for a new advanced GPL compiler that will hide all of this in the same way modern languages did with garbage collection, concurrency and synchronization.</p>
<p>Since I am not yet a compiler writer, and I aint got around an effective way of integrating database in mainstream languages (there is actually a very productive in memory database on Haskell that worth trying), I decide to keep the DSL, and to work with them without the need to hide them, but rather to get a better tooling to help converting between difference data structures.</p>
<p>I guess you&#8217;ve guessed it by now. For me LinQ is the mid-term solution for the data structure mismatch. No not LinQ to sql, nor Ado.Net entities (they are helpful and we might use them but lets focus on the solution). LinQ is a very productive tool that halps effectively transform anything to anything in few lines of code. I mean that I can still use powerful SQL to get data effeciently and then convert them with one line of LinQ into the destination data structure. No abstraction leak, less complexity: Use the right tool where it applies. </p>
<p>So what I would actually do is to use a framework that will execute my SQL or SQL like query on the database and get me the records in much the same way they are in the database,Much in the same way Active Record gets you records as they are in the database. No useless efforts from the framework to bend the data to make it look untruthfully like the destination model.&nbsp; I guess I know better my domain model, and with this better tooling I can get it done with no problem.</p>
<p>&nbsp;</p>
<div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:f663d4e1-de43-4d9c-9f89-7b45ec5b5a86" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px">Mots clÃ©s Technorati : <a href="http://technorati.com/tags/mapping" rel="tag">mapping</a>,<a href="http://technorati.com/tags/orm" rel="tag">orm</a>,<a href="http://technorati.com/tags/linq" rel="tag">linq</a>,<a href="http://technorati.com/tags/mismatch" rel="tag">mismatch</a>,<a href="http://technorati.com/tags/relational.%20data%20structure" rel="tag">relational. data structure</a>,<a href="http://technorati.com/tags/abstraction" rel="tag">abstraction</a>,<a href="http://technorati.com/tags/domain%20model" rel="tag">domain model</a>,<a href="http://technorati.com/tags/deseign" rel="tag">deseign</a></div>
]]></content:encoded>
			<wfw:commentRss>http://sadekdrobi.com/2008/11/10/refx-orms-relational-data-mismatch-linq-and-dsls/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Paradigm based Polyglot Programming</title>
		<link>http://sadekdrobi.com/2008/09/25/paradigm-based-polyglot-programming/</link>
		<comments>http://sadekdrobi.com/2008/09/25/paradigm-based-polyglot-programming/#comments</comments>
		<pubDate>Thu, 25 Sep 2008 07:08:16 +0000</pubDate>
		<dc:creator>Sadache</dc:creator>
				<category><![CDATA[Agile Programming]]></category>
		<category><![CDATA[Domain Driven Design]]></category>
		<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[InfoQ]]></category>
		<category><![CDATA[Lua]]></category>
		<category><![CDATA[Multi-Paradigm Design]]></category>
		<category><![CDATA[Multi-languages projects]]></category>
		<category><![CDATA[Polyglot Programming]]></category>

		<guid isPermaLink="false">http://sadekdrobi.com/2008/09/25/paradigm-based-polyglot-programming/</guid>
		<description><![CDATA[ 
How many languages are you using on the same project? If you go counting you will see that they are many. I mean XML, Java, XSLT, HTML, CSS&#8230; etc. But the reason why you are using almost all of them is that they happen to be mainstream and, oftentimes, they are the only language [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://sadekdrobi.com/wp-content/uploads/2008/09/dsc-01721.jpg"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="429" alt="DSC_0172" src="http://sadekdrobi.com/wp-content/uploads/2008/09/dsc-0172-thumb1.jpg" width="640" border="0"></a> </p>
<p>How many languages are you using on the same project? If you go counting you will see that they are many. I mean XML, Java, XSLT, HTML, CSS&#8230; etc. But the reason why you are using almost all of them is that they happen to be mainstream and, oftentimes, they are the only language choice for a needed framework. You are actually almost obliged to use them. <em>The choice is done for you. Style? CSS. Configuration? Often XML. Web interface description? Html.</em> However, if you want to adopt true polyglot programming, you will have to face inevitable decision of language choice.</p>
<p><span id="more-517"></span></p>
<p><a href="http://www.infoq.com/articles/paradigm-based-polyglot-prog">Read my article on Paradigm based Polyglot Programming at InfoQ.com</a></p>
]]></content:encoded>
			<wfw:commentRss>http://sadekdrobi.com/2008/09/25/paradigm-based-polyglot-programming/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

