<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-4479279983457560946</id><updated>2011-11-23T03:16:43.524-08:00</updated><category term='Expression Trees'/><category term='LINQ'/><title type='text'>.Net Development</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://bernhard-richter.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4479279983457560946/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://bernhard-richter.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Bernhard Richter</name><uri>http://www.blogger.com/profile/12224724359174112000</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>12</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-4479279983457560946.post-9066537174445241218</id><published>2011-09-28T12:12:00.000-07:00</published><updated>2011-09-28T12:14:25.978-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Expression Trees'/><category scheme='http://www.blogger.com/atom/ns#' term='LINQ'/><title type='text'>Implementing Find and Replace for Expression trees</title><content type='html'>&lt;p&gt;   &lt;br /&gt;UPDATE September 2011&lt;/p&gt;  &lt;p&gt;The code described below in now available from NuGet that installs a single file (ExpressionExtensions.cs) into your project.&lt;/p&gt;  &lt;p&gt;In addition to search and replace, methods for expression composition (AND/OR) has been added.&lt;/p&gt;  &lt;p&gt;Get it from &lt;a href="http://nuget.org/List/Packages/ExpressionTools"&gt;NuGet&lt;/a&gt;…….PM&amp;gt; Install-Package ExpressionTools&lt;/p&gt;  &lt;p&gt;The source is now hosted at &lt;a href="https://github.com/seesharper/ExpressionTools/wiki/Documentation"&gt;GitHub&lt;/a&gt;&lt;/p&gt;  &lt;h4&gt;Introduction&lt;/h4&gt;  &lt;p&gt;Imagine that you are working with an &lt;em&gt;Expression&lt;/em&gt; tree and you need to check     &lt;br /&gt;if the tree contains a certain &lt;em&gt;Expression&lt;/em&gt; with certain property values. &lt;/p&gt;  &lt;p&gt;What we basically would need to do is to visit every node(&lt;em&gt;Expression&lt;/em&gt;) in the tree     &lt;br /&gt;and check to see if the &lt;em&gt;Expression&lt;/em&gt; matches what we are searching for. &lt;/p&gt;  &lt;p&gt;First of all we need a class that visits all the nodes in the tree.&lt;/p&gt;  &lt;h4&gt;&lt;/h4&gt;  &lt;h4&gt;The ExpressionVisitor&lt;/h4&gt;  &lt;p&gt;The ExpressionVisitor class that can be found in the MSDN documentation is sort of an    &lt;br /&gt;all purpose class when dealing with expressions. &lt;/p&gt;  &lt;p&gt;It was originally written by Matt Warren (the father of Linq to Sql) and was first published    &lt;br /&gt;as part of his &amp;quot;&lt;a href="http://blogs.msdn.com/mattwar/pages/linq-links.aspx"&gt;Building an IQueryable Provider&lt;/a&gt;&amp;quot; series &lt;/p&gt;  &lt;p&gt;The expression visitor was considered so essential for everyone working with expression trees,    &lt;br /&gt;so they finally included the source in the &lt;a href="http://msdn.microsoft.com/en-us/library/bb882521.aspx"&gt;MSDN documentation&lt;/a&gt;.     &lt;br /&gt;The implementation is really quite simple. It takes care of visiting all the nodes in the tree as well as it takes care of rewriting the tree if we make any changes to it. Great stuff!! &lt;/p&gt;  &lt;p&gt;So what we are aiming for here is some kind of method that returns a list of expressions    &lt;br /&gt;that matches any predicate defined for the expression (sub)tree.&lt;/p&gt;  &lt;h4&gt;Implementing Find&lt;/h4&gt;  &lt;p&gt;Extension methods are a great way of adding new features to existing types and in this case    &lt;br /&gt;we want to extend the Expression class.&lt;/p&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; Returns a list of &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;typeparamref name=&amp;quot;TExpression&amp;quot;/&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt; instances &lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; that matches the &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;paramref name=&amp;quot;predicate&amp;quot;/&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;.&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;typeparam name=&amp;quot;TExpression&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The type of &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref=&amp;quot;Expression&amp;quot;/&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt; &lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; to search for.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/typeparam&amp;gt;&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;param name=&amp;quot;expression&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref=&amp;quot;Expression&amp;quot;/&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt; that represents the sub tree for which to start searching.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;param name=&amp;quot;predicate&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref=&amp;quot;Func{T,TResult}&amp;quot;/&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt; used to filter the result&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;returns&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;A list of &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref=&amp;quot;Expression&amp;quot;/&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt; instances that matches the given predicate.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;TExpression&amp;gt; Find&amp;lt;TExpression&amp;gt;(&lt;span style="color: blue"&gt;this&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt; expression, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;TExpression, &lt;span style="color: blue"&gt;bool&lt;/span&gt;&amp;gt; predicate) &lt;span style="color: blue"&gt;where&lt;/span&gt; TExpression : &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;{&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; finder = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ExpressionFinder&lt;/span&gt;&amp;lt;TExpression&amp;gt;();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; finder.Find(expression, predicate);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;}&lt;/p&gt; &lt;/div&gt;  &lt;pre class="code"&gt;Given the following expression:&lt;/pre&gt;

&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;, &lt;span style="color: blue"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt; expression = (s) =&amp;gt; s.Length == 5;&lt;/pre&gt;

&lt;pre class="code"&gt;we can do a search in the expression tree by doing:&lt;/pre&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;binaryExpressions = expression.Find&amp;lt;&lt;span style="color: #2b91af"&gt;BinaryExpression&lt;/span&gt;&amp;gt;(b =&amp;gt; &lt;span style="color: blue"&gt;true&lt;/span&gt;);&lt;/pre&gt;

&lt;pre class="code"&gt;or vi could search for ConstantExpression like this:&lt;/pre&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;constantExpressions = expression.Find&amp;lt;&lt;span style="color: #2b91af"&gt;ConstantExpression&lt;/span&gt;&amp;gt;(c =&amp;gt; (&lt;span style="color: blue"&gt;int&lt;/span&gt;)c.Value == 5);&lt;/pre&gt;

&lt;pre class="code"&gt;&amp;#160;&lt;/pre&gt;

&lt;pre class="code"&gt;&amp;#160;&lt;/pre&gt;

&lt;p&gt;A top-down search is performed in the sub tree that the target expression represents, 
  &lt;br /&gt;meaning that we can choose to investigate the whole tree or just individual sub trees.&lt;/p&gt;

&lt;h4&gt;The ExpressionFinder&lt;/h4&gt;

&lt;p&gt;This is a very simple class that override the Visit method that is called for every node in the expression tree. &lt;/p&gt;

&lt;p&gt;If the expression being visited matches the given predicate, we add it to the list of matching expressions. &lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
/// &lt;/span&gt;&lt;span style="color: green"&gt;A class used to search for &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref=&amp;quot;Expression&amp;quot;/&amp;gt; &lt;/span&gt;&lt;span style="color: green"&gt;instances. 
&lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
/// &amp;lt;typeparam name=&amp;quot;TExpression&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The type of &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref=&amp;quot;Expression&amp;quot;/&amp;gt; &lt;/span&gt;&lt;span style="color: green"&gt;to search for.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/typeparam&amp;gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ExpressionFinder&lt;/span&gt;&amp;lt;TExpression&amp;gt; : &lt;span style="color: #2b91af"&gt;ExpressionVisitor &lt;/span&gt;&lt;span style="color: blue"&gt;where &lt;/span&gt;TExpression : &lt;span style="color: #2b91af"&gt;Expression
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;private readonly &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;TExpression&amp;gt; _result = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;TExpression&amp;gt;();
    &lt;span style="color: blue"&gt;private &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;TExpression, &lt;span style="color: blue"&gt;bool&lt;/span&gt;&amp;gt; _predicate;

    &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="color: green"&gt;Returns a list of &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;typeparamref name=&amp;quot;TExpression&amp;quot;/&amp;gt; &lt;/span&gt;&lt;span style="color: green"&gt;instances that matches the &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;paramref name=&amp;quot;predicate&amp;quot;/&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;.
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
    /// &amp;lt;param name=&amp;quot;expression&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref=&amp;quot;Expression&amp;quot;/&amp;gt; &lt;/span&gt;&lt;span style="color: green"&gt;that represents the sub tree for which to start searching.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/param&amp;gt;
    /// &amp;lt;param name=&amp;quot;predicate&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref=&amp;quot;Func{T,TResult}&amp;quot;/&amp;gt; &lt;/span&gt;&lt;span style="color: green"&gt;used to filter the result&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/param&amp;gt;
    /// &amp;lt;returns&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;A list of &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref=&amp;quot;Expression&amp;quot;/&amp;gt; &lt;/span&gt;&lt;span style="color: green"&gt;instances that matches the given predicate.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/returns&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;TExpression&amp;gt; Find(&lt;span style="color: #2b91af"&gt;Expression &lt;/span&gt;expression, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;TExpression, &lt;span style="color: blue"&gt;bool&lt;/span&gt;&amp;gt; predicate)
    {
        _result.Clear();
        _predicate = predicate;
        Visit(expression);
        &lt;span style="color: blue"&gt;return &lt;/span&gt;_result;
    }

    &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="color: green"&gt;Visits each node of the &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref=&amp;quot;Expression&amp;quot;/&amp;gt; &lt;/span&gt;&lt;span style="color: green"&gt;tree checks 
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;if the current expression matches the predicate.         
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
    /// &amp;lt;param name=&amp;quot;expression&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref=&amp;quot;Expression&amp;quot;/&amp;gt; &lt;/span&gt;&lt;span style="color: green"&gt;currently being visited.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/param&amp;gt;
    /// &amp;lt;returns&amp;gt;&amp;lt;see cref=&amp;quot;Expression&amp;quot;/&amp;gt;&amp;lt;/returns&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue"&gt;protected override &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Expression &lt;/span&gt;Visit(&lt;span style="color: #2b91af"&gt;Expression &lt;/span&gt;expression)
    {                        
        &lt;span style="color: blue"&gt;if &lt;/span&gt;(expression != &lt;span style="color: blue"&gt;null &lt;/span&gt;&amp;amp;&amp;amp; expression &lt;span style="color: blue"&gt;is &lt;/span&gt;TExpression)
        {
            &lt;span style="color: blue"&gt;if &lt;/span&gt;(_predicate((TExpression)expression))
                _result.Add((TExpression)expression);
        }

        &lt;span style="color: blue"&gt;return base&lt;/span&gt;.Visit(expression);
    }
}&lt;/pre&gt;

&lt;h4&gt;Implementing Replace&lt;/h4&gt;

&lt;p&gt;In addition to being able to search for expressions it would also be nice if we could do a simple replace using a similar syntax.&lt;/p&gt;

&lt;p&gt;Another extension method is called for:&lt;/p&gt;

&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;
  &lt;p style="margin: 0px"&gt;&lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;&lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; Searches for expressions using the given &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;paramref name=&amp;quot;predicate&amp;quot;/&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt; and replaces matches with &lt;/span&gt;&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;&lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; the result from the &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;paramref name=&amp;quot;replaceWith&amp;quot;/&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt; delegate.&lt;/span&gt;&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;&lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;&lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;typeparam name=&amp;quot;TExpression&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The type of &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref=&amp;quot;Expression&amp;quot;/&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt; to search for.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/typeparam&amp;gt;&lt;/span&gt;&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;&lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;param name=&amp;quot;expression&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref=&amp;quot;Expression&amp;quot;/&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt; that represents the sub tree &lt;/span&gt;&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;&lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; for which to start searching.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;&lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;param name=&amp;quot;predicate&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref=&amp;quot;Func{T,TResult}&amp;quot;/&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt; used to filter the result&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;&lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;param name=&amp;quot;replaceWith&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref=&amp;quot;Func{T,TResult}&amp;quot;/&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt; used to specify the replacement expression.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;&lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;returns&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The modified &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref=&amp;quot;Expression&amp;quot;/&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt; tree.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt; Replace&amp;lt;TExpression&amp;gt;(&lt;span style="color: blue"&gt;this&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt; expression, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;TExpression, &lt;span style="color: blue"&gt;bool&lt;/span&gt;&amp;gt; predicate, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;TExpression,&lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;&amp;gt; replaceWith) &lt;span style="color: blue"&gt;where&lt;/span&gt; TExpression : &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;{&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; replacer = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ExpressionReplacer&lt;/span&gt;&amp;lt;TExpression&amp;gt;();&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; replacer.Replace(expression, predicate, replaceWith);&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;}&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;We might think of the &lt;em&gt;Replace&lt;/em&gt; method as an ad-hoc alternative to modifying the expression tree using an &lt;em&gt;ExpressionVisitor&lt;/em&gt; derivate. &lt;/p&gt;

&lt;p&gt;Given the following expression:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;, &lt;span style="color: blue"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt; expression = (s) =&amp;gt; s.Length == 5;&lt;/pre&gt;
we want to modify the expression tree by replacing the &lt;em&gt;BinaryExpression&lt;/em&gt;(Equal) with a &lt;em&gt;BinaryExpression&lt;/em&gt;(NotEqual). 

&lt;p&gt;This can now be achieved very easily by doing:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;modifiedExpression = expression.Replace&amp;lt;&lt;span style="color: #2b91af"&gt;BinaryExpression&lt;/span&gt;&amp;gt;(b =&amp;gt; &lt;span style="color: blue"&gt;true&lt;/span&gt;, 
    (b) =&amp;gt; &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.NotEqual(b.Left, b.Right));&lt;/pre&gt;

&lt;h4&gt;The ExpressionReplacer&lt;/h4&gt;

&lt;p&gt;Similar to the &lt;em&gt;ExpressionFinder&lt;/em&gt; it filter outs the target expressions, but instead of returning the matching expressions, it replaces the 

  &lt;br /&gt;matching expression with whatever the &lt;em&gt;Func&amp;lt;TExpression,Expression&amp;gt;&lt;/em&gt; delegate returns. &lt;/p&gt;

&lt;p&gt;One thing to notice here is that matching expression is passed to the &lt;em&gt;replaceWith&lt;/em&gt; delegate.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
/// &lt;/span&gt;&lt;span style="color: green"&gt;A class that is capable of doing a find and replace in an &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref=&amp;quot;Expression&amp;quot;/&amp;gt; &lt;/span&gt;&lt;span style="color: green"&gt;tree.
&lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
/// &amp;lt;typeparam name=&amp;quot;TExpression&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The type of &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref=&amp;quot;Expression&amp;quot;/&amp;gt; &lt;/span&gt;&lt;span style="color: green"&gt;to find and replace.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/typeparam&amp;gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ExpressionReplacer&lt;/span&gt;&amp;lt;TExpression&amp;gt; : &lt;span style="color: #2b91af"&gt;ExpressionVisitor &lt;/span&gt;&lt;span style="color: blue"&gt;where &lt;/span&gt;TExpression : &lt;span style="color: #2b91af"&gt;Expression
&lt;/span&gt;{
    
    &lt;span style="color: blue"&gt;private &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;TExpression, &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;&amp;gt; _replaceWith;
    &lt;span style="color: blue"&gt;private &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;TExpression, &lt;span style="color: blue"&gt;bool&lt;/span&gt;&amp;gt; _predicate;
    
    
    &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="color: green"&gt;Searches for expressions using the given &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;paramref name=&amp;quot;predicate&amp;quot;/&amp;gt; &lt;/span&gt;&lt;span style="color: green"&gt;and 
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;replaces matches with the result from the &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;paramref name=&amp;quot;replaceWith&amp;quot;/&amp;gt; &lt;/span&gt;&lt;span style="color: green"&gt;delegate.          
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
    /// &amp;lt;param name=&amp;quot;expression&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref=&amp;quot;Expression&amp;quot;/&amp;gt; &lt;/span&gt;&lt;span style="color: green"&gt;that 
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;represents the sub tree for which to start searching.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/param&amp;gt;
    /// &amp;lt;param name=&amp;quot;predicate&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref=&amp;quot;Func{T,TResult}&amp;quot;/&amp;gt; &lt;/span&gt;&lt;span style="color: green"&gt;used to filter the result&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/param&amp;gt;
    /// &amp;lt;param name=&amp;quot;replaceWith&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref=&amp;quot;Func{T,TResult}&amp;quot;/&amp;gt; 
    /// &lt;/span&gt;&lt;span style="color: green"&gt;used to specify the replacement expression.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/param&amp;gt;
    /// &amp;lt;returns&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The modified &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref=&amp;quot;Expression&amp;quot;/&amp;gt; &lt;/span&gt;&lt;span style="color: green"&gt;tree.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/returns&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Expression &lt;/span&gt;Replace(&lt;span style="color: #2b91af"&gt;Expression &lt;/span&gt;expression, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;TExpression, &lt;span style="color: blue"&gt;bool&lt;/span&gt;&amp;gt; predicate, 
        &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;TExpression, &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;&amp;gt; replaceWith)
    {
        _replaceWith = replaceWith;
        _predicate = predicate;
        &lt;span style="color: blue"&gt;return &lt;/span&gt;Visit(expression);
    }

    &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="color: green"&gt;Visits each node of the &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref=&amp;quot;Expression&amp;quot;/&amp;gt; &lt;/span&gt;&lt;span style="color: green"&gt;tree checks 
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;if the current expression matches the predicate. If a match is found 
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;the expression will be replaced.        
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
    /// &amp;lt;param name=&amp;quot;expression&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref=&amp;quot;Expression&amp;quot;/&amp;gt; &lt;/span&gt;&lt;span style="color: green"&gt;currently being visited.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/param&amp;gt;
    /// &amp;lt;returns&amp;gt;&amp;lt;see cref=&amp;quot;Expression&amp;quot;/&amp;gt;&amp;lt;/returns&amp;gt;        
    &lt;/span&gt;&lt;span style="color: blue"&gt;protected override &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Expression &lt;/span&gt;Visit(&lt;span style="color: #2b91af"&gt;Expression &lt;/span&gt;expression)
    {
        &lt;span style="color: blue"&gt;if &lt;/span&gt;(expression != &lt;span style="color: blue"&gt;null &lt;/span&gt;&amp;amp;&amp;amp; expression &lt;span style="color: blue"&gt;is &lt;/span&gt;TExpression)
        {
            &lt;span style="color: blue"&gt;if &lt;/span&gt;(_predicate((TExpression)expression))
                &lt;span style="color: blue"&gt;return &lt;/span&gt;_replaceWith((TExpression)expression);
        }
        &lt;span style="color: blue"&gt;return base&lt;/span&gt;.Visit(expression);
    }
}&lt;/pre&gt;

&lt;pre class="code"&gt;&amp;#160;&lt;/pre&gt;

&lt;pre class="code"&gt;&amp;#160;&lt;/pre&gt;

&lt;p&gt;Enjoy!!!&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4479279983457560946-9066537174445241218?l=bernhard-richter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bernhard-richter.blogspot.com/feeds/9066537174445241218/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4479279983457560946&amp;postID=9066537174445241218' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4479279983457560946/posts/default/9066537174445241218'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4479279983457560946/posts/default/9066537174445241218'/><link rel='alternate' type='text/html' href='http://bernhard-richter.blogspot.com/2010/02/implementing-find-and-replace-for.html' title='Implementing Find and Replace for Expression trees'/><author><name>Bernhard Richter</name><uri>http://www.blogger.com/profile/12224724359174112000</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4479279983457560946.post-922658358241920996</id><published>2011-09-06T07:55:00.001-07:00</published><updated>2011-09-06T07:55:35.617-07:00</updated><title type='text'>Spellchecking in Visual Studio 2010</title><content type='html'>&lt;p&gt;Visual Studio 2010 has no built in support for spell checking, but there is an extension available that fixes this.&lt;/p&gt;  &lt;p&gt;This amazing extension written by Roman Golovin and Michael Lehenl checks the spelling in your code comments as well as in html files.&lt;/p&gt;  &lt;p&gt;As I have understood it takes advantage of the spell checking support in WPF and uses this functionality to implement the extension.&lt;/p&gt;  &lt;p&gt;This all works fine if you’re located in the US or any other region that has a supported language.&lt;/p&gt;  &lt;p&gt;The extension sets up the language based on the locale on your machine so if you’re outside one of English speaking regions, you will not be able to do spell checking in English.&lt;/p&gt;  &lt;p&gt;But there is a way around this.&lt;/p&gt;  &lt;p&gt;The code for this extension is available from GitHub and we only need to make one tiny little change in the source code to make it work.&lt;/p&gt;  &lt;p&gt;Here is what to do.&lt;/p&gt;  &lt;p&gt;Download the &lt;a href="https://nodeload.github.com/NoahRic/Spellchecker/zipball/master"&gt;code&lt;/a&gt; from GitHub and locate the SpellingTagger class.&lt;/p&gt;  &lt;p&gt;In the CheckSpellings method we need to add one line of code to make it default to “en&amp;quot;-US”.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;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: &amp;#39;Courier New&amp;#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"&gt;   &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;     &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;TextBox textBox = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; TextBox();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;textBox.SpellCheck.IsEnabled = &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;            &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #008000"&gt;//Added to force english US language &lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"&gt;textBox.Language = XmlLanguage.GetLanguage(&lt;span style="color: #006080"&gt;&amp;quot;en-US&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Compile the solution and install the extension by executing the SpellChecker.vsix file located in the Bin folder.&lt;/p&gt;

&lt;p&gt;Now we can all have spell checking in Visual Studio 2010&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Enjoy!!!&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4479279983457560946-922658358241920996?l=bernhard-richter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bernhard-richter.blogspot.com/feeds/922658358241920996/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4479279983457560946&amp;postID=922658358241920996' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4479279983457560946/posts/default/922658358241920996'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4479279983457560946/posts/default/922658358241920996'/><link rel='alternate' type='text/html' href='http://bernhard-richter.blogspot.com/2011/09/spellchecking-in-visual-studio-2010.html' title='Spellchecking in Visual Studio 2010'/><author><name>Bernhard Richter</name><uri>http://www.blogger.com/profile/12224724359174112000</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4479279983457560946.post-8173924881709617661</id><published>2011-08-17T00:11:00.001-07:00</published><updated>2011-08-17T00:11:02.491-07:00</updated><title type='text'>Using ORDERBY in derived tables</title><content type='html'>&lt;p&gt;In Microsoft SQL Server you are not permitted to use ORDERBY in a derived table unless you also provide a TOP(n) constraint.&lt;/p&gt;  &lt;p&gt;Meaning that the following SQL is invalid&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:67d02abc-5001-41f0-9c40-1d776d75e7fa" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; max-height: 300px; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;SELECT&lt;/span&gt; &lt;span style="color:#808080"&gt;*&lt;/span&gt; &lt;span style="color:#0000ff"&gt;FROM &lt;/span&gt;&lt;span style="color:#808080"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff"&gt;SELECT&lt;/span&gt; EmployeeID &lt;span style="color:#0000ff"&gt;FROM&lt;/span&gt; Employees &lt;span style="color:#0000ff"&gt;ORDER&lt;/span&gt; &lt;span style="color:#0000ff"&gt;BY&lt;/span&gt; EmployeeID &lt;span style="color:#0000ff"&gt;DESC&lt;/span&gt;&lt;span style="color:#808080"&gt;)&lt;/span&gt; &lt;span style="color:#0000ff"&gt;AS&lt;/span&gt; e&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;This will throw an exception from SQL Server&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c0b9060d-e297-4e81-97fa-e80de30c7691" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; max-height: 300px; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px;"&gt; &lt;li&gt;The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;Based on the error message it is pretty obvious how to fix this.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:7e32defa-cc8f-4eb8-87c9-361415e9654f" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; max-height: 300px; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;SELECT&lt;/span&gt; &lt;span style="color:#808080"&gt;*&lt;/span&gt; &lt;span style="color:#0000ff"&gt;FROM &lt;/span&gt;&lt;span style="color:#808080"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff"&gt;SELECT&lt;/span&gt; &lt;span style="color:#0000ff"&gt;TOP&lt;/span&gt; 5 EmployeeID &lt;span style="color:#0000ff"&gt;FROM&lt;/span&gt; Employees &lt;span style="color:#0000ff"&gt;ORDER&lt;/span&gt; &lt;span style="color:#0000ff"&gt;BY&lt;/span&gt; EmployeeID &lt;span style="color:#0000ff"&gt;DESC&lt;/span&gt;&lt;span style="color:#808080"&gt;)&lt;/span&gt; &lt;span style="color:#0000ff"&gt;AS&lt;/span&gt; e&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;Now the query runs just fine and just top make sure that the sorting actually works we can return the result bottom-up.&lt;/p&gt;  &lt;p&gt;EmployeeID    &lt;br /&gt;-----------     &lt;br /&gt;10     &lt;br /&gt;9     &lt;br /&gt;8     &lt;br /&gt;7     &lt;br /&gt;6&lt;/p&gt;  &lt;p&gt;(5 row(s) affected)&lt;/p&gt;  &lt;p&gt;But what if we want to return all rows and still apply sorting?&lt;/p&gt;  &lt;p&gt;Many forum posts suggests the following approach.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:1c9843da-68be-400b-8c9a-a4712ae8f74e" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; max-height: 300px; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;SELECT&lt;/span&gt; &lt;span style="color:#808080"&gt;*&lt;/span&gt; &lt;span style="color:#0000ff"&gt;FROM &lt;/span&gt;&lt;span style="color:#808080"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff"&gt;SELECT&lt;/span&gt; &lt;span style="color:#0000ff"&gt;TOP&lt;/span&gt; 100 &lt;span style="color:#0000ff"&gt;PERCENT&lt;/span&gt; EmployeeID &lt;span style="color:#0000ff"&gt;FROM&lt;/span&gt; Employees &lt;span style="color:#0000ff"&gt;ORDER&lt;/span&gt; &lt;span style="color:#0000ff"&gt;BY&lt;/span&gt; EmployeeID &lt;span style="color:#0000ff"&gt;DESC&lt;/span&gt;&lt;span style="color:#808080"&gt;)&lt;/span&gt; &lt;span style="color:#0000ff"&gt;AS&lt;/span&gt; e&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;The query still executes but the sorting is now completely omitted.&lt;/p&gt;  &lt;p&gt;EmployeeID    &lt;br /&gt;-----------     &lt;br /&gt;10     &lt;br /&gt;3     &lt;br /&gt;4     &lt;br /&gt;8     &lt;br /&gt;1     &lt;br /&gt;2     &lt;br /&gt;6     &lt;br /&gt;7     &lt;br /&gt;5     &lt;br /&gt;9     &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;The the solution to this is to provide a number to the TOP (N) clause that represents all the rows from the table.&lt;/p&gt;  &lt;p&gt;The argument is a BIGINT and the highest possible value is 9223372036854775807.&lt;/p&gt;  &lt;p&gt;Now if we change the query into this:&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:89921883-d9fd-4cf9-9cbe-9619ad2aafc1" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; max-height: 300px; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;SELECT&lt;/span&gt; &lt;span style="color:#808080"&gt;*&lt;/span&gt; &lt;span style="color:#0000ff"&gt;FROM &lt;/span&gt;&lt;span style="color:#808080"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff"&gt;SELECT&lt;/span&gt; &lt;span style="color:#0000ff"&gt;TOP&lt;/span&gt; 9223372036854775807 EmployeeID &lt;span style="color:#0000ff"&gt;FROM&lt;/span&gt; Employees &lt;span style="color:#0000ff"&gt;ORDER&lt;/span&gt; &lt;span style="color:#0000ff"&gt;BY&lt;/span&gt; EmployeeID &lt;span style="color:#0000ff"&gt;DESC&lt;/span&gt;&lt;span style="color:#808080"&gt;)&lt;/span&gt; &lt;span style="color:#0000ff"&gt;AS&lt;/span&gt; e&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;We can now see that the result comes back as expected sorted by EmployeeID.&lt;/p&gt;  &lt;p&gt;EmployeeID    &lt;br /&gt;-----------     &lt;br /&gt;10     &lt;br /&gt;9     &lt;br /&gt;8     &lt;br /&gt;7     &lt;br /&gt;6     &lt;br /&gt;5     &lt;br /&gt;4     &lt;br /&gt;3     &lt;br /&gt;2     &lt;br /&gt;1     &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4479279983457560946-8173924881709617661?l=bernhard-richter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bernhard-richter.blogspot.com/feeds/8173924881709617661/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4479279983457560946&amp;postID=8173924881709617661' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4479279983457560946/posts/default/8173924881709617661'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4479279983457560946/posts/default/8173924881709617661'/><link rel='alternate' type='text/html' href='http://bernhard-richter.blogspot.com/2011/08/using-orderby-in-derived-tables.html' title='Using ORDERBY in derived tables'/><author><name>Bernhard Richter</name><uri>http://www.blogger.com/profile/12224724359174112000</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4479279983457560946.post-4815181492114116851</id><published>2011-01-13T05:34:00.001-08:00</published><updated>2011-01-13T05:34:38.207-08:00</updated><title type='text'>IL Merge and the 4.0 runtime</title><content type='html'>&lt;p&gt;If you are trying to ILMerge 4.0 assemblies make sure that you do the following:&lt;/p&gt;  &lt;p&gt;Do NOT specify the /targetplatform:v4 as a command line parameter to ILMerge.exe&lt;/p&gt;  &lt;p&gt;Create a file called ILMerge.exe.config and enter the following lines&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="html"&gt;xml&lt;/span&gt; &lt;span class="attr"&gt;version&lt;/span&gt; &lt;span class="kwrd"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt;?&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;configuration&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;startup&lt;/span&gt; &lt;span class="attr"&gt;useLegacyV2RuntimeActivationPolicy&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;requiredRuntime&lt;/span&gt; &lt;span class="attr"&gt;safemode&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;imageVersion&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;v4.0.30319&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;version&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;v4.0.30319&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;startup&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;configuration&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;That’s it!!!&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4479279983457560946-4815181492114116851?l=bernhard-richter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bernhard-richter.blogspot.com/feeds/4815181492114116851/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4479279983457560946&amp;postID=4815181492114116851' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4479279983457560946/posts/default/4815181492114116851'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4479279983457560946/posts/default/4815181492114116851'/><link rel='alternate' type='text/html' href='http://bernhard-richter.blogspot.com/2011/01/il-merge-and-40-runtime.html' title='IL Merge and the 4.0 runtime'/><author><name>Bernhard Richter</name><uri>http://www.blogger.com/profile/12224724359174112000</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4479279983457560946.post-4690424717815872057</id><published>2010-09-22T04:22:00.001-07:00</published><updated>2010-09-22T04:22:41.478-07:00</updated><title type='text'>ServiceLocator vs Dependency Injection</title><content type='html'>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;During the past weeks I have been trying to figure out if I should go with the DI(Dependency Injection) pattern or the SL (Service Locator) pattern.    &lt;br /&gt;I assume here that the reader know the difference between the two.&lt;/p&gt;  &lt;p&gt;I have been using the SL pattern for years know and it has proved to be very valuable. &lt;/p&gt;  &lt;p&gt;But guess what? Now they tell me that it is considered an anti-pattern. Have a look &lt;a href="http://blog.ploeh.dk/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx"&gt;here&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;What really bugs me is that all DI examples including the example from Mark Seemann, are so naive and simple.&lt;/p&gt;  &lt;p&gt;Sure if you have InterfaceA and InterfaceB, you can live with passing an implementation of InterfaceB to the constructor of&amp;#160; ClassA.&lt;/p&gt;  &lt;p&gt;But graphs usually gets a little more complex that and if we follow the Soc principle, we might end up with a lot of dependencies in order for ClassA to actually do its magic.&lt;/p&gt;  &lt;p&gt;The result of this is that the consumer of ClassA must manually wire up a dependency map before calling the constructor passing the root dependencies for ClassA.&lt;/p&gt;  &lt;p&gt;To illustrate this we are going to use Mark’s example.&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div class="csharpcode"&gt;   &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; IOrderProcessor&lt;/pre&gt;

  &lt;pre&gt;    {&lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;void&lt;/span&gt; Process(Order order);&lt;/pre&gt;

  &lt;pre&gt;    }&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;









.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; OrderProcessor&lt;/pre&gt;

  &lt;pre&gt;    {&lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; IOrderValidator _orderValidator;&lt;/pre&gt;

  &lt;pre&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; OrderProcessor(IOrderValidator orderValidator)&lt;/pre&gt;

  &lt;pre&gt;        {&lt;/pre&gt;

  &lt;pre class="alt"&gt;            _orderValidator = orderValidator;&lt;/pre&gt;

  &lt;pre&gt;        }&lt;/pre&gt;

  &lt;pre class="alt"&gt;    }&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;







.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;In order to use the order processor class we need to do the following:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;OrderProcessor orderProcessor = &lt;span class="kwrd"&gt;new&lt;/span&gt; OrderProcessor(&lt;span class="kwrd"&gt;new&lt;/span&gt; OrderValidator());&lt;/pre&gt;
&lt;style type="text/css"&gt;






.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;That does not look to bad at all.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;OrderProcessor&lt;/strong&gt; clearly states that it has a dependency upon an &lt;strong&gt;IOrderValidator &lt;/strong&gt;implementation.&lt;/p&gt;

&lt;p&gt;The question is… Do I really need to know about this dependencies? Let that rest for a minute while we expand the example.&lt;/p&gt;

&lt;p&gt;Lets just quickly review the example from Mark.&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Process(Order order)&lt;/pre&gt;

  &lt;pre&gt;{&lt;/pre&gt;

  &lt;pre class="alt"&gt;    var validator = Locator.Resolve&amp;lt;IOrderValidator&amp;gt;();&lt;/pre&gt;

  &lt;pre&gt;    &lt;span class="kwrd"&gt;if&lt;/span&gt; (validator.Validate(order))&lt;/pre&gt;

  &lt;pre class="alt"&gt;    {&lt;/pre&gt;

  &lt;pre&gt;        var collector = Locator.Resolve&amp;lt;IOrderCollector&amp;gt;();&lt;/pre&gt;

  &lt;pre class="alt"&gt;        collector.Collect(order);&lt;/pre&gt;

  &lt;pre&gt;        var shipper = Locator.Resolve&amp;lt;IOrderShipper&amp;gt;();&lt;/pre&gt;

  &lt;pre class="alt"&gt;        shipper.Ship(order);&lt;/pre&gt;

  &lt;pre&gt;    }&lt;/pre&gt;

  &lt;pre class="alt"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;style type="text/css"&gt;



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;He only shows the SL variant of the example and I can’t help but wonder why?&lt;/p&gt;

&lt;p&gt;Anyway, we see from the implementation that two more dependencies have been added to the &lt;strong&gt;OrderProcessor.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;An interesting observation here is that the dependencies are only needed if the order is valid…….&lt;/p&gt;

&lt;p&gt;But let’s stick with the DI approach and then we must add two more parameters to the OrderProcessor class.&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; OrderProcessor&lt;/pre&gt;

  &lt;pre&gt;    {&lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; IOrderValidator _orderValidator;&lt;/pre&gt;

  &lt;pre&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; OrderProcessor(IOrderValidator orderValidator, IOrderCollector orderCollector, IOrderShipper orderShipper)&lt;/pre&gt;

  &lt;pre&gt;        {&lt;/pre&gt;

  &lt;pre class="alt"&gt;            _orderValidator = orderValidator;&lt;/pre&gt;

  &lt;pre&gt;        }&lt;/pre&gt;

  &lt;pre class="alt"&gt;    }&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;style type="text/css"&gt;



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;To use the OrderProcessor we now need to do this:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;OrderProcessor orderProcessor = &lt;span class="kwrd"&gt;new&lt;/span&gt; OrderProcessor(&lt;span class="kwrd"&gt;new&lt;/span&gt; OrderValidator(), &lt;span class="kwrd"&gt;new&lt;/span&gt; OrderCollector(), &lt;span class="kwrd"&gt;new&lt;/span&gt; OrderShipper());&lt;/pre&gt;
&lt;style type="text/css"&gt;



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;In addition to introducing a breaking change this also starts to look a little bit messy, wouldn’t you say?&lt;/p&gt;

&lt;p&gt;We can also see here that we create &lt;strong&gt;OrderCollectior&lt;/strong&gt; and &lt;strong&gt;OrderShipper&lt;/strong&gt; instances regardless of the result from &lt;em&gt;validator.Validate(order)&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Lets take this even further by adding some logging capabilities.&lt;/p&gt;

&lt;p&gt;All classes should have logging enabled and that means even more breaking changes. &lt;/p&gt;

&lt;pre class="csharpcode"&gt;ILogger logger = &lt;span class="kwrd"&gt;new&lt;/span&gt; Logger();
OrderProcessor orderProcessor = &lt;span class="kwrd"&gt;new&lt;/span&gt; OrderProcessor(&lt;span class="kwrd"&gt;new&lt;/span&gt; OrderValidator(logger), &lt;span class="kwrd"&gt;new&lt;/span&gt; OrderCollector(logger), &lt;span class="kwrd"&gt;new&lt;/span&gt; OrderShipper(logger));&lt;/pre&gt;

&lt;p&gt;I don’t know about you, but to me this starts to look a little complicated.&lt;/p&gt;

&lt;p&gt;We might think of a solution where we let the &lt;strong&gt;OrderProcessor &lt;/strong&gt;class create its own &lt;strong&gt;Logger, &lt;/strong&gt;but that would be hiding a dependency, right? &lt;/p&gt;

&lt;p&gt;Let’s get back to the question ..”Do I really need to know about this dependencies?”&lt;/p&gt;

&lt;p&gt;I tell you what.. I just need to process orders and don’t really care that much how much magic is needed to actually do that.&lt;/p&gt;

&lt;p&gt;If we review the actual contract (&lt;strong&gt;IOrderProcessor&lt;/strong&gt;) involved to process orders it does not say anything about collectors and shippers.&lt;/p&gt;

&lt;p&gt;So why should I care?&lt;/p&gt;

&lt;p&gt;Do I really need to inject an IEngine instance before I can drive?&lt;/p&gt;

&lt;p&gt;Yes, I would have to if I was the constructor of the car, but in this case I am just the consumer, see?&lt;/p&gt;

&lt;p&gt;Remember Cole Trickle (Tom Cruise) from “Days of Thunder” .. (Yes, I’m actually that old)&lt;/p&gt;

&lt;p&gt;&amp;quot;&lt;em&gt;They told me to get in the car and drive&lt;/em&gt; and I could drive......but I don't know much about cars, o.k?!&lt;/p&gt;

&lt;p&gt;What if the engine suddenly got equipped with a turbo? &lt;/p&gt;

&lt;p&gt;So what? I DON’T CARE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! I just want to drive!!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To solve this nightmare of manual wiring we can use an Ioc container.&lt;/p&gt;

&lt;p&gt;Given that the container is configured it will happily resolve all dependencies for us.&lt;/p&gt;

&lt;p&gt;The configuration of the container remains pretty much the same whether we use DI or SL so that is not an argument.&lt;/p&gt;

&lt;p&gt;I feel that DI is somewhat of a “all or nothing” solution. &lt;/p&gt;

&lt;p&gt;In order for the container to resolve our dependencies we must use this pattern from the get go. Otherwise we are faced with manual dependency injection which I think we all can agree can become pretty messy.&lt;/p&gt;

&lt;p&gt;Feel free to leave a comment, I would love to hear your opinion&lt;/p&gt;

&lt;p&gt;Regards&lt;/p&gt;

&lt;p&gt;Bernhard Richter&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;style type="text/css"&gt;



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4479279983457560946-4690424717815872057?l=bernhard-richter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bernhard-richter.blogspot.com/feeds/4690424717815872057/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4479279983457560946&amp;postID=4690424717815872057' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4479279983457560946/posts/default/4690424717815872057'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4479279983457560946/posts/default/4690424717815872057'/><link rel='alternate' type='text/html' href='http://bernhard-richter.blogspot.com/2010/09/servicelocator-vs-dependency-injection.html' title='ServiceLocator vs Dependency Injection'/><author><name>Bernhard Richter</name><uri>http://www.blogger.com/profile/12224724359174112000</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4479279983457560946.post-6458451107576905772</id><published>2010-07-16T05:44:00.001-07:00</published><updated>2010-07-16T05:44:46.166-07:00</updated><title type='text'>Merging(ILMerge) assemblies targeting the .Net Runtime v4.0</title><content type='html'>&lt;p&gt;   &lt;br /&gt;Today I started the process of upgrading a lot of projects to Visual Studio 2010 and at the same time I set the target framework to 4.0.&lt;/p&gt;  &lt;p&gt;Everything went relatively smooth except for the some projects where we have modified the msbuild file to include a call to &lt;a href="http://research.microsoft.com/en-us/people/mbarnett/ilmerge.aspx"&gt;ILMerge&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;First it complained it could not resolve the reference to System.Core &lt;/p&gt;  &lt;pre class="code"&gt;Unresolved assembly reference not allowed: System.Core.&lt;/pre&gt;

&lt;p&gt;As it turned out I needed to tell ILMerge what framework version to use and where it is located.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://research.microsoft.com/en-us/people/mbarnett/ilmerge.aspx"&gt;Mike Barnet&lt;/a&gt; has mentioned this at Microsoft Research and the solution is to add a command line parameter like this: /targetplatform:v4,&amp;lt;path to your v4 framework directory&amp;gt;&amp;#160; &lt;/p&gt;

&lt;p&gt;So where is the v4 framework directory?&lt;/p&gt;

&lt;p&gt;It is located in “C:\Windows\Microsoft.NET\Framework\v4.0.30319” provided you have a default installation.&lt;/p&gt;

&lt;p&gt;So I added the command line parameter like this&lt;/p&gt;

&lt;p&gt;/targetplatform:v4“C:\Windows\Microsoft.NET\Framework\v4.0.30319”&lt;/p&gt;

&lt;p&gt;Make sure that you don’t have a space between the version and the path. It actually caused a stack overflow on my machine :)&lt;/p&gt;

&lt;p&gt;Anyway this worked very well and no more complaints about unresolved assemblies.&lt;/p&gt;

&lt;p&gt;That was until I tried to merge an assembly that referenced the PresentationCore.dll.&lt;/p&gt;

&lt;p&gt;Unresolved assembly reference..again.&lt;/p&gt;

&lt;p&gt;What now?&lt;/p&gt;

&lt;p&gt;After some investigation I found out that PresentationCore.dll is not located in “C:\Windows\Microsoft.NET\Framework\v4.0.30319”, but in a WPF subfolder.&lt;/p&gt;

&lt;p&gt;Aha, that must be it.&lt;/p&gt;

&lt;p&gt;The trick here is that the path to the framework should be this 
  &lt;br /&gt;“C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0”&lt;/p&gt;

&lt;p&gt;This directory contains all the framework assemblies including PresentationCore.dll&lt;/p&gt;

&lt;p&gt;So I changed the command line parameter to &lt;/p&gt;

&lt;p&gt;/targetplatform:v4“C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0”&lt;/p&gt;

&lt;p&gt;Suddenly all merging of assemblies succeeded.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4479279983457560946-6458451107576905772?l=bernhard-richter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bernhard-richter.blogspot.com/feeds/6458451107576905772/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4479279983457560946&amp;postID=6458451107576905772' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4479279983457560946/posts/default/6458451107576905772'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4479279983457560946/posts/default/6458451107576905772'/><link rel='alternate' type='text/html' href='http://bernhard-richter.blogspot.com/2010/07/mergingilmerge-assemblies-targeting-net.html' title='Merging(ILMerge) assemblies targeting the .Net Runtime v4.0'/><author><name>Bernhard Richter</name><uri>http://www.blogger.com/profile/12224724359174112000</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4479279983457560946.post-61651789513542656</id><published>2010-07-15T03:51:00.001-07:00</published><updated>2010-07-15T05:13:31.813-07:00</updated><title type='text'>Adding mocking features to LinFu.Ioc</title><content type='html'>&lt;p&gt;This post will show how to integrate a mocking library with the service container available in the &lt;a href="http://github.com/philiplaureano/LinFu"&gt;LinFu&lt;/a&gt; framework.&lt;br&gt;&lt;font size="2"&gt;We are going to kick this off by showing an example where the use of mock objects might be useful.&lt;/font&gt;&lt;/p&gt; &lt;p&gt;Imagine that we have two interfaces like the ones below.&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IServiceA
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;int &lt;/span&gt;CalculateSomething();
}


&lt;span style="color: blue"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IServiceB
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;int &lt;/span&gt;CalculateSomethingElse();
}
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;If we take a look at the implementation below we can see that &lt;strong&gt;ServiceA&lt;/strong&gt; has a dependency to an &lt;strong&gt;IServiceB&lt;/strong&gt; implementation.&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ServiceA &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;IServiceA
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;private &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IServiceB &lt;/span&gt;_serviceB;
    &lt;span style="color: blue"&gt;public &lt;/span&gt;ServiceA(&lt;span style="color: #2b91af"&gt;IServiceB &lt;/span&gt;serviceB) 
    {
        _serviceB = serviceB;
    }

    &lt;span style="color: blue"&gt;public int &lt;/span&gt;CalculateSomething()
    {
        &lt;span style="color: green"&gt;//Do something clever here....
        &lt;/span&gt;_serviceA.CalculateSomethingElse();
        &lt;span style="color: green"&gt;//Do even more intelligent suff...
        &lt;/span&gt;&lt;span style="color: blue"&gt;return &lt;/span&gt;42;
    }
}
&lt;/pre&gt;
&lt;p&gt;It should be noted that dependency injection can be done in a variety of ways, but here it is done using constructor injection.&lt;/p&gt;
&lt;p&gt;This means that when we request an instance of &lt;strong&gt;IServiceA&lt;/strong&gt; the service container will also resolve the &lt;strong&gt;IServiceB&lt;/strong&gt; implementation needed in the &lt;strong&gt;ServiceA&lt;/strong&gt; constructor.&lt;/p&gt;
&lt;p&gt;When writing our unit test for &lt;strong&gt;ServiceA&lt;/strong&gt;, we might not be interested in whatever &lt;strong&gt;IServiceB&lt;/strong&gt; does or even worse, it could represents a remote call not accessible to us when testing &lt;strong&gt;IServiceA&lt;/strong&gt;. So we need to replace the default &lt;strong&gt;IServiceB&lt;/strong&gt; implementation with a dummy implementation. &lt;/p&gt;
&lt;p&gt;It is obvious how this can be done the manual way since this is such a naive example, but setting up mocking objects can be pretty complex if we need to install multiple objects.&lt;/p&gt;
&lt;p&gt;It can also be the case that the dependency is created internally so that we do not have an easy way of replacing the implementation with a mocking instance.&lt;/p&gt;
&lt;p&gt;So we need a way to tell the service container that whenever a request for a &lt;strong&gt;IServiceB&lt;/strong&gt; instance is executed, it should return a mock instance.&lt;/p&gt;
&lt;p&gt;It should also be noted here that I have used the &lt;a href="http://code.google.com/p/moq/"&gt;Moq&lt;/a&gt; framework, but I guess this could very easily be adapted to other mocking frameworks as well. &lt;/p&gt;
&lt;p&gt;Anyway, this is what I came up with:&lt;/p&gt;&lt;pre class="code"&gt;ServiceContainer.StartMocking&amp;lt;&lt;span style="color: #2b91af"&gt;IServiceB&lt;/span&gt;&amp;gt;();
&lt;span style="color: blue"&gt;var &lt;/span&gt;serviceA = ServiceContainer.GetService&amp;lt;&lt;span style="color: #2b91af"&gt;IServiceA&lt;/span&gt;&amp;gt;();
&lt;span style="color: blue"&gt;var &lt;/span&gt;result = serviceA.CalculateSomething();
&lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual(42,result);&lt;/pre&gt;
&lt;p&gt;We can see that creating a mock and configuring the service container is pretty simple.&lt;/p&gt;
&lt;p&gt;ServiceContainer.StartMocking&amp;lt;&lt;span style="color: #2b91af"&gt;IServiceB&lt;/span&gt;&amp;gt;(); &lt;/p&gt;
&lt;p&gt;This is all that we really need to do in order to replace the actual &lt;strong&gt;IServiceB&lt;/strong&gt; implementation.&lt;/p&gt;
&lt;p&gt;Buy what about those expectations you say?….&lt;/p&gt;
&lt;p&gt;As it turns out the &lt;strong&gt;StartMocking &lt;/strong&gt;method&lt;strong&gt; &lt;/strong&gt;return a &lt;strong&gt;Mock&amp;lt;T&amp;gt;&lt;/strong&gt; instance so that we can configure the mocking instance.&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;mock =  ServiceContainer.StartMocking&amp;lt;&lt;span style="color: #2b91af"&gt;IServiceB&lt;/span&gt;&amp;gt;();
mock.Expect(s =&amp;gt; s.CalculateSomethingElse()).Returns(1);
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;So how is this actually possible? Well, we need to take a deeper look into LinFu.Ioc to answer that.&lt;/p&gt;
&lt;p&gt;LinFu.Ioc actually offers several ways of hooking into the process of resolving service types and one is by implementing the &lt;strong&gt;IPreProcessor&lt;/strong&gt; interface.&lt;/p&gt;
&lt;p&gt;An &lt;strong&gt;IPreProcessor&lt;/strong&gt; implementation allows us to inspects the service request and optionally replace its default factory. &lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
/// &lt;/span&gt;&lt;span style="color: green"&gt;Inspects the requested service type and returns a mocked service instance if 
&lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;mocking has been enabled for the service type.     
&lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
/// &amp;lt;seealso cref="ContainerExtensions"/&amp;gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MockPreprocessor &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;IPreProcessor
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;private readonly &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;MockServiceInfo&lt;/span&gt;&amp;gt; _mockedServices;

    &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="color: green"&gt;Initializes a new instance of the &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref="MockPreprocessor"/&amp;gt;
    /// &amp;lt;/summary&amp;gt;
    /// &amp;lt;param name="mockedServices"&amp;gt;&amp;lt;/param&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;MockPreprocessor(&lt;span style="color: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;MockServiceInfo&lt;/span&gt;&amp;gt; mockedServices)
    {
        _mockedServices = mockedServices;
    }

    &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="color: green"&gt;Inspects the service request and return a mocked service if available.
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;        
    &lt;/span&gt;&lt;span style="color: blue"&gt;public void &lt;/span&gt;Preprocess(&lt;span style="color: #2b91af"&gt;IServiceRequest &lt;/span&gt;request)
    {
        &lt;span style="color: blue"&gt;var &lt;/span&gt;serviceName = &lt;span style="color: blue"&gt;string&lt;/span&gt;.IsNullOrEmpty(request.ServiceName) ? &lt;span style="color: blue"&gt;null &lt;/span&gt;: request.ServiceName;
        
        &lt;span style="color: blue"&gt;var &lt;/span&gt;mockedServiceInfo =
            _mockedServices.Where(
                ms =&amp;gt; ms.ServiceType == request.ServiceType &amp;amp;&amp;amp; ms.ServiceName == serviceName).FirstOrDefault
                ();
        
        &lt;span style="color: blue"&gt;if &lt;/span&gt;(mockedServiceInfo != &lt;span style="color: blue"&gt;null&lt;/span&gt;)            
            request.ActualFactory = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;FunctorFactory&lt;/span&gt;(r =&amp;gt; mockedServiceInfo.Mock.Object);
    }
}
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;The &lt;strong&gt;MockPreProcessor&lt;/strong&gt; takes a list of &lt;strong&gt;MockServiceInfo&lt;/strong&gt; instances that basically tells the preprocessor what types for which to return mock objects.&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
/// &lt;/span&gt;&lt;span style="color: green"&gt;Contains details about the service type being mocked
&lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MockServiceInfo
&lt;/span&gt;{
    &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="color: green"&gt;Gets or sets the &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref="Mock"/&amp;gt; &lt;/span&gt;&lt;span style="color: green"&gt;instance.
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Mock &lt;/span&gt;Mock { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    
    &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="color: green"&gt;Gets or sets the service type being mocked.
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;ServiceType { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
            
    &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="color: green"&gt;Gets or sets the name of the service being mocked.
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue"&gt;public string &lt;/span&gt;ServiceName { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
}
&lt;/pre&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;Now that we have all the basic infrastructure in place, we need some extensions methods that defines the API for the &lt;a href="http://code.google.com/p/moq/"&gt;Moq&lt;/a&gt; integration.&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
/// &lt;/span&gt;&lt;span style="color: green"&gt;Contains extensions methods to integrate the Moq mocking library with the LinFu.Ioc framework.
&lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
/// &amp;lt;seealso cref="http://code.google.com/p/moq/"/&amp;gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;public static class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ContainerExtensions
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;private static readonly &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;MockServiceInfo&lt;/span&gt;&amp;gt; _mockedServices = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;MockServiceInfo&lt;/span&gt;&amp;gt;();

    &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="color: green"&gt;Creates a new &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref="Mock{T}"/&amp;gt; &lt;/span&gt;&lt;span style="color: green"&gt;instance and instructs the &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref="IServiceContainer"/&amp;gt; 
    /// &lt;/span&gt;&lt;span style="color: green"&gt;to return the &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref="Mock.Object"/&amp;gt; &lt;/span&gt;&lt;span style="color: green"&gt;instead of the actual service instance.
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
    /// &amp;lt;typeparam name="T"&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The type of service being mocked.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/typeparam&amp;gt;
    /// &amp;lt;param name="serviceContainer"&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref="IServiceContainer"/&amp;gt; &lt;/span&gt;&lt;span style="color: green"&gt;that will handle the mocked service request.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/param&amp;gt;
    /// &amp;lt;returns&amp;gt;&amp;lt;see cref="Mock{T}"/&amp;gt;&amp;lt;/returns&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Mock&lt;/span&gt;&amp;lt;T&amp;gt; StartMocking&amp;lt;T&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IServiceContainer &lt;/span&gt;serviceContainer) &lt;span style="color: blue"&gt;where &lt;/span&gt;T:&lt;span style="color: blue"&gt;class
    &lt;/span&gt;{            
        &lt;span style="color: blue"&gt;return &lt;/span&gt;StartMocking&amp;lt;T&amp;gt;(serviceContainer, &lt;span style="color: blue"&gt;null&lt;/span&gt;);            
    }

    &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="color: green"&gt;Creates a new &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref="Mock{T}"/&amp;gt; &lt;/span&gt;&lt;span style="color: green"&gt;instance and instructs the &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref="IServiceContainer"/&amp;gt; 
    /// &lt;/span&gt;&lt;span style="color: green"&gt;to return the &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref="Mock.Object"/&amp;gt; &lt;/span&gt;&lt;span style="color: green"&gt;instead of the actual service instance.
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
    /// &amp;lt;typeparam name="T"&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The type of service being mocked.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/typeparam&amp;gt;
    /// &amp;lt;param name="serviceContainer"&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref="IServiceContainer"/&amp;gt; &lt;/span&gt;&lt;span style="color: green"&gt;responsible for creating the mocked service.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/param&amp;gt;
    /// &amp;lt;param name="serviceName"&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The name of the service being mocked.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/param&amp;gt;
    /// &amp;lt;returns&amp;gt;&amp;lt;see cref="Mock{T}"/&amp;gt;&amp;lt;/returns&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Mock&lt;/span&gt;&amp;lt;T&amp;gt; StartMocking&amp;lt;T&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IServiceContainer &lt;/span&gt;serviceContainer, &lt;span style="color: blue"&gt;string &lt;/span&gt;serviceName) &lt;span style="color: blue"&gt;where &lt;/span&gt;T:&lt;span style="color: blue"&gt;class
    &lt;/span&gt;{
        InstallMockPreProcessor(serviceContainer);

        &lt;span style="color: blue"&gt;return &lt;/span&gt;CreateMock&amp;lt;T&amp;gt;(serviceName);
    }

    &lt;span style="color: blue"&gt;private static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Mock&lt;/span&gt;&amp;lt;T&amp;gt; CreateMock&amp;lt;T&amp;gt;(&lt;span style="color: blue"&gt;string &lt;/span&gt;serviceName) &lt;span style="color: blue"&gt;where &lt;/span&gt;T:&lt;span style="color: blue"&gt;class
    &lt;/span&gt;{                        
        &lt;span style="color: blue"&gt;var &lt;/span&gt;mock = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Mock&lt;/span&gt;&amp;lt;T&amp;gt;();            
        &lt;span style="color: blue"&gt;var &lt;/span&gt;mockServiceInfo = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MockServiceInfo&lt;/span&gt;();
        mockServiceInfo.Mock = mock;
        mockServiceInfo.ServiceName = &lt;span style="color: blue"&gt;string&lt;/span&gt;.IsNullOrEmpty(serviceName) ? &lt;span style="color: blue"&gt;null &lt;/span&gt;: serviceName;
        mockServiceInfo.ServiceType = &lt;span style="color: blue"&gt;typeof &lt;/span&gt;(T);
        _mockedServices.Add(mockServiceInfo);
        &lt;span style="color: blue"&gt;return &lt;/span&gt;mock;
    }

    &lt;span style="color: blue"&gt;private static void &lt;/span&gt;InstallMockPreProcessor(&lt;span style="color: #2b91af"&gt;IServiceContainer &lt;/span&gt;serviceContainer)
    {
        &lt;span style="color: blue"&gt;if &lt;/span&gt;(!serviceContainer.PreProcessors.Any(p =&amp;gt; p.GetType() == &lt;span style="color: blue"&gt;typeof &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;MockPreprocessor&lt;/span&gt;)))
            serviceContainer.PreProcessors.Add(&lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MockPreprocessor&lt;/span&gt;(_mockedServices));
    }


    &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="color: green"&gt;Stops mocking the requested service type.
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
    /// &amp;lt;typeparam name="T"&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The service type currently being mocked.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/typeparam&amp;gt;
    /// &amp;lt;param name="serviceContainer"&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref="IServiceContainer"/&amp;gt; &lt;/span&gt;&lt;span style="color: green"&gt;currenly responsible for creating the mocked service.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/param&amp;gt;        
    &lt;/span&gt;&lt;span style="color: blue"&gt;public static void &lt;/span&gt;StopMocking&amp;lt;T&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IServiceContainer &lt;/span&gt;serviceContainer) &lt;span style="color: blue"&gt;where &lt;/span&gt;T : &lt;span style="color: blue"&gt;class
    &lt;/span&gt;{
        StopMocking&amp;lt;T&amp;gt;(serviceContainer, &lt;span style="color: blue"&gt;null&lt;/span&gt;);
    }


    &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="color: green"&gt;Stops mocking the requested service type.
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
    /// &amp;lt;typeparam name="T"&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The service type currently being mocked.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/typeparam&amp;gt;
    /// &amp;lt;param name="serviceContainer"&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref="IServiceContainer"/&amp;gt; &lt;/span&gt;&lt;span style="color: green"&gt;currenly responsible for creating the mocked service.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/param&amp;gt;
    /// &amp;lt;param name="serviceName"&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The name of the service curretly being mocked.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/param&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue"&gt;public static void &lt;/span&gt;StopMocking&amp;lt;T&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IServiceContainer &lt;/span&gt;serviceContainer, &lt;span style="color: blue"&gt;string &lt;/span&gt;serviceName) &lt;span style="color: blue"&gt;where &lt;/span&gt;T : &lt;span style="color: blue"&gt;class
    &lt;/span&gt;{
        serviceName = &lt;span style="color: blue"&gt;string&lt;/span&gt;.IsNullOrEmpty(serviceName) ? &lt;span style="color: blue"&gt;null &lt;/span&gt;: serviceName;
        &lt;span style="color: blue"&gt;var &lt;/span&gt;mockServiceInfo =
            _mockedServices.Where(ms =&amp;gt; ms.ServiceType == &lt;span style="color: blue"&gt;typeof &lt;/span&gt;(T) &amp;amp;&amp;amp; ms.ServiceName == serviceName).
                FirstOrDefault();

        &lt;span style="color: blue"&gt;if &lt;/span&gt;(mockServiceInfo != &lt;span style="color: blue"&gt;null&lt;/span&gt;)
            _mockedServices.Remove(mockServiceInfo);
    }

    &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="color: green"&gt;Stops mocking all services.
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
    /// &amp;lt;param name="serviceContainer"&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref="IServiceContainer"/&amp;gt; &lt;/span&gt;&lt;span style="color: green"&gt;responsible for creating mocked services.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/param&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue"&gt;public static void &lt;/span&gt;StopMocking(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IServiceContainer &lt;/span&gt;serviceContainer)
    {
        _mockedServices.Clear();
    }

}
&lt;/pre&gt;
&lt;p&gt;There are also methods for actually stop mocking types in the service container and that might be useful if you have different needs in various tests targeting the same service container instance.&lt;/p&gt;
&lt;p&gt;Download the source &lt;a href="http://coreex.googlecode.com/files/LinFu.Ioc.MoqIntegration.zip"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Happy mocking!!!&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;pre class="code"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class="code"&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4479279983457560946-61651789513542656?l=bernhard-richter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bernhard-richter.blogspot.com/feeds/61651789513542656/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4479279983457560946&amp;postID=61651789513542656' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4479279983457560946/posts/default/61651789513542656'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4479279983457560946/posts/default/61651789513542656'/><link rel='alternate' type='text/html' href='http://bernhard-richter.blogspot.com/2010/07/adding-mocking-features-to-linfuioc.html' title='Adding mocking features to LinFu.Ioc'/><author><name>Bernhard Richter</name><uri>http://www.blogger.com/profile/12224724359174112000</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4479279983457560946.post-873552413159251212</id><published>2010-04-30T10:36:00.001-07:00</published><updated>2010-04-30T10:36:22.284-07:00</updated><title type='text'>Bringing new aspects to life using the LinFu framework</title><content type='html'>&lt;p&gt;   &lt;br /&gt;Today we are going to see some examples of Aspect Oriented Programming(AOP) and see how that can help us perform tasks that we otherwise might be forced to hard wire into our source code.&amp;#160; &lt;br /&gt;    &lt;br /&gt;AOP can be thought of as a way of bringing new aspects/behaviors/features into our application without changing the original implementation.     &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;The title for this article may be a little over the top, but if you're new to AOP, it may bring some new aspects to your life as a developer :)&lt;/p&gt;  &lt;p&gt;We are going take a look at some examples and finish up by creating a simple data access library. &lt;/p&gt;  &lt;p&gt;But first things first&lt;/p&gt;  &lt;h3&gt;Inversion Of Control&lt;/h3&gt;  &lt;p&gt;Before we delve into the world of AOP I thought it might be a good idea to talk a little about Inversion Of Control (IOC).&lt;/p&gt;  &lt;p&gt;Inversion Of Control (also referred to as Dependency Injection) as a way of separating the creation of objects from their actual use.&lt;/p&gt;  &lt;p&gt;I think maybe the term &amp;quot;Inversion of Control&amp;quot; is enough to scare people away from this type of software development. If you are already in control, why would you invert it as the term implies?. What does that even mean?&lt;/p&gt;  &lt;p&gt;Lets illustrate this with an example. Imagine that we have some calculator class that can add two numbers and return the result.&lt;/p&gt; &lt;span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium &amp;#39;Times New Roman&amp;#39;; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="font-family: verdana; font-size: 13px" class="Apple-style-span"&gt;     &lt;div style="background-image: none; background-color: white; margin-top: 0px; background-attachment: scroll; font-family: &amp;#39;Courier New&amp;#39;; background-position: 0% 0%; margin-bottom: 0px; color: black; background-origin: initial; background-clip: initial"&gt;       &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;             &lt;br /&gt;public&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;class&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#2b91af"&gt;Calculator&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;        &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;{&lt;/font&gt;&lt;/p&gt;        &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160; &lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;public&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;int&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;Add(&lt;font class="Apple-style-span" color="#0000ff"&gt;int&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;value1,&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;int&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;value2)&lt;/font&gt;&lt;/p&gt;        &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/font&gt;&lt;/p&gt;        &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;return&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;value1 + value2;&lt;/font&gt;&lt;/p&gt;        &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/font&gt;&lt;/p&gt;        &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;}&lt;/font&gt;&lt;/p&gt;     &lt;/div&gt;      &lt;p&gt;&lt;/p&gt;   &lt;/span&gt;Using this class from the client code would look something like the following:&lt;/span&gt;&lt;span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium &amp;#39;Times New Roman&amp;#39;; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; font-size: 13px" class="Apple-style-span"&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&lt;font class="Apple-style-span" color="#2b91af"&gt;Calculator&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;calculator =&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;new&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#2b91af"&gt;Calculator&lt;/font&gt;();&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;var&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;result = calculator.Add(2, 2);&lt;/font&gt;&lt;/p&gt;   &lt;/span&gt;&lt;/span&gt;  &lt;br class="Apple-interchange-newline" /&gt;  &lt;p&gt;While the code certainly works, it creates a direct dependency between the client code and the concrete Calculator class.    &lt;br /&gt;Let's try to make things a little better by creating an interface for the Calculator class to implement.     &lt;br /&gt;&lt;/p&gt; &lt;span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium &amp;#39;Times New Roman&amp;#39;; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; font-size: 13px" class="Apple-style-span"&gt;     &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;public&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;interface&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#2b91af"&gt;ICalculator&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;{&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160; &lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;int&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;Add(&lt;font class="Apple-style-span" color="#0000ff"&gt;int&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;value1,&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;int&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;value2);&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;}&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;public&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;class&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#2b91af"&gt;Calculator&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;:&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#2b91af"&gt;ICalculator&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;{&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160; &lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;public&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;int&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;Add(&lt;font class="Apple-style-span" color="#0000ff"&gt;int&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;value1,&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;int&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;value2)&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;return&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;value1 + value2;&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;}&lt;/font&gt;&lt;/p&gt;   &lt;/span&gt;&lt;/span&gt;  &lt;p&gt;   &lt;br /&gt;Now the client code would change to something like:&lt;/p&gt; &lt;span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium &amp;#39;Times New Roman&amp;#39;; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; font-size: 13px" class="Apple-style-span"&gt;     &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&lt;font class="Apple-style-span" color="#2b91af"&gt;ICalculator&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;calculator =&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;new&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#2b91af"&gt;Calculator&lt;/font&gt;();&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;var&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;result = calculator.Add(2, 2);&lt;/font&gt;&lt;/p&gt;   &lt;/span&gt;&lt;/span&gt;  &lt;br class="Apple-interchange-newline" /&gt;  &lt;p&gt;Now this is getting better by the minute, but we are still faced with the problem of how to create a Calculator instance without the dependency problem.    &lt;br /&gt;I can almost hear some of you wondering &amp;quot;Why don't he just use the Abstract Factory Pattern?&amp;quot;     &lt;br /&gt;And for those of you going &amp;quot;Abstract what?&amp;quot;, lets go ahead and implement it for our naive little example.&lt;/p&gt;  &lt;p&gt;First we need an abstract factory&lt;/p&gt; &lt;span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium &amp;#39;Times New Roman&amp;#39;; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; font-size: 13px" class="Apple-style-span"&gt;     &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;public&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;interface&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#2b91af"&gt;ICalculatorFactory&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;{&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160; &lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#2b91af"&gt;ICalculator&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;CreateCalculator();&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;}&lt;/font&gt;&lt;/p&gt;   &lt;/span&gt;&lt;/span&gt;  &lt;br class="Apple-interchange-newline" /&gt;  &lt;p&gt;Then we need a concrete factory    &lt;br class="Apple-interchange-newline" /&gt;&lt;span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium &amp;#39;Times New Roman&amp;#39;; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; font-size: 13px" class="Apple-style-span"&gt;&lt;font class="Apple-style-span" size="2"&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;           &lt;br /&gt;public&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;class&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#2b91af"&gt;CalculatorFactory&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;:&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#2b91af"&gt;ICalculatorFactory            &lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;font class="Apple-style-span" size="2"&gt;{          &lt;br /&gt;&lt;/font&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160; &lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;public&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#2b91af"&gt;ICalculator&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;CreateCalculator()           &lt;br /&gt;&lt;/font&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; {          &lt;br /&gt;&lt;/font&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;return&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;new&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#2b91af"&gt;Calculator&lt;/font&gt;();           &lt;br /&gt;&lt;/font&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; }          &lt;br /&gt;&lt;/font&gt;&lt;font class="Apple-style-span" size="2"&gt;}          &lt;br /&gt;          &lt;br /&gt;&lt;/font&gt;&lt;/span&gt;&lt;/span&gt;As for the abstract product and concrete product, we already have those defined (ICalculator and Calculator).     &lt;br /&gt;The client code would now be changed to this:&lt;/p&gt; &lt;span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium &amp;#39;Times New Roman&amp;#39;; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; font-size: 13px" class="Apple-style-span"&gt;     &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&lt;font class="Apple-style-span" color="#2b91af"&gt;ICalculatorFactory&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;factory =&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;new&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#2b91af"&gt;CalculatorFactory&lt;/font&gt;();&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&lt;font class="Apple-style-span" color="#2b91af"&gt;ICalculator&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;calculator =&amp;#160; factory.CreateCalculator();&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;var&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;result = calculator.Add(2, 2);&lt;/font&gt;&lt;/p&gt;   &lt;/span&gt;&lt;/span&gt;  &lt;br class="Apple-interchange-newline" /&gt;  &lt;p&gt;Given that the CalculatorFactory class and the Calculator class is not contained in the same assembly, the client code does not longer need to know anything about the concrete Calculator class.&lt;/p&gt;  &lt;p&gt;While this is a very naive example and does not fully show the power of the Abstract Factory Pattern, we still see that we need to do quite some work to create the abstractions we want.&lt;/p&gt;  &lt;p&gt;In the world of IOC, we don’t really deal with abstract factories and all the plumbing needed.    &lt;br /&gt;We just rely on the service container to resolve our dependencies.     &lt;br /&gt;    &lt;br /&gt;Using a service container we could instead just write:&lt;/p&gt;  &lt;p&gt;&lt;span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium &amp;#39;Times New Roman&amp;#39;; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; font-size: small" class="Apple-style-span"&gt;serviceContainer.GetService&amp;lt;&lt;font class="Apple-style-span" color="#2b91af"&gt;ICalculator&lt;/font&gt;&amp;gt;();&lt;/span&gt;&lt;/span&gt;     &lt;br /&gt;    &lt;br /&gt;We have no factory classes to support this so how can the service container figure out the concrete type?     &lt;br /&gt;&lt;/p&gt;  &lt;h3&gt;Introducing LinFu.Ioc&lt;/h3&gt;  &lt;p&gt;Several years ago I came across this &lt;a href="http://www.codeproject.com/KB/cs/LinFuPart4.aspx"&gt;article&lt;/a&gt; written by my good friend and colleague, &lt;a href="http://plaureano.blogspot.com/"&gt;Philip Laureano&lt;/a&gt;. It was this very article that really got me started on creating loosely coupled applications in the first place.&lt;/p&gt;  &lt;p&gt;There are a vast of Ioc frameworks available including the relatively new Unity framework from Microsoft, but I have yet to see something that is so simple to use and understand.    &lt;br /&gt;As you will learn throughout this articles, I am not a big fan of XML configuration (or any configuration for that matter) and as you will see, configuring the LinFu service container takes only one single line of code.     &lt;br /&gt;    &lt;br /&gt;&lt;span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium &amp;#39;Times New Roman&amp;#39;; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; font-size: small" class="Apple-style-span"&gt;serviceContainer.LoadFrom(&lt;font class="Apple-style-span" color="#2b91af"&gt;AppDomain&lt;/font&gt;.CurrentDomain.BaseDirectory,&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#a31515"&gt;&amp;quot;*.dll&amp;quot;&lt;/font&gt;);&lt;/span&gt;&lt;/span&gt;     &lt;br /&gt;    &lt;br /&gt;Now lets see what we need to do in order to have the container return an ICalculator instance. &lt;/p&gt; &lt;span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium &amp;#39;Times New Roman&amp;#39;; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; font-size: 13px" class="Apple-style-span"&gt;     &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;[&lt;font class="Apple-style-span" color="#2b91af"&gt;Implements&lt;/font&gt;(&lt;font class="Apple-style-span" color="#0000ff"&gt;typeof&lt;/font&gt;(&lt;font class="Apple-style-span" color="#2b91af"&gt;ICalculator&lt;/font&gt;))]&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;public&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;class&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#2b91af"&gt;Calculator&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;:&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#2b91af"&gt;ICalculator&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;{&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160; &lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;public&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;int&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;Add(&lt;font class="Apple-style-span" color="#0000ff"&gt;int&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;value1,&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;int&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;value2)&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;return&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;value1 + value2;&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;}&lt;/font&gt;&lt;/p&gt;   &lt;/span&gt;&lt;/span&gt;  &lt;br class="Apple-interchange-newline" /&gt;  &lt;p&gt;Notice the difference from our previous example?    &lt;br /&gt;The ImplementsAttribute tells the container that this is the concrete implementation to create when a request for an ICalculator instance is made.     &lt;br /&gt;I can only speak for my self here, but I find this a lot easier than learning how to configure this using some kind of XML format.     &lt;br /&gt;Besides from being incredible easy to use, it is also very very flexible.     &lt;br /&gt;If your not already familiar with LinFu.Ioc, I strongly recommend reading more about it here.&lt;/p&gt;  &lt;h3&gt;A word about project structure&lt;/h3&gt;  &lt;p&gt;When starting out to create a new library I always create at least three sub projects for the library. If we take the sample code for this article, this is how the solution is set up.&lt;/p&gt;  &lt;table border="1" cellspacing="0" cellpadding="2" width="398"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="202"&gt;AopDemo&lt;/td&gt;        &lt;td valign="top" width="194"&gt;Contains the interfaces&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="204"&gt;AopDemo.Implementation&lt;/td&gt;        &lt;td valign="top" width="193"&gt;Contains the implementation of the interfaces&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="205"&gt;AopDemo.Tests&lt;/td&gt;        &lt;td valign="top" width="192"&gt;Contains the unit tests&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;The thing to notice here is that the client code, here represented by the unit tests, will never reference the assembly containing the implementation.&lt;/p&gt;  &lt;p&gt;In fact, the implementation will never be referenced by any other assembly.    &lt;br /&gt;    &lt;br /&gt;&lt;/p&gt;  &lt;h3&gt;Aspect Oriented Programming&lt;/h3&gt;  &lt;p&gt;AOP can be thought of as a way of bringing new aspects/behaviors/features into our application without changing the original implementation.&lt;/p&gt;  &lt;p&gt;Given the previous example, say that we wanted to do a Console.WriteLine every time we add two numbers. How could we do that without touching the actual Calculator class?&lt;/p&gt;  &lt;p&gt;There are actually two ways of doing that and that is the &amp;quot;easy way&amp;quot; using a proxy and the rather more complicated approach that involves IL injection.    &lt;br /&gt;    &lt;br /&gt;Both methods have their advantages and disadvantages, but the proxy approach most certainly have the advantage of being simple to implement.&lt;/p&gt;  &lt;p&gt;So what we are going to to here is implement a proxy that adds our new aspect (Console.WriteLine) to the ICalculator implementation.&lt;/p&gt;  &lt;h3&gt;   &lt;br /&gt;The Proxy&lt;/h3&gt;  &lt;p&gt;Now lets start off by looking at what a proxy actually is.    &lt;br /&gt;A proxy is something that sits between the caller and the actual implementation.&lt;/p&gt;  &lt;p&gt;&lt;img src="http://docs.google.com/File?id=ddwkgxd6_71fj4vnvhp_b" /&gt;&lt;/p&gt;  &lt;p&gt;As we can see the proxy also implements the ICalculator interface and hence the caller is still obliviously happy since it does not really care about the actual implementation as long as it implements the expected interface.&amp;#160; &lt;br /&gt;In addition to forwarding method call from the caller to the actual implementation (Calculator), the proxy object also offers interception of the calls being made.&amp;#160; &lt;br /&gt;    &lt;br /&gt;So what we need here is an interceptor that we can use to implement our simple logging mechanism.     &lt;br /&gt;    &lt;br /&gt;&lt;/p&gt;  &lt;h3&gt;Introducing LinFu.Proxy&lt;/h3&gt;  &lt;p&gt;The LinFu framework not only ships with an excellent IOC container, but it also contains a very flexible proxy library.    &lt;br /&gt;    &lt;br /&gt;One thing that I really like about this library, is that it is built using the IOC principle/pattern which makes it easy if we need to handle corner case scenarios.&lt;/p&gt;  &lt;p&gt;That is, however not within the scope of this article so we are going to get back to creating the interceptor we need to perform logging in the calculator.&lt;/p&gt; &lt;span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium &amp;#39;Times New Roman&amp;#39;; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; font-size: 13px" class="Apple-style-span"&gt;     &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;public&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;class&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#2b91af"&gt;CalculatorInterceptor&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;:&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#2b91af"&gt;IInterceptor&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;{&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160; &lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;public&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;object&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;Intercept(&lt;font class="Apple-style-span" color="#2b91af"&gt;IInvocationInfo&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;info)&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&lt;font class="Apple-style-span" face="&amp;#39;courier new&amp;#39;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font class="Apple-style-span" color="#2b91af"&gt;Console&lt;/font&gt;&lt;font class="Apple-style-span" size="2"&gt;.WriteLine(&lt;/font&gt;&lt;font class="Apple-style-span" color="#a31515"&gt;&amp;quot;The Add method has been invoked&amp;quot;&lt;/font&gt;&lt;font class="Apple-style-span" size="2"&gt;);&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;return&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;info.TargetMethod.Invoke(info.Target, info.Arguments);&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;}&lt;/font&gt;&lt;/p&gt;   &lt;/span&gt;&lt;/span&gt;  &lt;br class="Apple-interchange-newline" /&gt;  &lt;p&gt;The Intercept method is called whenever the client code calls a method through the proxy object.&lt;/p&gt;  &lt;p&gt;The question now remains...How do we tell the service container to return a Calculator Proxy instead of the actual Calculator?&lt;/p&gt;  &lt;p&gt;Again the flexibility in LinFu opens up for several ways of doing that and here is an very elegant approach I am almost certain you have never seen before.&lt;/p&gt;  &lt;p&gt;Let's change the CalculatorInterceptor into the following:&lt;/p&gt; &lt;span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium &amp;#39;Times New Roman&amp;#39;; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; font-size: 13px" class="Apple-style-span"&gt;     &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;         &lt;br /&gt;[&lt;font class="Apple-style-span" color="#2b91af"&gt;Intercepts&lt;/font&gt;(&lt;font class="Apple-style-span" color="#0000ff"&gt;typeof&lt;/font&gt;(&lt;font class="Apple-style-span" color="#2b91af"&gt;ICalculator&lt;/font&gt;))]&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;public&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;class&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#2b91af"&gt;CalculatorInterceptor&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;:&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#2b91af"&gt;IInterceptor&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;{&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160; &lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;public&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;object&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;Intercept(&lt;font class="Apple-style-span" color="#2b91af"&gt;IInvocationInfo&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;info)&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#2b91af"&gt;Console&lt;/font&gt;.WriteLine(&lt;font class="Apple-style-span" color="#a31515"&gt;&amp;quot;The Add method has been invoked&amp;quot;&lt;/font&gt;);&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;return&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;info.TargetMethod.Invoke(info.Target, info.Arguments);&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;}&lt;/font&gt;&lt;/p&gt;   &lt;/span&gt;&lt;/span&gt;  &lt;br /&gt;  &lt;p&gt;Notice the difference? Take a look at the Intercepts attribute.    &lt;br /&gt;This attribute actually tells the service container to create a proxy object and forward method calls to this class.&lt;/p&gt;  &lt;p&gt;Believe it or not, we have actually added a new aspect to the calculator with just 9 lines of code (Counting the curly brackets).&lt;/p&gt;  &lt;p&gt;How's that for easy of use and flexibility?&lt;/p&gt;  &lt;p&gt;The only requirement for the CalculatorInterceptor is that it is located on disc when the container is configured.&lt;/p&gt;  &lt;p&gt;This means that we can add tracing to an already deployed application just by doing a simple XCopy. &lt;/p&gt;  &lt;p&gt;This is getting so good that I think we should spend a little time to take this to the next level.&lt;/p&gt;  &lt;p&gt;Say now that we wanted to allow for multiple logging targets and not just a simple Console.WriteLine.&lt;/p&gt;  &lt;p&gt;We could of course add each logging target to the CalculatorInterceptor, but lets make this a little more exciting.&lt;/p&gt;  &lt;p&gt;We start of by abstracting the actual logging out of the CalculatorInterceptor by creating an ILogger interface.    &lt;br /&gt;&lt;/p&gt; &lt;span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium &amp;#39;Times New Roman&amp;#39;; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="font-family: verdana; font-size: 13px" class="Apple-style-span"&gt;     &lt;div style="background-color: white; margin-top: 0px; font-family: &amp;#39;Courier New&amp;#39;; margin-bottom: 0px; color: black"&gt;       &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" face="&amp;#39;Courier New&amp;#39;"&gt;&lt;font class="Apple-style-span" size="2"&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;public&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;interface&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#2b91af"&gt;ILogger&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;        &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" face="&amp;#39;Courier New&amp;#39;"&gt;&lt;font class="Apple-style-span" size="2"&gt;{&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;        &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" face="&amp;#39;Courier New&amp;#39;"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160; &lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;void&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;Log(&lt;font class="Apple-style-span" color="#0000ff"&gt;string&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;message);&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;        &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" face="&amp;#39;Courier New&amp;#39;"&gt;&lt;font class="Apple-style-span" size="2"&gt;}&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;     &lt;/div&gt;     &lt;font class="Apple-style-span" size="2"&gt;       &lt;br /&gt;&lt;/font&gt;&lt;/span&gt;&lt;/span&gt;  &lt;p&gt;Then we are going to ever so slightly alter the CalculatorInterceptor class.&lt;/p&gt; &lt;span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium &amp;#39;Times New Roman&amp;#39;; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; font-size: 13px" class="Apple-style-span"&gt;     &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;[&lt;font class="Apple-style-span" color="#2b91af"&gt;Intercepts&lt;/font&gt;(&lt;font class="Apple-style-span" color="#0000ff"&gt;typeof&lt;/font&gt;(&lt;font class="Apple-style-span" color="#2b91af"&gt;ICalculator&lt;/font&gt;))]&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;public&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;class&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#2b91af"&gt;CalculatorInterceptor&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;:&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#2b91af"&gt;IInterceptor&lt;/font&gt;,&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#2b91af"&gt;IInitialize&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;{&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160; &lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;private&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#2b91af"&gt;IEnumerable&lt;/font&gt;&amp;lt;&lt;font class="Apple-style-span" color="#2b91af"&gt;ILogger&lt;/font&gt;&amp;gt; _loggers;&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160; &lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;public&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;object&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;Intercept(&lt;font class="Apple-style-span" color="#2b91af"&gt;IInvocationInfo&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;info)&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;foreach&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;(&lt;font class="Apple-style-span" color="#0000ff"&gt;var&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;logger&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;in&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;_loggers)&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; logger.Log(&lt;font class="Apple-style-span" color="#a31515"&gt;&amp;quot;The Add method has been invoked&amp;quot;&lt;/font&gt;);&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;return&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;info.TargetMethod.Invoke(info.Target, info.Arguments);&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160; &lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;public&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;void&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;Initialize(&lt;font class="Apple-style-span" color="#2b91af"&gt;IServiceContainer&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;source)&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _loggers = source.GetServices&amp;lt;&lt;font class="Apple-style-span" color="#2b91af"&gt;ILogger&lt;/font&gt;&amp;gt;();&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;}&lt;/font&gt;&lt;/p&gt;   &lt;/span&gt;&lt;/span&gt;  &lt;br class="Apple-interchange-newline" /&gt;  &lt;p&gt;What’s new here is that we have implemented the IInitialize interface that is used by the service container to expose itself to the services instances it creates.&lt;/p&gt;  &lt;p&gt;We then use the container to retrieve all implementations of the ILogger interface and handles the logging of to each instance.&lt;/p&gt;  &lt;p&gt;And as for the Console.WriteLine, it has been moved to an ILogger implementation.&lt;/p&gt; &lt;span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium &amp;#39;Times New Roman&amp;#39;; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; font-size: 13px" class="Apple-style-span"&gt;     &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;[&lt;font class="Apple-style-span" color="#2b91af"&gt;Implements&lt;/font&gt;(&lt;font class="Apple-style-span" color="#0000ff"&gt;typeof&lt;/font&gt;(&lt;font class="Apple-style-span" color="#2b91af"&gt;ILogger&lt;/font&gt;))]&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;public&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;class&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#2b91af"&gt;ConsoleLogger&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;:&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#2b91af"&gt;ILogger&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;{&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160; &lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;public&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;void&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;Log(&lt;font class="Apple-style-span" color="#0000ff"&gt;string&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;message)&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font class="Apple-style-span" color="#2b91af"&gt;Console&lt;/font&gt;.WriteLine(message);&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/font&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;font class="Apple-style-span" size="2"&gt;}&lt;/font&gt;&lt;/p&gt;   &lt;/span&gt;&lt;/span&gt;  &lt;br class="Apple-interchange-newline" /&gt;  &lt;p&gt;The beauty of this is that we can now create as many ILogger implementations as we want and they will all be invoked when the Add method is invoked.&lt;/p&gt;  &lt;p&gt;Pretty cool, wouldn't you say?    &lt;br /&gt;    &lt;br /&gt;&lt;/p&gt;  &lt;h3&gt;From naiveness towards the real world&lt;/h3&gt;  &lt;p&gt;While this so far has shown how flexible the LinFu framerwork is, it still remains a fact that the calculator example is pretty naive.&lt;/p&gt;  &lt;p&gt;Let's pick it up a notch and create something useful. Why don't we create a DBMS independent data access library with profiling capabilities?    &lt;br /&gt;That might sound like a daunting task, but thanks to LinFu, this practically becomes a breeze.&lt;/p&gt;  &lt;p&gt;Let's boil this data access paradigm down to what is it that we really need?    &lt;br /&gt;We need some object that we can use to execute sql, right?&lt;/p&gt;  &lt;p&gt;For me it sounds like a IDbCommand implementation should fit the picture just perfectly.    &lt;br /&gt;How do we get a IDbCommand implementation? Simple, we just hand it off to the container like this:&lt;/p&gt;  &lt;p&gt;&lt;span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium &amp;#39;Times New Roman&amp;#39;; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; font-size: small" class="Apple-style-span"&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;var&lt;/font&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;dbCommand = ServiceContainer.GetService&amp;lt;&lt;font class="Apple-style-span" color="#2b91af"&gt;IDbCommand&lt;/font&gt;&amp;gt;();&lt;/span&gt;&lt;/span&gt; &lt;/p&gt;  &lt;p&gt;But wait a minute, how does the container know what implementation of IDbCommand to create?&lt;/p&gt;  &lt;p&gt;The answer is that is does not so we need to teach the container how to create an IDbCommand instance.&lt;/p&gt;  &lt;p&gt;The code below shows a rudimentary factory implementation. &lt;/p&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;p style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;Factory&lt;/span&gt;(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;IDbCommand&lt;/span&gt;))]&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;DbCommandFactory&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;IFactory&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IDbCommand&lt;/span&gt;&amp;gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;{&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IDbCommand&lt;/span&gt; CreateInstance(&lt;span style="color: #2b91af"&gt;IFactoryRequest&lt;/span&gt; request)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; connectionStringSettings = GetConnectionStringSetting(request.ServiceName);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; providerFactory = CreateProviderFactory(connectionStringSettings);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; connection = providerFactory.CreateConnection();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; connection.ConnectionString = connectionStringSettings.ConnectionString;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; connection.Open();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; connection.CreateCommand();&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: #2b91af"&gt;DbProviderFactory&lt;/span&gt; CreateProviderFactory(&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;ConnectionStringSettings&lt;/span&gt; connectionStringSettings)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: #2b91af"&gt;DbProviderFactories&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .GetFactory(connectionStringSettings.ProviderName);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160; &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ConnectionStringSettings&lt;/span&gt; GetConnectionStringSetting(&lt;span style="color: blue"&gt;string&lt;/span&gt; connectionName)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;if&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;String&lt;/span&gt;.IsNullOrEmpty(connectionName))&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ConfigurationManager&lt;/span&gt;.ConnectionStrings&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Cast&amp;lt;&lt;span style="color: #2b91af"&gt;ConnectionStringSettings&lt;/span&gt;&amp;gt;().Last();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;ConfigurationManager&lt;/span&gt;.ConnectionStrings&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Cast&amp;lt;&lt;span style="color: #2b91af"&gt;ConnectionStringSettings&lt;/span&gt;&amp;gt;().Where(&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c =&amp;gt; c.Name.EndsWith(connectionName)).FirstOrDefault();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;}&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;Luckily for us, most of the abstractions has already been done for us. We just need to hook up with the appropriate DbProviderFactory and we are pretty much good to go.&lt;/p&gt;  &lt;p&gt;We can now go ahead and configure our connection in app/web config and that's all we really need to do. We can now get a ready and willing IDbCommand instance by doing:&lt;/p&gt;  &lt;p&gt;&lt;span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium &amp;#39;Times New Roman&amp;#39;; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="font-family: &amp;#39;courier new&amp;#39;; font-size: 13px" class="Apple-style-span"&gt;&lt;font class="Apple-style-span" color="#0000ff"&gt;var&lt;/font&gt;&lt;font class="Apple-style-span" size="2"&gt; dbCommand = ServiceContainer.GetService&amp;lt;&lt;/font&gt;&lt;font class="Apple-style-span" color="#2b91af"&gt;IDbCommand&lt;/font&gt;&lt;font class="Apple-style-span" size="2"&gt;&amp;gt;();&lt;/font&gt;&lt;/span&gt;&lt;/span&gt; &lt;/p&gt;  &lt;p&gt;Sweet, but didn't I promise profiling capabilities? Okay, let's spend another five minutes.&lt;/p&gt;  &lt;p&gt;We start off by creating an interface for the profilers&lt;/p&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;interface&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IDbProfiler&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;{&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;void&lt;/span&gt; BeforeExecute(&lt;span style="color: #2b91af"&gt;IDbCommand&lt;/span&gt; dbCommand, &lt;span style="color: #2b91af"&gt;MethodInfo&lt;/span&gt; methodInfo, &lt;span style="color: blue"&gt;object&lt;/span&gt;[] arguments);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;void&lt;/span&gt; AfterExecute(&lt;span style="color: #2b91af"&gt;IDbCommand&lt;/span&gt; dbCommand, &lt;span style="color: #2b91af"&gt;MethodInfo&lt;/span&gt; methodInfo, &lt;span style="color: blue"&gt;object&lt;/span&gt;[] arguments);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;}&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;Next we need an interceptor that intercepts calls made to the actual DbCommand.&lt;/p&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;p style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;Intercepts&lt;/span&gt;(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;IDbCommand&lt;/span&gt;))]&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;DbCommandInterceptor&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;IInterceptor&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;IInitialize&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;{&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IDbProfiler&lt;/span&gt;&amp;gt; _profilers;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;object&lt;/span&gt; Intercept(&lt;span style="color: #2b91af"&gt;IInvocationInfo&lt;/span&gt; info)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;foreach&lt;/span&gt; (&lt;span style="color: blue"&gt;var&lt;/span&gt; dbProfiler &lt;span style="color: blue"&gt;in&lt;/span&gt; _profilers)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; dbProfiler.BeforeExecute((&lt;span style="color: #2b91af"&gt;IDbCommand&lt;/span&gt;) info.Target, &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; info.TargetMethod, info.Arguments);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; returnValue =&amp;#160; info.TargetMethod.Invoke(info.Target, info.Arguments);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;foreach&lt;/span&gt; (&lt;span style="color: blue"&gt;var&lt;/span&gt; dbProfiler &lt;span style="color: blue"&gt;in&lt;/span&gt; _profilers)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; dbProfiler.AfterExecute((&lt;span style="color: #2b91af"&gt;IDbCommand&lt;/span&gt;) info.Target, &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; info.TargetMethod, info.Arguments);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; returnValue;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Initialize(&lt;span style="color: #2b91af"&gt;IServiceContainer&lt;/span&gt; source)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _profilers = source.GetServices&amp;lt;&lt;span style="color: #2b91af"&gt;IDbProfiler&lt;/span&gt;&amp;gt;().ToList();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;}&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;As we can see we are doing much of the same that with did with in the calculator&amp;#160; example.    &lt;br /&gt;    &lt;br /&gt;Now lets finish this off by creating a sample IDbCommand profiler.&lt;/p&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;p style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;Implements&lt;/span&gt;(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;IDbProfiler&lt;/span&gt;))]&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ConsoleDbProfiler&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;IDbProfiler&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;{&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;readonly&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Stopwatch&lt;/span&gt; _stopwatch = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Stopwatch&lt;/span&gt;();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; BeforeExecute(&lt;span style="color: #2b91af"&gt;IDbCommand&lt;/span&gt; dbCommand, &lt;span style="color: #2b91af"&gt;MethodInfo&lt;/span&gt; methodInfo, &lt;span style="color: blue"&gt;object&lt;/span&gt;[] arguments)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; {&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;if&lt;/span&gt; (ShouldProfile(methodInfo))&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _stopwatch.Reset();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _stopwatch.Start();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; AfterExecute(&lt;span style="color: #2b91af"&gt;IDbCommand&lt;/span&gt; dbCommand, &lt;span style="color: #2b91af"&gt;MethodInfo&lt;/span&gt; methodInfo, &lt;span style="color: blue"&gt;object&lt;/span&gt;[] arguments)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;if&lt;/span&gt; (ShouldProfile(methodInfo))&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _stopwatch.Stop();&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: blue"&gt;string&lt;/span&gt;.Format(&lt;span style="color: #a31515"&gt;&amp;quot;The command '{0}' executed in {1} milliseconds&amp;quot;&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; , dbCommand.CommandText, _stopwatch.ElapsedMilliseconds));&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;bool&lt;/span&gt; ShouldProfile(&lt;span style="color: #2b91af"&gt;MethodInfo&lt;/span&gt; methodInfo)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; methodName = methodInfo.Name;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; (methodName == &lt;span style="color: #a31515"&gt;&amp;quot;ExecuteReader&amp;quot;&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; || methodName == &lt;span style="color: #a31515"&gt;&amp;quot;ExecuteNonQuery&amp;quot;&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; || methodName == &lt;span style="color: #a31515"&gt;&amp;quot;ExecuteScalar&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;}&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;The fact is that we use a similar approach at work at it has proven invaluable to us when it comes to hunting down long running queries.&lt;/p&gt;  &lt;p&gt;The following code shows the execution of the query:&lt;/p&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; dbCommand = ServiceContainer.GetService&amp;lt;&lt;span style="color: #2b91af"&gt;IDbCommand&lt;/span&gt;&amp;gt;();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;dbCommand.CommandText = &lt;span style="color: #a31515"&gt;&amp;quot;SELECT * FROM Orders&amp;quot;&lt;/span&gt;;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; reader = dbCommand.ExecuteReader(&lt;span style="color: #2b91af"&gt;CommandBehavior&lt;/span&gt;.CloseConnection);&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;And in our debug output window we can now observe the following:&lt;/p&gt;  &lt;p&gt;&lt;span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium &amp;#39;Times New Roman&amp;#39;; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="font-family: &amp;#39;Courier New&amp;#39;; font-size: small" class="Apple-style-span"&gt;The command 'SELECT * FROM Orders' executed in 116 milliseconds&lt;/span&gt;&lt;/span&gt;     &lt;br /&gt;    &lt;br /&gt;And that my friends, is how we add new aspects to even a sealed type. (Yes, the SqlDbCommand is actually sealed) &lt;/p&gt;  &lt;p&gt;&lt;/p&gt; Please note that all exception handling has been omitted for the sake of brevity.  &lt;p&gt;&lt;/p&gt;  &lt;h3&gt;Conclusion&lt;/h3&gt;  &lt;p&gt;As we have seen, using the LinFu framework, taking the shift towards AOP is ridiculously simple to overcome.&lt;/p&gt;  &lt;p&gt;We have seen how we can add new aspects to existing classes with out touching the original code and even implemented a simple data access library.    &lt;br /&gt;    &lt;br /&gt;We can also draw the conclusion that AOP helps serving the Separation of Concerns principle as we don’t clutter our classes with aspects that is beyond their initial intent.&lt;/p&gt;  &lt;p&gt;So the next time you create a new class, make sure you do so by implementing an interface.&lt;/p&gt;  &lt;p&gt;You never know when new requirements (aspects) arrive.&lt;/p&gt;  &lt;p&gt;Enjoy!!!!&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4479279983457560946-873552413159251212?l=bernhard-richter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bernhard-richter.blogspot.com/feeds/873552413159251212/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4479279983457560946&amp;postID=873552413159251212' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4479279983457560946/posts/default/873552413159251212'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4479279983457560946/posts/default/873552413159251212'/><link rel='alternate' type='text/html' href='http://bernhard-richter.blogspot.com/2010/04/bringing-new-aspects-to-life-using.html' title='Bringing new aspects to life using the LinFu framework'/><author><name>Bernhard Richter</name><uri>http://www.blogger.com/profile/12224724359174112000</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4479279983457560946.post-8225558579833416667</id><published>2010-02-03T05:04:00.001-08:00</published><updated>2010-02-03T05:04:53.047-08:00</updated><title type='text'>Tracking changes made to POCO objects</title><content type='html'>&lt;p&gt;   &lt;br /&gt;Imagine that we retrieve a list of POCO (&lt;a href="http://en.wikipedia.org/wiki/Plain_Old_CLR_Object"&gt;Plain Old CLR Objects&lt;/a&gt;) and we find our self in need to track changes made to those objects.&lt;/p&gt;  &lt;p&gt;To illustrate this we use a very simple example like the Customer class:&lt;/p&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Customer&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt; CustomerID { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;Standard CLR properties has no built-in support for interception so the preferred way of doing this is to implement the &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx"&gt;INotifyPropertyChanged&lt;/a&gt; interface.&lt;/p&gt;  &lt;p&gt;This interface allows us to notify subscribers that the property has changed and its widely used when two-way data binding is required.&lt;/p&gt;  &lt;p&gt;If we bind, say a textbox to a property that does not support change notification, we end up with a situation where changes made to the textbox is automatically reflected in the property, but not the other way around.&lt;/p&gt;  &lt;p&gt;The data binding mechanism (WinForms and WPF), relies heavily on this interface to do its magic.&lt;/p&gt;  &lt;p&gt;The example below shows our modified Customer object with support for change notification.&lt;/p&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;     &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Customer&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;INotifyPropertyChanged&lt;/span&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt; _customerId;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt; CustomerID&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;get&lt;/span&gt; { &lt;span style="color: blue"&gt;return&lt;/span&gt; _customerId; }&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;set&lt;/span&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _customerId = &lt;span style="color: blue"&gt;value&lt;/span&gt;;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; OnPropertyChanged(&lt;span style="color: #a31515"&gt;&amp;quot;CustomerID&amp;quot;&lt;/span&gt;);&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;protected&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; OnPropertyChanged(&lt;span style="color: blue"&gt;string&lt;/span&gt; propertyName)&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;if&lt;/span&gt; (PropertyChanged != &lt;span style="color: blue"&gt;null&lt;/span&gt;)&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; PropertyChanged(&lt;span style="color: blue"&gt;this&lt;/span&gt;,&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;PropertyChangedEventArgs&lt;/span&gt;(propertyName));&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;event&lt;/span&gt; &lt;span style="color: #2b91af"&gt;PropertyChangedEventHandler&lt;/span&gt; PropertyChanged;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;   &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;Although the interface is quite easy to implement it also results in a lot of tidious code for us to write. Every property setter need that call to OnPropertyChanged passing along the property name. &lt;/p&gt;  &lt;p&gt;Sure we could create a base class that implemented this interface and by that simplify it to some extent, but that takes our classes away from the POCO principle. &lt;/p&gt;  &lt;h5&gt;AOP to the rescue&lt;/h5&gt;  &lt;p&gt;I'd say that we let our objects remain POCO objects and implement the &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx"&gt;INotifyPropertyChanged&lt;/a&gt; interface at runtime instead. &lt;/p&gt;  &lt;p&gt;You might wonder how that can be done and there are actually two ways of achieving that. &lt;/p&gt;  &lt;p&gt;We can find a lot of&amp;#160; documentation on &lt;a href="http://en.wikipedia.org/wiki/Aspect-oriented_programming"&gt;Aspect Oriented Programming&lt;/a&gt; that is more or less difficult to understand, but let us try to sum it up in one sentence.&lt;/p&gt;  &lt;p&gt;AOP is about adding new aspects to an object without the object ever knowing about it.&lt;/p&gt;  &lt;p&gt;The &lt;a href="http://en.wikipedia.org/wiki/Aspect_%28computer_science%29"&gt;Aspect&lt;/a&gt; in our case is implementing the &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx"&gt;INotifyPropertyChanged&lt;/a&gt; interface so that changes made to the Customer object are intercepted by the caller.&lt;/p&gt;  &lt;p&gt;And we want to do this at runtime.&lt;/p&gt;  &lt;p&gt;There are actually two ways of doing that and we can either use a proxy to the actual customer object or we can use IL injection to modify the customer object so that it implements what we need, in this case the &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx"&gt;INotifyPropertyChanged&lt;/a&gt; interface.&lt;/p&gt;  &lt;p&gt;Lets start of simple with the proxy approach.&lt;/p&gt;  &lt;h5&gt;The Proxy &lt;/h5&gt;  &lt;p&gt;A proxy is an object that sits between the caller and the actual target object. &lt;/p&gt;  &lt;p&gt;The proxy class will at least implement the target interface, but can also implement additional interfaces which is the key to our solution. &lt;/p&gt;  &lt;p&gt;Creating a proxy object can be done in two different ways:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Create a proxy by sub classing the target object.      &lt;br /&gt;This requires all members to be declared as virtual members so that they can be overridden in the proxy type. &lt;/li&gt;    &lt;li&gt;Create a proxy that implements the same interface that the actual target object does.      &lt;br /&gt;This requires the members to be declared in an interface implemented by the target object. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;We are going to stick to the second approach here as we will see later, provides us with a little more flexibility.&lt;/p&gt;  &lt;p&gt;So we extract the interface from our Customer object.&lt;/p&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;interface&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ICustomer&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;string&lt;/span&gt; CustomerID { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Customer&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;ICustomer&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt; CustomerID { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;Now the customer implements a contract that also can be implemented by our customer proxy type. &lt;/p&gt;  &lt;p&gt;As mentioned before the proxy class can implement additional interfaces and that means that the proxy type can implement the &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx"&gt;INotifyPropertyChanged&lt;/a&gt; interface.&lt;/p&gt;  &lt;p&gt;The figure below shows that the textbox now binds to the CustomerProxy and since it implements &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx"&gt;INotifyPropertyChanged&lt;/a&gt;, a two-way communication now exists between the     &lt;br /&gt;CustomerProxy and the textbox (client).&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_xLtC7MkWshs/S2l0cYoGNlI/AAAAAAAADPg/G4oKJzPFjRw/s1600-h/Proxy%5B16%5D.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Proxy" border="0" alt="Proxy" src="http://lh5.ggpht.com/_xLtC7MkWshs/S2l0cwqsbYI/AAAAAAAADPk/Bn0ngs4T7is/Proxy_thumb%5B10%5D.jpg?imgmax=800" width="568" height="93" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;So any changes to the CustomerID exposed by the CustomerProxy will now be reflected in the textbox. &lt;/p&gt;  &lt;p&gt;And this actually brings us to the downside of the proxy approach.    &lt;br /&gt;For all this to work, changes to the CustomerID (that we want to be reflected in the textbox), must be made through the CustomerProxy. &lt;/p&gt;  &lt;p&gt;If we have some logic inside the Customer object that internally updates the CustomerID property, the changes are not reflected in the textbox. &lt;/p&gt;  &lt;p&gt;Why? Simply because we are not invoking the Customer proxy that is resposible for the change notification. &lt;/p&gt;  &lt;p&gt;In the case that the proxied objects contains logic that may present a problem if the code actually updates the properties. &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;h5&gt;IL Injection. &lt;/h5&gt;  &lt;p&gt;This approach takes you far beyond the topics you might encounter in your average &amp;quot;Teach yourself C# in 21 days&amp;quot; book. &lt;/p&gt;  &lt;p&gt;What we are talking about here is modifying the Customer class in such a way that it implements the &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx"&gt;INotifyPropertyChanged&lt;/a&gt; interface. &lt;/p&gt;  &lt;p&gt;The modification is done by injecting the IL code that normally would be the result of implementing the interface in the first place. &lt;/p&gt;  &lt;p&gt;The injection can be done at runtime or after the assembly has been compiled. &lt;/p&gt;  &lt;p&gt;We are going concentrate on runtime modification here and that involves the following steps. &lt;/p&gt;  &lt;p&gt;1. Load the assembly from disc (not into the application domain) &lt;/p&gt;  &lt;p&gt;2. Modify the assembly (IL injection) &lt;/p&gt;  &lt;p&gt;3. Save the assembly back to disc or load it as an byte array into the application domain without touching the original assembly. &lt;/p&gt;  &lt;p&gt;There is only one library that I know of that actually allows us to do this, and that is Mono.Cecil. &lt;/p&gt;  &lt;p&gt;This is a very powerful library that pretty much lets us do whatever we want with our assembly. &lt;/p&gt;  &lt;p&gt;An interesting problem with this approach is how to get access to the target assembly before it is loaded into the application domain. &lt;/p&gt;  &lt;p&gt;As we already know, once an assembly is loaded into the application domain, there is not much we can do about that. It can not be unloaded nor modified. &lt;/p&gt;  &lt;p&gt;So we need to catch it BEFORE &lt;a href="http://www.grimes.demon.co.uk/workshops/fusionWS.htm"&gt;Fusion&lt;/a&gt; resolves the assembly and loads it into the application domain. &lt;/p&gt;  &lt;p&gt;Again this presents us with two possible solutions. &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Make sure that Fusion is unable to resolve the assembly and handle the AppDomain.AssemblyResolve event.      &lt;br /&gt;This can be done by placing the assembly in a subfolder &lt;/li&gt;    &lt;li&gt;Make sure that the assembly is not referenced directly      &lt;br /&gt;Load the assembly our self and make the necessary changes to it before it gets loaded into the application domain.&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;IMHO, depending on Fusion being unable to locate the assembly seems somewhat “hacky”.&lt;/p&gt;  &lt;p&gt;If the assembly is not referenced by the application itself, it can be loaded dynamically by an Inversion of Control container and we can hook into the load process and do our magic before the container starts to register services from that assembly.&lt;/p&gt;  &lt;p&gt;There is also another that presents a challenge when doing IL injection and that is how to enable debugging in our modified/weaved assemblies. &lt;/p&gt;  &lt;p&gt;If we modify the IL, the symbol information (pdb) also needs to be updated. If we don’t handle that properly, we will not be able to set a breakpoint inside a class that at runtime has been modified.&lt;/p&gt;  &lt;p&gt;Mono.Cecil provides some support here and from what I have tested so far, we should be able to actually debug weaved assemblies.&lt;/p&gt;  &lt;h5&gt;&lt;/h5&gt;  &lt;h5&gt;Conclusion&lt;/h5&gt;  &lt;p&gt;As we can see there are many decisions that needs to be made as to how we are going to inject new behavior into our objects and I would say it depends a lot of what kind of behavior we want to add.&lt;/p&gt;  &lt;p&gt;Initially I wanted to create some kind of state/change tracker that hooks up the PropertyChanged event and keeps track of changes made to an object graph.&lt;/p&gt;  &lt;p&gt;My gut feeling here is to go with the Proxy implementation as it allows us to keep everything i C# (No IL injection) and it allows proxies to be installed in a lazy fashion as we navigate down the object graph. &lt;/p&gt;  &lt;p&gt;It is also somewhat simpler to add support for new interfaces, for example the &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.idataerrorinfo.aspx"&gt;IDataErrorInfo&lt;/a&gt; interface if we should want to give some visual feedback when validation errors occur.&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4479279983457560946-8225558579833416667?l=bernhard-richter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bernhard-richter.blogspot.com/feeds/8225558579833416667/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4479279983457560946&amp;postID=8225558579833416667' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4479279983457560946/posts/default/8225558579833416667'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4479279983457560946/posts/default/8225558579833416667'/><link rel='alternate' type='text/html' href='http://bernhard-richter.blogspot.com/2010/02/tracking-changes-made-to-poco-objects.html' title='Tracking changes made to POCO objects'/><author><name>Bernhard Richter</name><uri>http://www.blogger.com/profile/12224724359174112000</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh5.ggpht.com/_xLtC7MkWshs/S2l0cwqsbYI/AAAAAAAADPk/Bn0ngs4T7is/s72-c/Proxy_thumb%5B10%5D.jpg?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4479279983457560946.post-5368607823873334420</id><published>2009-09-11T00:49:00.001-07:00</published><updated>2009-09-11T00:55:27.669-07:00</updated><title type='text'>CoreEx.Common.Validation</title><content type='html'>&lt;br&gt;
&lt;h1&gt;CoreEx.Common.Validation &lt;/h1&gt;
&lt;div&gt;When working with objects we often find the need to validate their contents before e.g. persisting them to a data store.&lt;br&gt;&lt;br&gt;I have been putting this off for a long time now and I decided it was time to take a look at the options available.&lt;br&gt;&lt;br&gt;Before we start lets define some requirements of the validation library.&lt;br&gt;&lt;br&gt;&lt;ul&gt;&lt;li&gt;Objects should be validated without the need to reference the validation library itself.&lt;/li&gt;&lt;/ul&gt;&lt;br&gt;&lt;ul&gt;&lt;li&gt;It must be possible to configure validation without the use of XML.&lt;/li&gt;&lt;/ul&gt;&lt;br&gt;&lt;ul&gt;&lt;li&gt;It must be possible to add new validation rules to an object without the need to recompile.&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;&lt;ul&gt;&lt;li&gt;It should be possible to validate a single property &lt;/li&gt;&lt;/ul&gt;&lt;br&gt;&lt;ul&gt;&lt;li&gt;And last, but not least , it must be easy to use.&lt;/li&gt;&lt;/ul&gt;&lt;br&gt;&lt;br&gt;This may seems like modest requirements, but in fact they rule out some of the existing validation frameworks.&lt;br&gt;Some of you may already be using the Validation Block from the Enterprise Library.&lt;br&gt;&lt;br&gt;That basically leaves you with two options when it comes to configuration.&lt;br&gt;&lt;br&gt;1. Using Attributes&lt;br&gt;&lt;br&gt;2. External configuration using an XML format specifically designed&amp;nbsp; to describe validation rules.&lt;br&gt;&lt;br&gt;As this violates at least two of our requirements, we must investigate further.&lt;br&gt;&lt;br&gt;&lt;/div&gt;
&lt;h2&gt;The beauty of Lambda Expressions &lt;/h2&gt;

&lt;div&gt;I started to read &lt;a title="this" href="http://www.codeproject.com/KB/cs/ValidationUsingLambdas.aspx" id="gnm6"&gt;this&lt;/a&gt; amazing article which have been resting in my reading backlog for a long time now.&lt;br&gt;&lt;br&gt;He uses Lambda expressions to specify the validation rules and I immediately thought.. what a great idea?&lt;br&gt;&lt;br&gt;Let the configuration be the code itself.&lt;br&gt;&lt;br&gt;For what is attributes and XML configuration if not just another way of specifying what code to execute?&lt;br&gt;&amp;nbsp;&lt;br&gt;This must be it. Let's start defining some interfaces for our new library.&lt;br&gt;&lt;br&gt;Imagine of we could something like &lt;br&gt;&lt;br&gt;

&lt;div style="background: white none repeat scroll 0% 0%; font-family: Courier New; color: black;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;(c =&amp;gt; c.CustomerID.Length &amp;lt; 5, &lt;span style="color: rgb(163, 21, 21);"&gt;"CustomerID cannot exceed five characters"&lt;/span&gt;)&lt;/font&gt;&amp;nbsp; &lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &lt;/div&gt;
&lt;div&gt;First we need something that represents a validation rule&lt;br&gt;&lt;font face="Courier New"&gt;&lt;span class="com"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/font&gt;

&lt;div style="background: white none repeat scroll 0% 0%; font-family: Courier New; color: black;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; Represents a validation rule.&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;typeparam name="T"&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;The type that this &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;see cref="IValidationRule{T}"/&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt; applies to.&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/typeparam&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;interface&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IValidationRule&lt;/span&gt;&amp;lt;T&amp;gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;{&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; Gets or sets the message used when the rule is broken.&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;string&lt;/span&gt; Message { &lt;span style="color: blue;"&gt;get&lt;/span&gt;; &lt;span style="color: blue;"&gt;set&lt;/span&gt;; }&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; Gets or sets the function delegate that points to the validating code. &lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: rgb(43, 145, 175);"&gt;Func&lt;/span&gt;&amp;lt;T, &lt;span style="color: blue;"&gt;bool&lt;/span&gt;&amp;gt; Rule { &lt;span style="color: blue;"&gt;get&lt;/span&gt;; &lt;span style="color: blue;"&gt;set&lt;/span&gt;; }&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;}&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/p&gt;&lt;/div&gt;&lt;div style="background: white none repeat scroll 0% 0%; font-family: Courier New; color: black; margin-left: 40px;"&gt;
&lt;/div&gt;

&lt;br&gt;The validation rule is specific to the target type and now we just need some way of adding new validation rules.&lt;br&gt;&lt;br&gt;

&lt;div style="background: white none repeat scroll 0% 0%; font-family: Courier New; color: black;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; Represents a set of validation rules.&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;typeparam name="T"&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;The target type that the validation rules applies to.&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/typeparam&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;interface&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IValidationRules&lt;/span&gt;&amp;lt;T&amp;gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;{&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; Adds a new validation rule.&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;param name="rule"&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;The &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;see cref="Expression{TDelegate}"/&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt; that contains the validating code.&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;param name="message"&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;The message to be used when a validation rule is broken.&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;void&lt;/span&gt; AddRule(&lt;span style="color: rgb(43, 145, 175);"&gt;Expression&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Func&lt;/span&gt;&amp;lt;T, &lt;span style="color: blue;"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt; rule, &lt;span style="color: blue;"&gt;string&lt;/span&gt; message);&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; Gets all the &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;see cref="IValidationRule{T}"/&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt; instances for the target type.&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;returns&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;An &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;see cref="IEnumerable{T}"/&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt; that contains &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;see cref="IValidationRule{T}"/&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt; instances.&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;IValidationRule&lt;/span&gt;&amp;lt;T&amp;gt;&amp;gt; GetRules();&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; Gets all the &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;see cref="IValidationRule{T}"/&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt; instances that applies to the target type and &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;paramref name="propertyName"/&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;.&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;param name="propertyName"&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;The name of the property for which to retrieve the validation rules.&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;returns&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;An &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;see cref="IEnumerable{T}"/&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt; that contains &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;see cref="IValidationRule{T}"/&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt; instances.&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;IValidationRule&lt;/span&gt;&amp;lt;T&amp;gt;&amp;gt; GetRules(&lt;span style="color: blue;"&gt;string&lt;/span&gt; propertyName);&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;}&lt;/font&gt;&lt;/p&gt;&lt;/div&gt;&lt;div style="background: white none repeat scroll 0% 0%; font-family: Courier New; color: black; margin-left: 40px;"&gt;
&lt;/div&gt;

&lt;font face="Courier New"&gt;&lt;span class="pln"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br&gt;&lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;We can now add new validation rules and return them as a whole or only rules specific to one property.&lt;br&gt;&lt;/div&gt;
&lt;div&gt;You migth notice that there is no overload to AddRule that allows us to specify the target property.&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;How is it then possible to retrieve validation rules that applies to e.g. CustomerID?&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;h2&gt;Expression&amp;lt;Func&amp;lt;T,bool&amp;gt;&amp;gt; vs Func&amp;lt;T,bool&amp;gt;&lt;/h2&gt;
&lt;div&gt;While Lambda expression are truly powerful, but their real potential comes to show when wrapped by a Expression&amp;lt;T&amp;gt;.&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;Expression trees kan be thought of like code that has not yet been compiled. It is sort of like an AST(Abstract syntax tree).&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;We can inspect and even change expression trees before they are compiled and executed.&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;I'm not saying here that compiled code can not be altered, but that is a quite different story which we will look into later.&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;So let's just get back to expression trees.&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;So how is that going to help us with regards to the validation library?&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;Since the requirements states that we must be able to validate any given property, we need some way to &lt;/div&gt;
&lt;div&gt;associate the expression and the target property.&amp;nbsp;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;The general rule here is if the expression references a property that belongs to the target type, we assoiate the expression with that property.&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;Example&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;&lt;div style="background: white none repeat scroll 0% 0%; font-family: Courier New; color: black;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;(c =&amp;gt; c.CustomerID.Length &amp;lt; 5, &lt;span style="color: rgb(163, 21, 21);"&gt;"CustomerId cannot exceed five characters"&lt;/span&gt;)&lt;/font&gt;&amp;nbsp; &lt;/p&gt;
&lt;/div&gt;&lt;div&gt;&amp;nbsp; &lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div&gt;The CustomerID property is declared by the Customer class and we can associate the expression accordingly.&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;But how do we inspect the expression to determine the target properties?&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;h2&gt;The ExpressionVisitor&lt;/h2&gt;
&lt;div&gt;Working with expression trees takes some getting used to.&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;Luckally for us we have a very useful class available to us when we want to inspect or rewrite an expression tree.&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;The ExpressionVisitor class is actually not available to us directly, but you can find it in the MSDN documentation.&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;If you ever get to browse the source code of a Linq provider, you are very likely to see this class used all over.&lt;br&gt;&lt;br&gt;&lt;/div&gt;

&lt;div&gt;It's purpose is basically to allow us to visit each node of the expression tree (visitor pattern) and reconstruct the tree for us if we make any changes to it.&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;Why do we want to make changes to the expression tree? Well, we will see in just a minute.&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;For now we just want to retrieve all target properties based on the lambda expression that makes up the validating code.&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;We go ahead and define an interface like this&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;

&lt;div style="background: white none repeat scroll 0% 0%; font-family: Courier New; color: black;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; Represents a class that is capable of determining a set of &lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;interface&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ITargetPropertyResolver&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;{&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; Tries to determine the target property or properties that the validation &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;paramref name="expression"/&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt; applies to.&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;param name="expression"&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;The validation expression&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;returns&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;An &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;see cref="IEnumerable{T}"/&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt; that contains the target properties.&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;PropertyInfo&lt;/span&gt;&amp;gt; ResolveFrom(&lt;span style="color: rgb(43, 145, 175);"&gt;Expression&lt;/span&gt; expression);&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;}&lt;/font&gt;&lt;/p&gt;
&lt;/div&gt;

&lt;font face="Courier New"&gt;&lt;br&gt;&lt;/font&gt;&lt;/div&gt;
&lt;div&gt;And the implementation goes like&lt;/div&gt;&lt;br&gt;

&lt;div style="background: white none repeat scroll 0% 0%; font-family: Courier New; color: black;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; Determines the properties that a validation expression should be associated with.&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;[&lt;span style="color: rgb(43, 145, 175);"&gt;Implements&lt;/span&gt;(&lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;ITargetPropertyResolver&lt;/span&gt;))]&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;TargetPropertyResolver&lt;/span&gt; : &lt;span style="color: rgb(43, 145, 175);"&gt;ExpressionVisitor&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;ITargetPropertyResolver&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;{&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;readonly&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;PropertyInfo&lt;/span&gt;&amp;gt; _targetProperties = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;PropertyInfo&lt;/span&gt;&amp;gt;();&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Type&lt;/span&gt; _validationTargetType;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; Tries to determine the target property or properties that the validation &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;paramref name="expression"/&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt; applies to.&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;param name="expression"&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;The validation expression&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;returns&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;An &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;see cref="IEnumerable{T}"/&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt; that contains the target properties.&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;PropertyInfo&lt;/span&gt;&amp;gt; ResolveFrom(&lt;span style="color: rgb(43, 145, 175);"&gt;Expression&lt;/span&gt; expression)&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; SetValidationTargetType(expression);&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Visit(expression);&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; _targetProperties;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; Determines if the &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;paramref name="m"/&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt; is considered a target property for the validation expression.&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;param name="m"&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;The currently accessed member.&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;returns&amp;gt;&amp;lt;see cref="Expression"/&amp;gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;protected&lt;/span&gt; &lt;span style="color: blue;"&gt;override&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Expression&lt;/span&gt; VisitMemberAccess(&lt;span style="color: rgb(43, 145, 175);"&gt;MemberExpression&lt;/span&gt; m)&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (IsTargetProperty(m))&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; RegisterMemberAsExpressionTarget(m);&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: blue;"&gt;base&lt;/span&gt;.VisitMemberAccess(m);&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; Sets the validation target types based on the validation expression&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;param name="expression"&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;The validation expression.&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; SetValidationTargetType(&lt;span style="color: rgb(43, 145, 175);"&gt;Expression&lt;/span&gt; expression)&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; _validationTargetType = ((&lt;span style="color: rgb(43, 145, 175);"&gt;LambdaExpression&lt;/span&gt;)expression).Parameters[0].Type;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; Determines if the &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;paramref name="memberExpression"/&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt; is a property declared by the target type.&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;param name="memberExpression"&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;The &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;see cref="MemberExpression"/&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt; that current is being visited.&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;returns&amp;gt;&amp;lt;b&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;True&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/b&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt; if the &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;paramref name="memberExpression"/&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt; is a property and is declared by the target type.&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;bool&lt;/span&gt; IsTargetProperty(&lt;span style="color: rgb(43, 145, 175);"&gt;MemberExpression&lt;/span&gt; memberExpression)&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; (memberExpression.Member.DeclaringType == _validationTargetType) &amp;amp;&amp;amp; memberExpression.Member &lt;span style="color: blue;"&gt;is&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;PropertyInfo&lt;/span&gt;;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; RegisterMemberAsExpressionTarget(&lt;span style="color: rgb(43, 145, 175);"&gt;MemberExpression&lt;/span&gt; memberExpression)&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; _targetProperties.Add((&lt;span style="color: rgb(43, 145, 175);"&gt;PropertyInfo&lt;/span&gt;)memberExpression.Member);&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;}&lt;/font&gt;&lt;/p&gt;
&lt;/div&gt;

&lt;br&gt;&lt;br&gt;&lt;div&gt;As we can see from the code the visitor class override the VisitMemberAccess method which is fired for every time a member is accessed in the lambda expression.&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;Then we basically just checks if the property is a target property by determining if the property is declared by the target type (e.g Customer)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;So now we have our expression and we also know its target property/properties.&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;Didn't I mention something about rewriting the expression tree?&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;&lt;h2&gt;Coerce those Null values&lt;/h2&gt;Let's take another look at our little example&lt;br&gt;&lt;br&gt;&lt;font size="2"&gt;(c =&amp;gt; c.CustomerID.Length &amp;lt; 5, &lt;span style="color: rgb(163, 21, 21);"&gt;"CustomerId cannot exceed five characters"&lt;/span&gt;)&lt;/font&gt;&amp;nbsp; &lt;br&gt;&lt;br&gt;Does anybody spot something potentially wrong with this code? No?&lt;br&gt;&lt;br&gt;If it is any consolation, neither did I until my unit test blew up in my face proclaiming a NullReferenceException somewhere.&lt;br&gt;&lt;br&gt;Then it finally hit me. What happens if the target property is null? How could I have missed that?&lt;br&gt;&lt;br&gt;And just when this library was all good and ready to jump into production.&lt;br&gt;&lt;br&gt;Then I started to think about how to deal with this problem.&lt;br&gt;&lt;br&gt;We could of course check the property value before executing the lambda expression, but then again the expression could target several properties and would have to check them all in order to safely execute the expression.&lt;br&gt;&lt;br&gt;We could also require the lambda expression to include a check for null, but that seems a little intrusive and certainly violates the requirement that states ease of use. &lt;br&gt;&amp;nbsp;&lt;br&gt;If we only could make sure we had a value in place once the check is executed.&lt;br&gt;&lt;br&gt;What we basically are looking for is turning this&lt;br&gt;&lt;br&gt;&lt;font size="2"&gt;(c =&amp;gt; c.CustomerID.Length &amp;lt; 5, &lt;span style="color: rgb(163, 21, 21);"&gt;"CustomerId cannot exceed five characters"&lt;/span&gt;)&lt;/font&gt;&amp;nbsp; &lt;br&gt;&lt;br&gt;into this&lt;br&gt;&lt;br&gt;

&lt;div style="background: white none repeat scroll 0% 0%; font-family: Courier New; color: black;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;(c =&amp;gt; (c.CustomerID ?? &lt;span style="color: rgb(163, 21, 21);"&gt;""&lt;/span&gt;).Length &amp;lt; 5, &lt;span style="color: rgb(163, 21, 21);"&gt;"CustomerId cannot exceed five characters"&lt;/span&gt;)&lt;/font&gt;&lt;/p&gt;
&lt;/div&gt;

&lt;br&gt;&lt;br&gt;That should take care of business, right?&lt;br&gt;&lt;br&gt;But what we actually wanted to test for null in addition to the string length?&lt;br&gt;&lt;br&gt;

&lt;div style="background: white none repeat scroll 0% 0%; font-family: Courier New; color: black;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;(c =&amp;gt; c.CustomerID.Length &amp;lt; 5 &amp;amp;&amp;amp; c.CustomerID != &lt;span style="color: blue;"&gt;null&lt;/span&gt;, &lt;span style="color: rgb(163, 21, 21);"&gt;"CustomerId cannot be null or exceed five characters"&lt;/span&gt;)&lt;/font&gt;&lt;/p&gt;
&lt;/div&gt;

&lt;br&gt;&lt;br&gt;So what we need to do is coerce those null values into their default values unless an explicit check for null is intended.&lt;br&gt;&lt;br&gt;That means that we need to turn it into this&lt;br&gt;&lt;br&gt;

&lt;div style="background: white none repeat scroll 0% 0%; font-family: Courier New; color: black;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;(c =&amp;gt; (c.CustomerID ?? &lt;span style="color: rgb(163, 21, 21);"&gt;""&lt;/span&gt;).Length &amp;lt; 5 &amp;amp;&amp;amp; c.CustomerID != &lt;span style="color: blue;"&gt;null&lt;/span&gt;, &lt;span style="color: rgb(163, 21, 21);"&gt;"CustomerId cannot be null or exceed five characters"&lt;/span&gt;)&lt;/font&gt;&lt;/p&gt;
&lt;/div&gt;

&lt;br&gt;&lt;br&gt;And since we are still working with expression trees, we can make the necessary changes by doing an expression tree rewrite.&lt;br&gt;&lt;br&gt;Next we go ahead and define an interface to do the rewrite.&lt;br&gt;&lt;br&gt;

&lt;div style="background: white none repeat scroll 0% 0%; font-family: Courier New; color: black;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; Represents a class that is capable of rewriting the expression tree in such &lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; as way that member expressions don't return &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;c&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;null&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/c&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt; values unless an explicit&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; check for &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;c&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;null&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/c&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt; is intended.&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;interface&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IRuleRewriter&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;{&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: rgb(43, 145, 175);"&gt;Expression&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Func&lt;/span&gt;&amp;lt;T, &lt;span style="color: blue;"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt; Rewrite&amp;lt;T&amp;gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;Expression&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Func&lt;/span&gt;&amp;lt;T, &lt;span style="color: blue;"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt; ruleExpression, &lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;PropertyInfo&lt;/span&gt;&amp;gt; targetProperties);&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;}&lt;/font&gt;&lt;/p&gt;
&lt;/div&gt;

&lt;br&gt;&lt;br&gt;&lt;br&gt;And the implementation is once again based on the ExpressionVisitor class.&lt;br&gt;&lt;br&gt;

&lt;div style="background: white none repeat scroll 0% 0%; font-family: Courier New; color: black;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;[&lt;span style="color: rgb(43, 145, 175);"&gt;Implements&lt;/span&gt;(&lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;IRuleRewriter&lt;/span&gt;))]&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;RuleRewriter&lt;/span&gt; : &lt;span style="color: rgb(43, 145, 175);"&gt;ExpressionVisitor&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;IRuleRewriter&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;{&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;readonly&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;MemberExpression&lt;/span&gt;&amp;gt; _excludeList = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;MemberExpression&lt;/span&gt;&amp;gt;();&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;PropertyInfo&lt;/span&gt;&amp;gt; _targetProperties;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&lt;span style="color: blue;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #region&lt;/span&gt; IRuleRewriter Members&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; Rewrites the &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;see cref="ruleExpression"/&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt; by replacing any &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;see cref="MemberExpression"/&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt; with a &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;see cref="ConditionalExpression"/&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; where the member is not explicitly checked for &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;c&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;null&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/c&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;.&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;typeparam name="T"&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;The target type to validate.&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/typeparam&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;param name="ruleExpression"&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;The validation expression.&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;param name="targetProperties"&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;The target properties for the &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;paramref name="ruleExpression"/&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt; that are candidates for rewriting.&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;returns&amp;gt;&amp;lt;see cref="Expression{TDelegate}"/&amp;gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Expression&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Func&lt;/span&gt;&amp;lt;T, &lt;span style="color: blue;"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt; Rewrite&amp;lt;T&amp;gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;Expression&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Func&lt;/span&gt;&amp;lt;T, &lt;span style="color: blue;"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt; ruleExpression,&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;PropertyInfo&lt;/span&gt;&amp;gt; targetProperties)&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; _targetProperties = targetProperties;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; (&lt;span style="color: rgb(43, 145, 175);"&gt;Expression&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Func&lt;/span&gt;&amp;lt;T, &lt;span style="color: blue;"&gt;bool&lt;/span&gt;&amp;gt;&amp;gt;) Visit(ruleExpression);&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&lt;span style="color: blue;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #endregion&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; Check to see if a &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;see cref="MemberExpression"/&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt; is part of an explicit check for &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;c&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;null&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/c&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;param name="binaryExpression"&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;The currently visited &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;see cref="BinaryExpression"/&amp;gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;returns&amp;gt;&amp;lt;see cref="BinaryExpression"/&amp;gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;protected&lt;/span&gt; &lt;span style="color: blue;"&gt;override&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Expression&lt;/span&gt; VisitBinary(&lt;span style="color: rgb(43, 145, 175);"&gt;BinaryExpression&lt;/span&gt; binaryExpression)&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (IsExplicitCheckForNull(binaryExpression))&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ExcludeFromMemberRewrite(binaryExpression);&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: blue;"&gt;base&lt;/span&gt;.VisitBinary(binaryExpression);&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; Replaces the &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;see cref="MemberExpression"/&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt; with a &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;see cref="ConditionalExpression"/&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; that return the default value if the member yields null.&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;param name="memberExpression"&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;The currently visited &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;see cref="MemberExpression"/&amp;gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;returns&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;A &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;see cref="ConditionalExpression"/&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt; if a rewrite has been performed, otherwise the original &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;see cref="MemberExpression"/&amp;gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;protected&lt;/span&gt; &lt;span style="color: blue;"&gt;override&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Expression&lt;/span&gt; VisitMemberAccess(&lt;span style="color: rgb(43, 145, 175);"&gt;MemberExpression&lt;/span&gt; memberExpression)&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (ShouldRewiteMemberExpression(memberExpression))&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; RewriteMemberExpression(memberExpression);&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: blue;"&gt;base&lt;/span&gt;.VisitMemberAccess(memberExpression);&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;bool&lt;/span&gt; ShouldRewiteMemberExpression(&lt;span style="color: rgb(43, 145, 175);"&gt;MemberExpression&lt;/span&gt; memberExpression)&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (!IsTargetProperty(memberExpression) || IsMemberExcluded(memberExpression))&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: blue;"&gt;false&lt;/span&gt;;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; MemberIsReferenceTypeOrNullableValueType(memberExpression);&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;bool&lt;/span&gt; MemberIsReferenceTypeOrNullableValueType(&lt;span style="color: rgb(43, 145, 175);"&gt;MemberExpression&lt;/span&gt; memberExpression)&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;var&lt;/span&gt; propertyInfo = (&lt;span style="color: rgb(43, 145, 175);"&gt;PropertyInfo&lt;/span&gt;) memberExpression.Member;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; !propertyInfo.PropertyType.IsValueType || propertyInfo.PropertyType.IsNullableType();&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Expression&lt;/span&gt; RewriteMemberExpression(&lt;span style="color: rgb(43, 145, 175);"&gt;MemberExpression&lt;/span&gt; memberExpression)&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;var&lt;/span&gt; propertyInfo = (&lt;span style="color: rgb(43, 145, 175);"&gt;PropertyInfo&lt;/span&gt;) memberExpression.Member;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Expression&lt;/span&gt;.Condition(GetTestExpression(memberExpression), GetDefaultValueExpression(propertyInfo),&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; memberExpression);&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;BinaryExpression&lt;/span&gt; GetTestExpression(&lt;span style="color: rgb(43, 145, 175);"&gt;MemberExpression&lt;/span&gt; memberExpression)&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Expression&lt;/span&gt;.Equal(memberExpression, &lt;span style="color: rgb(43, 145, 175);"&gt;Expression&lt;/span&gt;.Constant(&lt;span style="color: blue;"&gt;null&lt;/span&gt;));&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;UnaryExpression&lt;/span&gt; GetDefaultValueExpression(&lt;span style="color: rgb(43, 145, 175);"&gt;PropertyInfo&lt;/span&gt; propertyInfo)&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Expression&lt;/span&gt;.Convert(&lt;span style="color: rgb(43, 145, 175);"&gt;Expression&lt;/span&gt;.Constant(GetDefaultValue(propertyInfo.PropertyType)),&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; propertyInfo.PropertyType);&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; ExcludeFromMemberRewrite(&lt;span style="color: rgb(43, 145, 175);"&gt;BinaryExpression&lt;/span&gt; binaryExpression)&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (IsTargetProperty(binaryExpression.Left))&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; _excludeList.Add((&lt;span style="color: rgb(43, 145, 175);"&gt;MemberExpression&lt;/span&gt;) binaryExpression.Left);&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (IsTargetProperty(binaryExpression.Right))&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; _excludeList.Add((&lt;span style="color: rgb(43, 145, 175);"&gt;MemberExpression&lt;/span&gt;) binaryExpression.Right);&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;bool&lt;/span&gt; IsMemberExcluded(&lt;span style="color: rgb(43, 145, 175);"&gt;MemberExpression&lt;/span&gt; memberExpression)&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; _excludeList.Contains(memberExpression);&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;bool&lt;/span&gt; IsExplicitCheckForNull(&lt;span style="color: rgb(43, 145, 175);"&gt;BinaryExpression&lt;/span&gt; binaryExpression)&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (IsNullConstant(binaryExpression.Left) &amp;amp;&amp;amp; IsTargetProperty(binaryExpression.Right))&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: blue;"&gt;true&lt;/span&gt;;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (IsNullConstant(binaryExpression.Right) &amp;amp;&amp;amp; IsTargetProperty(binaryExpression.Left))&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: blue;"&gt;true&lt;/span&gt;;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: blue;"&gt;false&lt;/span&gt;;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; &lt;span style="color: blue;"&gt;bool&lt;/span&gt; IsNullConstant(&lt;span style="color: rgb(43, 145, 175);"&gt;Expression&lt;/span&gt; expression)&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;var&lt;/span&gt; constantExpression = (expression &lt;span style="color: blue;"&gt;as&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ConstantExpression&lt;/span&gt;);&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (constantExpression == &lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: blue;"&gt;false&lt;/span&gt;;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; (constantExpression.Value == &lt;span style="color: blue;"&gt;null&lt;/span&gt;);&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;bool&lt;/span&gt; IsTargetProperty(&lt;span style="color: rgb(43, 145, 175);"&gt;Expression&lt;/span&gt; expression)&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;var&lt;/span&gt; memberExpression = (expression &lt;span style="color: blue;"&gt;as&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;MemberExpression&lt;/span&gt;);&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (memberExpression == &lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: blue;"&gt;false&lt;/span&gt;;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;var&lt;/span&gt; propertyInfo = (&lt;span style="color: rgb(43, 145, 175);"&gt;PropertyInfo&lt;/span&gt;) memberExpression.Member;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (propertyInfo == &lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: blue;"&gt;false&lt;/span&gt;;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; _targetProperties.Contains(propertyInfo);&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; Gets the default value to be used as a surrogate for the supplied &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;c&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;null&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/c&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt; value.&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;param name="type"&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;The target type.&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: gray;"&gt;///&lt;/span&gt; &lt;span style="color: gray;"&gt;&amp;lt;returns&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;The default value as determined by the underlying value type.&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; &lt;span style="color: blue;"&gt;object&lt;/span&gt; GetDefaultValue(&lt;span style="color: rgb(43, 145, 175);"&gt;Type&lt;/span&gt; type)&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;//We need some special handling for the string data type &lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;//since it is a reference type, but behaves like a value type.&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (type == &lt;span style="color: blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color: blue;"&gt;string&lt;/span&gt;))&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt;.Empty;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; type.GetNonNullableType().GetDefaultValue();&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;font size="2"&gt;}&lt;/font&gt;&lt;/p&gt;
&lt;/div&gt;

&lt;br&gt;&lt;br&gt;What we are doing here is that we replace the MemberExpression with a ConditionalExpression that returns the default value (based on the property type) if the property yields null.&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;h2&gt;Configuring your application&lt;br&gt;&lt;/h2&gt;We have already stated that the validation target&amp;nbsp; should not have to reference the validation library itself.&lt;br&gt;&lt;br&gt;So where and how can we add new rules to our objects?&lt;br&gt;&lt;br&gt;One simple solution would be to retrieve the IValidationRules&amp;lt;T&amp;gt; instance and start adding new rules.&lt;br&gt;&lt;br&gt;example&lt;br&gt;&lt;br&gt;var rules = _serviceContainer.GetService&amp;lt;IValidationRules&amp;lt;Customer&amp;gt;&amp;gt;();&lt;br&gt;&lt;br&gt;rules.AddRule( c=&amp;gt; c.ContactName != null,"ContackName can not be null");&lt;br&gt;&lt;br&gt;But that code would have to reside somewhere, right?&lt;br&gt;&lt;br&gt;In order to really make life easy when adding new rules, take a look at the IRuleInjector&amp;lt;T&amp;gt; interface.&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;summary&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; /// Represents a class that is capable of injecting additional validation rules &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; /// that should apply to the target type.&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;/summary&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;typeparam name="T"&amp;gt;The target type to validate.&amp;lt;/typeparam&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public interface IRuleInjector&amp;lt;T&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;summary&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /// Allows additional validation rules to be added to the &amp;lt;see cref="IValidationRule{T}"/&amp;gt; instance.&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;/summary&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;param name="validationRules"&amp;gt;The &amp;lt;see cref="IValidationRules{T}"/&amp;gt; instance &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /// that contains the validation rules for the target type.&amp;lt;/param&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; void Inject(IValidationRules&amp;lt;T&amp;gt; validationRules);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;&lt;br&gt;&lt;br&gt;All we need to do is to implement the interface and make sure that it is located in an assembly loaded by the service container.&lt;br&gt;&lt;br&gt;example&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [Implements(typeof(IRuleInjector&amp;lt;IOrderDetail&amp;gt;))]&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public class SampleRuleInjector : IRuleInjector&amp;lt;IOrderDetail&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void Inject(IValidationRules&amp;lt;IOrderDetail&amp;gt; validationRules)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; validationRules.AddRule(od =&amp;gt; (decimal)od.Discount &amp;lt; od.UnitPrice&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,"Discount must be less than the unit price");&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;&lt;br&gt;This also means that we can add new rules without ever recompiling the application.&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;h2&gt;Validation in action&lt;/h2&gt;Now we pretty much have all the expressions in place and it is time to actually validate something.&lt;br&gt;&lt;br&gt;That something may be an object where we validate everything or an object where we want to validate just one property.&lt;br&gt;&lt;br&gt;Having the ability to validate just one property comes in very handy when using this library in conjunction with the IDataErrorInfo interface.&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;h2&gt;Using the code&lt;/h2&gt;We have already looked at how we add new validation rules to a target type and its time to see how we can execute the actual validation&lt;br&gt;&lt;br&gt;To validate the whole object&lt;br&gt;&lt;br&gt;var validator = _serviceContainer.GetService&amp;lt;IValidator&amp;lt;Customer&amp;gt;&amp;gt;();&amp;nbsp; &lt;br&gt;string result = validator.validate(someCustomer);&lt;br&gt;&lt;br&gt;To validate one single property&lt;br&gt;&lt;br&gt;var validator = _serviceContainer.GetService&amp;lt;IPropertyValidator&amp;lt;Customer&amp;gt;&amp;gt;();&amp;nbsp; &lt;br&gt;
string result = validator.validate(someCustomer,"CustomerID");&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;h2&gt;The IDataErrorInfo interface&lt;/h2&gt;As we can see using the IPropertyValidator&amp;lt;T&amp;gt; fits very well together with implementing the IDataErrorInfo interface.&lt;br&gt;&lt;br&gt;This interface has to be implemented by the binding target and how can we do that without having to reference the validation library.&lt;br&gt;&lt;br&gt;It is also very related to the UI and the data binding mechanism so it does not really belong in our objects either. What a puzzle.&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;h2&gt;A Proxy to the rescue&lt;/h2&gt;I sometimes get asked about why I have my domain object implement an interface and this is one of the reasons why.&lt;br&gt;&lt;br&gt;By using interfaces for our domain object, we can create proxies for them and those proxies can implement any additional interface.&lt;br&gt;&lt;br&gt;Let's stick with the customer object (Northwind) and see how it looks like &lt;br&gt;&lt;br&gt;The interface &lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public interface ICustomer &lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;string CustomerID { get; set; }&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;string CompanyName { get; set; }&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;string ContactName { get; set; }&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;string ContactTitle { get; set; }&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;string Address { get; set; }&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;string City { get; set; }&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;string Region { get; set; }&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;string PostalCode { get; set; }&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;string Country { get; set; }&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;string Phone { get; set; }&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;string Fax { get; set; }&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IList&amp;lt;ICustomerCustomerDemo&amp;gt; CustomerCustomerDemo { get; }&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IList&amp;lt;IOrder&amp;gt; Orders { get; }&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;br&gt;&lt;br&gt;&lt;br&gt;When asking the service container for an ICustomer instance, the only requirement for the concrete class is that it implement ICustomer.&lt;br&gt;&lt;br&gt;But it can also implement addition interfaces.&lt;br&gt;&lt;br&gt;The Linfu framework has a very powerful proxy library and we can actually use that to have our domain objects implement IDataErrorInfo at runtime.&lt;br&gt;&lt;br&gt;For those of you wondering what a proxy really is, we can sum it all up by saying that it sits between the calling code and the actual implementation.&lt;br&gt;&lt;br&gt;You can read more about them here.&lt;br&gt;&lt;br&gt;First we need some type of interceptor that gets called when calls are being made to the proxy instance.&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [Implements(typeof(IInterceptor),LifecycleType.OncePerThread, ServiceName = "DataErrorInfoInterceptor")]&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public class SampleDataErrorInfoInterceptor : IInterceptor, IInitialize&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; private IServiceContainer _serviceContainer;&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; private object _actualTarget;&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; private Type _targetType;&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public SampleDataErrorInfoInterceptor(object actualTarget, Type targetType)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _actualTarget = actualTarget;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _targetType = targetType;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public object Intercept(IInvocationInfo info)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (info.TargetMethod.Name == "get_Item")&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var propertyValidatorType = typeof (IPropertyValidator&amp;lt;&amp;gt;).MakeGenericType(_targetType);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var propertyValidator = _serviceContainer.GetService(propertyValidatorType);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var result = propertyValidatorType.DynamicInvoke(propertyValidator,"Validate", new[] {_actualTarget, info.Arguments[0]});&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return result;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return info.TargetMethod.DynamicInvoke(_actualTarget, info.Arguments);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void Initialize(IServiceContainer source)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _serviceContainer = source;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;&lt;br&gt;The interceptor forward calls to IDataErrorInfo.Item[] to the IPropertyValidator&amp;lt;T&amp;gt; that knows how to validate the target type.&lt;br&gt;&lt;br&gt;The last thing we need to is making sure that a proxy is returned when a request for an ICustomer instance is made.&lt;br&gt;&lt;br&gt;Using an IPostProcessor enables us to inspect the service instance and return our proxy instance.&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [PostProcessor]&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public class SampleDomainModelPostProcessor : IPostProcessor&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; private IProxyFactory _proxyFactory;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void PostProcess(IServiceRequestResult result)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (result.ServiceType.Namespace.Contains("DomainModel"))&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var proxyFactory = CreateProxyFactory(result.Container);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var interceptor = result.Container.GetService&amp;lt;IInterceptor&amp;gt;("DataErrorInfoInterceptor",result.OriginalResult,result.ServiceType);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var proxy = proxyFactory.CreateProxy(result.ServiceType, interceptor, typeof (IDataErrorInfo));&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; result.ActualResult = proxy;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; private IProxyFactory CreateProxyFactory(IServiceContainer serviceContainer)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (_proxyFactory == null)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _proxyFactory = serviceContainer.GetService&amp;lt;IProxyFactory&amp;gt;();&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return _proxyFactory;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;&lt;br&gt;&lt;br&gt;The proxy now implements both the ICustomer and the IDataErrorInfo interface.&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [Test]&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void ShouldImplementIDataErrorInfo()&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var customer = _serviceContainer.GetService&amp;lt;ICustomer&amp;gt;();&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Assert.IsTrue(typeof(IDataErrorInfo).IsAssignableFrom(customer.GetType()));&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;&lt;br&gt;&lt;br&gt;As a result of this we have now added transparent validation to our domain objects and all the plumbing &lt;br&gt;needed to visualize this in the UI has been abstracted away from the object itself.&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&amp;nbsp;&lt;br&gt;&amp;nbsp;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&amp;nbsp;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;h2&gt;&amp;nbsp; &lt;/h2&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;&lt;br&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4479279983457560946-5368607823873334420?l=bernhard-richter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bernhard-richter.blogspot.com/feeds/5368607823873334420/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4479279983457560946&amp;postID=5368607823873334420' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4479279983457560946/posts/default/5368607823873334420'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4479279983457560946/posts/default/5368607823873334420'/><link rel='alternate' type='text/html' href='http://bernhard-richter.blogspot.com/2009/09/coreexcommonvalidation.html' title='CoreEx.Common.Validation'/><author><name>Bernhard Richter</name><uri>http://www.blogger.com/profile/12224724359174112000</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4479279983457560946.post-1439280000078382215</id><published>2008-09-01T11:28:00.001-07:00</published><updated>2008-09-02T00:30:31.917-07:00</updated><title type='text'>Asynchronous Method Invocation</title><content type='html'>&lt;p&gt;I'm currently in the process of developing a plug-in framework that enables business developers to create individual modules/plug-in's to be hosted in a management console.&lt;/p&gt; &lt;p&gt;It soon became an issue that we would have to support some kind of multithreading features to avoid blocking the UI.&lt;/p&gt; &lt;p&gt;IMHO, the UI thread (main thread) should have one task and that is making the&amp;nbsp; application look alive to the end user.&lt;/p&gt; &lt;p&gt;We looked at several options from running each plug-in in its own app domain to loading forms on different threads. Nothing really ended up like we wanted to. Too much code, too much setup. There had to be a better way and it had to be easy to use.&lt;/p&gt; &lt;h4&gt;The Background Worker&lt;/h4&gt; &lt;p&gt;Our first thought was that the business developers could use the Background Worker to do their asynchronous stuff. &lt;/p&gt; &lt;p&gt;While this certainly solves the problem we still thought it would be good if we did not need to set up event handlers, pass arguments as objects and so on.&lt;/p&gt; &lt;p&gt;The good thing about it though, is that it takes care of invoking the&amp;nbsp; RunWorkerCompleted event on the appropriate thread. This way you don't run into cross-thread exceptions when accessing a UI component.&lt;/p&gt; &lt;h4&gt;The IAsyncInvocator interface&lt;/h4&gt;&lt;pre class="code"&gt;&lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
/// &lt;/span&gt;&lt;span style="color: green"&gt;Represents a class that is capable of invoking a method asynchronously. 
&lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IAsyncInvocator
&lt;/span&gt;{
    &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="color: green"&gt;Executes the function as described in the &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;paramref name="func"/&amp;gt; &lt;/span&gt;&lt;span style="color: green"&gt;delegate.
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
    /// &amp;lt;typeparam name="TTarget"&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The type of target that will be subject for invocation.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/typeparam&amp;gt;
    /// &amp;lt;typeparam name="TResult"&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The type of result from the method invocation.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/typeparam&amp;gt;
    /// &amp;lt;param name="target"&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;An instance of &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;paramref name="{TTarget}"/&amp;gt; &amp;lt;/param&amp;gt;
    /// &amp;lt;param name="func"&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;Represents the method to be invoked asynchronously.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/param&amp;gt;
    /// &amp;lt;param name="callBack"&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;Represents the method to be called after method has been executed.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/param&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue"&gt;void &lt;/span&gt;AsyncInvoke&amp;lt;TTarget, TResult&amp;gt;(TTarget target, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;TTarget, TResult&amp;gt; func, &lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;TResult&amp;gt; callBack);
}&lt;/pre&gt;&lt;pre class="code"&gt;&lt;/pre&gt;
&lt;p&gt;As mentioned we needed a simple API to let our class library users invoke they asynchronous methods. &lt;br&gt;For those of you not familiar with the Func&amp;lt;T&amp;gt; and Action&amp;lt;T&amp;gt; delegates, this interface may look a little complicated. &lt;br&gt;Lets put it to use and discover that is not all that complicated.&lt;/p&gt;&lt;pre class="code"&gt;invocator.AsyncInvoke(&lt;span style="color: blue"&gt;this&lt;/span&gt;, m =&amp;gt; m.SomeMethod(),r =&amp;gt; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;"Result: {0}" &lt;/span&gt;,r));&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;That's it. The method "SomeMethod" is invoked asynchronously and the result is passed to the callback that in our little example just prints out the result to the console.&lt;/p&gt;
&lt;h4&gt;Oh, I almost forgot&lt;/h4&gt;
&lt;p&gt;It's kind of funny actually, following the TDD (Test Driven Development) pattern, I often find myself forgetting about this last tiny detail:)&lt;/p&gt;
&lt;p&gt;Here is the code implementing the interface&lt;/p&gt;&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;Implements&lt;/span&gt;(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;IAsyncInvocator&lt;/span&gt;),&lt;span style="color: #2b91af"&gt;LifecycleType&lt;/span&gt;.OncePerRequest)]
&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;AsyncInvocator &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;IAsyncInvocator
&lt;/span&gt;{               
 
    &lt;span style="color: blue"&gt;#region &lt;/span&gt;IAsyncInvocator Members

    &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="color: green"&gt;Executes the function as described in the &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;paramref name="func"/&amp;gt; &lt;/span&gt;&lt;span style="color: green"&gt;delegate.
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
    /// &amp;lt;typeparam name="TTarget"&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The type of target that will be subject for invocation.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/typeparam&amp;gt;
    /// &amp;lt;typeparam name="TResult"&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The type of result from the method invocation.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/typeparam&amp;gt;
    /// &amp;lt;param name="target"&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;An instance of &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;paramref name="{TTarget}"/&amp;gt; &amp;lt;/param&amp;gt;
    /// &amp;lt;param name="func"&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;Represents the method to be invoked asynchronously.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/param&amp;gt;
    /// &amp;lt;param name="callBack"&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;Represents the method to be called after method has been executed.&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/param&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue"&gt;public void &lt;/span&gt;AsyncInvoke&amp;lt;TTarget, TResult&amp;gt;(TTarget target, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;TTarget, TResult&amp;gt; func, &lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;TResult&amp;gt; callBack)
    {           
        &lt;span style="color: #2b91af"&gt;AsyncOperation &lt;/span&gt;asyncOperation = &lt;span style="color: #2b91af"&gt;AsyncOperationManager&lt;/span&gt;.CreateOperation(&lt;span style="color: blue"&gt;null&lt;/span&gt;);
        &lt;span style="color: #2b91af"&gt;ThreadPool&lt;/span&gt;.QueueUserWorkItem(state =&amp;gt;
                                         {
                                             TResult result = func(target);
                                             asyncOperation.Post(argument =&amp;gt; callBack(result), &lt;span style="color: blue"&gt;null&lt;/span&gt;);
                                             asyncOperation.OperationCompleted();
                                         });
    }

    &lt;span style="color: blue"&gt;#endregion
&lt;/span&gt;}&lt;/pre&gt;
&lt;p&gt;As you may notice, the &lt;span style="color: #2b91af"&gt;AsyncOperationManager&lt;/span&gt; comes in very handy in this scenario. In short it takes care of executing the callback delegate on the calling thread.&lt;br&gt;Actually this is pretty much the same way the BackGroundWorker handles the RunWorkerCompleted event.&lt;br&gt;The observant reader may have noticed the Implements attribute at the class level. It is just a configuration attribute so that my favoritte IOC container can gain awareness over this type.&lt;/p&gt;
&lt;p&gt;TDD and IOC(Inversion of Control) goes hand in hand and I strongly recommend that you check out the &lt;a href="http://code.google.com/p/linfu/"&gt;LinFu&lt;/a&gt; framework for a container that actually is understandable. It provides a very simple API and has a 100 percent test coverage. The project owner's name is Philip Laureano and he is among, if not the most inspiring individual I have met in my 15 years as a developer.&amp;nbsp; My thanks goes out to you, my friend. &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4479279983457560946-1439280000078382215?l=bernhard-richter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bernhard-richter.blogspot.com/feeds/1439280000078382215/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4479279983457560946&amp;postID=1439280000078382215' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4479279983457560946/posts/default/1439280000078382215'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4479279983457560946/posts/default/1439280000078382215'/><link rel='alternate' type='text/html' href='http://bernhard-richter.blogspot.com/2008/09/asynchronous-method-invocation.html' title='Asynchronous Method Invocation'/><author><name>Bernhard Richter</name><uri>http://www.blogger.com/profile/12224724359174112000</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4479279983457560946.post-6041994504811375904</id><published>2007-11-29T13:39:00.000-08:00</published><updated>2007-11-29T13:40:34.715-08:00</updated><title type='text'>Welcome to my new blog</title><content type='html'>&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4479279983457560946-6041994504811375904?l=bernhard-richter.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bernhard-richter.blogspot.com/feeds/6041994504811375904/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4479279983457560946&amp;postID=6041994504811375904' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4479279983457560946/posts/default/6041994504811375904'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4479279983457560946/posts/default/6041994504811375904'/><link rel='alternate' type='text/html' href='http://bernhard-richter.blogspot.com/2007/11/welcome-to-my-new-blog.html' title='Welcome to my new blog'/><author><name>Bernhard Richter</name><uri>http://www.blogger.com/profile/12224724359174112000</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
