<?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; MVP</title>
	<atom:link href="http://sadekdrobi.com/category/architecture-patterns/mvp/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>Joined the NMVP open source project</title>
		<link>http://sadekdrobi.com/2007/01/02/joined-the-nmvp-open-source-project/</link>
		<comments>http://sadekdrobi.com/2007/01/02/joined-the-nmvp-open-source-project/#comments</comments>
		<pubDate>Tue, 02 Jan 2007 22:20:05 +0000</pubDate>
		<dc:creator>Sadache</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[DOTNET]]></category>
		<category><![CDATA[MVP]]></category>

		<guid isPermaLink="false">http://sadekdrobi.com/?p=21</guid>
		<description><![CDATA[I joined the NMVP open source project on codeplex, i ll be busy inÂ the evenings implementing Convention over Configurations, and Rapid Web NMVP Assembly that will facilates the framework use !
Â www.codeplex.com/nmvp
]]></description>
			<content:encoded><![CDATA[<p>I joined the NMVP open source project on codeplex, i ll be busy inÂ the evenings implementing Convention over Configurations, and Rapid Web NMVP Assembly that will facilates the framework use !</p>
<p>Â <a href="http://www.codeplex.com/nmvp">www.codeplex.com/nmvp</a></p>
]]></content:encoded>
			<wfw:commentRss>http://sadekdrobi.com/2007/01/02/joined-the-nmvp-open-source-project/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Refx:: DSL Tools, dÃ©monstration dâ€™un designer MVP (translation in english follows)</title>
		<link>http://sadekdrobi.com/2007/01/02/refx-dsl-tools-demonstration-d%e2%80%99un-designer-mvp-translation-in-english-follows/</link>
		<comments>http://sadekdrobi.com/2007/01/02/refx-dsl-tools-demonstration-d%e2%80%99un-designer-mvp-translation-in-english-follows/#comments</comments>
		<pubDate>Tue, 02 Jan 2007 21:52:28 +0000</pubDate>
		<dc:creator>Sadache</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[MVP]]></category>

		<guid isPermaLink="false">http://sadekdrobi.com/?p=18</guid>
		<description><![CDATA[Depuis quelques temps, je tombe frÃ©quemment sur des articles au sujet des DSL de Microsoft. Je me demande systÃ©matiquement : Â« pourquoi un autre langage de modÃ©lisation ? UML remplit bien ses fonctions et quand bien mÃªme, non seulement je ne crois pas en une modÃ©lisation forward blueprint (une modÃ©lisation exhaustive en dÃ©but de projet), [...]]]></description>
			<content:encoded><![CDATA[<p>Depuis quelques temps, je tombe frÃ©quemment sur des articles au sujet des DSL de Microsoft. Je me demande systÃ©matiquement : Â« pourquoi un autre langage de modÃ©lisation ? UML remplit bien ses fonctions et quand bien mÃªme, non seulement je ne crois pas en une modÃ©lisation forward blueprint (une modÃ©lisation exhaustive en dÃ©but de projet), mais en plus, cette pratique va Ã  lâ€™encontre de mes principes dâ€™agilitÃ© Â»</p>
<p>Ces lignes vont vous montrer pourquoi jâ€™ai eu tort de tourner la page.<span id="more-18"></span><br />
Microsoft Domain Specific Language Tools sont des outils pour implÃ©menter des DSL, des langages spÃ©cifiques conÃ§us pour rÃ©pondre Ã  un problÃ¨me donnÃ©. Ils sont fournis sous la forme d&#8217;un SDK qui peut Ãªtre installÃ© sur Visual Studio 2005 Professional.<br />
Ici, il peut y avoir confusion. Il ne faut pas voir les DSL Tools comme une partie intÃ©grante de Visual Studio mais plutÃ´t comme une solution Ã  part entiÃ¨re (standalone).</p>
<ul>
<li>Â« Un outil pour fabriquer des outils Â» : cette dÃ©finition, celle de Microsoft, souligne la diffÃ©rence avec les outils UML. La dÃ©marche UML/MDA (Model Driven Architecture) est jalonnÃ©e dâ€™une sÃ©rie de transformations de modÃ¨les (Ã©lÃ©ments UML gÃ©nÃ©riques), depuis dâ€™un haut niveau dâ€™abstraction, le CIM (Computation Independent Model), jusquâ€™Ã  lâ€™implÃ©mentation, le PSM (Platform Specific Model).<br />
Câ€™est la diffÃ©rence clÃ©. Ce que Microsoft propose :</li>
<li>Dans une premiÃ¨re phase, lâ€™architecte construit la dÃ©finition du langage pour le modÃ¨le spÃ©cifique, avec ses rÃ¨gles, ses validations, ses entitÃ©s et leurs relations (cette spÃ©cificitÃ© garantit lâ€™adÃ©quation avec le domaine) et il intÃ¨gre le tout dans un outil de modÃ©lisation Ã  utiliser par les dÃ©veloppeursÂ <br />
Â </li>
<li>Dans une deuxiÃ¨me phase, il laisse au dÃ©veloppeur le soin dâ€™utiliser cet outil et ses Ã©lÃ©ments pour dÃ©finir lâ€™implÃ©mentation de son application.</li>
</ul>
<p>Jâ€™ai jusquâ€™Ã  maintenant parlÃ© des DSL dans leur aspect vertical, c&#8217;est-Ã -dire propre Ã  un domaine mÃ©tier. Je vais illustrer lâ€™utilisation des DSL dans une dÃ©marche plutÃ´t horizontale, technique, pour apporter la preuve de leur flexibilitÃ©.<br />
Ma dÃ©monstration traite du pattern MVP (Model View Presenter). Je constate que le concept de code behind est souvent mal compris par les dÃ©veloppeurs. Certains ont tendance Ã  y inclure beaucoup de logique applicative. Or, tout ce qui est dans le code behind nâ€™est ni testable, ni rÃ©utilisable !<br />
Le pattern de conception MVP apporte une rÃ©ponse intÃ©ressante Ã  cette problÃ©matique en dÃ©portant la logique de prÃ©sentation dans un composant spÃ©cifique testable indÃ©pendant de la technologie choisie pour la vue. Il offre ainsi un dÃ©coupage des responsabilitÃ©s sain et robuste.<br />
Mais nâ€™allons pas plus loin dans la description de ce pattern (voir les rÃ©fÃ©rences pour plus dâ€™informations) et revenons aux DSL Tools.<br />
Il revient Ã  lâ€™architecte :</p>
<ul>
<li>De dÃ©finir le mÃ©ta-modÃ¨le du MVP (la sÃ©mantique du langage)</li>
<li>De crÃ©er un outil intÃ©grable Ã  lâ€™environnement de dÃ©veloppement Visual Studio et de dÃ©finir son apparence</li>
<li>Â Dâ€™assurer la validitÃ© du modÃ¨le implÃ©mentÃ© en sâ€™appuyant sur le mÃ©ta-modÃ¨le par la dÃ©finition de rÃ¨gles de validation (la syntaxe du langage).</li>
</ul>
<p>A partir dâ€™un nouveau projet de type Â« Langage Minimal Â», lâ€™architecte dÃ©finit le mÃ©ta-modÃ¨le, composÃ© des Ã©lÃ©ments suivants :</p>
<ul>
<li>View (qui correspond Ã  une vue, de type WinForm, WebForm, XAML,â€¦)</li>
<li>ViewContract (une interface pour la vue)</li>
<li>Presenter</li>
<li>Model (rÃ©fÃ©rence un Ã©lÃ©ment mÃ©tier)</li>
</ul>
<p>â€¦et des relations suivantes :</p>
<ul>
<li>Un ViewContract est implÃ©mentÃ© par une ou plusieurs View</li>
<li>Un Presenter manipule une vue par son contrat (ViewContract)</li>
<li>Un Presenter Â« utilise Â» un Ã©lÃ©ment du modÃ¨le</li>
</ul>
<p><a target="_blank" href="http://sadekdrobi.com/wp-content/uploads/2007/01/1.JPG"><img width="147" src="http://sadekdrobi.com/wp-content/uploads/2007/01/1.JPG" alt="1.JPG" height="93" style="width: 147px; height: 93px" id="image19" /></a></p>
<p>Sont ensuite dÃ©finies les rÃ¨gles de validation qui permettent dâ€™assurer la cohÃ©rence :</p>
<ul>
<li>Erreur bloquante si une View nâ€™implÃ©mente aucun ViewContract</li>
<li>Erreur bloquante si un ViewContract nâ€™est rÃ©fÃ©rencÃ©e par aucun Presenter</li>
<li>Message dâ€™alerte si un ViewContract nâ€™est implÃ©mentÃ© par aucune View</li>
<li>Message dâ€™alerte si un Presenter ne rÃ©fÃ©rence aucun ViewContract.</li>
</ul>
<p><span lang="EN-GB">Â <span lang="EN-GB">Â <span lang="EN-GB">using System;<br />
</span><span lang="EN-GB">using Microsoft.VisualStudio.Modeling.Validation;<br />
</span><span lang="EN-GB">using System.Globalization;<br />
</span><span lang="EN-GB"><span lang="EN-GB">namespace Valtech.ModelViewPresenter<br />
</span><span lang="EN-GB">{<br />
</span><span lang="EN-GB">Â Â Â  ///<br />
</span><span lang="EN-GB">Â Â  Â /// Validation checks that will be applied to every ViewContract<br />
</span><span lang="EN-GB">Â Â Â  ///<br />
</span><span lang="EN-GB">Â Â Â  [Microsoft.VisualStudio.Modeling.Validation.ValidationState<br />
</span><span lang="EN-GB">Â Â Â  (Microsoft.VisualStudio.Modeling.Validation.ValidationState.Enabled)]<br />
</span><span lang="EN-GB">Â Â Â  public partial class ViewContract<br />
</span><span lang="EN-GB">Â Â Â  {<br />
</span><span lang="EN-GB">Â Â Â Â Â Â Â  ///<br />
</span><span lang="EN-GB">Â Â Â Â Â Â Â  /// PresenterReference : Validates that ViewContract is manipulated by a Presenter<br />
</span><span lang="EN-GB">Â Â Â Â Â Â Â  ///<br />
</span><span lang="EN-GB">Â Â Â Â Â Â Â  ///Â Â Â Â Â Â Â Â Â Â Â Â </p>
<param name="context"></param><span lang="EN-GB">Â Â Â Â Â Â Â  [ValidationMethod(ValidationCategories.Menu<br />
</span><span lang="EN-GB">Â Â Â Â Â Â Â Â Â Â Â  | ValidationCategories.Open<br />
</span><span lang="EN-GB">Â Â Â Â Â Â Â Â Â Â Â  | ValidationCategories.Save)]<br />
</span><span lang="EN-GB">Â Â Â Â Â Â Â  private void ValidatePresenterReference(ValidationContext context)<br />
</span><span lang="EN-GB">Â Â Â Â Â Â Â  {<br />
</span><span lang="EN-GB">Â Â Â Â Â Â Â Â Â Â Â  if (this.Presenters == null || this.Presenters.Count != 1)<br />
</span><span lang="EN-GB">Â Â Â Â Â Â Â Â Â Â Â  {<br />
</span><span lang="EN-GB">Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  string description = String.Format(CultureInfo.CurrentCulture, Validation.DomainModel_Resource.PresenterReference);<br />
</span><span lang="EN-GB">Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  context.LogError(description, &#8220;PresenterReference&#8221;, this);<br />
</span><span lang="EN-GB">Â Â Â Â Â Â Â Â Â Â Â  }<br />
</span><span lang="EN-GB">Â Â Â Â Â Â Â  }<br />
</span><span lang="EN-GB"><span lang="EN-GB">Â Â Â Â Â Â Â  [ValidationMethod(ValidationCategories.Menu<br />
</span><span lang="EN-GB">Â Â Â Â Â Â Â Â Â Â Â  | ValidationCategories.Open<br />
</span><span lang="EN-GB">Â Â Â Â Â Â Â Â Â Â Â  | ValidationCategories.Save)]<br />
</span><span lang="EN-GB">Â Â Â Â Â Â Â  private void ValidateContractImplemented(ValidationContext context)<br />
</span><span lang="EN-GB">Â Â Â Â Â Â Â  {<br />
</span><span lang="EN-GB">Â Â Â Â Â Â Â Â Â Â Â  if (this.Views == null || this.Views.Count == 0)<br />
</span><span lang="EN-GB">Â Â Â Â Â Â Â Â Â Â Â  </span>{<br />
Â Â  Â Â Â Â Â Â Â Â Â Â Â Â Â string description = String.Format(CultureInfo.CurrentCulture, Validation.DomainModel_Resource.ContractImplemented);<br />
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  context.LogWarning(description, &#8220;ContractImplemented&#8221;, this);<br />
Â Â Â Â Â Â Â Â Â Â Â  }<br />
Â Â Â Â Â Â Â  }<br />
Â Â Â  }<br />
}Â Â Â Â Le dÃ©veloppeur dÃ©finit le modÃ¨le grÃ¢ce Ã  une interface graphique sur laquelle il peut dÃ©poser les composants du MVP et dÃ©finir leurs relations. La qualitÃ© de lâ€™implÃ©mentation est garantie par les rÃ¨gles de validation. On peut mÃªme aller plus loin avec les Â« text templates Â» qui permettent de gÃ©nÃ©rer du code pour les classes, les tests unitaires, des bouchons (mocks).</p>
<p><a target="_blank" href="http://sadekdrobi.com/wp-content/uploads/2007/01/2.JPG"><img width="128" src="http://sadekdrobi.com/wp-content/uploads/2007/01/2.JPG" alt="2.JPG" height="93" style="width: 128px; height: 93px" id="image20" /></a></p>
<p><span lang="EN-GB">Â <span lang="EN-GB">Â <span lang="EN-GB">Â <span lang="EN-GB">Â <span lang="EN-GB">&lt;#@ template inherits=&#8221;Microsoft.VisualStudio.TextTemplating.VSHost.ModelingTextTransformation&#8221;#&gt;<br />
</span><span lang="EN-GB">&lt;#@ ModelViewPresenter processor=&#8221;ModelViewPresenterDirectiveProcessor&#8221; requires=&#8221;fileName=&#8217;../mvp.mvp&#8217;&#8221; #&gt;<br />
</span><span lang="EN-GB"><span lang="EN-GB">&lt;#<br />
</span><span lang="EN-GB">Â Â Â Â Â Â  foreach(MVPComponents type in this.MVP.Elements)<br />
</span><span lang="EN-GB">Â Â Â  {<br />
</span><span lang="EN-GB">Â Â Â Â Â Â Â Â Â Â Â Â  if (type is ViewContract)<br />
</span><span lang="EN-GB">Â Â Â Â Â Â Â Â Â Â Â Â  {<br />
</span><span lang="EN-GB">#&gt;<br />
</span><span lang="EN-GB">Â Â Â  public interface &lt;#= type.Name#&gt;<br />
</span><span lang="EN-GB">Â Â Â  </span>{<br />
Â Â Â  }<br />
&lt;#<br />
Â Â Â Â Â Â Â Â Â Â Â Â  }<br />
Â Â Â  }<br />
#&gt;Â Â Â Â Â Â Â Â Â Le GAT (Guidance Automation Toolkit), qui est une Ã©volution des Â« Entrerprise Templates Â», peut Ãªtre utilisÃ© pour la dÃ©finition de la structure des solutions et des projets ou pour ajouter une aide contextuelle Ã  lâ€™utilisateur de DSL.<br />
Dâ€™autres fonctionnalitÃ©s seront certainement proposÃ©es dans les futures versions du SDK pour une plus grande flexibilitÃ© et une meilleure Â« expÃ©rience utilisateur Â». Citons la synchronisation bidirectionnelle entre le code et le modÃ¨le.</p>
<p>Parlons agilitÃ©. La dÃ©marche de Microsoft DSL Tools nâ€™est pas celle d&#8217;une modÃ©lisation forward blueprint. Il sâ€™agit de donner la responsabilitÃ© de la dÃ©fintion dâ€™un langage spÃ©cifique aux experts du domaine et aux architectes, qui dÃ©tiennent les rÃ¨gles mÃ©tier de validation, tout en donnant la souplesse Ã  lâ€™utilisateur avec un outil qui peut Ãªtre utilisÃ© dans une dÃ©marche agile itÃ©rative (sketchs de conception, implÃ©mentation, tests).</p>
<p>Un autre aspect sur lequel le DSL Tool est supÃ©rieur Ã  UML/MDA, est sa rÃ©utilisabilitÃ© car il est conÃ§u pour tout un domaine (mÃ©tier ou technique). Pour cela, on peut imaginer que des DSL Tools feront lâ€™objet dâ€™offres commerciales.</p>
<p>Les DSL occupent une place importante dans la vision de lâ€™usine logicielle selon Microsoft (Software Factory Initiative). UtilisÃ©s conjointement avec le GAT, ils contribuent Ã  lâ€™extensivitÃ© de lâ€™environnement de dÃ©veloppement et câ€™est bien dans ce contexte quâ€™ils montrent toute leur valeur. A lâ€™heure de lâ€™industrialisation et alors que les mÃ©thodes agiles apportent la preuve de leur efficacitÃ©, nous pouvons Ãªtre certains quâ€™il sâ€™agit dâ€™une technologie sur laquelle il faut compter.</p>
<p><strong>Â Â Â Â Â Â Â Â Â Â Â Â <strong><span></span></strong><strong>Julien Delhomme<br />
</strong><a href="mailto:Julien.Delhomme@valtech.fr">Julien.Delhomme@valtech.fr</a></p>
<p><a href="http://www.juliendelhomme.com/">www.juliendelhomme.com</a><br />
<strong>Sadek Drobi<br />
</strong><a href="mailto:Sadek.Drobi@valtech.fr">Sadek.Drobi@valtech.fr</a><br />
http://www.sadekdrobi.com<br clear="all" /></p>
<p>Consultants Valtech, experts .NET &#8211; Industrialisation<br />
www.valtech.fr<br />
<strong>RÃ©fÃ©rences<br />
</strong>Martin Fowler<br />
<a href="http://www.martinfowler.com/eaaDev/SupervisingPresenter.html"><font color="#800080">http://www.martinfowler.com/eaaDev/SupervisingPresenter.html</font></a><br />
Â <br />
Article MSDN sur MVP<br />
<a href="http://msdn.microsoft.com/msdnmag/issues/06/08/DesignPatterns/">http://msdn.microsoft.com/msdnmag/issues/06/08/DesignPatterns/</a><br />
Page dâ€™accueil DSL Tools sur le site de Microsoft<br />
<a href="http://msdn.microsoft.com/vstudio/DSLTools">http://msdn.microsoft.com/vstudio/DSLTools</a></p>
<p>Â Â Â Â Â Â Â Â Â Â </p>
<p>Â Â Â Â Â Â Â Â Â Â </p>
<p></strong></p>
<p></span></p>
<p></span></p>
<p></span></p>
<p></span></p>
<p></span></p>
<p></span></p>
<p></span></span></p>
<p></span></p>
<p></span></p>
]]></content:encoded>
			<wfw:commentRss>http://sadekdrobi.com/2007/01/02/refx-dsl-tools-demonstration-d%e2%80%99un-designer-mvp-translation-in-english-follows/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OBSEV: Model View Presenter w/Command (DataGrid)</title>
		<link>http://sadekdrobi.com/2006/11/22/obsev-model-view-presenter-wcommand-datagrid/</link>
		<comments>http://sadekdrobi.com/2006/11/22/obsev-model-view-presenter-wcommand-datagrid/#comments</comments>
		<pubDate>Wed, 22 Nov 2006 16:21:23 +0000</pubDate>
		<dc:creator>Sadache</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[MVP]]></category>

		<guid isPermaLink="false">http://sadekdrobi.com/?p=16</guid>
		<description><![CDATA[This weekent went fast, i didnt really have time to work on a sample code . Here i post a sketch, a class and a sequence diagram, showing an example of Model View Presenter with a command pattern implementation , for a datagrid.
The initBinder method on the binderObject subscribes it to the deffirent events of [...]]]></description>
			<content:encoded><![CDATA[<p id="msgcns!E7DF11A26294CBD2!143">This weekent went fast, i didnt really have time to work on a sample code . Here i post a sketch, a class and a sequence diagram, showing an example of Model View Presenter with a command pattern implementation , for a datagrid.</p>
<p>The initBinder method on the binderObject subscribes it to the deffirent events of the datagrid.</p>
<p>then treatement can be added for the deffirent events.</p>
<p>DataBinders with xml mappping can be provided, so that reflexion can be used to map model to the dataBinderObject at run time.<span id="more-16"></span></p>
<p>i promise more explaination about benefits of applying such a pattern, and the more sophisticated Demo.</p>
<p>for now i ll let you view the diagrams, feel free to comment!</p>
<p>Class Diagram:</p>
<p><img src="http://sadekdrobi.com/wp-content/uploads/2006/11/gif_1.gif" /></p>
<p>Sequence Diagram :</p>
<p><img src="http://sadekdrobi.com/wp-content/uploads/2006/11/gif_2.gif" /></p>
]]></content:encoded>
			<wfw:commentRss>http://sadekdrobi.com/2006/11/22/obsev-model-view-presenter-wcommand-datagrid/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>OBSEV: Model View Presenter w/Command (sadek)</title>
		<link>http://sadekdrobi.com/2006/11/22/obsev-model-view-presenter-wcommand-sadek/</link>
		<comments>http://sadekdrobi.com/2006/11/22/obsev-model-view-presenter-wcommand-sadek/#comments</comments>
		<pubDate>Wed, 22 Nov 2006 15:20:27 +0000</pubDate>
		<dc:creator>Sadache</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[MVP]]></category>

		<guid isPermaLink="false">http://sadekdrobi.com/?p=7</guid>
		<description><![CDATA[Thinking about Martinâ€™s Article, and the MSDN extension of MVP,I realize the importance of this pattern. I even see that a whole extension framework can be built on this pattern, I will explain moreâ€¦
I believe in the dumb view, a view that doesnâ€™t contain any logic, not even data binding.
Martinâ€™s MVP together with the MSDNâ€™s [...]]]></description>
			<content:encoded><![CDATA[<div>Thinking about Martinâ€™s Article, and the MSDN extension of MVP,I realize the importance of this pattern. I even see that a whole extension framework can be built on this pattern, I will explain moreâ€¦<br />
I believe in the dumb view, a view that doesnâ€™t contain any logic, not even data binding.<br />
Martinâ€™s MVP together with the MSDNâ€™s article answered several questions, and showed ways to keep the view really clean, but we sacrifice ASP.netâ€™s productivity (DataSource xml mappers, binding mechanismâ€¦etc )<br />
But here comes a question, should we really sacrifice all of that? The answer is yes and no!<br />
In my point of view, yes we should avoid putting logic in the page handler, I know that wizards are easier, but they are not healthier! This binding and mapping mechanism live in the code behind, which is not unit tested, that means a source code that can grow buggy, and yet not covered by the tests.<br />
But the good news is, No we donâ€™t have to forget about productivity, as long as we are capable of creating our healthy ways to do it.<br />
In my observation I will try to suggest solutions to these kind of problems, u are free to take it, to like it, or not. Anyway any comment will be helpful to develop the solution.<br />
My solution is inherited from the Command Pattern (GoF).<br />
Back to the dumb view, so we move all the logic to the presenter, and we leave the code behind with properties and events, and we subscribe the presenter to these events.<br />
The DataBinding is done by a command object, actually I am calling it command just to remind that all commands share the same interface, with one method (the Execute() method) and these commands encapsulateÂ  the logic.<br />
I donâ€™t guess that sticking to the strict definition of the command pattern is important, we can have the Interface having more than one method, the most important thing, is to share the interface with one or more executing methods .<br />
Following sketch is a class diagram and a sequence diagram that illustrates the interaction between the the page (view) , the presenter, the command and the model.<br />
The benefits of such a model is to avoid code duplication by providing the data bindingÂ  logic , encapsulating it insideÂ  a class, and providing the possibility of implementing an xml mapping to the model (using app.config or even custom configuration files that look like the asp.net DataSource mapping).</div>
<div>This example is quite simple and straightforward as it is about the simplest databinding (which is to a drop down list)<br />
Later I will provide another observation , with a more sophisticated example, illustrating more functionalities (update,delete,edit).</div>
<div />
<div>Â <img src="http://sadekdrobi.com/wp-content/uploads/2006/11/modelviewpresenter.jpg" /></div>
<div>
Â <font face="Arial" color="#000066">Â </font><font face="Arial" color="#000066"><img src="http://sadekdrobi.com/wp-content/uploads/2006/11/modelviewpresenterclass.jpg" />Â Â </p>
<p>Â </p>
<p /></font></div>
]]></content:encoded>
			<wfw:commentRss>http://sadekdrobi.com/2006/11/22/obsev-model-view-presenter-wcommand-sadek/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Refx &gt;&gt; Model-View-Presenter for Asp.net</title>
		<link>http://sadekdrobi.com/2006/11/22/refx-model-view-presenter-for-aspnet/</link>
		<comments>http://sadekdrobi.com/2006/11/22/refx-model-view-presenter-for-aspnet/#comments</comments>
		<pubDate>Wed, 22 Nov 2006 15:15:33 +0000</pubDate>
		<dc:creator>Sadache</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[MVP]]></category>

		<guid isPermaLink="false">http://sadekdrobi.com/?p=4</guid>
		<description><![CDATA[Martin Fowler talked about the Model-View-Presenter in his blog , then he came back and splitted it into 3 patterns :
Â Presentation Model, Supervising Controller, Passive View
The degree of seperation between model, controller and view varies in each of the three.
I recommend reading Martin&#8217;s articles about the MVP for deeper understanding of the patterns.
Follows is a [...]]]></description>
			<content:encoded><![CDATA[<div>Martin Fowler talked about the Model-View-Presenter in his blog , then he came back and splitted it into 3 patterns :</div>
<div>Â Presentation Model, Supervising Controller, Passive View<br />
The degree of seperation between model, controller and view varies in each of the three.<br />
I recommend reading Martin&#8217;s articles about the MVP for deeper understanding of the patterns.<br />
Follows is a REFLECTION, an article in msdn Magazine, implementing the MVP in asp.net, quite interesting, Especially on this event based web framework.</div>
<div>The author chose to present the Model View Presenter using Test Driven Development, pointing it out (MVP) as solution for implementing the TDD approach in asp.net .</div>
<div />
<div><a href="http://msdn.microsoft.com/msdnmag/issues/06/08/DesignPatterns/default.aspx"><u><font color="#0000ff">http://msdn.microsoft.com/msdnmag/issues/06/08/DesignPatterns/default.aspx</font></u></a></div>
]]></content:encoded>
			<wfw:commentRss>http://sadekdrobi.com/2006/11/22/refx-model-view-presenter-for-aspnet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

