<?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>Alkampfer&#039;s Place&#187; Software Architecture</title>
	<atom:link href="http://www.codewrecks.com/blog/index.php/category/software-architecture/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codewrecks.com/blog</link>
	<description>Wrecks of code floating in the sea of Internet</description>
	<lastBuildDate>Tue, 27 Jul 2010 10:16:27 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Cache layer</title>
		<link>http://www.codewrecks.com/blog/index.php/2010/07/07/cache-layer/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2010/07/07/cache-layer/#comments</comments>
		<pubDate>Wed, 07 Jul 2010 12:47:00 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Software Architecture]]></category>
		<category><![CDATA[Aop]]></category>
		<category><![CDATA[Architecture Castle]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2010/07/07/cache-layer/</guid>
		<description><![CDATA[
			
				
			
		
Thanks to AOP concept, creating a cache layer that is transparent to the caller is not a big deal. I have a piece of code that in a loop calls this method


   1: ClientCompetitor competitor = Repository.ClientCompetitor.GetByCriteria(

   2:     Query.CreateEq(&#34;AssociatedClient&#34;, clieid))

   3: .SingleOrDefault();

   4: [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2010%2F07%2F07%2Fcache-layer%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2010%2F07%2F07%2Fcache-layer%2F&amp;source=alkampfer&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Thanks to AOP concept, creating a cache layer that is transparent to the caller is not a big deal. I have a piece of code that in a loop calls this method</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> ClientCompetitor competitor = Repository.ClientCompetitor.GetByCriteria(</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span>     Query.CreateEq(<span style="color: #006080">&quot;AssociatedClient&quot;</span>, clieid))</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> .SingleOrDefault();</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>            </pre>
<p><!--CRLF--></div>
</div>
<p>This is based on a standard implementation of repository and a <a target="_blank" href="http://en.wikipedia.org/wiki/Specification_pattern">Specification Pattern</a> as a query model. With <a target="_blank" href="http://nhprof.com/">nhibernate profiler</a> I see that during the execution of this task, a lot of identical query are issued, because it gets called around 1000 times always with the same clieid.</p>
<p>Now before changing the code of the class to store in a dictionary&lt;ClieId, ClientCompetitor&gt; cached result, I like to handle this with a transparent cache layer used with AOP. This could be achieved registering this interceptor.</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">component</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span>         <span style="color: #ff0000">id</span><span style="color: #0000ff">=&quot;RepositoryCache&quot;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>         <span style="color: #ff0000">service</span><span style="color: #0000ff">=&quot;Castle.Core.Interceptor.IInterceptor, Castle.Core&quot;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>         <span style="color: #ff0000">type</span><span style="color: #0000ff">=&quot;xxx.BaseServices.Castle.Cache2Interceptor, xxx.BaseServices&quot;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span>         <span style="color: #ff0000">lifestyle</span><span style="color: #0000ff">=&quot;transient&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">parameters</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span>         <span style="color: #0000ff">&lt;</span><span style="color: #800000">invocationCache</span><span style="color: #0000ff">&gt;</span>${WindowsCache}<span style="color: #0000ff">&lt;/</span><span style="color: #800000">invocationCache</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span>         <span style="color: #0000ff">&lt;</span><span style="color: #800000">Rules</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span>             <span style="color: #0000ff">&lt;</span><span style="color: #800000">dictionary</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span>                 <span style="color: #0000ff">&lt;</span><span style="color: #800000">entry</span> <span style="color: #ff0000">key</span><span style="color: #0000ff">=&quot;ClientCompetitor.*GetByCriteria&quot;</span><span style="color: #0000ff">&gt;</span>.classname(ClientCompetitor) .absolute(600)<span style="color: #0000ff">&lt;/</span><span style="color: #800000">entry</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span>             <span style="color: #0000ff">&lt;/</span><span style="color: #800000">dictionary</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span>         <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Rules</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span>     <span style="color: #0000ff">&lt;/</span><span style="color: #800000">parameters</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">component</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></div>
</div>
<p>I simply create a Cache2Interceptor object that has a property called Rules to define regular expression to specify witch methods I want to apply cache to. With this configuration I ask to put cache in ClientCompetitor repository and with GetByCriteria method. The cache component use the Specification Pattern object for retrieving the key to the real cache implementation, and I tell cache component that the classname is (ClientCompetitor). The className is used because I can register other method as cache invalidator, as an example I can tell that the method Save of ClientCompetitor repository will invalidate the entire ClientCompetitor class.</p>
<p>When the interceptor is configured, I can simply tell castle to apply it to ClientCompetitor repository class, and the game is done.</p>
<p>Then I rerun the code and only one query gets issued, with no modification to the original code.</p>
<p>alk. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2010/07/07/cache-layer/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Writing windows services, smoke test form</title>
		<link>http://www.codewrecks.com/blog/index.php/2009/12/16/writing-windows-services-smoke-test-form/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2009/12/16/writing-windows-services-smoke-test-form/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 17:58:08 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Software Architecture]]></category>
		<category><![CDATA[Architecture]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2009/12/16/writing-windows-services-smoke-test-form/</guid>
		<description><![CDATA[
			
				
			
		
I work often with windows services, and one of the most painfully experience is that you do not have an UI and quite often they have to do some scheduled task at certain time, so whenever you have a deploy you can find yourself in this situation.
 
The question mark means that you are not [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F12%2F16%2Fwriting-windows-services-smoke-test-form%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F12%2F16%2Fwriting-windows-services-smoke-test-form%2F&amp;source=alkampfer&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>I work often with windows services, and one of the most painfully experience is that you do not have an UI and quite often they have to do some scheduled task at certain time, so whenever you have a deploy you can find yourself in this situation.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2009/12/image15.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2009/12/image_thumb15.png" width="244" height="130" /></a> </p>
<p>The question mark means that you are not sure if your new deploy is really ok, because maybe some of the scheduled tasks will fail for misconfiguration or something else. Usually production machine are different to developement ones, you can miss components, point to a wrong db, forget to update configuration, etc, etc. To avoid this situation you need a series of smoke checks, I usually proceed in this way.</p>
<p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:61c9a360-447c-4ea6-b302-16c398e9a116" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">class</span><span style="color: #000000;"> TestResult
{
    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> Boolean Result { </span><span style="color: #0000FF;">get</span><span style="color: #000000;">; </span><span style="color: #0000FF;">set</span><span style="color: #000000;">; }
    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> String Comment { </span><span style="color: #0000FF;">get</span><span style="color: #000000;">; </span><span style="color: #0000FF;">set</span><span style="color: #000000;">; }

    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> </span><span style="color: #0000FF;">readonly</span><span style="color: #000000;"> TestResult Ok </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> TestResult() {Result </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">true</span><span style="color: #000000;">};
}

</span><span style="color: #0000FF;">interface</span><span style="color: #000000;"> ITest
{
    TestResult Execute();
    String Name { </span><span style="color: #0000FF;">get</span><span style="color: #000000;">; }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>This is the interface of the a component that is able to do a test, I can now write stuff like this </p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:ff7f23e6-0637-44c6-a6aa-bd5eecedf28e" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">class</span><span style="color: #000000;"> ResolveIoCTest: ITest
{
    </span><span style="color: #0000FF;">#region</span><span style="color: #000000;"> ITest Members</span><span style="color: #000000;">

    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> TestResult Execute()
    {
        IoC.Resolve</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">IMYInterface</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">(</span><span style="color: #800000;">"</span><span style="color: #800000;">xxxxx</span><span style="color: #800000;">"</span><span style="color: #000000;">);
        </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> TestResult.Ok;
    }

    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">string</span><span style="color: #000000;"> Name
    {
        </span><span style="color: #0000FF;">get</span><span style="color: #000000;"> { </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">ResolveIoCTest</span><span style="color: #800000;">"</span><span style="color: #000000;">; }
    }

    </span><span style="color: #0000FF;">#endregion</span><span style="color: #000000;"> </span><span style="color: #000000;">
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Just to verify that I’m able to resolve a specific interface or a specific component, but you can actually verify every stuff you like, then I write a simple form with a button and a ritch text box.</p>
<p>The next step is finding in the assembly all test classes</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:ee758acd-5cb5-47c9-842f-e3f71da04921" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #000000;">Type testInterfaceType </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">typeof</span><span style="color: #000000;">(ITest);
IoC.FluentRegistration(
    AllTypes.Pick()
    .FromAssembly(
        Assembly.GetExecutingAssembly())
    .If(testInterfaceType.IsAssignableFrom)
    .Configure(reg </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> reg.LifeStyle.Transient)
    .WithService.FirstInterface());</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Thanks to fluent registration it is easy to autoregister all test classes, now the test form simply call IoC.ResolveAll&lt;ITest&gt; and for each test execute it in a Try catch, and here is a typical result.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2009/12/image16.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2009/12/image_thumb16.png" width="636" height="315" /></a> </p>
<p>Some exception occurred. </p>
<p>Now each time I write a component that needs verification, I write a simple test, then each time I deploy I runs this form and verify that I do not miss anything. This saves me pain, because I know that at least a set of smoke tests are passing correctly.</p>
<p>alk.</p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/Software Architecture" rel="tag">Software Architecture</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2009/12/16/writing-windows-services-smoke-test-form/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Wise architects</title>
		<link>http://www.codewrecks.com/blog/index.php/2009/12/09/wise-architects/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2009/12/09/wise-architects/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 10:54:37 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Software Architecture]]></category>
		<category><![CDATA[Architecture]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2009/12/09/wise-architects/</guid>
		<description><![CDATA[
			
				
			
		
Software architects are similar to civil architects, both of them work to create an artifact (software or building) on paper that will be subsequent realized. If you are committed to make a project of a building, witch of them you will like more to create?
 
 
I’m sure that the first one is probably the [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F12%2F09%2Fwise-architects%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F12%2F09%2Fwise-architects%2F&amp;source=alkampfer&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Software architects are similar to civil architects, both of them work to create an artifact (software or building) on paper that will be subsequent realized. If you are committed to make a project of a building, witch of them you will like more to create?</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2009/12/image9.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2009/12/image_thumb9.png" width="244" height="184" /></a> </p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2009/12/image10.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2009/12/image_thumb10.png" width="244" height="164" /></a> </p>
<p>I’m sure that the first one is probably the first choice, for many reasons. It is more challenging, it will probably make you famous, it is surely more beautiful. The problem is that those two buildings are really different in use. The first can be a congress center, a museum, but the other one is a simple house for a standard family. The problem arise if a customer ask you for “an house for me and my family” and you will give him the project to build the first one…. probably it is not the expected result, from the perspective of the customer.</p>
<p>The first solution is more expensive to build, it is probably more expensive to maintain, in winter you will spend tons of money to keep it warm and bla bla bla. The point is that the first building is a perfect structure for a museum, but absolutely the wrong one for a family, so if you, as an architect, will create the first one for a family, you have failed.</p>
<p>In software architecture the situation is the same, but with a great difference: the artifact that gets created, is immaterial (a software cannot be seen), and the customer does not perceive its complexity. Everyone can say “hey, I asked for an house, not for a congress center”, but it is really more difficult to look at a software and say, hey, I asked for a simple web site for my football club, but you gave me a full e-commerce, that I does not need.</p>
<p>In this situation, the customer can ask for a software to manage some data from hardware RFID readers. The architect can create a big software made of separated executables, WCF, Remoting, flexible and pluggable algorithms, etc etc. The big problem is that probably it is enough a single executable connected to the readers, that stores raw data in db, plus another executables that manage those data and finally a bunch of web pages that show the result. The three parts are completely disconnected, and the whole architecture is Simple.</p>
<p>Sometimes architects discard simple solutions, they want the customer to think “This architect is really good, he created really a good project that uses latest technologies”. Then they realize, once in production, that they simply created the wrong stuff, because the whole think is difficult to maintain and probably the customer is not satisfied with the final result.</p>
<p>The conclusion is that, in my opinion, <em>simplicity </em>is a really great value for an architecture, and wise architects always takes it in consideration when they make a project. Creating a simple structure can be less challenging and less interesting, but the final goal is finding what the customer really needs, not giving fame or pleasure to the architect. So the wise architect is the one who really understand the needs of the customer and resists the temptation to create an architecture only for his personal pleasure.</p>
<p>Alk.</p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/Software architecture" rel="tag">Software architecture</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2009/12/09/wise-architects/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Automatic deployment of a web application with TFS Build</title>
		<link>http://www.codewrecks.com/blog/index.php/2009/10/23/automatic-deployment-of-a-web-application-with-tfs-build/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2009/10/23/automatic-deployment-of-a-web-application-with-tfs-build/#comments</comments>
		<pubDate>Fri, 23 Oct 2009 16:38:28 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Software Architecture]]></category>
		<category><![CDATA[Team Foundation Server]]></category>
		<category><![CDATA[Tfs]]></category>
		<category><![CDATA[TfsBuild]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2009/10/23/automatic-deployment-of-a-web-application-with-tfs-build/</guid>
		<description><![CDATA[
			
				
			
		
When you develop web applications you usually have X developers solving bugs and implementing features, and a series of testers that test application during developing process. A must to have requirement is that

Modifications to the trunk are visible as soon as possible to testers. 
Data in test database gets preserved 

Point 2 is especially important, [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F10%2F23%2Fautomatic-deployment-of-a-web-application-with-tfs-build%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F10%2F23%2Fautomatic-deployment-of-a-web-application-with-tfs-build%2F&amp;source=alkampfer&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>When you develop web applications you usually have X developers solving bugs and implementing features, and a series of testers that test application during developing process. A must to have requirement is that</p>
<ol>
<li>Modifications to the trunk are visible as soon as possible to testers. </li>
<li>Data in test database gets preserved </li>
</ol>
<p>Point 2 is especially important, testers usually work with the site and fills database with data. Suppose that tester John find a bug that occurs only with specific data, a developer correct the bug, then a deploy is done, all test data are wiped away, and the tester is not able to verify if the bug is gone.</p>
<p>Moreover testers usually fill the database with real data useful for testing, then you need to deploy update to web application while updating database schema preserving data. The optimum solution is the one represented by this schema</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2009/10/image8.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2009/10/image-thumb8.png" width="678" height="370" /></a> </p>
<p>When a developer does a check-in or at a scheduled time the build server gets latest bit of the tip, then compile it, and update the web server pointing IIS to the new version and updating schema of the database in the meanwhile, preserving all the data inside the test db.</p>
<p>It is absolutely not a complex stuff to do, here is the only modification I need to do into the msbuild file of the build.</p>
<p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:9c132251-c243-4e6b-97ba-f2b01627d309" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;overflow: auto;"><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">UsingTask
    </span><span style="color: #FF0000;">TaskName</span><span style="color: #0000FF;">="DotNetMarche.MsBuildExtensions.Administrative.IISChangePhisicalDirTask"</span><span style="color: #FF0000;">
    AssemblyFile</span><span style="color: #0000FF;">="..\sources\libs\MsBuildCustomTasks\DotNetMarche.MsBuildExtensions.dll"</span><span style="color: #0000FF;">/&gt;</span><span style="color: #000000;">

</span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">UsingTask
    </span><span style="color: #FF0000;">TaskName</span><span style="color: #0000FF;">="DotNetMarche.MsBuildExtensions.Xml.XmlPokeTask"</span><span style="color: #FF0000;">
    AssemblyFile</span><span style="color: #0000FF;">="..\sources\libs\MsBuildCustomTasks\DotNetMarche.MsBuildExtensions.dll"</span><span style="color: #0000FF;">/&gt;</span><span style="color: #000000;">

</span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">Target </span><span style="color: #FF0000;">Name</span><span style="color: #0000FF;">="AfterDropBuild"</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
    </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">Message </span><span style="color: #FF0000;">Text</span><span style="color: #0000FF;">="Deploy web application for $(BuildNumber)"</span><span style="color: #FF0000;"> </span><span style="color: #0000FF;">/&gt;</span><span style="color: #000000;">

    </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">XmlPokeTask
        </span><span style="color: #FF0000;">FilePath</span><span style="color: #0000FF;">="$(DropLocation)\$(BuildNumber)\debug\_PublishedWebsites\NorthwindWeb\web.config"</span><span style="color: #FF0000;">
        XPath</span><span style="color: #0000FF;">="/connectionStrings/add[@name='Northwind']/@connectionString"</span><span style="color: #FF0000;">
        NewValue</span><span style="color: #0000FF;">='Data </span><span style="color: #FF0000;">Source</span><span style="color: #0000FF;">=localhost\sql2008;Initial </span><span style="color: #FF0000;">Catalog</span><span style="color: #0000FF;">=NorthwindCiTest;user=sa;pwd=Pa$$w0rd'
        </span><span style="color: #FF0000;">FailIfError</span><span style="color: #0000FF;">='false' </span><span style="color: #0000FF;">/&gt;</span><span style="color: #000000;">

    </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">IISChangePhisicalDirTask
        </span><span style="color: #FF0000;">Username</span><span style="color: #0000FF;">="administrator"</span><span style="color: #FF0000;">
        Password</span><span style="color: #0000FF;">="xxxxxxxxxxx"</span><span style="color: #FF0000;">
        MachineName</span><span style="color: #0000FF;">="WS2008V1"</span><span style="color: #FF0000;">
        SiteName</span><span style="color: #0000FF;">="NorthWindTest"</span><span style="color: #FF0000;">
        NewPhisicalDirectory</span><span style="color: #0000FF;">="C:\Drops\NorthwindTest\$(BuildNumber)\debug\_PublishedWebsites\NorthwindWeb\"</span><span style="color: #FF0000;"> </span><span style="color: #0000FF;">/&gt;</span><span style="color: #000000;">

    </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">MSBuild </span><span style="color: #FF0000;">Projects</span><span style="color: #0000FF;">="$(SolutionRoot)\src\DbEdition\NorthwindTest\NorthwindTest\NorthwindTest.dbproj"</span><span style="color: #FF0000;">
                Properties</span><span style="color: #0000FF;">="OutDir=$(DropLocation)\$(BuildNumber)\debug\;TargetDatabase=NorthwindCiTest;DefaultDataPath=C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\DATA;TargetConnectionString=Data Source=WS2008V1\SQL2008,1433%3Buser=sa%3Bpwd=Pa$$w0rd;DeployToDatabase=true;"</span><span style="color: #FF0000;">
                Targets</span><span style="color: #0000FF;">="Deploy"</span><span style="color: #FF0000;">  </span><span style="color: #0000FF;">/&gt;</span><span style="color: #000000;">
</span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">Target</span><span style="color: #0000FF;">&gt;</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>The first two &lt;UsingTask directive are needed to import a couple of custom msbuild task. The first is the XmlPokeTask, used to change part of an xml file. In this example you can verify how I change the connection string, because the Test Server can have a different setup, in this situation I use sa credentials, this is very bad for security, but this is only an example. In real scenario you must use integrated security.</p>
<p>The second task is a simple task that uses the technique described in <a href="http://www.codewrecks.com/blog/index.php/2009/10/12/change-base-directory-of-a-site-in-iis/">this post</a>, I basically change the directory used by the site NorthWindtest of a machine called WS200v1. The most important stuff here, is that drop location is on WS2008V1 machine, and since I know that the physical directory is c:\drops\ I can simply know the real directory from the BuildNuber. This action permits me to point the test site on the latest build.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2009/10/image9.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2009/10/image-thumb9.png" width="630" height="286" /></a>&#160; </p>
<p>Finally since the database could be changed I need to follow the instruction of <a href="http://www.codewrecks.com/blog/index.php/2009/10/06/deploy-a-database-project-with-tfs-build/">this post</a> to deploy the data on test database. Now each time a build is triggered after the build Test machine IIS was automatically redirected to the latest version, the web.config was changed to suite the test environment, and the database is automatically upgraded. I simply did the first checkin of the project and after the build I test the site from my dev machine</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2009/10/image10.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2009/10/image-thumb10.png" width="389" height="269" /></a> </p>
<p>The site was deployed, and I’m able to insert a record in the new empty database created by the first build.</p>
<p>Alk.</p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/Team Foundation Server" rel="tag">Team Foundation Server</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2009/10/23/automatic-deployment-of-a-web-application-with-tfs-build/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Branching policies</title>
		<link>http://www.codewrecks.com/blog/index.php/2009/09/15/branching-policies/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2009/09/15/branching-policies/#comments</comments>
		<pubDate>Tue, 15 Sep 2009 17:42:04 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Software Architecture]]></category>
		<category><![CDATA[Continuous Integration]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2009/09/15/branching-policies/</guid>
		<description><![CDATA[
			
				
			
		
I just read this post of Martin Fowler, and I found it very interesting. In my opinion, even small projects will greatly benefit from Continuous Integration. Despite of the Branching policies that you choose, having a machine for CI is vital during the lifetime of a project. 
Usually I do not like very much CherryPicking [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F09%2F15%2Fbranching-policies%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F09%2F15%2Fbranching-policies%2F&amp;source=alkampfer&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>I just read <a target="_blank" href="http://martinfowler.com/bliki/FeatureBranch.html">this post</a> of <a target="_blank" href="http://martinfowler.com">Martin Fowler</a>, and I found it very interesting. In my opinion, even small projects will greatly benefit from <a target="_blank" href="http://martinfowler.com/articles/continuousIntegration.html">Continuous Integration</a>. Despite of the Branching policies that you choose, having a machine for CI is vital during the lifetime of a project. </p>
<p>Usually I do not like very much <a target="_blank" href="http://chestofbooks.com/computers/revision-control/subversion-svn/Cherrypicking-Branchmerge-Cherrypicking.html">CherryPicking</a> even if sometimes it cannot be avoided. In the Promiscuous Integration model, people are doing CherryPicking from other branches and this scares me. The purpose of a branch is to keep changes isolated until they are ready to be moved in the trunk, or to keep copies of specific version of the software, and usually merging between branches can be problematic. Some Source control system do not permit to merge changes between branches that are not contiguous.</p>
<p>In the example made by Fowler, with Promiscuous Integragion, DrPlum and Reverend Green works together in two different branches, but since they have great communication, they periodically merge from one branches to another, but this scheme is a little confused in my opinion.</p>
<p>A better strategy could be this one. Since DrPlum and Reverend Green have great communication, it can be possible to work in different way. Say DrPlum decide to create a new big feature, so he creates a branch and starts to work on that branch. Suppose he do not want to integrate in the trunk until it is completely finished, so he do not use CI.&#160; He ask to other members of the team if someone is working on a branch, all people say no, so he create a new branch and start working on it. </p>
<p>After some time Reverend Green want also to create a new big feature, he ask to the team, and DrPlum says him that he is also working on a feature that have some code in common with the new feature of REverend Green. At this time both Reverend Green and Dr Plum create another branch from the original branch of DrPlum. The situation is the following, we have the trunk, then we have the first branch of DRplum (call it <strong>B1</strong>), from this branch we have two other branches, one for Reverend Green (<strong>B2G</strong>), and the other for DrPlum (<strong>B2P</strong>). Now we can configure a Continuos Integration Machine to integrate <strong>B1.</strong> Now both DrPlum and Reverend green are working on isolated branches, but they merge changes often with <strong>B1</strong>, so they never need a big merge. In this scheme <strong>B1 </strong>acts like a trunk for both developers. As bugfix or changes are made in the trunk, one of them bring those changes in B1, verify that all tests are good, and when <strong>B1</strong> is stable again, each one propagates changes from <strong>B1</strong> to his own branch.</p>
<p>With such a scheme, two developers can work together on new feature that have code in common, without the risk of big merge, using Continuos Integration, but avoiding put stuff in the trunk until the work is finished. Say Reverend Green has finished his feature, he first integrate all trunk changes in <strong>B1</strong>, verify that everything is ok, then merge changes from <strong>B1</strong> to <strong>B2G</strong>, when everything is ok he merge remaining changes from <strong>B2G</strong> to <strong>B1 (</strong>in the meanwhile DrPlum could have changed <strong>B1</strong>), then from <strong>B1</strong> to the <strong>trunk</strong>, and the game is done. The same is done from DrPlum when he finished.</p>
<p>I admit that I never used such a complex scheme, because I prefer to have developers continuously merge in the trunk, and thanks to CC.net integration problems are mitigated. </p>
<p>Alk.</p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/Continuous Integration" rel="tag">Continuous Integration</a> <a target="_blank" href="http://technorati.com/tag/Source Control" rel="tag">Source Control</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2009/09/15/branching-policies/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Writing modular software with castle.</title>
		<link>http://www.codewrecks.com/blog/index.php/2009/09/14/writing-modular-software-with-castle/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2009/09/14/writing-modular-software-with-castle/#comments</comments>
		<pubDate>Mon, 14 Sep 2009 16:13:08 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Software Architecture]]></category>
		<category><![CDATA[Architecture]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2009/09/14/writing-modular-software-with-castle/</guid>
		<description><![CDATA[
			
				
			
		
Thanks to Castle Windsor you can write really modular software. I’m building a project in WinForm that needs to access the domain model through a service layer on a WS* service. One of the most annoying stuff is the need to setup a local service to test the application and the interface.
The key to speedup [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F09%2F14%2Fwriting-modular-software-with-castle%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F09%2F14%2Fwriting-modular-software-with-castle%2F&amp;source=alkampfer&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Thanks to <a target="_blank" href="http://www.google.it/url?sa=t&amp;source=web&amp;ct=res&amp;cd=1&amp;url=http%3A%2F%2Fwww.castleproject.org%2Fcontainer%2Findex.html&amp;ei=wmKqStPfLIuwsAbHhZjkBw&amp;usg=AFQjCNFGZtGw-ordWFZS86FEFPnx-S_Jtg&amp;sig2=G6cxIH_hY5aZJ93egyGxEw">Castle Windsor</a> you can write really modular software. I’m building a project in WinForm that needs to access the domain model through a service layer on a WS* service. One of the most annoying stuff is the need to setup a local service to test the application and the interface.</p>
<p>The key to speedup development is writing high modular software, thanks to castle this is really simple. First of all I use <a target="_blank" href="http://www.nablasoft.com/guardian/index.php/2009/06/08/unity-wcf-service-resolution-container-extension/">guardian facility</a> to create proxy. This permits me to decide with configuration file if a service interface must be resolved with a proxy, or with a real class. This gives you immense flexibility.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2009/09/image17.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2009/09/image-thumb17.png" width="574" height="484" /></a> </p>
<p>Simply changing a configuration file I can use the concrete class that have access to the database, or pass through a ws* wcf webservice. All people inside the organization, that have vpn and access through the vpn network to the database, can use the concrete class of the service, completely bypassing wcf, while people outside the organization use a config that use wcf service.</p>
<p>The application is structured with a light custom MVC pattern, if a controller needs to access a specific service he simply declare a dependency.</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:655b2c46-8095-44f7-866a-665783e4a0c6" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;overflow: auto;"><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">class</span><span style="color: #000000;"> XXXManagementController : IXXXManagementController
{
    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> IXXXManagementService XXXManagementService { </span><span style="color: #0000FF;">get</span><span style="color: #000000;">; </span><span style="color: #0000FF;">set</span><span style="color: #000000;">; }

    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> ICustomerService CustomerService { </span><span style="color: #0000FF;">get</span><span style="color: #000000;">; </span><span style="color: #0000FF;">set</span><span style="color: #000000;">; }</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>Here is the startup of the application.</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:a4ddb3a4-c7cf-4b05-8982-11792c24e7c3" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;overflow: auto;"><span style="color: #000000;">[STAThread]
</span><span style="color: #0000FF;">static</span><span style="color: #000000;"> </span><span style="color: #0000FF;">void</span><span style="color: #000000;"> Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(</span><span style="color: #0000FF;">false</span><span style="color: #000000;">);
    Application.Run(login </span><span style="color: #000000;">=</span><span style="color: #000000;"> IoC.Resolve</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Login</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">());
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Thanks to an autoscan facility, all view are registered with code, in this software the Login View is always the same form, so I can register with an attribute.</p>
</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:4fd274bf-46db-4fbb-9607-fa1a869cc47a" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;overflow: auto;"><span style="color: #000000;">[AutoscanComponent(LifestyleType.Transient, IsDefault </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">true</span><span style="color: #000000;">, Id </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">Login</span><span style="color: #800000;">"</span><span style="color: #000000;">, ServiceType </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">typeof</span><span style="color: #000000;">(Login))]
</span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">partial</span><span style="color: #000000;"> </span><span style="color: #0000FF;">class</span><span style="color: #000000;"> Login : Form</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>Thanks to autoscan I no need to register all the views in configuration file. </p>
<p>If it seems complex, you should consider the advantage of using a MCV architecture when is come time to test.</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:185d1211-78b4-4cc7-b5fb-6774b491f456" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;overflow: auto;"><span style="color: #000000;">container.Register(Component.For(</span><span style="color: #0000FF;">typeof</span><span style="color: #000000;"> (LoginController)).ImplementedBy</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">LoginController</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">());
LoginController sut </span><span style="color: #000000;">=</span><span style="color: #000000;"> container.Resolve</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">LoginController</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">();
sut.Settings.LastLoggedUserName </span><span style="color: #000000;">=</span><span style="color: #800000;">"</span><span style="color: #800000;">MyUser</span><span style="color: #800000;">"</span><span style="color: #000000;">;
sut.Settings.LastLoggedCustomerId </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">3</span><span style="color: #000000;">;

List</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Customer</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> customers </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> List</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Customer</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">()
                             {
                                 </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> Customer(</span><span style="color: #800080;">1</span><span style="color: #000000;">, </span><span style="color: #800000;">"</span><span style="color: #800000;">pippo</span><span style="color: #800000;">"</span><span style="color: #000000;">, </span><span style="color: #800000;">""</span><span style="color: #000000;">),
                                 </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> Customer(</span><span style="color: #800080;">2</span><span style="color: #000000;">, </span><span style="color: #800000;">"</span><span style="color: #800000;">pippo1</span><span style="color: #800000;">"</span><span style="color: #000000;">, </span><span style="color: #800000;">""</span><span style="color: #000000;">),
                                 </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> Customer(</span><span style="color: #800080;">3</span><span style="color: #000000;">, </span><span style="color: #800000;">"</span><span style="color: #800000;">pippo2</span><span style="color: #800000;">"</span><span style="color: #000000;">, </span><span style="color: #800000;">""</span><span style="color: #000000;">),
                             };

sut.CustomerService.Expect(c </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> c.GetAllCustomers()).Return(customers);
BindingSource bs </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> BindingSource();
bs.DataSource </span><span style="color: #000000;">=</span><span style="color: #000000;"> customers;
sut.View.Expect(v </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> v.SetCustomers(customers)).Return(bs);
sut.Start();
Assert.That(bs.Position, Is.EqualTo(</span><span style="color: #800080;">2</span><span style="color: #000000;">));
sut.View.AssertWasCalled(v </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> v.SetUserText(</span><span style="color: #800000;">"</span><span style="color: #800000;">MyUser</span><span style="color: #800000;">"</span><span style="color: #000000;">));
sut.View.AssertWasCalled(v </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> v.SetFocusToPassword());</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>This seems a complex test but is really simple, I used a custom version of a <a target="_blank" href="http://blog.eleutian.com/CommentView,guid,762249da-e25a-4503-8f20-c6d59b1a69bc.aspx">AutoMockingContainer</a> to resolve the LoginController and creating Mocks for each dependency. Since I’ve abstracted the settings of the application with an Interface, settings property of the controller is a mock, so I can setup lastloggedUserName and LastLoggerCustomerId, then I create a list of customer, and finally setup the expectations. The only bad stuff is that in windows form we does not have the power of binding of WPF, so I need to manage binding with BindingSource objects. The View is a WinForm that has some BindingSource defined in designer, and those BindingSource are passed to the controller. With this test I’m asserting that if the user was already logged, the controller sets last customer and last user in the interface.</p>
<p>The good stuff of this test, is that it uses mocks, so he do not need service, wcf, or anything else to be run. </p>
<p>alk.</p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/Architecture" rel="tag">Architecture</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2009/09/14/writing-modular-software-with-castle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Manipulate Expression Tree in DtoGenerator</title>
		<link>http://www.codewrecks.com/blog/index.php/2009/08/12/manipulate-expression-tree-in-dtogenerator/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2009/08/12/manipulate-expression-tree-in-dtogenerator/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 09:06:00 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Software Architecture]]></category>
		<category><![CDATA[ExpressionTree]]></category>
		<category><![CDATA[T4 Generator]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2009/08/12/manipulate-expression-tree-in-dtogenerator/</guid>
		<description><![CDATA[
			
				
			
		
I’m writing a simple Dto generator, and today I found a challenging problem. I supported dto composition like this:
 
I have a CustomerDto3 that have only CustomerId and ContactName properties, then I want to autogenerate a OrderTestDto that have a Customers property of type CustomerDto3. The syntax on My T4 generator is this one.

SyntaxCode syntax [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F08%2F12%2Fmanipulate-expression-tree-in-dtogenerator%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F08%2F12%2Fmanipulate-expression-tree-in-dtogenerator%2F&amp;source=alkampfer&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>I’m writing a simple <a href="http://www.codewrecks.com/blog/index.php/2009/07/31/dto-generator-and-repository-integration/">Dto generator</a>, and today I found a challenging problem. I supported dto composition like this:</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2009/08/image19.png"><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="265" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2009/08/image-thumb19.png" width="450" border="0" /></a> </p>
<p>I have a CustomerDto3 that have only CustomerId and ContactName properties, then I want to autogenerate a OrderTestDto that have a Customers property of type CustomerDto3. The syntax on My T4 generator is this one.</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:8f382282-83e3-4c14-9b64-466fb66b8333" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #000000;">SyntaxCode syntax </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> SyntaxCode(
    </span><span style="color: #800000;">@"</span><span style="color: #800000;">bin\Debug\DtoFactory.Exe</span><span style="color: #800000;">"</span><span style="color: #000000;">,
    Path.GetDirectoryName(Host.TemplateFile),
    </span><span style="color: #800000;">"</span><span style="color: #800000;">DtoFactory.Dto</span><span style="color: #800000;">"</span><span style="color: #000000;">,
    </span><span style="color: #800000;">"</span><span style="color: #800000;">http://dtonamespace.org</span><span style="color: #800000;">"</span><span style="color: #000000;">);   

syntax.Render(
    </span><span style="color: #800000;">"</span><span style="color: #800000;">DtoFactory.Orders[OrdersTestDto] (OrderID,OrderDate,Customers) (Customers|CustomerDto3)</span><span style="color: #800000;">"</span><span style="color: #000000;">,
    </span><span style="color: #800000;">"</span><span style="color: #800000;">Generated\\OrderDto3.cs</span><span style="color: #800000;">"</span><span style="color: #000000;">); </span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>I’ve simplified dto creation, and supported generation of each dto in a different file. The main problem supporting this scenario is <em>how to write the assembler of the container class</em>. I’ve started generating this code.</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:24e2be1c-4dd6-4411-b9c1-2fd6045eaa70" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> Expression</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Func</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Orders, OrdersTestDto</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;"> ExpressionSelector;

</span><span style="color: #0000FF;">static</span><span style="color: #000000;"> Assembler() {

ExpressionSelector </span><span style="color: #000000;">=</span><span style="color: #000000;"> obj </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> OrdersTestDto() {
        OrderDate </span><span style="color: #000000;">=</span><span style="color: #000000;"> obj.OrderDate,
        OrderID </span><span style="color: #000000;">=</span><span style="color: #000000;"> obj.OrderID,
        Customers </span><span style="color: #000000;">=</span><span style="color: #000000;"> CustomerDto3.Assembler.FromOriginal(obj.Customers),
        }; </span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>It works perfectly, after all since all the Dto are generated by the same generator, when I have to generate code that transforms an original Orders object into an OrdersTestDto, I know that the creation of CustomerDto3 can be delegated to the assembler of CustomerDto3 object. This is good but now this code wont work.</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c9fed9d7-dca5-49d2-9dd9-b76f37d2b790" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #0000FF;">using</span><span style="color: #000000;"> (NorthwindEntities context </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> NorthwindEntities())
    {
        var Query </span><span style="color: #000000;">=</span><span style="color: #000000;"> context.Orders
            .Where(o </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> o.Customers.CustomerID </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">ALFKI</span><span style="color: #800000;">"</span><span style="color: #000000;">)
            .Select(OrdersTestDto.Assembler.ExpressionSelector);</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>If you read my older article, you find that one the most useful feature of dto generator, is the possibility to use expressions to make a projection in the database, but with previous generated Expression, Entity Framework give me this error</p>
<p><div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:0f0a5cc7-f93b-4f84-a095-076108f78fba" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #000000;">failed: System.NotSupportedException : LINQ to Entities does not recognize the method </span><span style="color: #800000;">'</span><span style="color: #800000;">DtoFactory.Dto.CustomerDto3 FromOriginal(</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>This is obvious, because when EF translate the ExpressionTree into SQL code, it finds a call to user function, and he cannot know how to proceed. This seems to me a really bad limitation, so I decided to fix it. The problem is that the expression should be generated this way.</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:3460fb86-048d-4cd5-a106-637cd695b59b" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> Expression</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Func</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Orders, OrdersTestDto</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;"> Expression2Selector
    </span><span style="color: #000000;">=</span><span style="color: #000000;"> obj </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> OrdersTestDto()
      {
          OrderDate </span><span style="color: #000000;">=</span><span style="color: #000000;"> obj.OrderDate,
          OrderID </span><span style="color: #000000;">=</span><span style="color: #000000;"> obj.OrderID,
          Customers </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> CustomerDto3()
          {
            CustomerID </span><span style="color: #000000;">=</span><span style="color: #000000;">  obj.Customers.CustomerID,
            ContactName </span><span style="color: #000000;">=</span><span style="color: #000000;"> obj.Customers.ContactName,
          },
      };</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>With such an expression the EF provider is able to do the projection, but now a big problem arise. I’m working with T4 code generation, when the generator of OrdersTestDto has to generate the Expression, he does not know how the CustomerDto3 was generated, so he cannot generate such an expression. I really want to avoid the need to find a way to make different generators to share data.</p>
<p> After a brief thinking I realize that I already have the right expression built in CustomerDto3, so the only stuff I need is to compose the two expression, but this can be more complex than you can think. After lot of experiments, I came to this little trick.</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:486f65e3-a78d-4ba4-88df-db5dc34645fe" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #0000FF;">private</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> </span><span style="color: #0000FF;">void</span><span style="color: #000000;"> ParseSelector()
{
    ExpressionSelector </span><span style="color: #000000;">=</span><span style="color: #000000;"> (Expression</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Func</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Orders, OrdersTestDto</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">)
        </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> DtoExpressionVisitor().ReplaceDtoBinding(ExpressionSelector);
}

</span><span style="color: #0000FF;">static</span><span style="color: #000000;"> Assembler() {

    ExpressionSelector </span><span style="color: #000000;">=</span><span style="color: #000000;"> ...
    ParseSelector();
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>The trick works in this way, I generate the ExpressionSelector in the usual way, with the call to FromOriginal that does not work in EF LINQ query, but in the static constructor I call a ParseSelector function that rewrite the expression tree making it works with LINQ query <img src='http://www.codewrecks.com/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> . Here is the full code of the DtoExpressionVisitor().</p>
<div class="wlWriterEditableSmartContent" id="scid:DFDE9937-D816-47f4-A306-7B60D5CE5AC0:eecf8639-fa23-4d75-97f9-cc082545deea" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre class="brush: csharp; gutter: true; first-line: 1; tab-size: 4;  toolbar: false; ">class DtoExpressionVisitor : ExpressionVisitor
{
	private ParameterExpression parameter;
	public Expression ReplaceDtoBinding(LambdaExpression exp)
	{
		parameter = exp.Parameters[0];
		return this.Visit(exp);
	}

	protected override System.Linq.Expressions.MemberBinding VisitBinding(System.Linq.Expressions.MemberBinding binding)
	{
		if (binding is MemberAssignment)
		{
			MemberAssignment assign = (MemberAssignment)binding;
			if (assign.Expression is MethodCallExpression)
			{
				MethodCallExpression callex = (MethodCallExpression)assign.Expression;
				if (callex.Arguments.Count == 1)
				{
					MemberExpression argument = (MemberExpression) callex.Arguments[0];
					Type assembler = callex.Method.DeclaringType;
					if (assembler != null)
					{
						//The object is a dto and it is assemblable.
						FieldInfo expSelector = assembler.GetField(&quot;ExpressionSelector&quot;, BindingFlags.Static | BindingFlags.Public);
						if (expSelector != null)
						{
							LambdaExpression lambda = (LambdaExpression)expSelector.GetValue(null);
							MemberInitExpression minit = (MemberInitExpression)lambda.Body;
							List&lt;MemberBinding&gt; bindings = new List&lt;MemberBinding&gt;();
							foreach (MemberBinding oribind in minit.Bindings)
							{
								//1&#194;&#176; step. access obj.Property
								MemberExpression accessOriObj = Expression.Property(parameter, argument.Member.Name);
								//2&#194;&#176; step. access obj.Property.p'roperty
								MemberExpression accessObjProperty = Expression.Property(accessOriObj, oribind.Member.Name);
								//3&#194;&#176; recreate the binding and store into a list
								MemberBinding newPropertyBind = Expression.Bind(oribind.Member, accessObjProperty);
								bindings.Add(newPropertyBind);
							}
							NewExpression constructor = Expression.New(((PropertyInfo) assign.Member).PropertyType);
							MemberInitExpression newInitExpression = Expression.MemberInit(constructor, bindings.ToArray());
							MemberBinding newBindingExpression = Expression.Bind(assign.Member, newInitExpression);
							return newBindingExpression;
						}
					}
				}
			}
		}
		return base.VisitBinding(binding);
	}</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>This is quite complicated code, and it works by replacing part of the original expression tree thus composing two expression tree into one. All initial conditions are needed to identify nodes that need correction, I need to correct all MemberBinding node that are MethodCallExpression, because those ones are the ones calling the FromOriginal function from the other dto. When I find a node to change, I otbain with reflection the original expression selector (a lambdaexpression), where the body is the one used to initialize the Dto. Now I have to regenerate a similar expression using the parameter of the original lambda. Basically the problem is the following, the OrdersTestDto expressionSelector has a parameter named obj of type orders, but the ExpressionSelector of CustomersDto3 has one parameter named obj but of type Customers. This means that I need to regenerate the whole ExpressionTree part, changing how parameters works.</p>
<p>The first step is to regenerate all MemberBinding expression part, with the original parameter (line 34-38) and storing them into a collection (line 39). The operation is the following, I need a MemberExpression that extract from the parameter the object needed to generate the Dto. In the above example accessing the Customers property of Orders to gets a Customer object. Then another MemberExpression to take the corresponding field from the Customers object, and finally a MemberBinding. </p>
<p>Then I need to recreate the BindingExpression (line 41-43) and returning in place of the original one. All the code is based on the <a target="_blank" href="class DtoExpressionVisitor : ExpressionVisitor">original Tree Visitor by microsoft</a>. With this quite complex trick I’m able to change the expression at runtime, thus keeping the generator simple.</p>
<p>alk.</p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/T4 Generator" rel="tag">T4 Generator</a> <a target="_blank" href="http://technorati.com/tag/Expression Tree" rel="tag">Expression Tree</a> <a target="_blank" href="http://technorati.com/tag/LINQ" rel="tag">LINQ</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2009/08/12/manipulate-expression-tree-in-dtogenerator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dto generator and repository integration</title>
		<link>http://www.codewrecks.com/blog/index.php/2009/07/31/dto-generator-and-repository-integration/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2009/07/31/dto-generator-and-repository-integration/#comments</comments>
		<pubDate>Fri, 31 Jul 2009 09:02:37 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software Architecture]]></category>
		<category><![CDATA[CodeGeneration T4TemplateEngine]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2009/07/31/dto-generator-and-repository-integration/</guid>
		<description><![CDATA[
			
				
			
		
I have a project where I use repository pattern, interface code access domain object through a service, and the service return Dto. One of the bad side of the dto is that they are boring to write and to maintain. The main risk is that developers does this error
Mmmm I had to show a combo [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F07%2F31%2Fdto-generator-and-repository-integration%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F07%2F31%2Fdto-generator-and-repository-integration%2F&amp;source=alkampfer&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>I have a project where I use repository pattern, interface code access domain object through a service, and the service return Dto. One of the bad side of the dto is that they are boring to write and to maintain. The main risk is that developers does this error</p>
<p>Mmmm I had to show a combo with all the typologies of the current customer, let me seee, I have already a service that return Typlogy object given the customer Id, ok I’ll use it. Can you spot the error???</p>
<p>The error is that the Typology object can contain (and in my situation is true) a lot of properties, some of them are long string, so the developer is retrieving a lot of unnecessary data from the service. The obvious solution is Dto, but noone wants to write them, so it is time to use code generation. With a T4 template I’m able to write stuff like this.</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:9f68f616-4066-4646-a3cb-b3095ab9e652" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #000000;">    template = new DtoFactoryTemplate (
        @"..\..\..\..\Common\Model\MyProject.Analyzer.Entities.Dll",
        Path.GetDirectoryName(Host.TemplateFile),
        "MyProject.Entities.Analysis.Typology",
        "TypologyLinkDto",
        "MyProject.Service.Dto",
        "http://www.nablasoft.com",
        1,
        new String[] {"Id", "Name", "CustomerId"},
        null,
        true, true, true);
    template.RenderToFile(@"..\Dto\Generated\TypologyLinkDto.cs"); </span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Ok it still needs to be cleaned a little, because a lot of the parameter are optional but it does the work, it generates this object.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2009/07/image27.png"><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="476" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2009/07/image-thumb27.png" width="256" border="0" /></a> </p>
<p>It is a good DTO with the three property names I requested, now with Linq2Nhibernate and repository pattern I can write this service method.</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:155529ec-f193-4fe5-9787-b8dd7d36f126" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #0000FF;">public</span><span style="color: #000000;"> List</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">TypologyLinkDto</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> GetTipologiesLinkForCustomer(Int32 customerId)
{
    </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> Repository.Typology.Query(
        t </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> t.CustomerId </span><span style="color: #000000;">==</span><span style="color: #000000;"> customerId,
        TypologyLinkDto.Assembler.ExpressionSelector);
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>Thanks to autogenerated code I can use autogenerated Assembler.ExpressionSelector that can be passed as a selector function, resulting query is.</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:6e7c9945-954a-454c-983a-f7f49ab52ace" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #0000FF;">exec</span><span style="color: #000000;"> sp_executesql N</span><span style="color: #FF0000;">'</span><span style="color: #FF0000;">SELECT this_.kety_id as y0_, this_.kety_clieId as y1_, this_.kety_name as y2_ FROM KeyTypology this_ WHERE this_.kety_clieId = @p0</span><span style="color: #FF0000;">'</span><span style="color: #000000;">,N</span><span style="color: #FF0000;">'</span><span style="color: #FF0000;">@p0 int</span><span style="color: #FF0000;">'</span><span style="color: #000000;">,</span><span style="color: #008000;">@p0</span><span style="color: #808080;">=</span><span style="color: #800000; font-weight: bold;">1</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>This permits to speed up the application because now I retrieve only the data I needed from the database and nothing more, and the good part is that dto is autogenerated, so I can write this code in almost 20 seconds.</p>
<p>alk.</p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/Code Generation" rel="tag">Code Generation</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2009/07/31/dto-generator-and-repository-integration/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Value of architecture</title>
		<link>http://www.codewrecks.com/blog/index.php/2009/07/28/value-of-architecture/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2009/07/28/value-of-architecture/#comments</comments>
		<pubDate>Tue, 28 Jul 2009 10:19:46 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Castle]]></category>
		<category><![CDATA[Software Architecture]]></category>
		<category><![CDATA[Architecture Castle]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2009/07/28/value-of-architecture/</guid>
		<description><![CDATA[
			
				
			
		
If I have to tell what is the main property of a good architecture, I surely will answer that a good architecture centralize common operations and make simple to extend the application.
Here is a typical example. I have a WCF services called from external clients, but I use the same service inside my organization. I [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F07%2F28%2Fvalue-of-architecture%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F07%2F28%2Fvalue-of-architecture%2F&amp;source=alkampfer&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>If I have to tell what is the main property of a good architecture, I surely will answer that a good architecture centralize common operations and make simple to extend the application.</p>
<p>Here is a typical example. I have a WCF services called from external clients, but I use the same service inside my organization. I created a little WPF project that needs to use services functions, but since this program is run internally, I do not want it to pass in the WCF stack, but I simply want to use concrete services classes that access the database.</p>
<p>The solution is really simple, here it is:</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:190c7482-3ac3-4f37-960c-e1fa4e455979" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #000000;">[AutoscanComponent(LifestyleType.Transient, IsDefault </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">true</span><span style="color: #000000;">, Id </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">KeywordService</span><span style="color: #800000;">"</span><span style="color: #000000;">, ServiceType </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">typeof</span><span style="color: #000000;">(IKeywordService))]
</span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">class</span><span style="color: #000000;"> KeywordService : IKeywordService</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>AutoscanComponent is used by a custom Castle facility, and is used to automatically register a component. For services I’m sure that there is always only an instance registered for each interface, so I can simply configure castle to use the autoscan facility, and when the code declare a dependency to IKeywordService, Castle resolve it with the concrete class instead of the WCF Proxy.</p>
<p>But this is not enough, I begin to notice that the program is slow, and after a simple inspection I see that there are too many calls to the database. The problem is that the user interact with a WPF UI and there are too many call to the database because the service interfaces are thought for a different usage pattern. This problem could be easily resolved with caching, so I create a simple interceptor.</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:7a911122-21e4-425f-9c39-0288ed0623af" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">class</span><span style="color: #000000;"> CacheInterceptor : </span><span style="color: #0000FF;">global</span><span style="color: #000000;">::Castle.Core.Interceptor.IInterceptor
{
    </span><span style="color: #0000FF;">private</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> String BuildCacheToken(IInvocation invocation)
    {
        StringBuilder sb </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> StringBuilder(</span><span style="color: #800080;">100</span><span style="color: #000000;">);
        sb.Append(invocation.TargetType.Name)
            .Append(</span><span style="color: #800000;">"</span><span style="color: #800000;">|</span><span style="color: #800000;">"</span><span style="color: #000000;">)
            .Append(invocation.Method.Name)
            .Append(</span><span style="color: #800000;">"</span><span style="color: #800000;">|</span><span style="color: #800000;">"</span><span style="color: #000000;">);
        </span><span style="color: #0000FF;">foreach</span><span style="color: #000000;"> (</span><span style="color: #0000FF;">object</span><span style="color: #000000;"> argument </span><span style="color: #0000FF;">in</span><span style="color: #000000;"> invocation.Arguments)
        {
            sb.Append(argument.ToString())
                .Append(</span><span style="color: #800000;">"</span><span style="color: #800000;">|</span><span style="color: #800000;">"</span><span style="color: #000000;">);
        }
        </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> sb.ToString();
    }

    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> Int32 CacheDurationInMinutes { </span><span style="color: #0000FF;">get</span><span style="color: #000000;">; </span><span style="color: #0000FF;">set</span><span style="color: #000000;">; }

    </span><span style="color: #0000FF;">#region</span><span style="color: #000000;"> IInterceptor Members</span><span style="color: #000000;">

    ICacheManager invocationCache </span><span style="color: #000000;">=</span><span style="color: #000000;"> CacheFactory.GetCacheManager();

    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">void</span><span style="color: #000000;"> Intercept(IInvocation invocation)
    {

        String cacheToken </span><span style="color: #000000;">=</span><span style="color: #000000;"> BuildCacheToken(invocation);
        Object cacheObject;
        </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> ((cacheObject </span><span style="color: #000000;">=</span><span style="color: #000000;"> invocationCache.GetData(cacheToken)) </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">null</span><span style="color: #000000;">)
        {
            invocation.ReturnValue </span><span style="color: #000000;">=</span><span style="color: #000000;"> cacheObject;
        }
        </span><span style="color: #0000FF;">else</span><span style="color: #000000;">
        {
            invocation.Proceed();
            invocationCache.Add(cacheToken, invocation.ReturnValue, CacheItemPriority.Normal,
                </span><span style="color: #0000FF;">null</span><span style="color: #000000;">,
                </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> AbsoluteTime(DateTimeService.Now.AddMinutes(CacheDurationInMinutes)));
        }
    }

    </span><span style="color: #0000FF;">#endregion</span><span style="color: #000000;">
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>This component is really simple, It builds a string key that represent the cache composing <em>service name + method name + parameterlist</em>, so I’m sure that I cache only calls with the same parameters call. Then I simply use <a target="_blank" href="http://msdn.microsoft.com/en-us/library/dd203248.aspx">entlib caching application block</a> to store data in cache. Now I simply need to configure this interceptor in config file.</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:2bcd68b8-d6c5-4ddc-bcd9-fd72ff703d49" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">component </span><span style="color: #FF0000;">id</span><span style="color: #0000FF;">="CacheInterceptor"</span><span style="color: #FF0000;">
     service</span><span style="color: #0000FF;">="Castle.Core.Interceptor.IInterceptor, Castle.Core"</span><span style="color: #FF0000;">
     type</span><span style="color: #0000FF;">="BaseServices.Castle.CacheInterceptor, BaseServices"</span><span style="color: #FF0000;">
     lifestyle</span><span style="color: #0000FF;">="singleton"</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
  </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">parameters</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
      </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">CacheDurationInMinutes</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">5</span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">CacheDurationInMinutes</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
  </span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">parameters</span><span style="color: #0000FF;">&gt;</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Next I need to instruct my facility to add this interceptor to all autoregistered components</p>
<div class="wlWriterEditableSmartContent" id="scid:DFDE9937-D816-47f4-A306-7B60D5CE5AC0:c72519ed-4a22-4190-89d9-1d2c265b40ba" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre class="brush: xml; gutter: false; first-line: 1; tab-size: 4;  toolbar: true; ">&lt;facility id=&quot;AutoScan&quot; type=&quot;BaseServices.Castle.AutoscanFacility, BaseServices&quot; &gt;
	&lt;assembly name=&quot;DataService&quot; /&gt;
	&lt;assembly name=&quot;Analyzer.Service&quot; /&gt;
	&lt;interceptor name=&quot;CacheInterceptor&quot; /&gt;
	&lt;interceptor name=&quot;SessionPerCallInterceptor&quot; /&gt;
&lt;/facility&gt;</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>With this configuration I’m sure that each service is intercepted by the cache component, cache duration is 5 minutes, and the application gains a tremendous speed increase.</p>
<p>This is possible because the architecture extensively use IoC to resolve services and components, and is simple to add features with <a target="_blank" href="http://en.wikipedia.org/wiki/Aspect-oriented_programming">AOP</a>.</p>
<p>alk.</p>
<div class="wlWriterEditableSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:e4e22d34-8f59-41c5-9b08-2f3b73eeb596" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">Technorati Tags: <a target="_blank" href="http://technorati.com/tags/Software+Architecture" rel="tag">Software Architecture</a></div>
]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2009/07/28/value-of-architecture/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A better version of dtofactory</title>
		<link>http://www.codewrecks.com/blog/index.php/2009/07/07/a-better-version-of-dtofactory/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2009/07/07/a-better-version-of-dtofactory/#comments</comments>
		<pubDate>Tue, 07 Jul 2009 07:15:00 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[.NET framework]]></category>
		<category><![CDATA[Software Architecture]]></category>
		<category><![CDATA[T4]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2009/07/07/a-better-version-of-dtofactory/</guid>
		<description><![CDATA[
			
				
			
		
In this post I dealt with a primitive version of a T4 template to generate dto starting from domain objects. In these days I had little time to improve it a little bit, and I created a simple test project to verify some of the basic functionality.
Usage of the T4 template is really simple, I’ve [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F07%2F07%2Fa-better-version-of-dtofactory%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F07%2F07%2Fa-better-version-of-dtofactory%2F&amp;source=alkampfer&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>In <a href="http://www.codewrecks.com/blog/index.php/2009/06/24/a-dto-factory-code-generator-with-visual-studio-t4/">this post</a> I dealt with a primitive version of a T4 template to generate dto starting from domain objects. In these days I had little time to improve it a little bit, and I created a simple test project to verify some of the basic functionality.</p>
<p>Usage of the T4 template is really simple, I’ve created in the test project an Entity Factory model to the ubiquitous northwind database, then I added a OrderDto.tt file to the project with this content.</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:5deb330a-dc6c-4159-8e24-1f036418c09e" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">#@ template </span><span style="color: #FF0000;">language</span><span style="color: #0000FF;">="C#"</span><span style="color: #FF0000;"> hostspecific</span><span style="color: #0000FF;">="True"</span><span style="color: #FF0000;"> debug</span><span style="color: #0000FF;">="True"</span><span style="color: #FF0000;"> #</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
</span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">#@ output </span><span style="color: #FF0000;">extension</span><span style="color: #0000FF;">="cs"</span><span style="color: #FF0000;"> #</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
</span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">#@ include </span><span style="color: #FF0000;">file</span><span style="color: #0000FF;">="DtoFactory.tt"</span><span style="color: #FF0000;"> #</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
</span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">#
// &lt;copyright </span><span style="color: #FF0000;">file</span><span style="color: #0000FF;">="OrdersDto.tt"</span><span style="color: #FF0000;"> company</span><span style="color: #0000FF;">="Gian Maria Ricci"</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
//  Copyright Â© Gian Maria Ricci. All Rights Reserved.
// </span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">copyright</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
    GenerateDto(
        @"bin\Debug\DtoFactory.Exe",
        "DtoFactory.Orders",
        "OrdersBaseDto",
        "DtoFactory",
        "http://www.nablasoft.com/dotnet/",
        1,
        new String[] {"OrderId", "OrderDate", "Customers.CustomerID", "Customers.ContactName"},
        null,
        true, true, true);
#&gt;</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>It is quite the same of the previous version, but now I inserted some enhancements. If you look at the property list you can see that I inserted properties like “Customers.CustomerID” and “Customers.ContactName”, that belongs to the customers object. When you use a domain model you are working with a graph of object, but dto must be plain. My generator will generate this class:</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2009/07/image14.png"><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="415" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2009/07/image-thumb14.png" width="221" border="0" /></a> </p>
<p>My dto class implements various interfaces and it has properties like Customers_ContactName and Customers_CustomerID, so it is a simple transfer object that contains all information we need. The assembler class is responsible to create a dto from original object and as usual can be used in this way:</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:d2331bb3-590e-405b-a4e3-a5208c6d43d9" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #0000FF;">using</span><span style="color: #000000;"> (NorthwindEntities context </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> NorthwindEntities())
{
    var Query </span><span style="color: #000000;">=</span><span style="color: #000000;"> context.Orders
        .Where(o </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> o.Customers.CustomerID </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">ALFKI</span><span style="color: #800000;">"</span><span style="color: #000000;">)
        .Select(OrdersBaseDto.Assembler.ExpressionSelector);

    </span><span style="color: #0000FF;">foreach</span><span style="color: #000000;"> (var order </span><span style="color: #0000FF;">in</span><span style="color: #000000;"> Query)
    {
        Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">OrderId:{0} Customer:{1} Date:{2}</span><span style="color: #800000;">"</span><span style="color: #000000;">,
            order.OrderID, order.Customers_CustomerID, order.OrderDate);
    }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>This code simply use the ExpressionSelector contained in nested class Assembler of my dto of choice, then I can iterate into the query using various properties of the dto object. The <strong>most important stuff is that the expression selector actually does a projection</strong>, here is the generated query captured from the profiler.</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:47f77f30-4fe1-46e3-96c4-d72ed8e5a04d" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #0000FF;">SELECT</span><span style="color: #000000;">
</span><span style="color: #800000; font-weight: bold;">1</span><span style="color: #000000;"> </span><span style="color: #0000FF;">AS</span><span style="color: #000000;"> </span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">C1</span><span style="color: #FF0000;">]</span><span style="color: #000000;">,
</span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">Extent1</span><span style="color: #FF0000;">]</span><span style="color: #000000;">.</span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">OrderDate</span><span style="color: #FF0000;">]</span><span style="color: #000000;"> </span><span style="color: #0000FF;">AS</span><span style="color: #000000;"> </span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">OrderDate</span><span style="color: #FF0000;">]</span><span style="color: #000000;">,
</span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">Extent1</span><span style="color: #FF0000;">]</span><span style="color: #000000;">.</span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">OrderID</span><span style="color: #FF0000;">]</span><span style="color: #000000;"> </span><span style="color: #0000FF;">AS</span><span style="color: #000000;"> </span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">OrderID</span><span style="color: #FF0000;">]</span><span style="color: #000000;">,
</span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">Extent1</span><span style="color: #FF0000;">]</span><span style="color: #000000;">.</span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">CustomerID</span><span style="color: #FF0000;">]</span><span style="color: #000000;"> </span><span style="color: #0000FF;">AS</span><span style="color: #000000;"> </span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">CustomerID</span><span style="color: #FF0000;">]</span><span style="color: #000000;">,
</span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">Extent2</span><span style="color: #FF0000;">]</span><span style="color: #000000;">.</span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">ContactName</span><span style="color: #FF0000;">]</span><span style="color: #000000;"> </span><span style="color: #0000FF;">AS</span><span style="color: #000000;"> </span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">ContactName</span><span style="color: #FF0000;">]</span><span style="color: #000000;">
</span><span style="color: #0000FF;">FROM</span><span style="color: #000000;">  </span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">dbo</span><span style="color: #FF0000;">]</span><span style="color: #000000;">.</span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">Orders</span><span style="color: #FF0000;">]</span><span style="color: #000000;"> </span><span style="color: #0000FF;">AS</span><span style="color: #000000;"> </span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">Extent1</span><span style="color: #FF0000;">]</span><span style="color: #000000;">
</span><span style="color: #808080;">LEFT</span><span style="color: #000000;"> </span><span style="color: #808080;">OUTER</span><span style="color: #000000;"> </span><span style="color: #808080;">JOIN</span><span style="color: #000000;"> </span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">dbo</span><span style="color: #FF0000;">]</span><span style="color: #000000;">.</span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">Customers</span><span style="color: #FF0000;">]</span><span style="color: #000000;"> </span><span style="color: #0000FF;">AS</span><span style="color: #000000;"> </span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">Extent2</span><span style="color: #FF0000;">]</span><span style="color: #000000;"> </span><span style="color: #0000FF;">ON</span><span style="color: #000000;"> </span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">Extent1</span><span style="color: #FF0000;">]</span><span style="color: #000000;">.</span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">CustomerID</span><span style="color: #FF0000;">]</span><span style="color: #000000;"> </span><span style="color: #808080;">=</span><span style="color: #000000;"> </span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">Extent2</span><span style="color: #FF0000;">]</span><span style="color: #000000;">.</span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">CustomerID</span><span style="color: #FF0000;">]</span><span style="color: #000000;">
</span><span style="color: #0000FF;">WHERE</span><span style="color: #000000;"> N</span><span style="color: #FF0000;">'</span><span style="color: #FF0000;">ALFKI</span><span style="color: #FF0000;">'</span><span style="color: #000000;"> </span><span style="color: #808080;">=</span><span style="color: #000000;"> </span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">Extent1</span><span style="color: #FF0000;">]</span><span style="color: #000000;">.</span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">CustomerID</span><span style="color: #FF0000;">]</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Only the field related to the dto are retrieved, and thanks to Entity Framework you get the join and projection for free, no unnecessary data is taken from database. This is a quite good result, because with few lines of code my generator is able to geneate a dto composed by properties taken from various objects, and thanks to LINQ I also got a projection for free that permits me to gain maximum efficiency avoiding to retrieve full objects.</p>
<p>Another interesting stuff is this one, you can link various Dto objects between them</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:3c99f7ec-a127-4fa7-bd44-560a0e9139ad" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #000000;">    GenerateDto(
        </span><span style="color: #800000;">@"</span><span style="color: #800000;">bin\Debug\DtoFactory.Exe</span><span style="color: #800000;">"</span><span style="color: #000000;">,
        </span><span style="color: #800000;">"</span><span style="color: #800000;">DtoFactory.Orders</span><span style="color: #800000;">"</span><span style="color: #000000;">,
        </span><span style="color: #800000;">"</span><span style="color: #800000;">OrdersTestDto</span><span style="color: #800000;">"</span><span style="color: #000000;">,
        </span><span style="color: #800000;">"</span><span style="color: #800000;">DtoFactory</span><span style="color: #800000;">"</span><span style="color: #000000;">,
        </span><span style="color: #800000;">"</span><span style="color: #800000;">http://www.nablasoft.com/dotnet/</span><span style="color: #800000;">"</span><span style="color: #000000;">,
        </span><span style="color: #800080;">1</span><span style="color: #000000;">,
        </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> String[] {</span><span style="color: #800000;">"</span><span style="color: #800000;">OrderID</span><span style="color: #800000;">"</span><span style="color: #000000;">, </span><span style="color: #800000;">"</span><span style="color: #800000;">OrderDate</span><span style="color: #800000;">"</span><span style="color: #000000;">, </span><span style="color: #800000;">"</span><span style="color: #800000;">Customers</span><span style="color: #800000;">"</span><span style="color: #000000;">},
        </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> PropertyData[] {</span><span style="color: #0000FF;">new</span><span style="color: #000000;"> PropertyData(</span><span style="color: #800000;">"</span><span style="color: #800000;">Customers</span><span style="color: #800000;">"</span><span style="color: #000000;">, </span><span style="color: #800000;">"</span><span style="color: #800000;">CustomersDto</span><span style="color: #800000;">"</span><span style="color: #000000;">)},
        </span><span style="color: #0000FF;">true</span><span style="color: #000000;">, </span><span style="color: #0000FF;">true</span><span style="color: #000000;">, </span><span style="color: #0000FF;">true</span><span style="color: #000000;">); </span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>
</p>
<p>I’m asking to generate an OrdersTestDto object that contains the “Customers” property, but I specify that I do not want full Customers object, but simply a CustomersDto, so you will end in two dto connected together </p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2009/07/image15.png"><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="303" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2009/07/image-thumb15.png" width="523" border="0" /></a> </p>
<p>This facility permits me to reuse dto but actually the ExpressionSelector of this dto cannot be directly used with LINQ, it needs still more work <img src='http://www.codewrecks.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
<p>All the project is still in a very rought form, but I’m quite satisfied on it, if you like it you can download <a href="http://www.codewrecks.com/blog/storage/dtofactoryblog.zip">here</a>. The code is provided as-is <img src='http://www.codewrecks.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  do not blame the author.</p>
<p>alk.</p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/Dto" rel="tag">Dto</a> <a target="_blank" href="http://technorati.com/tag/T4" rel="tag">T4</a> <a target="_blank" href="http://technorati.com/tag/Code generation" rel="tag">Code generation</a></p></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2009/07/07/a-better-version-of-dtofactory/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Design UI First</title>
		<link>http://www.codewrecks.com/blog/index.php/2009/07/06/design-ui-first/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2009/07/06/design-ui-first/#comments</comments>
		<pubDate>Mon, 06 Jul 2009 12:26:00 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Software Architecture]]></category>
		<category><![CDATA[Mockups]]></category>
		<category><![CDATA[UI]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2009/07/06/design-ui-first/</guid>
		<description><![CDATA[Is it possible to address a project with an "UI First" approach? With the right tool probably it can be the most feasible approach.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F07%2F06%2Fdesign-ui-first%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F07%2F06%2Fdesign-ui-first%2F&amp;source=alkampfer&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Some days ago we have a question on the possibility to have a “<a target="_blank" href="http://www.guisa.org/forums/t/182.aspx">Design UI first</a>” approach to a project. (the forum post is in Italian). If you asked me this question some years ago, I surely have answered “Probably not”. I must admit that I always thought that some people gave too many importance to the UI. Quite often Project managers are the kind of people that always ask for impressive interface to show to final customers, while we, as programmers, tend to spent more attention to the “code part” of the project, and usually do not care about UI. After all where is the pride in putting some labels and textboxes in a form respect in designing a well crafted Domain Model with ORM and AOP etc etc.</p>
<blockquote><p>I now realize that <em>this is wrong!!</em></p>
</blockquote>
<p>The problem is that the UI is directly perceived by the customer and final user, while internal structure is completely hidden. If you build a perfect program with a wrong ui, the customer will blame you, because even the most well crafted architecture fails if the UI is poorly designed. After all what is the point in having a perfect domain model, if the user can use it only through a poor interface?. This is the same of looking at a very good film full of special effects in an old 20’’ black and white screen, the film can be the best in the world, but you cannot perceive it because of viewing it through a poor interface.</p>
<p>Another key factor about UI is <a target="_blank" href="http://www.amazon.com/Think-Common-Sense-Approach-Usability/dp/0789723107">usability</a>. Building a good UI does not means using 3D animations and shocking effects, but it is all about <strong>usability. </strong>If a software fails in usability, he is <a target="_blank" href="http://www.amazon.com/Why-Software-Sucks-What-About/dp/0321466756/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1246642730&amp;sr=1-1">doomed to disappear</a>.The problem here is that the world is not made of developers, and what is good for a developer, probably is not good for normal people. </p>
<p>For all those reason, probably it is possible to approach a problem starting with the UI, maybe side by side with Use Case approach. If you focus on the UI you need a tool that permits you to rapidly prototype a sketch of the interface, so the customer can immediately have a visual impact on the aspect of the final application. In this scenario <a target="_blank" href="http://www.balsamiq.com/">Balsamiq</a> can save your life. Consider this mockup as example, it took me no more than 10 minutes to sketch, and it is one of the first time I use balsamiq</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2009/07/image8.png"><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="430" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2009/07/image-thumb8.png" width="644" border="0" /></a> </p>
<p>This simple sketch can be immediately shown to the customer/user for review and validation. Moreover even if this is only a sketch, it gives immediately a clue on some technical difficulties that can arise. As an example we need Google Map integration, and we need to show various graphic artifacts like icons depending on properties of object. We also need internal link, because you can see a link that states “Go to Order”. We must show picture, use various tab like controls that shows tabs on top or on left, etc. </p>
<p>This interface also&#160; capture the minimum amount of properties of various object, from it we can be sure that Customer object need a Name, Last Name, Age, Nickname, IsAKid (boolean) properties, and we also need to extract from database projections, like “Number of orders” for each customer. This means that even if we start with a UI prototyping, we also begin focusing on data that the software must handle, as well as relations between various objects.</p>
<p>Moreover if we begin the analysis of the project with use cases or other similar techniques, then build the software and finally show the result to the user, we have an high probability that the customer want to change something in the interface, and such changes sometimes impact on the overall architecture. (think to add a Drag and Drop to a web page). If you begin the analysis phase with UI mockup, the probability that the customer want to change the UI when it is finished is considerably lower. </p>
<p>After all <a target="_blank" href="http://en.wikipedia.org/wiki/A_picture_is_worth_a_thousand_words">A picture is <em>worth</em> a thousand words</a> <img src='http://www.codewrecks.com/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  so I think that a UI First approach can be feasible.</p>
<p>Alk.</p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/Ui prototyping" rel="tag">Ui prototyping</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2009/07/06/design-ui-first/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Team foundation Build &#8211; Share Builds among multiple servers</title>
		<link>http://www.codewrecks.com/blog/index.php/2009/06/25/team-foundation-build-share-builds-among-multiple-servers/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2009/06/25/team-foundation-build-share-builds-among-multiple-servers/#comments</comments>
		<pubDate>Thu, 25 Jun 2009 19:17:22 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Software Architecture]]></category>
		<category><![CDATA[Team Foundation Server]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2009/06/25/team-foundation-build-share-builds-among-multiple-servers/</guid>
		<description><![CDATA[
			
				
			
		
When you begin to use Team Foundation Server, you will create different builds for all of your company’s projects. Since building complex products can be resource intensive, it is likely that your Team Foundation Server machine starts to perform slowly. This is a typical issue of Continuous integration servers, since they compile projects at each [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F06%2F25%2Fteam-foundation-build-share-builds-among-multiple-servers%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F06%2F25%2Fteam-foundation-build-share-builds-among-multiple-servers%2F&amp;source=alkampfer&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>When you begin to use Team Foundation Server, you will create different builds for all of your company’s projects. Since building complex products can be resource intensive, it is likely that your Team Foundation Server machine starts to perform slowly. This is a typical issue of Continuous integration servers, since they compile projects at each check-in you will end with a lot of builds and a lot of work to do. </p>
<p>Team Foundation Server addresses this issue separating <em><a target="_blank" href="http://msdn.microsoft.com/en-us/library/ms181710.aspx">build machines</a></em> from <em>Team Foundation Server</em>. If you find that the build machine is becoming slow, you can simply use another machine in the network to execute some of the builds. In real environment you can even avoid to install build engine in the Tfs machine, delegating builds to other servers. First of all go into another machine, fire the installer of Team Foundation Server, and choose to install a “Team Foundation Build”. </p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2009/06/image35.png"><img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="203" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2009/06/image-thumb35.png" width="510" border="0" /></a></p>
<p><strong><em>Note:</em></strong> if you do not have Active Directory<em> you Must create in this machine an account with the same name/password of an account of the machine where Team Foundation Server is running and that account must have right to access tfs</em>. This is a standar requirement, the Build Server needs to access Team Foundation Server to get sources to build and store build result, etc, etc. so it is of vital importance that it runs with sufficient privilege to access TFS server. If you have active directory you do not need to create a specific account, but you must select one AD user that have credential to access TFS during the installation.</p>
<p>When the installer finished, you can go to Visual Studio, Right Click on the Builds in the Team Foundation Server Explorer Tree, then choose “Manage Build Agents”</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2009/06/image36.png"><img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="141" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2009/06/image-thumb36.png" width="249" border="0" /></a> </p>
<p>Now you can add additional Build Agent, and you can choose the new machine name, where you had previously installed the Team Foundation Build.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2009/06/image37.png"><img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="408" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2009/06/image-thumb37.png" width="444" border="0" /></a> </p>
<p>Now you can use this new machine to run Builds simply specifying this agent when you create a Build Definition, or you can use it when you manually queue new build. It is really important that the user credentials used to run the Build Service in the new Build machine has the right to access the Team Foundation Server Machine as stated before.</p>
<p>If you had already installed the team Build engine with a wrong user don’t panic, you can still manually change build machine settings to correctly access TFS. Go to Service control panel and change the credential used by the “Visual Studio Team Foundation Build” service to use an user that has sufficient credential for TFS. Now restart the service.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2009/06/image38.png"><img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="242" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2009/06/image-thumb38.png" width="617" border="0" /></a> </p>
<p>It is highly possible that when you restart the service you get “<strong>access is Denied</strong>” error, it means that the new account does not have sufficient rights to run, first of all the user needs rights to access the folder where you install the Build Agent, but this is usually not enough. <em>The only way to understand exactly what is wrong <strong>is going into the Event Viewer</strong></em> where you can find detailed errors like this one.</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e797dcdf-51f5-4dc5-8039-ba55a7f64343" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #000000;">Detailed Message: TF224004: The Visual Studio Team Foundation Build service failed to </span><span style="color: #0000FF;">start</span><span style="color: #000000;"> because WS2008V1</span><span style="color: #000000;">\</span><span style="color: #000000;">alkampfer does </span><span style="color: #0000FF;">not</span><span style="color: #000000;"> have the required access permissions </span><span style="color: #0000FF;">for</span><span style="color: #000000;"> address http:</span><span style="color: #000000;">//</span><span style="color: #000000;">ws2008v1:</span><span style="color: #000000;">9191</span><span style="color: #000000;">/</span><span style="color: #000000;">Build</span><span style="color: #000000;">/</span><span style="color: #000000;">v2</span><span style="color: #000000;">.</span><span style="color: #000000;">0</span><span style="color: #000000;">/</span><span style="color: #000000;">AgentService</span><span style="color: #000000;">.</span><span style="color: #000000;">asmx</span><span style="color: #000000;">.</span><span style="color: #000000;">
Exception Message: HTTP could </span><span style="color: #0000FF;">not</span><span style="color: #000000;"> register URL http:</span><span style="color: #000000;">//+</span><span style="color: #000000;">:</span><span style="color: #000000;">9191</span><span style="color: #000000;">/</span><span style="color: #000000;">Build</span><span style="color: #000000;">/</span><span style="color: #000000;">v2</span><span style="color: #000000;">.</span><span style="color: #000000;">0</span><span style="color: #000000;">/</span><span style="color: #000000;">AgentService</span><span style="color: #000000;">.</span><span style="color: #000000;">asmx</span><span style="color: #000000;">/.</span><span style="color: #000000;"> Your process does </span><span style="color: #0000FF;">not</span><span style="color: #000000;"> have access rights to this namespace </span><span style="color: #000000;">(</span><span style="color: #000000;">see http:</span><span style="color: #000000;">//</span><span style="color: #000000;">go</span><span style="color: #000000;">.</span><span style="color: #000000;">microsoft</span><span style="color: #000000;">.</span><span style="color: #000000;">com</span><span style="color: #000000;">/</span><span style="color: #000000;">fwlink</span><span style="color: #000000;">/</span><span style="color: #000000;">?LinkId</span><span style="color: #000000;">=</span><span style="color: #000000;">70353</span><span style="color: #000000;"> </span><span style="color: #0000FF;">for</span><span style="color: #000000;"> details</span><span style="color: #000000;">).</span><span style="color: #000000;"> </span><span style="color: #000000;">(</span><span style="color: #0000FF;">type</span><span style="color: #000000;"> AddressAccessDeniedException</span><span style="color: #000000;">)</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
</p>
</p>
<p>This message is one of the most dreadful, it means that the user has no <a target="_blank" href="http://msdn.microsoft.com/en-us/library/ms733768.aspx">rights to use that url to host&#160; a web service</a>. The solution is to run this couple of commands.</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e58a65f3-aa13-47e3-b866-7fdca531b246" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #000000;">netsh http delete urlacl url</span><span style="color: #000000;">=</span><span style="color: #000000;">http:</span><span style="color: #000000;">//+</span><span style="color: #000000;">:</span><span style="color: #000000;">9191</span><span style="color: #000000;">/</span><span style="color: #000000;">Build</span><span style="color: #000000;">/</span><span style="color: #000000;">v2</span><span style="color: #000000;">.</span><span style="color: #000000;">0</span><span style="color: #000000;">/</span><span style="color: #000000;">AgentService</span><span style="color: #000000;">.</span><span style="color: #000000;">asmx
netsh http add urlacl url</span><span style="color: #000000;">=</span><span style="color: #000000;">http:</span><span style="color: #000000;">//+</span><span style="color: #000000;">:</span><span style="color: #000000;">9191</span><span style="color: #000000;">/</span><span style="color: #000000;">Build</span><span style="color: #000000;">/</span><span style="color: #000000;">v2</span><span style="color: #000000;">.</span><span style="color: #000000;">0</span><span style="color: #000000;">/</span><span style="color: #000000;">AgentService</span><span style="color: #000000;">.</span><span style="color: #000000;">asmx user</span><span style="color: #000000;">=</span><span style="color: #000000;">alkampfer</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>With these two lines of code you are giving to user <em>Alkampfer </em>(the one that has right to access Tfs and the one used to run build service) enough rights to create a web service in that url. Now you should be able to start the service. Verify if the Build Agent is running browsing the WebService page (something like <a target="_blank" title="http://10.0.0.11:9191/Build/v2.0/AgentService.asmx" href="http://10.0.0.11:9191/Build/v2.0/AgentService.asmx">http://10.0.0.11:9191/Build/v2.0/AgentService.asmx</a>). Now if everything is ok, you should go to “Manage Build Agents” menu and reenable agent if necessary. This step is needed because Tfs disables all Agents that are unable to run. If you fire a build with an agent that has no right to access TFS the TFS disables that agent because it is useless. When you correct the problem, you can simply edit the Build Agent definition, and change its status back to active, now you can schedule again builds on it.</p>
<p>With such a configuration you can divide build tasks across multiple machines, and the whole build process is more scalable. The advantage is that you have a single point of access (the Tfs machine), and from that point you can subdivide works between machines. Since you can install how many build server you want, the whole system is highly scalable.</p>
<p>Alk.</p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/Team Foundation Server" rel="tag">Team Foundation Server</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2009/06/25/team-foundation-build-share-builds-among-multiple-servers/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>A dto factory code generator with visual studio T4</title>
		<link>http://www.codewrecks.com/blog/index.php/2009/06/24/a-dto-factory-code-generator-with-visual-studio-t4/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2009/06/24/a-dto-factory-code-generator-with-visual-studio-t4/#comments</comments>
		<pubDate>Wed, 24 Jun 2009 17:29:00 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Software Architecture]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2009/06/24/a-dto-factory-code-generator-with-visual-studio-t4/</guid>
		<description><![CDATA[
			
				
			
		
Data Transfer Objects, best known as Dto, are an essential part of projects that uses services or Domain Model. Dto are great, but the disadvantage is that maintaining Dto objects is a pain, because they are composed by repetitive code, and you have no fun in writing them.
Since building a Dto is a mechanical process [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F06%2F24%2Fa-dto-factory-code-generator-with-visual-studio-t4%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F06%2F24%2Fa-dto-factory-code-generator-with-visual-studio-t4%2F&amp;source=alkampfer&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p><a target="_blank" href="http://en.wikipedia.org/wiki/Data_Transfer_Object">Data Transfer Objects</a>, best known as Dto, are an essential part of projects that uses services or Domain Model. Dto are great, but the disadvantage is that maintaining Dto objects is a pain, because they are composed by repetitive code, and you have no fun in writing them.</p>
<p>Since building a Dto is a mechanical process it is better to use a code generation tool to generate them with little effort. If you work with visual studio 2008 the best choice in Code Generator is T4, because it is included in Visual Studio and is really good. I do not want to cover T4 syntax, because you can find tons of information following <a target="_blank" href="http://www.hanselman.com/blog/T4TextTemplateTransformationToolkitCodeGenerationBestKeptVisualStudioSecret.aspx">this post links</a>.</p>
<p>You can <a href="http://www.codewrecks.com/blog/Storage/DtoFactory.zip">download here</a> a little project that contains a simple DtoFActory.tt file used to generate basic Dto out from a domain object. It is written with little effort, and it can contain errors, but I’m quite satisfied by the general result. To use it simply add the DtoFactory.tt file to your solution, then you can use in other tt file. In the <a href="http://www.codewrecks.com/blog/Storage/DtoFactory.zip">example</a> I simply generate a EntityFramework edmx against northwind database, then I create a CustomerDto.tt file with this content.</p>
<p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:89222878-18f0-44bf-a12e-fe226e942258" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #000000;">&lt;</span><span style="color: #000000;">#@ template language</span><span style="color: #000000;">=</span><span style="color: #800000;">"</span><span style="color: #800000;">C#</span><span style="color: #800000;">"</span><span style="color: #000000;"> hostspecific</span><span style="color: #000000;">=</span><span style="color: #800000;">"</span><span style="color: #800000;">True</span><span style="color: #800000;">"</span><span style="color: #000000;"> debug</span><span style="color: #000000;">=</span><span style="color: #800000;">"</span><span style="color: #800000;">True</span><span style="color: #800000;">"</span><span style="color: #000000;"> #</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">
</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">#@ output extension</span><span style="color: #000000;">=</span><span style="color: #800000;">"</span><span style="color: #800000;">cs</span><span style="color: #800000;">"</span><span style="color: #000000;"> #</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">
</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">#@ include file</span><span style="color: #000000;">=</span><span style="color: #800000;">"</span><span style="color: #800000;">DtoFactory.tt</span><span style="color: #800000;">"</span><span style="color: #000000;"> #</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">
</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">#
</span><span style="color: #008000;">//</span><span style="color: #008000;"> &lt;copyright file="CustomerDto.tt" company="Gian Maria Ricci"&gt;
</span><span style="color: #008000;">//</span><span style="color: #008000;">  Copyright Â© Gian Maria Ricci. All Rights Reserved.
</span><span style="color: #008000;">//</span><span style="color: #008000;"> &lt;/copyright&gt;</span><span style="color: #008000;">
</span><span style="color: #000000;">    GenerateDto(
        </span><span style="color: #800000;">@"</span><span style="color: #800000;">bin\Debug\DtoFactory.Exe</span><span style="color: #800000;">"</span><span style="color: #000000;">,
        </span><span style="color: #800000;">"</span><span style="color: #800000;">DtoFactory.Customers</span><span style="color: #800000;">"</span><span style="color: #000000;">,
        </span><span style="color: #800000;">"</span><span style="color: #800000;">CustomersDto</span><span style="color: #800000;">"</span><span style="color: #000000;">,
        </span><span style="color: #800000;">"</span><span style="color: #800000;">DtoFactory</span><span style="color: #800000;">"</span><span style="color: #000000;">,
        </span><span style="color: #800000;">"</span><span style="color: #800000;">http://www.nablasoft.com/dotnet/</span><span style="color: #800000;">"</span><span style="color: #000000;">,
        </span><span style="color: #800080;">1</span><span style="color: #000000;">,
        </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> PropertyData[] {</span><span style="color: #800000;">"</span><span style="color: #800000;">CustomerId</span><span style="color: #800000;">"</span><span style="color: #000000;">, </span><span style="color: #800000;">"</span><span style="color: #800000;">CompanyName</span><span style="color: #800000;">"</span><span style="color: #000000;">, </span><span style="color: #800000;">"</span><span style="color: #800000;">ContactName</span><span style="color: #800000;">"</span><span style="color: #000000;">},
        </span><span style="color: #0000FF;">true</span><span style="color: #000000;">, </span><span style="color: #0000FF;">true</span><span style="color: #000000;">, </span><span style="color: #0000FF;">true</span><span style="color: #000000;">); 

#</span><span style="color: #000000;">&gt;</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>As you can see I import the DtoFactory.tt file, then call the GenerateDto function. The first parameter is the relative path of the assembly that contains the original object. Relative means respect to the tt file itself, this is one of the part I do not like very much because you need to find out relative path and refers to the debug directory, but it can be acceptable. </p>
<p>Then I specify the fullname of the class, the name of the Dto object, the name of the namespace were to put generated class. Then I need to specify the namespace used in the DataContract attribute (I want my Dto to be transferrable by WCF), then the number of base classes to examine to find properties, a list of PropertyData object with names of the property of the original object I want in my dto, finally three boolean values to specify if I need my dto to implement INotifyPropertyChanged, INotifyPropertyChanged, and IEditableObject.</p>
<p>You can see that this simple template will generate a CustomerDto.cs file that contains a CustomersDto class that contains that three properties. The interesting part is how I generate the assembler of the Dto. To build a Dto the standard way to proceed is to take the original object and copy all needed properties into corresponding properties of the dto object. This is a suboptimal approach, because using such a technique, you are forced to load the entire object from the database. In My situation CustomersDto has only three properties, <em>why I need to load an entire Customers object into memory to build a CustomersDto object that contains only three properties</em>? The solution is in how my dtoFactory generates the assembler. </p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:606dedbe-b9b2-4f18-9482-fee451426055" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #000000;">
</span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> Expression</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Func</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Customers, CustomersDto</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;"> ExpressionSelector;
</span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> Func</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Customers, CustomersDto</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> Selector;

</span><span style="color: #0000FF;">static</span><span style="color: #000000;"> CustomersDtoAssembler()
{

    ExpressionSelector </span><span style="color: #000000;">=</span><span style="color: #000000;"> obj </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> CustomersDto()
    {
        CompanyName </span><span style="color: #000000;">=</span><span style="color: #000000;"> obj.CompanyName,
        ContactName </span><span style="color: #000000;">=</span><span style="color: #000000;"> obj.ContactName,
        CustomerID </span><span style="color: #000000;">=</span><span style="color: #000000;"> obj.CustomerID,
    };
    Selector </span><span style="color: #000000;">=</span><span style="color: #000000;"> obj </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> CustomersDto()
    {
        CompanyName </span><span style="color: #000000;">=</span><span style="color: #000000;"> obj.CompanyName,
        ContactName </span><span style="color: #000000;">=</span><span style="color: #000000;"> obj.ContactName,
        CustomerID </span><span style="color: #000000;">=</span><span style="color: #000000;"> obj.CustomerID,
    };
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>As you can see my generator generates an <strong><em>Expression&lt;Func&lt;Customers, CustomersDto&gt;&gt;,</em></strong> and simply initialize it in static constructor with a simple lambda expression. If you are wondering why I need such a strange object let’s see how I use it</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:b51f2fb9-c390-483c-9bd8-b55119b63661" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #0000FF;">using</span><span style="color: #000000;"> (NorthwindEntities context </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> NorthwindEntities())
{
    var Query </span><span style="color: #000000;">=</span><span style="color: #000000;"> context.Customers.
       Where(c </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> c.CustomerID.StartsWith(</span><span style="color: #800000;">"</span><span style="color: #800000;">A</span><span style="color: #800000;">"</span><span style="color: #000000;">))
       .Select(CustomersDtoAssembler.ExpressionSelector);

    </span><span style="color: #0000FF;">foreach</span><span style="color: #000000;"> (CustomersDto customerDto </span><span style="color: #0000FF;">in</span><span style="color: #000000;"> Query)
    {
        Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">ID: {0}, ContactName: {1}, CompanyName {2}</span><span style="color: #800000;">"</span><span style="color: #000000;">,
            customerDto.CustomerID, customerDto.ContactName, customerDto.CompanyName);
    }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Thanks to LINQ to Entities, I can simply build a query to the NorthwindEntites data context, selecting Customers, applying a filter and specify the assembler <strong>ExpressionSelector </strong>seen before. This produces this query against my database.</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:6e5a60af-327d-4c4e-91db-f9f220786901" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #0000FF;">SELECT</span><span style="color: #000000;">
</span><span style="color: #800000; font-weight: bold;">1</span><span style="color: #000000;"> </span><span style="color: #0000FF;">AS</span><span style="color: #000000;"> </span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">C1</span><span style="color: #FF0000;">]</span><span style="color: #000000;">,
</span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">Extent1</span><span style="color: #FF0000;">]</span><span style="color: #000000;">.</span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">CompanyName</span><span style="color: #FF0000;">]</span><span style="color: #000000;"> </span><span style="color: #0000FF;">AS</span><span style="color: #000000;"> </span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">CompanyName</span><span style="color: #FF0000;">]</span><span style="color: #000000;">,
</span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">Extent1</span><span style="color: #FF0000;">]</span><span style="color: #000000;">.</span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">ContactName</span><span style="color: #FF0000;">]</span><span style="color: #000000;"> </span><span style="color: #0000FF;">AS</span><span style="color: #000000;"> </span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">ContactName</span><span style="color: #FF0000;">]</span><span style="color: #000000;">,
</span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">Extent1</span><span style="color: #FF0000;">]</span><span style="color: #000000;">.</span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">CustomerID</span><span style="color: #FF0000;">]</span><span style="color: #000000;"> </span><span style="color: #0000FF;">AS</span><span style="color: #000000;"> </span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">CustomerID</span><span style="color: #FF0000;">]</span><span style="color: #000000;">
</span><span style="color: #0000FF;">FROM</span><span style="color: #000000;"> </span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">dbo</span><span style="color: #FF0000;">]</span><span style="color: #000000;">.</span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">Customers</span><span style="color: #FF0000;">]</span><span style="color: #000000;"> </span><span style="color: #0000FF;">AS</span><span style="color: #000000;"> </span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">Extent1</span><span style="color: #FF0000;">]</span><span style="color: #000000;">
</span><span style="color: #0000FF;">WHERE</span><span style="color: #000000;"> (</span><span style="color: #FF00FF;">CAST</span><span style="color: #000000;">(</span><span style="color: #FF00FF;">CHARINDEX</span><span style="color: #000000;">(N</span><span style="color: #FF0000;">'</span><span style="color: #FF0000;">A</span><span style="color: #FF0000;">'</span><span style="color: #000000;">, </span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">Extent1</span><span style="color: #FF0000;">]</span><span style="color: #000000;">.</span><span style="color: #FF0000;">[</span><span style="color: #FF0000;">CustomerID</span><span style="color: #FF0000;">]</span><span style="color: #000000;">) </span><span style="color: #0000FF;">AS</span><span style="color: #000000;"> </span><span style="color: #0000FF;">int</span><span style="color: #000000;">)) </span><span style="color: #808080;">=</span><span style="color: #000000;"> </span><span style="color: #800000; font-weight: bold;">1</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Thanks to the <strong>Expression&lt;Func&lt;Customers, CustomerDto&gt;&gt;,</strong> Entity Framework knows how to retrieve only the data he need to create the Dto, wonderful <img src='http://www.codewrecks.com/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> . With such a powerful generator I can create Dto objects with few lines of code.</p>
<p>Since this generator is not still fully tested feel free to send me suggestions, bugs, feature you wish to have, etc, etc.</p>
<p>Alk.</p>
<p><a href="http://www.codewrecks.com/blog/Storage/DtoFactory.zip">Code Download.</a></p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/Dto" rel="tag">Dto</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2009/06/24/a-dto-factory-code-generator-with-visual-studio-t4/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Plan for change</title>
		<link>http://www.codewrecks.com/blog/index.php/2009/06/15/plan-for-change/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2009/06/15/plan-for-change/#comments</comments>
		<pubDate>Mon, 15 Jun 2009 14:40:11 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Software Architecture]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2009/06/15/plan-for-change/</guid>
		<description><![CDATA[
			
				
			
		
Here is a possible dialog between Customer/PM/Analyst and a programmer
 
Developers usually end with one or more of those thought

Analyst is a moron, he did a bad job not finding the real requirement of the customer
Customer are stupid because they does not know what they want, they are constantly changing requirements.
We lack requirement gathering phase, [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F06%2F15%2Fplan-for-change%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F06%2F15%2Fplan-for-change%2F&amp;source=alkampfer&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Here is a possible dialog between Customer/PM/Analyst and a programmer</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2009/06/image7.png"><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="277" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2009/06/image-thumb7.png" width="644" border="0" /></a> </p>
<p>Developers usually end with one or more of those thought</p>
<ol>
<li>Analyst is a moron, he did a bad job not finding the real requirement of the customer</li>
<li>Customer are stupid because they does not know what they want, they are constantly changing requirements.</li>
<li>We lack requirement gathering phase, now we must loose time following desires of Analyst/Customer</li>
</ol>
<p>Finally the programmer complains a lot about too much code to change, and how is painful working in an environment where noone is able to catch the real requirement of the customer.</p>
<p>Situation is much more different if you understand that the needs of the customer quite often change with time, we are in a fast world where moving slowly quite often leads to death, so it is absolutely normal that requirements changes with time. The only solution is <em>Plan in advance for change</em>, if you are ready for changes, you will having really less problem when some requirement changes over time. </p>
<p>Technically speaking, to have a software that can react quickly to change the key concept is <em>loose coupling</em>, through heavy use of Inversion of Control, you can minimize dependencies between the various part of the software. The more the software is loosely copuled, the more you can modify a part without affecting other parts. </p>
<p>Then you need a lot of tests, that can spot immediately when a change in some part of the software has introduced some bug. Then You need to setup a Continuous Integration machine, and make your tests run at each check-in, this assures you that tests are run frequently and automatically. Then you must setup a real good logging infrastructure, to trace immediately what is going wrong with the system. Finally you should work in asynchronous mode when possible, minimizing the coupling between different tiers in the software.</p>
<p>But probably the most difficult step is <em>changing the way we manage software developement. </em>We must change the way we approach the software, not fearing changes and reacting differently to change in requirements.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2009/06/image8.png"><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="273" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2009/06/image-thumb8.png" width="644" border="0" /></a> </p>
<p>alk.</p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/Software Architecture" rel="tag">Software Architecture</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2009/06/15/plan-for-change/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Maintain integrity in NHibernate bidirectional association</title>
		<link>http://www.codewrecks.com/blog/index.php/2009/05/05/maintain-integrity-in-nhibernate-bidirectional-association/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2009/05/05/maintain-integrity-in-nhibernate-bidirectional-association/#comments</comments>
		<pubDate>Tue, 05 May 2009 12:38:01 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Nhibernate]]></category>
		<category><![CDATA[Software Architecture]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2009/05/05/maintain-integrity-in-nhibernate-bidirectional-association/</guid>
		<description><![CDATA[
			
				
			
		
Using Bidirectional Associations in nhibernate can be tricky, the problem is that you need to manage associations for both ends. This is really important: suppose you have a class called Parent with a collection of childs called Childs, and a child class with a property called Parent; if you expose these two property to the [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F05%2F05%2Fmaintain-integrity-in-nhibernate-bidirectional-association%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F05%2F05%2Fmaintain-integrity-in-nhibernate-bidirectional-association%2F&amp;source=alkampfer&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Using Bidirectional Associations in nhibernate can be tricky, the problem is that you need to manage associations for both ends. This is really important: suppose you have a class called Parent with a collection of childs called Childs, and a child class with a property called Parent; if you expose these two property to the user, you can write this</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:ce8e548f-5626-435f-b7f5-086a685a2387" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #000000;">Parent John </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> Parent();
Parent Mark </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> Parent();
Child Bart </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> Child();

John.Childs.Add(Bart);
Bart.Parent </span><span style="color: #000000;">=</span><span style="color: #000000;"> Mark;</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>With such a code Bart’s parent is Mark, but Bart is in John Child collection, this is WRONG. To avoid this problem you should maintain integrity in the domain model, managing the consistency of the relationship. Suppose you have the same situation with two objects, a StockKeepingUnit and a StockContainer, here it is the code for StockKeepingUnit</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:8d123a49-dee1-4116-8d09-d3a93b239f8d" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #000000;"> </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">virtual</span><span style="color: #000000;"> StockContainer Parent
 {
    </span><span style="color: #0000FF;">get</span><span style="color: #000000;"> { </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> parent;}
    </span><span style="color: #0000FF;">set</span><span style="color: #000000;">
    {
       </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (parent </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">null</span><span style="color: #000000;">)
          parent._Content.Remove(</span><span style="color: #0000FF;">this</span><span style="color: #000000;">);

       parent </span><span style="color: #000000;">=</span><span style="color: #000000;"> value;

       </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (value </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">null</span><span style="color: #000000;">)
          value._Content.Add(</span><span style="color: #0000FF;">this</span><span style="color: #000000;">);
    }
 }

 </span><span style="color: #0000FF;">private</span><span style="color: #000000;"> StockContainer parent;</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>As you can see in the setter part I first check if this object has a parent, if yes we need to remove it from the _Content collection of the parent object (because actually I’m detatching from it); then if the value is different from null it means that we have a new parent, so the object assign itself to the _Content collection of the new father to mantain integrity. Surely you now need to map this property in directly on the field to avoid problem</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:49913753-53e5-4175-80df-24cd1f74c89e" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">many-to-one </span><span style="color: #FF0000;">name</span><span style="color: #0000FF;">="parent"</span><span style="color: #FF0000;"> access</span><span style="color: #0000FF;">="field"</span><span style="color: #FF0000;"> class</span><span style="color: #0000FF;">="StockKeepingUnit"</span><span style="color: #FF0000;"> ... </span><span style="color: #0000FF;">/&gt;</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>It is of fundamental importance that you map to the field, this because when nhibernate rebuild the object from database, you must avoiding calling the Set part of the Parent property, because the coherence of the objects are guaranteed by nhibernate itself. Now here is how I implemented the Collection part on the SockContainer class</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:5d6c4df4-489d-440b-9bfb-9f4c63f1b37d" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #000000;"> </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">virtual</span><span style="color: #000000;"> IEnumerable </span><span style="color: #000000;">&lt;</span><span style="color: #000000;">StockKeepingUnit</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> Content
 {
    </span><span style="color: #0000FF;">get</span><span style="color: #000000;"> { </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> _Content;}
 }

 </span><span style="color: #0000FF;">internal</span><span style="color: #000000;"> </span><span style="color: #0000FF;">virtual</span><span style="color: #000000;"> ISet</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">StockKeepingUnit</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> _Content
 {
    </span><span style="color: #0000FF;">get</span><span style="color: #000000;">
    {
       </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> content </span><span style="color: #000000;">??</span><span style="color: #000000;"> (content </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> HashedSet </span><span style="color: #000000;">&lt;</span><span style="color: #000000;">StockKeepingUnit</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">());
    }
    </span><span style="color: #0000FF;">set</span><span style="color: #000000;">
    {
        content </span><span style="color: #000000;">=</span><span style="color: #000000;"> value;
    }
 }
 </span><span style="color: #0000FF;">private</span><span style="color: #000000;"> ISet</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">StockKeepingUnit</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> content;</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>First of all the ISet class is a private field, and the accessor property, called _Content, is internal because it must be accessed by the StockKeepingUnit code seen before, but must not be accessed from external code. The property is implemented with lazy creation, if the content property is null, I simply create an HashedSet, this is useful when you first create the object, because initially this property is null. When nhibernate rebuild the object from database data, the content private field is set by nhibernate, so I can avoid unnecessary creation of HashedSet. With this kind of structure you can map nhibernate to the _Content property and not to the field.</p>
<p>Now a question arise: How can the user of this class access the Collection of StockKeepingUnit if the _Content property is private? The answer is in the property Content that is of type <em>IEnumerable&lt;StockKeepingUnit&gt;.</em> With Ienumerable I permit the user to use fully LINQ syntax to access collection of StockKeepingUnit, this is enough for most scenarios because with LINQ you can iterate, filter, select and doing whatever you want to access all StockKeepingUnit contained in a stockContainer.</p>
<p>Alk</p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/NHibernate" rel="tag">NHibernate</a> <a target="_blank" href="http://technorati.com/tag/Domain Model" rel="tag">Domain Model</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2009/05/05/maintain-integrity-in-nhibernate-bidirectional-association/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Adapting a model to make binding simpler.</title>
		<link>http://www.codewrecks.com/blog/index.php/2009/03/17/adapting-a-model-to-make-binding-simpler/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2009/03/17/adapting-a-model-to-make-binding-simpler/#comments</comments>
		<pubDate>Tue, 17 Mar 2009 16:56:53 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[.NET framework]]></category>
		<category><![CDATA[Software Architecture]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2009/03/17/adapting-a-model-to-make-binding-simpler/</guid>
		<description><![CDATA[
			
				
			
		
I have a domain model composed by Clients, each client has a series of associated Typology objects and each Typology object has a certain number of associated Target. Now I need to bind this model to a series of three combo in WPF, but I have this little problem
The model is not directly accessible, I [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F03%2F17%2Fadapting-a-model-to-make-binding-simpler%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F03%2F17%2Fadapting-a-model-to-make-binding-simpler%2F&amp;source=alkampfer&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>I have a domain model composed by Clients, each client has a series of associated Typology objects and each Typology object has a certain number of associated Target. Now I need to bind this model to a series of three combo in WPF, but I have this little problem</p>
<p>The model is not directly accessible, I have already a service that has functions like GetAllCustomer, GetTyplogyForCustomer(Int32 customerId) and GetTargetForTyplogy(Int32 typologyId). Each method returns a list of the corresponding object, but they are DTO and not real object, so the Customer, Typology and Target returned from the service have no relation between them. Moreover I do not want to load everything when the application starts, because it will be extremely slow, I want to be able to load data as it needed, with lazy load. I created a couple of classes that I can use to lazily load entities</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2009/03/image3.png"><img title="image" style="border-right: 0px; border-top: 0px; display: inline; margin-left: 0px; border-left: 0px; margin-right: 0px; border-bottom: 0px" height="394" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2009/03/image-thumb3.png" width="197" align="left" border="0" /></a>&#160; The <strong>LazyPartitionedList</strong> class is used to load collection of data partitioned by some value. I need such a base class for typology, I do not want to load all typologies in a single shot, I need only to load for currently selected customer, then when the current customer changed on the WPF interface I need to load (if it was not already loaded) from the service the list of typologies for the new customer. The LazyPartitionedList does this all for me. Then I create a browser class that link all toghether</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:6228e410-b775-4051-a18b-f5ee18b30228" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;overflow: auto;"><span style="color: #000000;">   </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">class</span><span style="color: #000000;"> AnalysisDefinitionBrowser
   {
      </span><span style="color: #0000FF;">private</span><span style="color: #000000;"> LazyCollection</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Client</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> customers;
      </span><span style="color: #0000FF;">private</span><span style="color: #000000;"> LazyPartitionedCollection</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">TypologyDto, Client</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> typologies;</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>The class analysisDefinitionBrowser have a lazycollection of customers (it loads all customer in one shot) then It has a LazyPartitionedCollection of TypologyDto partitioned by Client object. In the constructor of this object I initialize everything</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:85229f8b-13c9-4a19-8a7c-992595ffb75d" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;overflow: auto;"><span style="color: #000000;">customers </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> LazyCollection</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Client</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">(
   () </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> Services.CustomerService.GetAllClient().ToList());
typologies </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> LazyPartitionedCollection</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">TypologyDto, Client</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">(
  c </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> Services.KeywordService.GetTipologiesForCustomer(c.Id));
...
customers.Reinit();
cvCustomers </span><span style="color: #000000;">=</span><span style="color: #000000;"> CollectionViewSource.GetDefaultView(customers);
</span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (cvCustomers </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">null</span><span style="color: #000000;">)
{
  cvCustomers.CurrentChanged </span><span style="color: #000000;">+=</span><span style="color: #000000;"> cvCustomers_CurrentChanged;
}
</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>Customers gets immediately loaded (Reinit Method), the interesting thing is that the LazyPartitionedCollection for typology Id is initialized with a function that calls the service to load typologies for current customer, actually no typology gets loaded. Another interesting thing is that using the CollectionViewSource I’m able to retrieve the default view for the Customer list.</p>
<p><div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:30d4a04b-1ad2-4d5f-b428-e1f4fab86f21" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;overflow: auto;"><span style="color: #0000FF;">void</span><span style="color: #000000;"> cvCustomers_CurrentChanged(</span><span style="color: #0000FF;">object</span><span style="color: #000000;"> sender, EventArgs e)
{
   CurrentClient </span><span style="color: #000000;">=</span><span style="color: #000000;"> (Client)cvCustomers.CurrentItem;
}
</span><span style="color: #0000FF;">private</span><span style="color: #000000;"> Client CurrentClient
{
   </span><span style="color: #0000FF;">get</span><span style="color: #000000;"> { </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> currentClient; }
   </span><span style="color: #0000FF;">set</span><span style="color: #000000;">
   {
      currentClient </span><span style="color: #000000;">=</span><span style="color: #000000;"> value;
      </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (value </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">null</span><span style="color: #000000;">)
         typologies.LoadPartition(value);
      currentTypologies.Clear();
      </span><span style="color: #0000FF;">foreach</span><span style="color: #000000;"> (TypologyDto typology </span><span style="color: #0000FF;">in</span><span style="color: #000000;"> typologies.Where(t </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> t.CustomerId </span><span style="color: #000000;">==</span><span style="color: #000000;"> value.Id))
      {
         currentTypologies.Add(typology);
      }
   }
}
</span><span style="color: #0000FF;">private</span><span style="color: #000000;"> Client currentClient;</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>The property CurrentClient have a particular Setter part, it loads the partition of the current Client object, then it copy in an ObservableCollection&lt;TypologyDto&gt; called currentTypologies only the typologies that are associated to current customer. With this trick if I already loaded that partition data are in memory and the service gets called only the first time, and only when it is needed. With this code I’ve created a class that load data only when it is needed, and keep selection in sync with the current properties. The beautiful thing of this object is that I can test it with no problem</p>
<p><div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c82e3e4f-dfc0-49be-9fa4-e6d7e0244bc9" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;overflow: auto;"><span style="color: #0000FF;">private</span><span style="color: #000000;"> </span><span style="color: #0000FF;">void</span><span style="color: #000000;"> TestTemplate(
   Action</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">ICustomerService,IKeywordService</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> SetupAction,
   Action ExerciseAction)
{
   ICustomerService cs </span><span style="color: #000000;">=</span><span style="color: #000000;"> RhinoRepository.StrictMock</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">ICustomerService</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">();
   IKeywordService ks </span><span style="color: #000000;">=</span><span style="color: #000000;"> RhinoRepository.StrictMock</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">IKeywordService</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">();

   SetupAction(cs, ks);
   RhinoRepository.ReplayAll();
   </span><span style="color: #0000FF;">using</span><span style="color: #000000;"> (Services.CustomerService.Override(cs))
   </span><span style="color: #0000FF;">using</span><span style="color: #000000;"> (Services.KeywordService.Override(ks))
   {
      ExerciseAction();
      RhinoRepository.VerifyAll();
   }
}

[Test]
</span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">void</span><span style="color: #000000;"> VerifyLoadingOfTheSecondLevelTwoCustomer()
{
   Client c1 </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> Client() { Id </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">111</span><span style="color: #000000;"> };
   Client c2 </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> Client() { Id </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">222</span><span style="color: #000000;"> };
   TypologyDto t1 </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> TypologyDto() { Id </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">444</span><span style="color: #000000;">, Name </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">1</span><span style="color: #800000;">"</span><span style="color: #000000;">, CustomerId </span><span style="color: #000000;">=</span><span style="color: #000000;"> c1.Id };
   TypologyDto t2 </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> TypologyDto() { Id </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">445</span><span style="color: #000000;">, Name </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">2</span><span style="color: #800000;">"</span><span style="color: #000000;">, CustomerId </span><span style="color: #000000;">=</span><span style="color: #000000;"> c2.Id };
   TypologyDto t3 </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> TypologyDto() { Id </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">446</span><span style="color: #000000;">, Name </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">3</span><span style="color: #800000;">"</span><span style="color: #000000;">, CustomerId </span><span style="color: #000000;">=</span><span style="color: #000000;"> c2.Id };
   TestTemplate((cs, ks) </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;">
   {
      Expect.Call(cs.GetAllClient()).Return(</span><span style="color: #0000FF;">new</span><span style="color: #000000;">[] { c1, c2 });
      Expect.Call(ks.GetTipologiesForCustomer(c1.Id))
         .Repeat.Once()
         .Return(</span><span style="color: #0000FF;">new</span><span style="color: #000000;"> List</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">TypologyDto</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> { t1 });
      Expect.Call(ks.GetTipologiesForCustomer(c2.Id))
         .Return(</span><span style="color: #0000FF;">new</span><span style="color: #000000;"> List</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">TypologyDto</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> { t2, t3 });
   },
    () </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;">
    {
       AnalysisDefinitionBrowser sut </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> AnalysisDefinitionBrowser();
       sut.cvCustomers.MoveCurrentToPosition(</span><span style="color: #000000;">-</span><span style="color: #800080;">1</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;">Resetto il binding</span><span style="color: #008000;">
</span><span style="color: #000000;">       sut.cvCustomers.MoveCurrentToFirst(); </span><span style="color: #008000;">//</span><span style="color: #008000;"> Mi muovo sul primo</span><span style="color: #008000;">
</span><span style="color: #000000;">       Assert.That(sut.CurrentTipologies, Has.Count(</span><span style="color: #800080;">1</span><span style="color: #000000;">));
       sut.cvCustomers.MoveCurrentToPosition(</span><span style="color: #800080;">1</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> Mi muovo sul primo</span><span style="color: #008000;">
</span><span style="color: #000000;">       Assert.That(sut.CurrentTipologies, Has.Count(</span><span style="color: #800080;">2</span><span style="color: #000000;">));
       sut.cvCustomers.MoveCurrentToFirst(); </span><span style="color: #008000;">//</span><span style="color: #008000;"> Mi muovo sul primo</span><span style="color: #008000;">
</span><span style="color: #000000;">       Assert.That(sut.CurrentTipologies, Has.Count(</span><span style="color: #800080;">1</span><span style="color: #000000;">));
    });
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>The TestTemplate is used to create and setup mock for services, my services classes are static wrapper that permits me to override the real instance of service used, so I can use a different mock for each call of the service. Then the test simply setup some expectation and exercise the AnalysisDefinitionBrowser with direct manipulation of the ICollectionView object, thus simulating the user that changes currently selected item in the interface. </p>
<p>This test access the cvCustomers internal ICollectionView object because the Test assembly is InternalVisibleTo the test that contains the sut. The XAML code is something like this (I’ve bound a controller as the root source of the page, the controller initialize an instance of AnalysisDefinitoinBrowser and expose it with a dependency property called AnalysisDefinition)</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:7fa158ac-c5e7-4e2a-b025-f326bed6f832" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;overflow: auto;"><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">ComboBox </span><span style="color: #FF0000;">DockPanel.Dock</span><span style="color: #0000FF;">="Top"</span><span style="color: #FF0000;">
          ItemsSource</span><span style="color: #0000FF;">="</span><span style="color: #808000;">{Binding AnalysisDefinition.Customers}</span><span style="color: #0000FF;">"</span><span style="color: #FF0000;">
          IsSynchronizedWithCurrentItem</span><span style="color: #0000FF;">="True"</span><span style="color: #FF0000;">
          DisplayMemberPath</span><span style="color: #0000FF;">="Name"</span><span style="color: #FF0000;">
          SelectedValuePath</span><span style="color: #0000FF;">="Id"</span><span style="color: #0000FF;">&gt;&lt;/</span><span style="color: #800000;">ComboBox</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
          ...
</span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">ComboBox </span><span style="color: #FF0000;">ItemsSource</span><span style="color: #0000FF;">="</span><span style="color: #808000;">{Binding AnalysisDefinition.CurrentTipologies}</span><span style="color: #0000FF;">"</span><span style="color: #FF0000;">
          IsSynchronizedWithCurrentItem</span><span style="color: #0000FF;">="True"</span><span style="color: #FF0000;">
          DisplayMemberPath</span><span style="color: #0000FF;">="Name"</span><span style="color: #FF0000;">
          SelectedValuePath</span><span style="color: #0000FF;">="Id"</span><span style="color: #FF0000;"> </span><span style="color: #0000FF;">&gt;&lt;/</span><span style="color: #800000;">ComboBox</span><span style="color: #0000FF;">&gt;</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Now when I fired the application and look into the sql server profiler I can see that object gets transparently loaded while I’m browsing the objects with the two combo, each time I select a new customer I see query that load related typologies, when I select a customer that was already selected in the past data are already in memory. </p>
<p>alk.</p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/WPF" rel="tag">WPF</a> <a target="_blank" href="http://technorati.com/tag/Binding" rel="tag">Binding</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2009/03/17/adapting-a-model-to-make-binding-simpler/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Setting up the trunk of a project</title>
		<link>http://www.codewrecks.com/blog/index.php/2009/02/16/setting-up-the-trunk-of-a-project/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2009/02/16/setting-up-the-trunk-of-a-project/#comments</comments>
		<pubDate>Mon, 16 Feb 2009 09:03:28 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Software Architecture]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2009/02/16/setting-up-the-trunk-of-a-project/</guid>
		<description><![CDATA[
			
				
			
		
In this very interesting link brendan spoke about how to organize the trunk of a project. I think that this is a very important subject, especially for medium sized or big project. If you fail to organize your source code including everything you will end with trouble. In the past it happened to me to [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F02%2F16%2Fsetting-up-the-trunk-of-a-project%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F02%2F16%2Fsetting-up-the-trunk-of-a-project%2F&amp;source=alkampfer&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>In <a target="_blank" href="http://brendan.enrick.com/blog/organizing-software-projects/">this very interesting link</a> brendan spoke about how to organize the trunk of a project. I think that this is a very important subject, especially for medium sized or big project. If you fail to organize your source code including everything you will end with trouble. In the past it happened to me to work on project where you need to spend a lot of time just to have the first run ok. I need to download and install tools, libraries, then I need to open a solution, compile and move dll into another folder etc etc etc. This situation is a clear indication of a bad organization of the trunk.</p>
<p>A project is not only composed by source code, so you need to include in your repository everything you need to build the project. I include all the installer of third part library, and clear indication on how to get trial license if needed, I also included all binary version of open source libraries (like nhibernate and nunit), I included every document that you need to read to make the software run (EX. manuals of hardware if your software need to communicate with various RFID libraries) but the tougher part is creating a good structure for the project and creating a good build script.</p>
<p>In big project building everything is not so simple, we cannot simply have a solution with 100+ projects, but we usually divide the software into smaller areas, then you need a series of script that build everything from the ground up, moving all the binaries in a well known directory (the artifact or the build one) so you can divide the project into smaller solution that are more manageable.</p>
<p>The goal is having a project where any new developer can start to work with little three steps</p>
<ol>
<li>checkout the solution in a local folder </li>
<li>Reads documentation and install any needed third party packages </li>
<li>Launch a build script </li>
</ol>
<p>After step 3 the developer should be able to open any solution in the project and being able to work, this means that the script should create local databases for test sandbox, MSMQ if needed, and any other stuff that is needed in order to have a running solution. This means having a &quot;alive project&quot;.</p>
<p>alk.</p>
</p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/project management" rel="tag">project management</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2009/02/16/setting-up-the-trunk-of-a-project/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Extend BindingList with filter functionality</title>
		<link>http://www.codewrecks.com/blog/index.php/2008/11/22/extend-bindinglist-with-filter-functionality/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2008/11/22/extend-bindinglist-with-filter-functionality/#comments</comments>
		<pubDate>Sat, 22 Nov 2008 11:14:16 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Languages]]></category>
		<category><![CDATA[Software Architecture]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2008/11/22/extend-bindinglist-with-filter-functionality/</guid>
		<description><![CDATA[
			
				
			
		
If you still work with windows forms and cannot move all of your projects to WPF, you probably gets annoyed by the limitation of the standard BindingList&#60;T&#62; included in the framework. In an old post I showed how can you create a specialized BindingList&#60;T&#62; to support generic Find() thanks to reflection, but this is not [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2008%2F11%2F22%2Fextend-bindinglist-with-filter-functionality%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2008%2F11%2F22%2Fextend-bindinglist-with-filter-functionality%2F&amp;source=alkampfer&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>If you still work with windows forms and cannot move all of your projects to WPF, you probably gets annoyed by the limitation of the standard BindingList&lt;T&gt; included in the framework. In an <a href="http://www.codewrecks.com/blog/index.php/2007/10/12/bindinglistfind-and-notimplementedexception/">old post</a> I showed how can you create a specialized BindingList&lt;T&gt; to support generic Find() thanks to reflection, but this is not enough.</p>
<p>Another annoying limitation is that BindingList does not support IBindingListView. For those that does not knows this interface, it is the one used by the windows forms binding engine to support filtering and advanced sorting. I think that filtering is a real basic functionality needed by the binding engine, and in fact WPF support for filtering, sorting and grouping, make these operations really a breeze. But BindingList does not offers such a functionality.</p>
<p>My question is: <em>is it possible to inherits from BindingList&lt;T&gt; adding generic filtering support as for the DataView component</em>?. The answer is yes, here is a simple implementation.</p>
<p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:de608857-b994-4adb-b41b-595614a13852" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">string</span><span style="color: #000000;"> Filter
{
    </span><span style="color: #0000FF;">get</span><span style="color: #000000;">
    {
        </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> filterClause;
    }
    </span><span style="color: #0000FF;">set</span><span style="color: #000000;">
    {
        </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (filterClause </span><span style="color: #000000;">==</span><span style="color: #000000;"> value) </span><span style="color: #0000FF;">return</span><span style="color: #000000;">;
        filterClause </span><span style="color: #000000;">=</span><span style="color: #000000;"> value;
        DoFilter();
    }
}</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>This is really simple, I implement the FIlter property with a simple check, if the filterClause changes, I call the DoFilter function.</p>
<p><div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:b8433304-b00b-4678-beb8-ee43366860bd" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #0000FF;">private</span><span style="color: #000000;"> Func</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">T, Boolean</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> filterPredicate;

</span><span style="color: #0000FF;">private</span><span style="color: #000000;"> List</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">T</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> original </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> List</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">T</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">();

</span><span style="color: #0000FF;">private</span><span style="color: #000000;"> </span><span style="color: #0000FF;">void</span><span style="color: #000000;"> DoFilter()
{
    </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (String.IsNullOrEmpty(filterClause))
    {
        Items.Clear();
        original.ForEach(e </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> Items.Add(e));
    }
    </span><span style="color: #0000FF;">else</span><span style="color: #000000;">
    {
        original.Clear();
        original.AddRange(Items);
        Items.Clear();
        filterPredicate </span><span style="color: #000000;">=</span><span style="color: #000000;"> DynamicLinq.ParseToFunction</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">T, Boolean</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">(filterClause);
        </span><span style="color: #0000FF;">foreach</span><span style="color: #000000;"> (T element </span><span style="color: #0000FF;">in</span><span style="color: #000000;"> original.Where(filterPredicate))
           Items.Add(element);
    }
}</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>The implementation is based on this standard trick, if a filter is applied I copy all the elements in a temp list, then clear the base.Items list and add again only the elements that satisfies the filter. The big work is done in DynamicLinq.ParseToFunction that takes a string and dynamically compiles a function with that criteria. Here is a simple test showing this class in action.</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:3d8e51f2-bca9-4bd9-914c-4ded1c5abc29" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #000000;">[Test]
</span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">void</span><span style="color: #000000;"> TestLogicAnd()
{
    Func</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Customer, Boolean</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> f </span><span style="color: #000000;">=</span><span style="color: #000000;"> DynamicLinq.ParseToFunction</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Customer, Boolean</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">Name == 'Gian Maria' &amp;&amp; Age &gt; 5</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">);
    Assert.That(f(aCustomer), Is.True);
}</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
</p>
<p>Actually I use three main component, a tokenizer that takes a string and separates into token, then I pass the tokenized expression (is a List&lt;String&gt;) to another component that convert the infix form in postfix form (<a target="_blank" href="http://en.wikipedia.org/wiki/Reverse_Polish_notation">reverse Polish</a>), finally I create an expression from the reverse Polish form, creates a LambdaExpression on it and finally call compile() to make dynamic generation happens. The result is a dynamic generator of LINQ expression from a string. It still misses a lot, you cannot express condition on nested properties and parameter support is still experimental, but the basic is there.</p>
<p>The code is still under development, if you are curios you can check out the repository at <a target="_blank" title="http://dotnetmarcheproject.googlecode.com/svn/trunk" href="http://dotnetmarcheproject.googlecode.com/svn/trunk">http://dotnetmarcheproject.googlecode.com/svn/trunk</a> the code is still in rough form, but if you like it you can give it a shot. The class is the BindingListExt&lt;T&gt;</p>
<p>Alk.</p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/BindingList" rel="tag">BindingList</a> <a target="_blank" href="http://technorati.com/tag/IBindingView" rel="tag">IBindingView</a> <a target="_blank" href="http://technorati.com/tag/Dynamic Expression Parser" rel="tag">Dynamic Expression Parser</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2008/11/22/extend-bindinglist-with-filter-functionality/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Impedence between domain objects and Ui Layer</title>
		<link>http://www.codewrecks.com/blog/index.php/2008/11/20/impedence-between-domain-objects-and-ui-layer/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2008/11/20/impedence-between-domain-objects-and-ui-layer/#comments</comments>
		<pubDate>Thu, 20 Nov 2008 17:09:37 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[.NET framework]]></category>
		<category><![CDATA[Software Architecture]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2008/11/20/impedence-between-domain-objects-and-ui-layer/</guid>
		<description><![CDATA[
			
				
			
		
When you work with domain model your domain objects become more complex than a simple containers for table values. This makes somewhat problematic using these object directly with the UI, especially when you use native binding. Let’s make an example, I have this object called Domain
 
As you can see the Domain class has a [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2008%2F11%2F20%2Fimpedence-between-domain-objects-and-ui-layer%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2008%2F11%2F20%2Fimpedence-between-domain-objects-and-ui-layer%2F&amp;source=alkampfer&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>When you work with domain model your domain objects become more complex than a simple containers for table values. This makes somewhat problematic using these object directly with the UI, especially when you use native binding. Let’s make an example, I have this object called <strong>Domain</strong></p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2008/11/image10.png"><img title="image" style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="300" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2008/11/image-thumb10.png" width="675" border="0" /></a> </p>
<p>As you can see the Domain class has a property called ContentBlackList that is a List of String. The setter is private and in the getter the list is created lazily, then I have a <a href="http://www.codewrecks.com/blog/index.php/2008/10/21/some-details-on-older-post-about-usertype/">User Type</a> to map this property into a single field of a table in a list of string separated by the charachter #. The object works well, until I want to bind this property directly to a textbox in a form. </p>
<p>This is a common problem, if objects are not simple container for database fields it could happens that they cannot be used directly with the binding engine. </p>
<p>The first problem is, I need to convert the list&lt;String&gt; to a string to show data in textbox and back when the user change content of the textbox. This is fairly simple in windows forms thanks to Parse() and Format() methods of Binding object</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:99071fdf-d180-40d2-b7ad-45d55452d073" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #008080;"> 1</span> <span style="color: #0000FF;">private</span><span style="color: #000000;"> </span><span style="color: #0000FF;">void</span><span style="color: #000000;"> AddFormattingForStringList(TextBox textBoxControl)
</span><span style="color: #008080;"> 2</span> <span style="color: #000000;">        {
</span><span style="color: #008080;"> 3</span> <span style="color: #000000;">            Binding b </span><span style="color: #000000;">=</span><span style="color: #000000;"> textBoxControl.DataBindings[</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">Text</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">];
</span><span style="color: #008080;"> 4</span> <span style="color: #000000;">            </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (b </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #0000FF;">null</span><span style="color: #000000;">) </span><span style="color: #0000FF;">return</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 5</span> <span style="color: #000000;">            b.Parse </span><span style="color: #000000;">+=</span><span style="color: #000000;"> ConvertStringToList;
</span><span style="color: #008080;"> 6</span> <span style="color: #000000;">            b.Format </span><span style="color: #000000;">+=</span><span style="color: #000000;"> ConvertListToString;
</span><span style="color: #008080;"> 7</span> <span style="color: #000000;">        }
</span><span style="color: #008080;"> 8</span> <span style="color: #000000;">
</span><span style="color: #008080;"> 9</span> <span style="color: #000000;">        </span><span style="color: #0000FF;">void</span><span style="color: #000000;"> ConvertListToString(</span><span style="color: #0000FF;">object</span><span style="color: #000000;"> sender, ConvertEventArgs e)
</span><span style="color: #008080;">10</span> <span style="color: #000000;">        {
</span><span style="color: #008080;">11</span> <span style="color: #000000;">
</span><span style="color: #008080;">12</span> <span style="color: #000000;">            List</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">String</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> list </span><span style="color: #000000;">=</span><span style="color: #000000;"> (List</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">String</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">)e.Value;
</span><span style="color: #008080;">13</span> <span style="color: #000000;">            </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (list </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #0000FF;">null</span><span style="color: #000000;"> </span><span style="color: #000000;">||</span><span style="color: #000000;"> list.Count </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">)
</span><span style="color: #008080;">14</span> <span style="color: #000000;">                e.Value </span><span style="color: #000000;">=</span><span style="color: #000000;"> String.Empty;
</span><span style="color: #008080;">15</span> <span style="color: #000000;">            </span><span style="color: #0000FF;">else</span><span style="color: #000000;">
</span><span style="color: #008080;">16</span> <span style="color: #000000;">                e.Value </span><span style="color: #000000;">=</span><span style="color: #000000;"> list.Aggregate((s1, s2) </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> s1 </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #800000;">&quot;</span><span style="color: #800000;">,</span><span style="color: #800000;">&quot;</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> s2);
</span><span style="color: #008080;">17</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">18</span> <span style="color: #000000;">
</span><span style="color: #008080;">19</span> <span style="color: #000000;">        </span><span style="color: #0000FF;">void</span><span style="color: #000000;"> ConvertStringToList(</span><span style="color: #0000FF;">object</span><span style="color: #000000;"> sender, ConvertEventArgs e)
</span><span style="color: #008080;">20</span> <span style="color: #000000;">        {
</span><span style="color: #008080;">21</span> <span style="color: #000000;">            String value </span><span style="color: #000000;">=</span><span style="color: #000000;"> (String)e.Value;
</span><span style="color: #008080;">22</span> <span style="color: #000000;">            </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (String.IsNullOrEmpty(value))
</span><span style="color: #008080;">23</span> <span style="color: #000000;">                e.Value </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> List</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">String</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">();
</span><span style="color: #008080;">24</span> <span style="color: #000000;">            </span><span style="color: #0000FF;">else</span><span style="color: #000000;">
</span><span style="color: #008080;">25</span> <span style="color: #000000;">                e.Value </span><span style="color: #000000;">=</span><span style="color: #000000;"> value.Split(</span><span style="color: #800000;">'</span><span style="color: #800000;">,</span><span style="color: #800000;">'</span><span style="color: #000000;">, </span><span style="color: #800000;">'</span><span style="color: #800000;"> </span><span style="color: #800000;">'</span><span style="color: #000000;">, </span><span style="color: #800000;">'</span><span style="color: #800000;">|</span><span style="color: #800000;">'</span><span style="color: #000000;">, </span><span style="color: #800000;">'</span><span style="color: #800000;">;</span><span style="color: #800000;">'</span><span style="color: #000000;">).ToList();
</span><span style="color: #008080;">26</span> <span style="color: #000000;">        }</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>As you can see in line 3 I get the Binding for property “Text” of a textBox, this is necessary because I could have more bindings to other properties. Then if the binding is not null I listen for the Parse and Format events. The parse event is invoked when the Binding takes the value from the control (Textbox.Text property, a string) and assign it to the original property (a List of String) and the attached ConvertStringtoList simply split the string with various separator and uses ToList() to create a list from the array. The ConvertListToString attached to the format event, does the reverse operation, converting a list of string in a comma separated list of string, this is a breeze thanks to the Aggregate Linq operator.</p>
<p>But this is not enough, if you look closely at the property implementation of the original object you can see that the set part of ContentBlackList is private. This is needed because the user must not be able to change the inner IList&lt;String&gt; used by the object. This is especially important if you use an ORM like nhibernate, because usually NHibernate use lazy list and you should not substitute with other list. Since NHibernate can access private properties I’m sure that noone can tamper the original IList&lt;String&gt; implementation. </p>
<p>Now the problem is: Binding Engine cannot set private property, so the binding is broken.</p>
<p>This happens because the Domain Object is not designed with Ui in mind, and this is a&#160; good thing, because the Domain should not depend from the Ui. To solve this typical problem you can resort to the old Proxy pattern. </p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2008/11/image11.png"><img title="image" style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="331" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2008/11/image-thumb11.png" width="561" border="0" /></a> </p>
<p>As you can see the proxy wraps a real Domain object and exposes the ContentBlackList to the UI and presents both get and set methods. The Set methods simply remove all objects from the original list and then insert again thus permitting me to bind this object directly to a textbox.</p>
<p>The general problem discussed here is that Objects from Domain quite often are not directly usable with the UI layer, and a proxy can be a good solution, even if in domain with hundreds of classes, doing a proxy for each class can be a manteniance nightmare.</p>
<p>alk.</p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/Binding" rel="tag">Binding</a> <a target="_blank" href="http://technorati.com/tag/Proxy" rel="tag">Proxy</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2008/11/20/impedence-between-domain-objects-and-ui-layer/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The eternal conflict between customers and programmers</title>
		<link>http://www.codewrecks.com/blog/index.php/2008/09/11/the-eternal-conflicts/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2008/09/11/the-eternal-conflicts/#comments</comments>
		<pubDate>Thu, 11 Sep 2008 17:52:26 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Experiences]]></category>
		<category><![CDATA[Software Architecture]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2008/09/11/the-eternal-conflicts/</guid>
		<description><![CDATA[
			
				
			
		
In my experience it is not a good idea to put in direct communication stakeholders (customers) and programmers, here is the typical result
 
Lets analyze this typical situation from the perspective of the stakeholder; he have all the reason to ask for new features, he does not possess strong technical skills, and since he is [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2008%2F09%2F11%2Fthe-eternal-conflicts%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2008%2F09%2F11%2Fthe-eternal-conflicts%2F&amp;source=alkampfer&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>In my experience it is not a good idea to put in direct communication stakeholders (customers) and programmers, here is the typical result</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2008/09/drawing.jpg"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="772" alt="drawing" src="http://www.codewrecks.com/blog/wp-content/uploads/2008/09/drawing-thumb.jpg" width="450" border="0"></a> </p>
<p>Lets analyze this typical situation from the perspective of the stakeholder; he have all the reason to ask for new features, he does not possess strong technical skills, and since he is the one who actually <em>pay to build the software</em>, he does not understand why he cannot ask for a simple modification. Moreover the developer is speaking some obscure technical jargon, so he could not understand the reason behind these difficulties.</p>
<p>From the perspective of the programmer, the customer is the one who always change the game, he does not understand the technical implications that can lie behind a simple button, so he resort to intimidate him with technical jargon, hoping that at least he will understand that adding that ***** button will not be an easy task.</p>
<p>In the end both the contendants&nbsp; feels that the other part is not doing his part of the work, it is not a good situation to manage.</p>
<p>But the ultimate question is &#8220;there is a solution?&#8221;, in my opinion the answer is &#8220;you need an analyst&#8221;.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2008/09/drawing2.jpg"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="772" alt="drawing2" src="http://www.codewrecks.com/blog/wp-content/uploads/2008/09/drawing2-thumb.jpg" width="466" border="0"></a> </p>
<p>The analyst is capable to speak with customer in plain english, and since he possess some technical skills, he can anticipate some of the problems. Then he is capable to analyze the needs of the customer to find the way to fulfill them with alternate solution, quite often the alternate solution could be even better for the stakeholder. Then the analyst speak with the development team, he shows all the possibilities to the developers that will give to each alternative a weight. The final step is choosing the possibility that satisfy the customer but have no great impact on the system, if any.</p>
<p>The analyst is the logical connection between the needs of the customer and the programming team. His ultimate goal is to fully understand all stakeholder&#8217;s needs, finding alternatives to each goal, trying to anticipate problems and most important, he is constantly trying to eviscerate <strong>all customer&#8217;s needs</strong> as soon as possible. Without a good analysis you cannot write good software.</p>
<p>alk.</p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/Analysis" rel="tag">Analysis</a> <a target="_blank" href="http://technorati.com/tag/Software" rel="tag">Software</a> </p>
<div class="wlWriterSmartContent" id="scid:C16BAC14-9A3D-4c50-9394-FBFEF7A93539:d2e63ad1-950f-41e7-89d9-243f0d42caf1" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"><!--dotnetkickit--></div>
]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2008/09/11/the-eternal-conflicts/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>I love fluent interfaces.</title>
		<link>http://www.codewrecks.com/blog/index.php/2008/09/04/i-love-fluent-interfaces/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2008/09/04/i-love-fluent-interfaces/#comments</comments>
		<pubDate>Thu, 04 Sep 2008 13:26:57 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Software Architecture]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2008/09/04/i-love-fluent-interfaces/</guid>
		<description><![CDATA[
			
				
			
		
I&#8217;m developing a little library that basically does these steps
1) open a docx file2) Find a particular bookmark in the file3)Add some content at bookmark position4) Save the updated file.
This is a typical call
 
I like very much this kind of syntax, since we chain every method we always have intellisense that suggest us what [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2008%2F09%2F04%2Fi-love-fluent-interfaces%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2008%2F09%2F04%2Fi-love-fluent-interfaces%2F&amp;source=alkampfer&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>I&#8217;m developing a little library that basically does these steps</p>
<p>1) open a docx file<br />2) Find a particular bookmark in the file<br />3)Add some content at bookmark position<br />4) Save the updated file.</p>
<p>This is a typical call</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2008/09/image.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="536" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2008/09/image-thumb.png" width="735" border="0"></a> </p>
<p>I like very much this kind of syntax, since we chain every method we always have intellisense that suggest us what to call, if you read the code you can easily check that we add two simple cell with text, then we add a cell with complex text, then another paragraph and finally another table, here is the result on the final document.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2008/09/image1.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="96" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2008/09/image-thumb1.png" width="244" border="0"></a> </p>
<p>Fluent interfaces like these fully benefit from intellisense and are really simple to use.</p>
<p>alk.</p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/fluent interface" rel="tag">fluent interface</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2008/09/04/i-love-fluent-interfaces/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Relations are really so useful?</title>
		<link>http://www.codewrecks.com/blog/index.php/2008/07/23/relations-are-really-so-useful/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2008/07/23/relations-are-really-so-useful/#comments</comments>
		<pubDate>Wed, 23 Jul 2008 06:55:25 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Software Architecture]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2008/07/23/relations-are-really-so-useful/</guid>
		<description><![CDATA[
			
				
			
		
Today I was reading this post from my friend Marco, where he points out one of the most frequent problem in the design of domain Model: too much use of relations. The problem arises from the fact that an ORM like NHibernate, seems to solve all the problems because it handles all the complexities, but [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2008%2F07%2F23%2Frelations-are-really-so-useful%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2008%2F07%2F23%2Frelations-are-really-so-useful%2F&amp;source=alkampfer&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Today I was reading <a target="_blank" href="http://www.codemetropolis.com/archive/2008/07/22/domain-model-amp-aggregates-when-do-master-detail-associations-happen.aspx">this post</a> from my friend <a target="_blank" href="http://www.codemetropolis.com/">Marco</a>, where he points out one of the most frequent problem in the design of domain Model: <i>too much use of relations</i>. The problem arises from the fact that an ORM like NHibernate, seems to solve all the problems because it handles all the complexities, but there are a lot of considerations to be done before establish a relation between entities. Let’s have a brief look at the classical example of Customer-Orders
<p>Since an order is related to a Customer, it is good to have relation from Order to Customer, it has business meaning since an order can live only when a customer places it with E-commerce site or in some other way. Lazy load minimize the hit to the database, issuing the select only when you access the data. If you want to associate an order with a Customer, you can avoid loading Customer data if you have CustomerId stored somewhere</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:725b2d8b-943a-4ae5-9c75-6c7b3e08b30d" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #000000;">Order.Customer </span><span style="color: #000000;">=</span><span style="color: #000000;"> session.Load</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Customer</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">(customerId);</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>This instruction uses ISession.Load(), that actually creates a proxy of the Customer Object, and since NHibernate needs only the Id of an object to set the relation, customer table never gets queried. This is good</p>
<p>Association between objects seems to make life easier, so one of the most frequent error is thinking that if an association is good, bidirectional association is two times good. With an ORM, lazy load, persistence by reachability, add associations between objects is a tempting strategy….but pay attention, this is the devil whispering to you hear, because bidirectional associations can be really dangerous.</p>
<p>To understand what is happening behind the scenes, suppose you decide to create an association from customer to order, with a simple property Orders of type IList&lt;Order&gt; in the Customer object. This is not a bidirectional association yet, but it is already problematic. First example: adding an order and a new Customer. </p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:d59afa7c-d957-43d2-96b1-1d1d2c5d7109" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #000000;">Customer gianMaria </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> Customer(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">Gian Maria</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">, </span><span style="color: #800000;">&quot;</span><span style="color: #800000;">Ricci</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">);
Order order1 </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> Order(DateTime.Now, </span><span style="color: #800080;">10.00f</span><span style="color: #000000;">);
gianMaria.Orders.Add(order1);
session.Save(gianMaria);</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Here is the sql:</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:3c896836-a9b3-46e7-a697-24fafceb2265" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; width: 809px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #000000;">NHibernate: </span><span style="color: #0000FF;">INSERT</span><span style="color: #000000;"> </span><span style="color: #0000FF;">INTO</span><span style="color: #000000;"> CustomerOne (Name, Surname) </span><span style="color: #0000FF;">VALUES</span><span style="color: #000000;"> (</span><span style="color: #008000;">@p0</span><span style="color: #000000;">, </span><span style="color: #008000;">@p1</span><span style="color: #000000;">); </span><span style="color: #0000FF;">select</span><span style="color: #000000;"> </span><span style="color: #FF00FF;">SCOPE_IDENTITY</span><span style="color: #000000;">(); </span><span style="color: #008000;">@p0</span><span style="color: #000000;"> </span><span style="color: #808080;">=</span><span style="color: #000000;"> </span><span style="color: #FF0000;">'</span><span style="color: #FF0000;">Gian Maria</span><span style="color: #FF0000;">'</span><span style="color: #000000;">, </span><span style="color: #008000;">@p1</span><span style="color: #000000;"> </span><span style="color: #808080;">=</span><span style="color: #000000;"> </span><span style="color: #FF0000;">'</span><span style="color: #FF0000;">Ricci</span><span style="color: #FF0000;">'</span><span style="color: #000000;">
NHibernate: </span><span style="color: #0000FF;">INSERT</span><span style="color: #000000;"> </span><span style="color: #0000FF;">INTO</span><span style="color: #000000;"> Orders (Date, Total) </span><span style="color: #0000FF;">VALUES</span><span style="color: #000000;"> (</span><span style="color: #008000;">@p0</span><span style="color: #000000;">, </span><span style="color: #008000;">@p1</span><span style="color: #000000;">); </span><span style="color: #0000FF;">select</span><span style="color: #000000;"> </span><span style="color: #FF00FF;">SCOPE_IDENTITY</span><span style="color: #000000;">(); </span><span style="color: #008000;">@p0</span><span style="color: #000000;"> </span><span style="color: #808080;">=</span><span style="color: #000000;"> </span><span style="color: #FF0000;">'</span><span style="color: #FF0000;">7/22/2008 8:39:09 PM</span><span style="color: #FF0000;">'</span><span style="color: #000000;">, </span><span style="color: #008000;">@p1</span><span style="color: #000000;"> </span><span style="color: #808080;">=</span><span style="color: #000000;"> </span><span style="color: #FF0000;">'</span><span style="color: #FF0000;">10</span><span style="color: #FF0000;">'</span><span style="color: #000000;">
NHibernate: </span><span style="color: #0000FF;">UPDATE</span><span style="color: #000000;"> Orders </span><span style="color: #0000FF;">SET</span><span style="color: #000000;"> CustomerId </span><span style="color: #808080;">=</span><span style="color: #000000;"> </span><span style="color: #008000;">@p0</span><span style="color: #000000;"> </span><span style="color: #0000FF;">WHERE</span><span style="color: #000000;"> id </span><span style="color: #808080;">=</span><span style="color: #000000;"> </span><span style="color: #008000;">@p1</span><span style="color: #000000;">; </span><span style="color: #008000;">@p0</span><span style="color: #000000;"> </span><span style="color: #808080;">=</span><span style="color: #000000;"> </span><span style="color: #FF0000;">'</span><span style="color: #FF0000;">4</span><span style="color: #FF0000;">'</span><span style="color: #000000;">, </span><span style="color: #008000;">@p1</span><span style="color: #000000;"> </span><span style="color: #808080;">=</span><span style="color: #000000;"> </span><span style="color: #FF0000;">'</span><span style="color: #FF0000;">7</span><span style="color: #FF0000;">'</span><span style="color: #000000;">
</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>You can see that the order is first inserted into the table with the customerId field equals to null, then the relation is setup with an Update, this means that two queries are needed to setup a relation, this is not scaring, but here is another example, adding an order to an existing customer.</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:882896c6-da09-4e59-a11a-3cbc7bdd1bef" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #000000;">Customer loaded </span><span style="color: #000000;">=</span><span style="color: #000000;"> session.Get</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Customer</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">(gianMaria.Id);
Order order2 </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> Order(DateTime.Now, </span><span style="color: #800080;">10.00f</span><span style="color: #000000;">);
loaded.Orders.Add(order2);
session.Flush();</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>This code seems really simple, you load a customer knowing its id, then create a new order, and finally add the order to the inner collection of orders to setup the association, but if you knows how lazy load works you already expecting this SQL</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:e7e4c623-ea28-47e1-8773-462de17feadf" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #000000;">NHibernate: </span><span style="color: #0000FF;">SELECT</span><span style="color: #000000;"> customer0_.id </span><span style="color: #0000FF;">as</span><span style="color: #000000;"> id1_0_, customer0_.Name </span><span style="color: #0000FF;">as</span><span style="color: #000000;"> Name1_0_, customer0_.Surname </span><span style="color: #0000FF;">as</span><span style="color: #000000;"> Surname1_0_ </span><span style="color: #0000FF;">FROM</span><span style="color: #000000;"> CustomerOne customer0_ </span><span style="color: #0000FF;">WHERE</span><span style="color: #000000;"> customer0_.id</span><span style="color: #808080;">=</span><span style="color: #008000;">@p0</span><span style="color: #000000;">; </span><span style="color: #008000;">@p0</span><span style="color: #000000;"> </span><span style="color: #808080;">=</span><span style="color: #000000;"> </span><span style="color: #FF0000;">'</span><span style="color: #FF0000;">4</span><span style="color: #FF0000;">'</span><span style="color: #000000;">
NHibernate: </span><span style="color: #0000FF;">SELECT</span><span style="color: #000000;"> orders0_.CustomerId </span><span style="color: #0000FF;">as</span><span style="color: #000000;"> CustomerId__1_, orders0_.id </span><span style="color: #0000FF;">as</span><span style="color: #000000;"> id1_, orders0_.id </span><span style="color: #0000FF;">as</span><span style="color: #000000;"> id0_0_, orders0_.Date </span><span style="color: #0000FF;">as</span><span style="color: #000000;"> Date0_0_, orders0_.Total </span><span style="color: #0000FF;">as</span><span style="color: #000000;"> Total0_0_ </span><span style="color: #0000FF;">FROM</span><span style="color: #000000;"> Orders orders0_ </span><span style="color: #0000FF;">WHERE</span><span style="color: #000000;"> orders0_.CustomerId</span><span style="color: #808080;">=</span><span style="color: #008000;">@p0</span><span style="color: #000000;">; </span><span style="color: #008000;">@p0</span><span style="color: #000000;"> </span><span style="color: #808080;">=</span><span style="color: #000000;"> </span><span style="color: #FF0000;">'</span><span style="color: #FF0000;">4</span><span style="color: #FF0000;">'</span><span style="color: #000000;">
NHibernate: </span><span style="color: #0000FF;">INSERT</span><span style="color: #000000;"> </span><span style="color: #0000FF;">INTO</span><span style="color: #000000;"> Orders (Date, Total) </span><span style="color: #0000FF;">VALUES</span><span style="color: #000000;"> (</span><span style="color: #008000;">@p0</span><span style="color: #000000;">, </span><span style="color: #008000;">@p1</span><span style="color: #000000;">); </span><span style="color: #0000FF;">select</span><span style="color: #000000;"> </span><span style="color: #FF00FF;">SCOPE_IDENTITY</span><span style="color: #000000;">(); </span><span style="color: #008000;">@p0</span><span style="color: #000000;"> </span><span style="color: #808080;">=</span><span style="color: #000000;"> </span><span style="color: #FF0000;">'</span><span style="color: #FF0000;">7/22/2008 8:48:53 PM</span><span style="color: #FF0000;">'</span><span style="color: #000000;">, </span><span style="color: #008000;">@p1</span><span style="color: #000000;"> </span><span style="color: #808080;">=</span><span style="color: #000000;"> </span><span style="color: #FF0000;">'</span><span style="color: #FF0000;">10</span><span style="color: #FF0000;">'</span><span style="color: #000000;">
NHibernate: </span><span style="color: #0000FF;">UPDATE</span><span style="color: #000000;"> Orders </span><span style="color: #0000FF;">SET</span><span style="color: #000000;"> CustomerId </span><span style="color: #808080;">=</span><span style="color: #000000;"> </span><span style="color: #008000;">@p0</span><span style="color: #000000;"> </span><span style="color: #0000FF;">WHERE</span><span style="color: #000000;"> id </span><span style="color: #808080;">=</span><span style="color: #000000;"> </span><span style="color: #008000;">@p1</span><span style="color: #000000;">; </span><span style="color: #008000;">@p0</span><span style="color: #000000;"> </span><span style="color: #808080;">=</span><span style="color: #000000;"> </span><span style="color: #FF0000;">'</span><span style="color: #FF0000;">4</span><span style="color: #FF0000;">'</span><span style="color: #000000;">, </span><span style="color: #008000;">@p1</span><span style="color: #000000;"> </span><span style="color: #808080;">=</span><span style="color: #000000;"> </span><span style="color: #FF0000;">'</span><span style="color: #FF0000;">6</span><span style="color: #FF0000;">'</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>First of all, <em>even if you have the Id of the customer you need to load it entirely from db</em>, but the worst thing happens when you add the order to the orders collection, because nhibernate does lazy load, loading <i><strong>all the orders of the customer in memory</strong></i>, finally he generate the insert and update as before.</p>
<p>Think again to it “<i>To add an order you have to load in memory <b>all customer orders</b></i>”. What happens if the Customer have hundreds of orders??? Sounds bad, isn’t it? Moreover you are wasting memory for nothing….you never access all the orders, you only want add new one.</p>
<p>Similar thing happens whenever you access Orders collection, if you access the first element <i>all the orders are loaded</i>, if you call Count <i>all orders gets loaded</i>. To avoid this you need to use ISession.CreateFilter() that permits you to issue a Count(*) query without loading the elements or scan orders with pagination, but you can agree that this is not a good situation.</p>
<p>And with a bidirectional association things get worse, because you have also to keep the domain consistent, what happens if we add an Order in the collection of Customer1 and set the Customer property of the same order to Customer2?</p>
<p>The rule of thumb is that you need to set bag relations only when the containing object really needs the collection of child to have business meaning. If we have the FootballClub object it makes sense to have a collection of Players, after all, a Football Club without players has really little business meaning. </p>
<p>The whole problem originates when the developer use relations to reduce the explicit load operations from the database. Since ORM have lazy load, one can think to relate every object, in this way when you have an instance you can retrieve every other instance simply traversing the tree. But the domain <em>is a representation of our business done with objects</em>, and relations <em>are used to explain what concepts are related and how</em>, resists the temptation to use relations only as a way to navigate object tree, you should use repository for that.</p>
<p>When designing relations between object the aggregate pattern (<a target="_blank" href="http://domaindrivendesign.org/discussion/messageboardarchive/Aggregates.html">http://domaindrivendesign.org/discussion/messageboardarchive/Aggregates.html</a>) is really useful, because a wise use can limit relations. The key concept is</p>
<blockquote>
<p>Allow external objects to hold references to root only.</p>
</blockquote>
<p>This means that no relation can be done between two objects that are no root, but another important thing is</p>
<blockquote>
<p>Transient references to the internal members can be passed out for use within a single operation only.</p>
</blockquote>
<p>Is the <i>root </i>that have complete control of the objects inside the aggregate, we create repositories only for Roots object limiting the way the user can access the domain. </p>
<p>Another concept that is of key importance is “<a target="_blank" href="http://domaindrivendesign.org/discussion/messageboardarchive/ValueObjects.html">Value objects</a>” . Lets first point out that in DDD terminology, the term “value object” <b>has nothing to do with the concept of .NET value object </b>they are completely different thing. A value object in DDD is an <em>object that has no identity and cannot live without a Entity object that contains it</em>. Here is the definition of Evans in Domain Driven Design</p>
<blockquote>
<p>An object that represents a descriptive aspect of the domain with no conceptual identity is called a VALUE OBJECT.</p>
</blockquote>
<p>Sometimes is not simple to understand if an object is a value or entity and this distinction mainly depends from the domain. Lets do an example with an hypothetical Address object like this</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:ff75183e-4562-4bea-b667-b579bcdc45d0" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #000000;">Street: Loc Piano Di Frassineta
Number: </span><span style="color: #800080;">1</span><span style="color: #000000;">
City: Sassoferrato</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>This is my address <img src='http://www.codewrecks.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> , but it is a value or an entity object? In a software for an E-Commerce we can agree that having an address without a customer, or supplier or some other entity is really not useful, so the address is a good candidate to be a Value Object. The situation can be different for a software that need to calculate ICI (ICI is a tax on house in Italy), in such a software an address can be an entity, because the address identify the entity that has to be taxed. The key question that makes us distinguish between value and entity object is “This object can have a lifecycle of his own?”.</p>
<p><em>A correct use of Value Objects leads to reduction of relations</em>, because relations from or to a Value Object is not permitted except from the object that contains it. Quite often the value object is stored in the same table as the owning object, it has no id and he share the lifecycle with containing object, so making a relation to it has really no sense. </p>
<p>Today Marco told me that he saw domains where we have object like Address, Street, Street Number, Zip code, all having bidirectional association between them. Rethinking such structure with Value objects greatly reduces problems. </p>
<p>Finally remember the <a target="_blank" href="http://en.wikipedia.org/wiki/You_Ain't_Gonna_Need_It">YAGNI</a>&nbsp; principle, avoid adding an association unless you really needs one and try to set up relations only when they have business meaning. I want to point out agaion that relations are not to be used for navigation because the purpose of retrieving object from the storage is duty of the repository. If we really want to access orders from customer object we can simply set up a method in Customer object called GetOrders(DateTime from, DateTime to) that internally access repository and gets order with a criteria.</p>
<p>Alk. </p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/Domain Driven Design" rel="tag">Domain Driven Design</a> <a target="_blank" href="http://technorati.com/tag/Value Object" rel="tag">Value Object</a> <a target="_blank" href="http://technorati.com/tag/Domain Relation" rel="tag">Domain Relation</a> </p>
<p><div class="wlWriterSmartContent" id="scid:C16BAC14-9A3D-4c50-9394-FBFEF7A93539:182388f2-d347-47a6-94f8-b453aae79f1d" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"><!--dotnetkickit--></div></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2008/07/23/relations-are-really-so-useful/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
