<?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; Architecture</title>
	<atom:link href="http://sadekdrobi.com/category/architecture-patterns/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>Functional Web: Functional Programing for Web Integration and Mashups</title>
		<link>http://sadekdrobi.com/2010/11/16/functional-web-functional-programing-for-web-integration-and-mashups/</link>
		<comments>http://sadekdrobi.com/2010/11/16/functional-web-functional-programing-for-web-integration-and-mashups/#comments</comments>
		<pubDate>Tue, 16 Nov 2010 22:17:18 +0000</pubDate>
		<dc:creator>Sadache</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Erlang]]></category>
		<category><![CDATA[F#]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[fsharp]]></category>
		<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[integration]]></category>
		<category><![CDATA[mashups]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[woa]]></category>

		<guid isPermaLink="false">http://sadekdrobi.com/?p=705</guid>
		<description><![CDATA[Functional programming is often perceived as being good for either mathematics or multi-core programming. As for its huge benefits for modern web architecture and development, they are not really known. This video gives a few arguments about why functional programming matters for today’s and tomorrow’s web.
http://www.zengularity.com/item/1519646134/functional-web-functional-programing-for-web
]]></description>
			<content:encoded><![CDATA[<p>Functional programming is often perceived as being good for either mathematics or multi-core programming. As for its huge benefits for modern web architecture and development, they are not really known. This video gives a few arguments about why functional programming matters for today’s and tomorrow’s web.</p>
<p><a href="http://www.zengularity.com/item/1519646134/functional-web-functional-programing-for-web">http://www.zengularity.com/item/1519646134/functional-web-functional-programing-for-web</a></p>
]]></content:encoded>
			<wfw:commentRss>http://sadekdrobi.com/2010/11/16/functional-web-functional-programing-for-web-integration-and-mashups/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My Computation Abstraction Talk Slides for Functional Exchange</title>
		<link>http://sadekdrobi.com/2009/12/07/my-computation-abstraction-slides-for-functional-exchange/</link>
		<comments>http://sadekdrobi.com/2009/12/07/my-computation-abstraction-slides-for-functional-exchange/#comments</comments>
		<pubDate>Mon, 07 Dec 2009 17:46:25 +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[LinQ]]></category>
		<category><![CDATA[Scala Haskell fsharp fp monads csharp]]></category>

		<guid isPermaLink="false">http://sadekdrobi.com/2009/12/07/my-computation-abstraction-slides-for-functional-exchange/</guid>
		<description><![CDATA[Computation Abstraction: Going beyond programming language control syntax, or what we&#8217;ve missed from FP for so long in mainstream
For a long time, and due to the lack of main FP concepts in most mainstream languages, we missed opportunities to abstraction and code expressiveness and conciseness. With today&#8217;s democratization of FP, Computational Abstraction is what will [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Computation Abstraction: Going beyond programming language control syntax, or what we&#8217;ve missed from FP for so long in mainstream</strong></p>
<p>For a long time, and due to the lack of main FP concepts in most mainstream languages, we missed opportunities to abstraction and code expressiveness and conciseness. With today&#8217;s democratization of FP, Computational Abstraction is what will enable us to be less dependent on specific programming language syntax offering; creating libraries of control structures and composition forms that help find concise and expresive solutions to enterprise programming challenges (null, lists treatment, error handling), capturing elegantly important business concepts in code, and programming at the right level of abstraction.For a long time, and due to the lack of main FP concepts in most mainstream languages, we missed opportunities to abstraction and code expressiveness and conciseness. With today&#8217;s democratization of FP, Computational Abstraction is what will enable us to be less dependent on specific programming language syntax offering; creating libraries of control structures and composition forms that help find concise and expresive solutions to enterprise programming challenges (null, lists treatment, error handling), capturing elegantly important business concepts in code, and programming at the right level of abstraction. </p>
<p>Slides:&#160; <a href="http://sadekdrobi.com/wp-content/uploads/2009/12/Computation-Abstraction666iiioii6jjjjjjhjj-5.pdf">http://sadekdrobi.com/wp-content/uploads/2009/12/Computation-Abstraction666iiioii6jjjjjjhjj-5.pdf</a></p>
<p>Google wave:&#160; <a href="https://wave.google.com/wave/#restored:wave:googlewave.com!w%252BPgcakhgiA">https://wave.google.com/wave/#restored:wave:googlewave.com!w%252BPgcakhgiA</a></p>
]]></content:encoded>
			<wfw:commentRss>http://sadekdrobi.com/2009/12/07/my-computation-abstraction-slides-for-functional-exchange/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>Ian Robinson and Jim Webber on Web-based Integration</title>
		<link>http://sadekdrobi.com/2009/06/25/ian-robinson-and-jim-webber-on-web-based-integration/</link>
		<comments>http://sadekdrobi.com/2009/06/25/ian-robinson-and-jim-webber-on-web-based-integration/#comments</comments>
		<pubDate>Thu, 25 Jun 2009 00:06:32 +0000</pubDate>
		<dc:creator>Sadache</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Rest]]></category>
		<category><![CDATA[jim-webber]]></category>

		<guid isPermaLink="false">http://sadekdrobi.com/?p=640</guid>
		<description><![CDATA[Jim Webber from http://www.infoq.com/interviews/robinson-webber-rest 00:37:08:
We did put abstraction after abstraction onto our distributed system infrastructure and you know what: it hasn&#8217;t worked out that well for us. Some of the largest and most sophisticated distributed systems on the planet haven&#8217;t been all that large or sophisticated and then this kind of crappy protocol comes along [...]]]></description>
			<content:encoded><![CDATA[<p>Jim Webber from <a href="http://www.infoq.com/interviews/robinson-webber-rest">http://www.infoq.com/interviews/robinson-webber-rest</a> 00:37:08:</p>
<blockquote><p>We did put abstraction after abstraction onto our distributed system infrastructure and you know what: it hasn&#8217;t worked out that well for us. Some of the largest and most sophisticated distributed systems on the planet haven&#8217;t been all that large or sophisticated and then this kind of crappy protocol comes along that insists on being synchronous, and insists on being text-driven and it scales globally. That&#8217;s shocking and does not make sense to us as engineers. That&#8217;s the web paradox &#8211; it&#8217;s the rubbishest thing on the planet, but it scaled and for me that is what&#8217;s hit the reset button because I was totally up for XML-based protocols that do all sorts of funky stuff.
</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://sadekdrobi.com/2009/06/25/ian-robinson-and-jim-webber-on-web-based-integration/feed/</wfw:commentRss>
		<slash:comments>0</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>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>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>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>Quiz:: SharpLight &#124;&gt; What does this do?</title>
		<link>http://sadekdrobi.com/2009/02/03/quiz-sharplight-what-does-this-do/</link>
		<comments>http://sadekdrobi.com/2009/02/03/quiz-sharplight-what-does-this-do/#comments</comments>
		<pubDate>Tue, 03 Feb 2009 14:02:09 +0000</pubDate>
		<dc:creator>Sadache</dc:creator>
				<category><![CDATA[Agile Programming]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[DOTNET]]></category>
		<category><![CDATA[DSL]]></category>
		<category><![CDATA[F#]]></category>
		<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[sharplight]]></category>

		<guid isPermaLink="false">http://sadekdrobi.com/2009/02/03/quiz-sharplight-what-does-this-do/</guid>
		<description><![CDATA[
]]></description>
			<content:encoded><![CDATA[<p><a href="http://sadekdrobi.com/wp-content/uploads/2009/02/combination1.jpg"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="combination" src="http://sadekdrobi.com/wp-content/uploads/2009/02/combination-thumb1.jpg" width="627" height="105" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://sadekdrobi.com/2009/02/03/quiz-sharplight-what-does-this-do/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I just do not get it! Why use Spring Beans?</title>
		<link>http://sadekdrobi.com/2008/12/30/i-just-do-not-get-it-why-use-spring-beans/</link>
		<comments>http://sadekdrobi.com/2008/12/30/i-just-do-not-get-it-why-use-spring-beans/#comments</comments>
		<pubDate>Tue, 30 Dec 2008 15:14:44 +0000</pubDate>
		<dc:creator>Sadache</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Spring Framework]]></category>

		<guid isPermaLink="false">http://sadekdrobi.com/2008/12/30/i-just-do-not-get-it-why-use-spring-beans/</guid>
		<description><![CDATA[ 
In short, I do believe that Spring framework forced you for a long time to use a subset of Java, a subset of frameworks you use, and has been implicitly very intrusive on all the frameworks that support it. It constrains innovation and limit choices. And more!

Unfortunately I would not be able to list [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://sadekdrobi.com/wp-content/uploads/2008/12/photo1.jpg"><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="photo" src="http://sadekdrobi.com/wp-content/uploads/2008/12/photo-thumb1.jpg" width="260" height="179" /></a> </p>
<p>In short, I do believe that Spring framework forced you for a long time to use a subset of Java, a subset of frameworks you use, and has been implicitly very intrusive on all the frameworks that support it. It constrains innovation and limit choices. And more!</p>
<p><span id="more-566"></span></p>
<p>Unfortunately I would not be able to list in this post all the arguments I have because of lack of time. I would rather provide few examples that shows why the Spring culture is unacceptable for me. Of course you can reply saying that Spring will support this, or is supporting this starting with version x. Well I am afraid I don&#8217;t care. Spring existed for a long time now, and my judgement is based on a long history. Besides what I am targeting is the culture of the framework.</p>
<p>First of all, if you want to use Spring Beans you need to believe in magic. Don&#8217;t agree?</p>
<p>class MyServiceClass {   <br />&#160; private IMyDao myDao;    <br />&#160; // Other stuff goes here.    <br />}</p>
<p>this field &quot;myDao&quot; is never initialized, not by any constructor and it is private. But if you believe in magic, then you can use it. Because you know what? There is a wizard config file that you can find if you search enough in your project that will &quot;inject&quot; it. What?!!</p>
<p>First of all this solution violates member visibility rules in Java. But ignoring that, this results in a practice or a habit to ignore a never initialized field believing in magic! A framework that offers and advocates such a violating is not worth even trying IMHO!</p>
<p>Now the suggested solution is to write a setter for this field that then will be used by Spring, but this changes nothing as anyway you will either make it public (allowing any public user of your API to set you DAO!) or put it back private and back to the never initialized field! Did I mention &quot;final&quot; yet? :)</p>
<p>Spring claims that it is not intrusive. What? Now imagine that you remove Spring from your dependencies list, your application is no more useful and you suddenly get null pointer exception everywhere my friend! Who will inject the private field? or a private setter? This claim is ridiculous!</p>
<p>Now did it happen to you to forget to declare or mistype your beans in your config? What happens? Runtime exception! What? I thought Java applications are statically typed!</p>
<p>Now I can hear you, use annotations! Annotations solves your problem! Well I am afraid not. Annotations like @Required or @AutoWired is just like begging the user not delete the field or property, and it is not mandatory and not forced by any checker. Remember, I am talking culture here&#8230;</p>
<p>Now if you are a defender of injection using constructors, then I believe you got something right and most probably you reject most of the hacks I mentioned above (Including setters based injection, private field injection,&#160; aspects based injection (in short 80% of what Spring Beans mainly offers and actually does)). But then, you turned anyway your architecture dependency into dynamic, never checked and its composition specified in desperate XML files! Think of code navigability, and software flow and dependencies that you can not trace without the help of a debugger using this approach.</p>
<p>Oh, what about JavaConfig. Well yeah, <a href="http://discuss.joelonsoftware.com/default.asp?joel.3.219431.12">I do not need Spring to do that</a>! &quot;new&quot;, you remember the keyword &quot;new&quot;? yeah OOP, new, instances. That is Java programming. Trust me &quot;new&quot; is no evil, and guess what, you need no dependencies to any framework to resolve your dependencies in a statically checked, navigable, readable, simple, clear, elegant, concise, safe and beautiful way!</p>
<p>Last thing for now, you need to learn Spring-FU to use any, any framework that wasn&#8217;t designed specifically for use with Spring and yet you may not succeed (though framework designers are forced to be distracted by Spring design when defining their architecture, for examples see Restlet and Jersey) ! That reminds me of something&#8230;</p>
<p>YAGNI!</p>
]]></content:encoded>
			<wfw:commentRss>http://sadekdrobi.com/2008/12/30/i-just-do-not-get-it-why-use-spring-beans/feed/</wfw:commentRss>
		<slash:comments>34</slash:comments>
		</item>
	</channel>
</rss>

