<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>C#</title>
        <link>http://geekswithblogs.net/Podwysocki/category/4491.aspx</link>
        <description>C#</description>
        <language>en-US</language>
        <copyright>Matthew Podwysocki</copyright>
        <managingEditor>matthew.podwysocki@gmail.com</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <item>
            <title>Adventures in F# - F# 101 Part 8 (Mutables and Reference Cells)</title>
            <link>http://geekswithblogs.net/Podwysocki/archive/2008/04/15/121272.aspx</link>
            <description>Time for another adventure in F#, covering some of the basics of functional programming and F# in particular.Â  Today we'll manage to look more at regular .NET integration and .NET programming.Â  With the previous efforts, we've looked more at &lt;a href="http://en.wikipedia.org/wiki/Functional_programming"&gt;functional programming&lt;/a&gt; and in turn F# specific things, but want to show that you can do anything normally in F# that you can in C#.Â  To me, F# is the perfect all-purpose language because it can do a lot of the things C# can do, but in turn, F# can do things much more elegantly than C# can, such as &lt;a href="http://en.wikipedia.org/wiki/Pattern_matching"&gt;Pattern Matching&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt; Where We Are&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Before we begin today, let's catch up to where we are today:&lt;br /&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;a mce_href="http://weblogs.asp.net/podwysocki/archive/2008/03/17/adventures-in-f-f-101-part-1.aspx" href="http://weblogs.asp.net/podwysocki/archive/2008/03/17/adventures-in-f-f-101-part-1.aspx"&gt;Part 1 - Basic functional programming&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a mce_href="http://weblogs.asp.net/podwysocki/archive/2008/03/17/adventures-in-f-f-101-part-2.aspx" href="http://weblogs.asp.net/podwysocki/archive/2008/03/17/adventures-in-f-f-101-part-2.aspx"&gt;Part 2 - Currying and Tuples&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a mce_href="http://weblogs.asp.net/podwysocki/archive/2008/03/17/adventures-in-f-f-101-part-3.aspx" href="http://weblogs.asp.net/podwysocki/archive/2008/03/17/adventures-in-f-f-101-part-3.aspx"&gt;Part 3 - Scope, Recursion and Anonymous Functions&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a mce_href="http://weblogs.asp.net/podwysocki/archive/2008/03/17/adventures-in-f-f-101-part-4.aspx" href="http://weblogs.asp.net/podwysocki/archive/2008/03/17/adventures-in-f-f-101-part-4.aspx"&gt;Part 4 - History of F#, Operators and Lists&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a mce_href="http://weblogs.asp.net/podwysocki/archive/2008/03/17/adventures-in-f-f-101-part-5-pattern-matching.aspx" href="http://weblogs.asp.net/podwysocki/archive/2008/03/17/adventures-in-f-f-101-part-5-pattern-matching.aspx"&gt;Part 5 - Pattern Matching&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://weblogs.asp.net/podwysocki/archive/2008/03/21/adventures-in-f-f-101-part-6-lazy-evaluation.aspx"&gt;Part 6 - Lazy Evaluation&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://weblogs.asp.net/podwysocki/archive/2008/04/02/adventures-in-f-f-101-part-7-creating-types.aspx"&gt;Part 7 - Creating Types&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
Today's topic will be covering imperative and object oriented programming in F#.Â  There is a lot to cover, so let's get started.Â  But there are a few administrative things to get out of the way first.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Learning F# ala Ted Neward?&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://www.tedneward.com/"&gt;Ted Neward&lt;/a&gt; recently announced on &lt;a href="http://www.dotnetrocks.com/default.aspx?showNum=332"&gt;DotNetRocks Episode 332&lt;/a&gt;Â  that he's in the process of creating a class for F# for &lt;a href="http://www.pluralsight.com/"&gt;Pluralsight&lt;/a&gt;.Â  That should be interesting to those who are interested in this series, as well as F# in general.Â  Right now the community is rather small, so efforts like this should be rather rewarding I would hope.Â  Ted's a pretty brilliant guy, so I'd imagine only the best.Â  I'm hoping more details come out soon.Â  &lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Pattern Matching in C#&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Part of this series is intended to bring such concepts as Pattern Matching, &lt;a href="http://en.wikipedia.org/wiki/Currying"&gt;Currying&lt;/a&gt; and other Functional Programming concepts to the C# developer.Â  After all, the more C# language evolves, the more it seems to fall into the Functional Programming category.Â  In previous posts, I showed how to relate currying to C# and it was less elegant than F# to say the least.&lt;br /&gt;
&lt;br /&gt;
But, let's look at Pattern Matching.Â  &lt;a href="http://community.bartdesmet.net/blogs/bart/"&gt;Bart De Smet&lt;/a&gt; has been posting recently on his blog about bringing the beauty of pattern matching to C#.Â  So far it's been a good six posts into it and I urge you to go ahead and take a look at this series.Â  &lt;br /&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;a href="http://community.bartdesmet.net/blogs/bart/archive/2008/04/06/pattern-matching-in-c-part-0.aspx"&gt;Pattern Matching in C# - Part 0&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://community.bartdesmet.net/blogs/bart/archive/2008/04/07/pattern-matching-in-c-part-1.aspx"&gt;Pattern Matching in C# - Part 1&lt;/a&gt;&lt;a href="http://community.bartdesmet.net/blogs/bart/archive/2008/04/06/pattern-matching-in-c-part-0.aspx"&gt;&lt;br /&gt;
    &lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://community.bartdesmet.net/blogs/bart/archive/2008/04/08/pattern-matching-in-c-part-2.aspx"&gt;Pattern Matching in C# - Part 2&lt;br /&gt;
    &lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://community.bartdesmet.net/blogs/bart/archive/2008/04/09/pattern-matching-in-c-part-3.aspx"&gt;Pattern Matching in C# - Part 3&lt;/a&gt;&lt;a href="http://community.bartdesmet.net/blogs/bart/archive/2008/04/06/pattern-matching-in-c-part-0.aspx"&gt;&lt;br /&gt;
    &lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://community.bartdesmet.net/blogs/bart/archive/2008/04/10/pattern-matching-in-c-part-4.aspx"&gt;Pattern Matching in C# - Part 4&lt;br /&gt;
    &lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://community.bartdesmet.net/blogs/bart/archive/2008/04/11/pattern-matching-in-c-part-5.aspx"&gt;Pattern Matching in C# - Part 5&lt;br /&gt;
    &lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://community.bartdesmet.net/blogs/bart/archive/2008/04/14/pattern-matching-in-c-part-6.aspx"&gt;Pattern Matching in C# - Part 6&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://community.bartdesmet.net/blogs/bart/archive/2008/04/15/pattern-matching-in-c-part-7.aspx"&gt;Pattern Matching in C# - Part 7&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
But when you read the series, it's all about getting into the low level and compiling expression trees to make the same simple beauty that is F#.Â  Sure, it can be done in C#, but nowhere near as elegant.Â  Performance is another issue that comes to mind with these.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Imperative Programming in F#&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
This section I'll lay out some of the basics of imperative style programming before I get into the full object oriented approach to programming.Â  So, we'll cover just a few topics and then I'll feel comfortable moving onto the real parts of creating classes and such.Â  We'll cover such things as void types and mutability in this section.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;The unit Type&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
One of the first things I forgot to mention when describing F# functions and values in the unit type.Â  Think of this as the void type in C# that you are used to.Â  It's the type that doesn't accept or return a value.Â  First, let's look at the typical C# program with the void type for Hello World.&lt;br /&gt;
&lt;br /&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Monaco;}}{\colortbl;??\red255\green255\blue255;\red40\green40\blue40;\red255\green128\blue0;\red255\green255\blue0;\red0\green255\blue0;}??\fs16 \cf1\cb2\highlight2     \cf3 class\cf1  \cf4 Program\par ??\cf1     \{\par ??        \cf3 static\cf1  \cf3 void\cf1  Main(\cf3 string\cf1 [] args)\par ??        \{\par ??            \cf4 Console\cf1 .WriteLine(\cf5 "Hello World"\cf1 );\par ??        \}\par ??    \}}
--&gt;
&lt;div style="background: rgb(40, 40, 40) none repeat scroll 0% 50%; font-family: Monaco; font-size: 8pt; color: white; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;class&lt;/span&gt; &lt;span style="color: yellow;"&gt;Program&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;void&lt;/span&gt; Main(&lt;span style="color: rgb(255, 128, 0);"&gt;string&lt;/span&gt;[] args)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  &lt;span style="color: yellow;"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: lime;"&gt;"Hello World"&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  }&lt;/p&gt;
&lt;/div&gt;
&lt;br /&gt;
Now, let's go ahead and look at it from the F# perspective. &lt;br /&gt;
&lt;br /&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Monaco;}}{\colortbl;??\red255\green255\blue255;\red40\green40\blue40;\red255\green128\blue0;}??\fs16 \cf1\cb2\highlight2 #light\par ??\par ??\cf3 let\cf1  \cf3 rec\cf1  fib n = \par ??  \cf3 match\cf1  n \cf3 with\par ??\cf1   | 0 \cf3 -&amp;gt;\cf1  1\par ??  | 1 \cf3 -&amp;gt;\cf1  1\par ??  | x \cf3 -&amp;gt;\cf1  fib(n - 2) + fib(n - 1)\par ??  \par ??print_any (fib (-1))}
--&gt;
&lt;div style="background: rgb(40, 40, 40) none repeat scroll 0% 50%; font-family: Monaco; font-size: 8pt; color: white; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;
&lt;p style="margin: 0px;"&gt;#light&lt;br /&gt;
&lt;br /&gt;
&lt;span style="color: rgb(255, 128, 0);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt; &lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;/span&gt;main() =&lt;br /&gt;
Â  printfn &lt;span style="color: rgb(0, 255, 0);"&gt;"Hello World"&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
main()&lt;br /&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;br /&gt;
As you will see when you hover over our code is that it is a unit type.Â  That in itself isn't all that interesting.Â   But, what we'll run into is problems when functions return a value, but we're not all that interested in them.Â  What happens?Â  Well, F# will complain that your return value isn't compatible with the unit type, which is essentially true.Â  So, how do you get around that?Â  Let's walk through a simple unit test of a Stack written in F# and unit testing with the &lt;a href="http://codeplex.com/xunit"&gt;xUnit.net&lt;/a&gt; framework.&lt;br /&gt;
&lt;br /&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Monaco;}}{\colortbl;??\red255\green255\blue255;\red40\green40\blue40;\red255\green128\blue0;}??\fs16 \cf1\cb2\highlight2 #light\par ??\par ??\cf3 let\cf1  \cf3 rec\cf1  fib n = \par ??  \cf3 match\cf1  n \cf3 with\par ??\cf1   | 0 \cf3 -&amp;gt;\cf1  1\par ??  | 1 \cf3 -&amp;gt;\cf1  1\par ??  | x \cf3 -&amp;gt;\cf1  fib(n - 2) + fib(n - 1)\par ??  \par ??print_any (fib (-1))}
--&gt;
&lt;div style="background: rgb(40, 40, 40) none repeat scroll 0% 50%; font-family: Monaco; font-size: 8pt; color: white; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;
&lt;p style="margin: 0px;"&gt;#light&lt;br /&gt;
&lt;br /&gt;
#R &lt;span style="color: rgb(0, 255, 0);"&gt;@"D:\Tools\xunit-build-1252\xunit.dll"&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="color: rgb(255, 153, 0);"&gt;open &lt;/span&gt;System&lt;br /&gt;
&lt;span style="color: rgb(255, 153, 0);"&gt;open &lt;/span&gt;System.Collections.Generic&lt;br /&gt;
&lt;span style="color: rgb(255, 153, 0);"&gt;open &lt;/span&gt;Xunit&lt;br /&gt;
&lt;br /&gt;
&lt;span style="color: rgb(255, 153, 0);"&gt;type &lt;/span&gt;Stack&amp;lt;'t&amp;gt; = &lt;span style="color: rgb(255, 153, 0);"&gt;class&lt;/span&gt;&lt;br /&gt;
Â  &lt;span style="color: rgb(255, 153, 0);"&gt;val &lt;/span&gt;elements : LinkedList&amp;lt;'t&amp;gt;&lt;br /&gt;
Â &lt;br /&gt;
Â  &lt;span style="color: rgb(255, 153, 0);"&gt;new&lt;/span&gt;() = { elements = &lt;span style="color: rgb(255, 153, 0);"&gt;new &lt;/span&gt;LinkedList&amp;lt;'t&amp;gt;() }&lt;br /&gt;
Â &lt;br /&gt;
Â  &lt;span style="color: rgb(255, 153, 0);"&gt;member &lt;/span&gt;x.IsEmpty&lt;br /&gt;
Â Â Â  &lt;span style="color: rgb(255, 153, 0);"&gt;with &lt;/span&gt;get() = x.elements.Count = 0&lt;br /&gt;
Â Â  &lt;br /&gt;
Â  &lt;span style="color: rgb(255, 153, 0);"&gt;member &lt;/span&gt;x.Push element =&lt;br /&gt;
Â Â Â  x.elements.AddFirst(element:'t)&lt;br /&gt;
Â Â  &lt;br /&gt;
Â  &lt;span style="color: rgb(255, 153, 0);"&gt;member &lt;/span&gt;x.Top&lt;br /&gt;
Â Â Â  &lt;span style="color: rgb(255, 153, 0);"&gt;with &lt;/span&gt;get() =&lt;br /&gt;
Â Â Â Â Â  &lt;span style="color: rgb(255, 153, 0);"&gt;if &lt;/span&gt;x.elements.Count = 0 &lt;span style="color: rgb(255, 153, 0);"&gt;then&lt;/span&gt;&lt;br /&gt;
Â Â Â Â Â Â Â  raise (InvalidOperationException("cannot top an empty stack"))&lt;br /&gt;
Â Â Â Â Â  x.elements.First.Value&lt;br /&gt;
Â Â Â Â  &lt;br /&gt;
Â  &lt;span style="color: rgb(255, 153, 0);"&gt;member &lt;/span&gt;x.Pop() =&lt;br /&gt;
Â Â Â  let top = x.Top&lt;br /&gt;
Â Â Â  x.elements.RemoveFirst()&lt;br /&gt;
Â Â Â  top&lt;br /&gt;
&lt;span style="color: rgb(255, 153, 0);"&gt;end&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
[&amp;lt;Fact&amp;gt;]Â  &lt;br /&gt;
&lt;span style="color: rgb(255, 128, 0);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt; &lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;/span&gt;PopEmpty () =&lt;br /&gt;
Â  &lt;span style="color: rgb(255, 128, 0);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt; &lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;/span&gt;stack = &lt;span style="color: rgb(255, 153, 0);"&gt;new&lt;/span&gt; Stack&amp;lt;string&amp;gt;()&lt;br /&gt;
Â  Assert.Throws&amp;lt;InvalidOperationException&amp;gt;(&lt;span style="color: rgb(255, 153, 0);"&gt;fun &lt;/span&gt;() -&amp;gt; stack.Pop() |&amp;gt; ignore )&lt;br /&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;br /&gt;
The real interesting part you should pay attention to is the last line.Â  As you can see, I am using the forward operator to indicate that I really don't care what the function returns, just that I'm interested in that it executes.Â  This is most likely during such functions that have some sort of side effect to them.Â  I could also use the ignore function instead of the forward operator such as this:&lt;br /&gt;
&lt;br /&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Monaco;}}{\colortbl;??\red255\green255\blue255;\red40\green40\blue40;\red255\green128\blue0;}??\fs16 \cf1\cb2\highlight2 #light\par ??\par ??\cf3 let\cf1  \cf3 rec\cf1  fib n = \par ??  \cf3 match\cf1  n \cf3 with\par ??\cf1   | 0 \cf3 -&amp;gt;\cf1  1\par ??  | 1 \cf3 -&amp;gt;\cf1  1\par ??  | x \cf3 -&amp;gt;\cf1  fib(n - 2) + fib(n - 1)\par ??  \par ??print_any (fib (-1))}
--&gt;
&lt;div style="background: rgb(40, 40, 40) none repeat scroll 0% 50%; font-family: Monaco; font-size: 8pt; color: white; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;
&lt;p style="margin: 0px;"&gt;[&amp;lt;Fact&amp;gt;]Â  &lt;br /&gt;
&lt;span style="color: rgb(255, 153, 0);"&gt;let &lt;/span&gt;PopEmpty () =&lt;br /&gt;
Â  &lt;span style="color: rgb(255, 153, 0);"&gt;let &lt;/span&gt;stack &lt;span style="color: rgb(255, 153, 0);"&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;=&lt;/span&gt; new &lt;/span&gt;Stack&amp;lt;string&amp;gt;()&lt;br /&gt;
Â  Assert.Throws&amp;lt;InvalidOperationException&amp;gt;(&lt;span style="color: rgb(255, 153, 0);"&gt;fun &lt;/span&gt;() -&amp;gt; ignore(stack.Pop()) )&lt;br /&gt;
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â &lt;/p&gt;
&lt;/div&gt;
&lt;br /&gt;
This is very helpful in these cases where we really don't care about the return value, instead want to mutate the state of our given object, such as removing a value from a collection and so on.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Mutables&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
As I said in many posts before, by default all "variables" by which I mean values in F# are immutable.Â  This is a standard in functional programming and all in the ML family.Â  You can easily redefine a value by using the let keyword, but not actually mutate its state.Â  But, since F# is a multi-purpose language on the .NET platform, mutable state can be had.Â  To take advantage of this, mark your value as mutable.Â  Then to change the value, just use the &amp;lt;- operator to reassign the value.Â  Below is a simple example of this:&lt;br /&gt;
&lt;br /&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Monaco;}}{\colortbl;??\red255\green255\blue255;\red40\green40\blue40;\red255\green128\blue0;}??\fs16 \cf1\cb2\highlight2 #light\par ??\par ??\cf3 let\cf1  \cf3 rec\cf1  fib n = \par ??  \cf3 match\cf1  n \cf3 with\par ??\cf1   | 0 \cf3 -&amp;gt;\cf1  1\par ??  | 1 \cf3 -&amp;gt;\cf1  1\par ??  | x \cf3 -&amp;gt;\cf1  fib(n - 2) + fib(n - 1)\par ??  \par ??print_any (fib (-1))}
--&gt;
&lt;div style="background: rgb(40, 40, 40) none repeat scroll 0% 50%; font-family: Monaco; font-size: 8pt; color: white; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;
&lt;p style="margin: 0px;"&gt;#light&lt;br /&gt;
&lt;br /&gt;
&lt;span style="color: rgb(255, 153, 0);"&gt;let mutable&lt;/span&gt; answer = 42&lt;br /&gt;
printfn &lt;span style="color: rgb(0, 255, 0);"&gt;"Answer is %i"&lt;/span&gt; answer&lt;br /&gt;
answer &amp;lt;- 8&lt;br /&gt;
printfn &lt;span style="color: rgb(0, 255, 0);"&gt;"Answer is %i"&lt;/span&gt; answer&lt;br /&gt;
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â &lt;/p&gt;
&lt;/div&gt;
&lt;br /&gt;
A key difference from the reassignment is that you cannot change the value type.Â  Whereas I can redefine answer by keep using the let keyword, I can only keep my answer in this above example of the int type.Â  &lt;br /&gt;
&lt;br /&gt;
This can also apply to record types as well where you can change the fields.Â  In the last installment, we talked about record types.Â  Well, by default there as well, the fields for the record type are immutable.Â  But, as with before, that can be changed.Â  I of course like to caution people that mutable state takes a lot of the value proposition away from the side effect free programming that you gain with F# by default.Â  But, nevertheless, you can still do it as noted below:&lt;br /&gt;
&lt;br /&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Monaco;}}{\colortbl;??\red255\green255\blue255;\red40\green40\blue40;\red255\green128\blue0;}??\fs16 \cf1\cb2\highlight2 #light\par ??\par ??\cf3 let\cf1  \cf3 rec\cf1  fib n = \par ??  \cf3 match\cf1  n \cf3 with\par ??\cf1   | 0 \cf3 -&amp;gt;\cf1  1\par ??  | 1 \cf3 -&amp;gt;\cf1  1\par ??  | x \cf3 -&amp;gt;\cf1  fib(n - 2) + fib(n - 1)\par ??  \par ??print_any (fib (-1))}
--&gt;
&lt;div style="background: rgb(40, 40, 40) none repeat scroll 0% 50%; font-family: Monaco; font-size: 8pt; color: white; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;
&lt;p style="margin: 0px;"&gt;#light&lt;br /&gt;
&lt;br /&gt;
&lt;span style="color: rgb(255, 153, 0);"&gt;type &lt;/span&gt;Person = { FirstName : string; &lt;span style="color: rgb(255, 153, 0);"&gt;mutable &lt;/span&gt;LastName : string; &lt;span style="color: rgb(255, 153, 0);"&gt;mutable &lt;/span&gt;IsMarried : bool }&lt;br /&gt;
&lt;br /&gt;
&lt;span style="color: rgb(255, 153, 0);"&gt;let &lt;/span&gt;friend = { FirstName = "Courtney"; LastName = "Cox"; IsMarried = &lt;span style="color: rgb(255, 153, 0);"&gt;false &lt;/span&gt;}&lt;br /&gt;
friend.LastName &amp;lt;- "Cox-Arquette"&lt;br /&gt;
friend.IsMarried &amp;lt;- trueÂ &lt;/p&gt;
&lt;/div&gt;
&lt;br /&gt;
What I was able to do was define a Person record and change a couple of fields while using the &amp;lt;- operator and defining the fields as mutable.Â  Yes, I could have used some scientific calculation or something, but this was easy.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Reference Cells&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The last thing I want to touch on in this post is reference cells.Â  You can think of these much as pointers in other languages or reference types.Â  These of course can be of any type.Â  The idea behind using these is to make updating fields as easy as possible.Â  As with mutable fields, you cannot change the type once it has been assigned.Â  To use these, you need to remember three basic operators&lt;br /&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;span style="font-weight: bold;"&gt;ref &lt;/span&gt;- Allocates a reference cell&lt;/li&gt;
    &lt;li&gt;&lt;span style="font-weight: bold;"&gt;:=&lt;/span&gt; - Mutates a reference cell&lt;/li&gt;
    &lt;li&gt;&lt;span style="font-weight: bold;"&gt;!&lt;/span&gt; - Reads the reference cell&lt;br /&gt;
    &lt;/li&gt;
&lt;/ul&gt;
Below is a quick example of mutation through reference cells:&lt;br /&gt;
&lt;br /&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Monaco;}}{\colortbl;??\red255\green255\blue255;\red40\green40\blue40;\red255\green128\blue0;}??\fs16 \cf1\cb2\highlight2 #light\par ??\par ??\cf3 let\cf1  \cf3 rec\cf1  fib n = \par ??  \cf3 match\cf1  n \cf3 with\par ??\cf1   | 0 \cf3 -&amp;gt;\cf1  1\par ??  | 1 \cf3 -&amp;gt;\cf1  1\par ??  | x \cf3 -&amp;gt;\cf1  fib(n - 2) + fib(n - 1)\par ??  \par ??print_any (fib (-1))}
--&gt;
&lt;div style="background: rgb(40, 40, 40) none repeat scroll 0% 50%; font-family: Monaco; font-size: 8pt; color: white; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;
&lt;p style="margin: 0px;"&gt;#light&lt;br /&gt;
&lt;br /&gt;
&lt;span style="color: rgb(255, 153, 0);"&gt;let &lt;/span&gt;x = ref 2&lt;br /&gt;
x := !x + 25&lt;br /&gt;
printfn &lt;span style="color: rgb(0, 255, 0);"&gt;"%i"&lt;/span&gt; !x&lt;br /&gt;
&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â &lt;/p&gt;
&lt;/div&gt;
&lt;br /&gt;
What the code example above lets me do is define a reference to the number 2.Â  Then I can change that reference by reading the current x value and adding 25.Â  Then I mutate the existing x value with the result.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Conclusion&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
This is just meant to be a brief overview to some imperative programming constructs that you might see in .NET, Java, C, C++ and so on.Â  F# is a first class language all the way with constructs that support these things as well as your normal functional programming constructs.Â  I hope we get to cover some of this at &lt;a href="http://altdotnet.org/events/seattle"&gt;ALT.NET Open Spaces, Seattle&lt;/a&gt; at some point because I'm sure a lot of people will be interested.Â  Until next time...
&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://geekswithblogs.net/Podwysocki/archive/2008/04/15/121272.aspx"&gt;&lt;img border="0" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://geekswithblogs.net/Podwysocki/archive/2008/04/15/121272.aspx" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=121272"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=121272" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/Podwysocki/aggbug/121272.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Matthew Podwysocki</dc:creator>
            <guid>http://geekswithblogs.net/Podwysocki/archive/2008/04/15/121272.aspx</guid>
            <pubDate>Tue, 15 Apr 2008 23:08:37 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/Podwysocki/comments/121272.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/Podwysocki/archive/2008/04/15/121272.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/Podwysocki/comments/commentRss/121272.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/Podwysocki/services/trackbacks/121272.aspx</trackback:ping>
        </item>
        <item>
            <title>xUnit.net RC3 Just Released</title>
            <link>http://geekswithblogs.net/Podwysocki/archive/2008/04/10/121177.aspx</link>
            <description>Well, &lt;a href="http://bradwilson.typepad.com/blog/"&gt;Brad Wilson&lt;/a&gt; and &lt;a href="http://blogs.msdn.com/jamesnewkirk/"&gt;Jim Newkirk&lt;/a&gt; must really be busy lately.Â  After I talked about the release of xUnit.net RC2, just today, Brad announced the release of RC3.Â  As always, you can find the latest bits &lt;a href="http://codeplex.com/xunit"&gt;here&lt;/a&gt;.Â  This fixes a number of bugs and adds &lt;a href="http://ccnet.thoughtworks.com/"&gt;CruiseControl.NET&lt;/a&gt; and &lt;a href="http://asp.net/mvc/"&gt;ASP.NET MVC Preview 2&lt;/a&gt; support as well in addition to the &lt;a href="http://www.jetbrains.com/resharper/"&gt;Resharper 3.1&lt;/a&gt; and &lt;a href="http://www.testdriven.net/"&gt;TestDriven.NET&lt;/a&gt; support.Â  For more information about it, check out Brad's post &lt;a href="http://bradwilson.typepad.com/blog/2008/04/xunitnet-10-rc3.html"&gt;here&lt;/a&gt;.Â  More or less, they are feature complete for version 1.0 and the only that I think really is needed at this point is a decent GUI runner and that's well acknowledged as something they are working on.Â  Visual Studio integration would be nice as well...&lt;br /&gt;
&lt;br /&gt;
For my other posts in this series, check them out here:&lt;br /&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;a href="http://weblogs.asp.net/podwysocki/archive/2008/03/31/xunit-net-rc2-released.aspx"&gt;xUnit.net RC2 Released&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://weblogs.asp.net/podwysocki/archive/2008/04/04/relooking-at-xunit-net-rc2.aspx"&gt;Relooking at xUnit.net RC2&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
If you were in attendance at last night's &lt;a href="http://rocknug.org/"&gt;RockNUG&lt;/a&gt; appearance, all tests for my demos were using xUnit.net, so I am actively using it right now and will be for my &lt;a href="http://www.cmap-online.org/CodeCamp/"&gt;CMAP Code Camp&lt;/a&gt; appearance this weekend.Â  However, I did not show the GUI runner because, well, it's not there yet, and instead, the console runner works just fine, thank you.Â  So, go ahead and pick up the latest bits and give the team feedback!&lt;br /&gt;
&lt;br /&gt;
One last note regarding Brad, he was &lt;a href="http://howsoftwareisbuilt.com/2008/04/08/interview-with-brad-wilson-software-developer-officelabs-microsoft/"&gt;recently interviewed&lt;/a&gt; by &lt;a href="http://howsoftwareisbuilt.com/about-scott-swigart/"&gt;Scott Swigart&lt;/a&gt; and &lt;a href="http://howsoftwareisbuilt.com/about-sean-campbell/"&gt;Sean Campbell&lt;/a&gt; over at &lt;a href="http://howsoftwareisbuilt.com"&gt;How Software Is Built&lt;/a&gt; and gives some interesting insights in the open source world inside and outside Microsoft and his contributions to it.Â  Very good interview and well worth the time to read.
&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://geekswithblogs.net/Podwysocki/archive/2008/04/10/121177.aspx"&gt;&lt;img border="0" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://geekswithblogs.net/Podwysocki/archive/2008/04/10/121177.aspx" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=121177"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=121177" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/Podwysocki/aggbug/121177.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Matthew Podwysocki</dc:creator>
            <guid>http://geekswithblogs.net/Podwysocki/archive/2008/04/10/121177.aspx</guid>
            <pubDate>Thu, 10 Apr 2008 22:37:38 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/Podwysocki/comments/121177.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/Podwysocki/archive/2008/04/10/121177.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/Podwysocki/comments/commentRss/121177.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/Podwysocki/services/trackbacks/121177.aspx</trackback:ping>
        </item>
        <item>
            <title>Covering NUnit 2.4.7</title>
            <link>http://geekswithblogs.net/Podwysocki/archive/2008/04/01/120934.aspx</link>
            <description>It's interesting that a flurry of releases of testing frameworks have been released lately with &lt;a href="http://www.gallio.org/"&gt;Gallio&lt;/a&gt;, &lt;a href="http://codeplex.com/xunit"&gt;xUnit.net&lt;/a&gt; and of course &lt;a href="http://nunit.com/index.php"&gt;NUnit&lt;/a&gt;.&lt;a href="http://blogs.nunit.com/"&gt;Â  Charlie Poole&lt;/a&gt; recently announced on a lot of the discussion boards I belong to recently that &lt;a href="http://nunit.com/index.php?p=download"&gt;NUnit 2.4.7&lt;/a&gt; was recently released.Â  You can read all about what's been added &lt;a href="http://nunit.com/index.php?p=releaseNotes&amp;amp;r=2.4.7"&gt;here &lt;/a&gt;with the release notes.Â  &lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;What's New and Interesting?&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
What's interesting about this release?Â  Well, NUnit no longer depends on log4net, thus getting rid of some dependency issues with regards to the applications under test.Â  What's also interesting is that addition of the RowTest extension to allowÂ  multiple sets of arguments to be given to a test.Â  This is the first one that's ever been incorporated into the core of NUnit.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Static Tests in NUnit?&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
As &lt;a href="http://weblogs.asp.net/astopford/"&gt;Andy Stopford&lt;/a&gt; noted in my blog yesterday regarding xUnit.net, &lt;a href="http://sourceforge.net/mailarchive/forum.php?thread_name=004401c887ad%249e816030%246501a8c0%40ferrari&amp;amp;forum_name=nunit-developer"&gt;Charlie recently checked in code&lt;/a&gt; to allow static methods for static methods to be tests as well as Setup and TearDown to be static as well.Â  Jim Newkirk, the man behind NUnit and co-creator of xUnit.net, favors that approach of allowing statics.Â  To me, I don't care as much with regards to Startup and TearDown with statics as most of my statics for testing F# is side effect free programming.Â  As it should be with functional programming.Â  Anyhow, what I'd like to see in NUnit is the support for something that I can do in xUnit.net:&lt;br /&gt;
&lt;br /&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Monaco;}}{\colortbl;??\red255\green255\blue255;\red40\green40\blue40;\red255\green128\blue0;}??\fs16 \cf1\cb2\highlight2 #light\par ??\par ??\cf3 let\cf1  \cf3 rec\cf1  fib n = \par ??  \cf3 match\cf1  n \cf3 with\par ??\cf1   | 0 \cf3 -&amp;gt;\cf1  1\par ??  | 1 \cf3 -&amp;gt;\cf1  1\par ??  | x \cf3 -&amp;gt;\cf1  fib(n - 2) + fib(n - 1)\par ??  \par ??print_any (fib (-1))}
--&gt;
&lt;div style="background: rgb(40, 40, 40) none repeat scroll 0% 50%; font-family: Monaco; font-size: 8pt; color: white; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;
&lt;p style="margin: 0px;"&gt;#light&lt;br /&gt;
&lt;br /&gt;
#R &lt;span style="color: rgb(0, 255, 0);"&gt;@"D:\Program Files\NUnit 2.4.7\bin\nunit.core.dll"&lt;/span&gt;&lt;br /&gt;
#R &lt;span style="color: rgb(0, 255, 0);"&gt;@"D:\Program Files\NUnit 2.4.7\bin\nunit.framework.dll"&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="color: rgb(255, 128, 0);"&gt;open&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt; &lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;/span&gt;NUnit.Framework&lt;br /&gt;
&lt;br /&gt;
&lt;span style="color: rgb(255, 128, 0);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt; &lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;rec&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt; &lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;/span&gt;fib n =&lt;br /&gt;
Â  &lt;span style="color: rgb(255, 128, 0);"&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;match&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt; &lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;/span&gt;n &lt;span style="color: rgb(255, 128, 0);"&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;with&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt; &lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
Â  | 0 | 1 &lt;span style="color: rgb(255, 128, 0);"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt; &lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;&lt;/span&gt; 0&lt;br /&gt;
Â  | n &lt;span style="color: rgb(255, 128, 0);"&gt;-&amp;gt;&lt;/span&gt; fib(n-2) + fib(n-1)&lt;br /&gt;
Â  | n &lt;span style="color: rgb(255, 128, 0);"&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;when&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;&lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;/span&gt; n &amp;lt; 0 &lt;span style="color: rgb(255, 128, 0);"&gt;-&amp;gt;&lt;/span&gt; failwith &lt;span style="color: rgb(0, 255, 0);"&gt;"Cannot be less than zero"&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
[&amp;lt;TestFixture&amp;gt;]&lt;br /&gt;
&lt;span style="color: rgb(255, 128, 0);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt; &lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;&lt;/span&gt;FibOfOneShouldReturnZero() =&lt;br /&gt;
Â  &lt;span style="color: rgb(255, 128, 0);"&gt;let&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt; &lt;span style="color: rgb(255, 255, 255);"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;&lt;/span&gt;fibResult = fib(1)&lt;br /&gt;
Â  Assert.AreEqual(fibResult, 0)&lt;br /&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;br /&gt;
If I saw something like this, I'd be a happy man.Â  But until then, I can dream...&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Conclusion&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Go ahead and pick up your copy today of NUnit.Â  It's been the old standby for many of my projects throughout my career and served me quite well.Â  Next time I'll cover Gallio a little bit as I have neglected it, but it's a huge release.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://geekswithblogs.net/Podwysocki/archive/2008/04/01/120934.aspx"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://geekswithblogs.net/Podwysocki/archive/2008/04/01/120934.aspx" /&gt;&lt;/a&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=120934"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=120934" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/Podwysocki/aggbug/120934.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Matthew Podwysocki</dc:creator>
            <guid>http://geekswithblogs.net/Podwysocki/archive/2008/04/01/120934.aspx</guid>
            <pubDate>Tue, 01 Apr 2008 21:34:37 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/Podwysocki/comments/120934.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/Podwysocki/archive/2008/04/01/120934.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/Podwysocki/comments/commentRss/120934.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/Podwysocki/services/trackbacks/120934.aspx</trackback:ping>
        </item>
        <item>
            <title>IoC and Unity - Configuration Changes for the Better</title>
            <link>http://geekswithblogs.net/Podwysocki/archive/2008/03/27/120808.aspx</link>
            <description>In my &lt;a href="http://weblogs.asp.net/podwysocki/archive/2008/03/25/ioc-and-unity-the-basics-and-interception.aspx"&gt;previous post&lt;/a&gt; about Unity and IoC containers, I made note of some changes in the latest drop of the Unity Application Block.Â  As &lt;a href="http://blogs.msdn.com/agile/"&gt;Grigori Melnik&lt;/a&gt;, the PM of the Unity and Enterprise Library team noted, &lt;a href="http://blogs.msdn.com/agile/archive/2008/03/20/unity-release-date-changes-to-april-7.aspx"&gt;Unity should be released&lt;/a&gt; in its final form on April 7th, so stay tuned.Â  In the mean time, the &lt;a href="http://www.codeplex.com/unity/Release/ProjectReleases.aspx?ReleaseId=11956"&gt;latest drop of Unity&lt;/a&gt; was on March 24th, so go ahead and it pick it up.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Configuration Changes&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
As I noted from above, the public APIs really haven't changed all that much.Â  Instead, most of the efforts recently have been around performance improvements in the ObjectBuilder base and the configuration of the container itself.Â  I must admit that previous efforts left me a little cold with having to decorate my classes with the DependencyAttribute.Â  Well, you shouldn't have to do that anymore, now that the TypeInjectionElement has been added so that you can map your constructor arguments and so on.Â  Let's walk through a simple example of doing so.&lt;br /&gt;
&lt;br /&gt;
First, let's go through my basic anti-corruption container that I use for Unity and any other container that I use for registration and so on.&lt;br /&gt;
&lt;br /&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Monaco;}}{\colortbl;??\red255\green128\blue0;\red40\green40\blue40;\red255\green255\blue255;\red255\green255\blue0;\red43\green145\blue175;}??\fs16 \cf1\cb2\highlight2 namespace\cf3  UnitySamples\par ??\{\par ??    \cf1 public\cf3  \cf1 static\cf3  \cf1 class\cf3  \cf4 IoC\par ??\cf3     \{\par ??        \cf1 private\cf3  \cf1 static\cf3  \cf5 IDependencyResolver\cf3  resolver;\par ??\par ??        \cf1 public\cf3  \cf1 static\cf3  \cf1 void\cf3  Initialize(\cf5 IDependencyResolver\cf3  resolver)\par ??        \{\par ??            \cf4 IoC\cf3 .resolver = resolver;\par ??        \}\par ??\par ??        \cf1 public\cf3  \cf1 static\cf3  T Resolve&amp;lt;T&amp;gt;()\par ??        \{\par ??            \cf1 return\cf3  resolver.Resolve&amp;lt;T&amp;gt;();\par ??        \}\par ??\par ??        \cf1 public\cf3  \cf1 static\cf3  T Resolve&amp;lt;T&amp;gt;(\cf1 string\cf3  name)\par ??        \{\par ??            \cf1 return\cf3  resolver.Resolve&amp;lt;T&amp;gt;(name);\par ??        \}\par ??    \}\par ??\}\par ??}
--&gt;
&lt;div style="background: rgb(40, 40, 40) none repeat scroll 0% 50%; font-family: Monaco; font-size: 8pt; color: white; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;namespace&lt;/span&gt; UnitySamples&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;class&lt;/span&gt; &lt;span style="color: yellow;"&gt;IoC&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IDependencyResolver&lt;/span&gt; resolver;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;void&lt;/span&gt; Initialize(&lt;span style="color: rgb(43, 145, 175);"&gt;IDependencyResolver&lt;/span&gt; resolver)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  &lt;span style="color: yellow;"&gt;IoC&lt;/span&gt;.resolver = resolver;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;static&lt;/span&gt; T Resolve&amp;lt;T&amp;gt;()&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;return&lt;/span&gt; resolver.Resolve&amp;lt;T&amp;gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;static&lt;/span&gt; T Resolve&amp;lt;T&amp;gt;(&lt;span style="color: rgb(255, 128, 0);"&gt;string&lt;/span&gt; name)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;return&lt;/span&gt; resolver.Resolve&amp;lt;T&amp;gt;(name);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;br /&gt;
Remember this is just a quick spike sample of my anti-corruption container which was taken from Ayende.Â  And then in order to configure my UnityContainer through the implementation of my IDependencyResolver interface.Â  Let's take a brief look at that:&lt;br /&gt;
&lt;br /&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Monaco;}}{\colortbl;??\red255\green128\blue0;\red40\green40\blue40;\red255\green255\blue255;\red43\green145\blue175;}??\fs16 \cf1\cb2\highlight2 namespace\cf3  UnitySamples\par ??\{\par ??    \cf1 public\cf3  \cf1 interface\cf3  \cf4 IDependencyResolver\par ??\cf3     \{\par ??        T Resolve&amp;lt;T&amp;gt;(\cf1 string\cf3  name);\par ??\par ??        T Resolve&amp;lt;T&amp;gt;();\par ??    \}\par ??\}\par ??}
--&gt;
&lt;div style="background: rgb(40, 40, 40) none repeat scroll 0% 50%; font-family: Monaco; font-size: 8pt; color: white; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;namespace&lt;/span&gt; UnitySamples&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;interface&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IDependencyResolver&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  T Resolve&amp;lt;T&amp;gt;(&lt;span style="color: rgb(255, 128, 0);"&gt;string&lt;/span&gt; name);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  T Resolve&amp;lt;T&amp;gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;br /&gt;
And then the implementation of the interface for Unity would look like:&lt;br /&gt;
&lt;br /&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Monaco;}}{\colortbl;??\red255\green128\blue0;\red40\green40\blue40;\red255\green255\blue255;\red255\green255\blue0;\red43\green145\blue175;\red0\green255\blue0;}??\fs16 \cf1\cb2\highlight2 using\cf3  System.Configuration;\par ??\cf1 using\cf3  Microsoft.Practices.Unity;\par ??\cf1 using\cf3  Microsoft.Practices.Unity.Configuration;\par ??\par ??\cf1 namespace\cf3  UnitySamples\par ??\{\par ??    \cf1 public\cf3  \cf1 class\cf3  \cf4 UnityDependencyResolver\cf3 : \cf5 IDependencyResolver\par ??\cf3     \{\par ??        \cf1 private\cf3  \cf5 IUnityContainer\cf3  container;\par ??\par ??        \cf1 public\cf3  UnityDependencyResolver()\par ??        \{\par ??            container = \cf1 new\cf3  \cf4 UnityContainer\cf3 ();\par ??            \cf4 UnityConfigurationSection\cf3  section = (\cf4 UnityConfigurationSection\cf3 )\cf4 ConfigurationManager\cf3 .GetSection(\cf6 "unity"\cf3 );\par ??            section.Containers.Default.Configure(container);\par ??        \}\par ??\par ??        \cf1 public\cf3  T Resolve&amp;lt;T&amp;gt;()\par ??        \{\par ??            \cf1 return\cf3  container.Resolve&amp;lt;T&amp;gt;();\par ??        \}\par ??\par ??        \cf1 public\cf3  T Resolve&amp;lt;T&amp;gt;(\cf1 string\cf3  name)\par ??        \{\par ??            \cf1 return\cf3  container.Resolve&amp;lt;T&amp;gt;(name);\par ??        \}\par ??    \}\par ??\}\par ??}
--&gt;
&lt;div style="background: rgb(40, 40, 40) none repeat scroll 0% 50%; font-family: Monaco; font-size: 8pt; color: white; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;using&lt;/span&gt; System.Configuration;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;using&lt;/span&gt; Microsoft.Practices.Unity;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;using&lt;/span&gt; Microsoft.Practices.Unity.Configuration;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;namespace&lt;/span&gt; UnitySamples&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;class&lt;/span&gt; &lt;span style="color: yellow;"&gt;UnityDependencyResolver&lt;/span&gt;: &lt;span style="color: rgb(43, 145, 175);"&gt;IDependencyResolver&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IUnityContainer&lt;/span&gt; container;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; UnityDependencyResolver()&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  container = &lt;span style="color: rgb(255, 128, 0);"&gt;new&lt;/span&gt; &lt;span style="color: yellow;"&gt;UnityContainer&lt;/span&gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  &lt;span style="color: yellow;"&gt;UnityConfigurationSection&lt;/span&gt; section = (&lt;span style="color: yellow;"&gt;UnityConfigurationSection&lt;/span&gt;)&lt;span style="color: yellow;"&gt;ConfigurationManager&lt;/span&gt;.GetSection(&lt;span style="color: lime;"&gt;"unity"&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  section.Containers.Default.Configure(container);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; T Resolve&amp;lt;T&amp;gt;()&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;return&lt;/span&gt; container.Resolve&amp;lt;T&amp;gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; T Resolve&amp;lt;T&amp;gt;(&lt;span style="color: rgb(255, 128, 0);"&gt;string&lt;/span&gt; name)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;return&lt;/span&gt; container.Resolve&amp;lt;T&amp;gt;(name);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;br /&gt;
Now, if you look at the constructor for the UnityDependencyResolver above, I am using the default container in order to configure my container.Â  But, I have the option of specifying a name for it as well or even just an index.Â  I could just easily change that code to this and it would work if I name my container default.Â  This is a little bit of a change from before when I had to use the GetConfigCommand() method in order to configure the container which was a bit too chatty for my tastes. &lt;br /&gt;
&lt;br /&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Monaco;}}{\colortbl;??\red255\green255\blue255;\red40\green40\blue40;\red255\green128\blue0;\red255\green255\blue0;\red0\green255\blue0;}??\fs16 \cf1\cb2\highlight2             container = \cf3 new\cf1  \cf4 UnityContainer\cf1 ();\par ??            \cf4 UnityConfigurationSection\cf1  section = (\cf4 UnityConfigurationSection\cf1 )\cf4 ConfigurationManager\cf1 .GetSection(\cf5 "unity"\cf1 );\par ??            section.Containers[\cf5 "default"\cf1 ].Configure(container);}
--&gt;
&lt;div style="background: rgb(40, 40, 40) none repeat scroll 0% 50%; font-family: Monaco; font-size: 8pt; color: white; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  container = &lt;span style="color: rgb(255, 128, 0);"&gt;new&lt;/span&gt; &lt;span style="color: yellow;"&gt;UnityContainer&lt;/span&gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  &lt;span style="color: yellow;"&gt;UnityConfigurationSection&lt;/span&gt; section = (&lt;span style="color: yellow;"&gt;UnityConfigurationSection&lt;/span&gt;)&lt;span style="color: yellow;"&gt;ConfigurationManager&lt;/span&gt;.GetSection(&lt;span style="color: lime;"&gt;"unity"&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  section.Containers[&lt;span style="color: lime;"&gt;"default"&lt;/span&gt;].Configure(container);&lt;/p&gt;
&lt;/div&gt;
&lt;br /&gt;
So, the idea that I want to do is create an object graph so that I have classes with dependencies that have dependencies.Â  Without doing AOP and some basic interception, we could take the approach of keeping around something like an IContext which would have our cross-cutting concerns in one location such as logging and whatnot in one area so your objects don't sit there with 16 constructor parameters, and instead has a context from which it can pull.Â  This approach has worked for me in the past, so let's just go through that one right now.&lt;br /&gt;
&lt;br /&gt;
First, let's look at the context that has those cross-cutting concerns and the actual implementation:&lt;br /&gt;
&lt;br /&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Monaco;}}{\colortbl;??\red255\green128\blue0;\red40\green40\blue40;\red255\green255\blue255;\red43\green145\blue175;}??\fs16 \cf1\cb2\highlight2 namespace\cf3  UnitySamples\par ??\{\par ??    \cf1 public\cf3  \cf1 interface\cf3  \cf4 IContext\par ??\cf3     \{\par ??        \cf4 ILogger\cf3  Logger \{ \cf1 get\cf3 ; \}\par ??    \}\par ??\}\par ??}
--&gt;
&lt;div style="background: rgb(40, 40, 40) none repeat scroll 0% 50%; font-family: Monaco; font-size: 8pt; color: white; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;namespace&lt;/span&gt; UnitySamples&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;interface&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IContext&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &lt;span style="color: rgb(43, 145, 175);"&gt;ILogger&lt;/span&gt; Logger { &lt;span style="color: rgb(255, 128, 0);"&gt;get&lt;/span&gt;; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;br /&gt;
And then the concrete implementation might look something like this:&lt;br /&gt;
&lt;br /&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Monaco;}}{\colortbl;??\red255\green128\blue0;\red40\green40\blue40;\red255\green255\blue255;\red255\green255\blue0;\red43\green145\blue175;}??\fs16 \cf1\cb2\highlight2 namespace\cf3  UnitySamples\par ??\{\par ??    \cf1 public\cf3  \cf1 class\cf3  \cf4 UnityContext\cf3  : \cf5 IContext\par ??\cf3     \{\par ??        \cf1 private\cf3  \cf1 readonly\cf3  \cf5 ILogger\cf3  logger;\par ??\par ??        \cf1 public\cf3  UnityContext(\cf5 ILogger\cf3  logger)\par ??        \{\par ??            \cf1 this\cf3 .logger = logger;\par ??        \}\par ??\par ??        \cf1 public\cf3  \cf5 ILogger\cf3  Logger\par ??        \{\par ??            \cf1 get\cf3  \{ \cf1 return\cf3  logger; \}\par ??        \}\par ??    \}\par ??\}\par ??}
--&gt;
&lt;div style="background: rgb(40, 40, 40) none repeat scroll 0% 50%; font-family: Monaco; font-size: 8pt; color: white; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;namespace&lt;/span&gt; UnitySamples&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;class&lt;/span&gt; &lt;span style="color: yellow;"&gt;UnityContext&lt;/span&gt; : &lt;span style="color: rgb(43, 145, 175);"&gt;IContext&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;readonly&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ILogger&lt;/span&gt; logger;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; UnityContext(&lt;span style="color: rgb(43, 145, 175);"&gt;ILogger&lt;/span&gt; logger)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;this&lt;/span&gt;.logger = logger;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ILogger&lt;/span&gt; Logger&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;get&lt;/span&gt; { &lt;span style="color: rgb(255, 128, 0);"&gt;return&lt;/span&gt; logger; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;br /&gt;
So, what I do here is inject my logger into my context, and probably anything else that might be cross-cutting as well.Â  So, now in one of my classes, then I can accept the IContext in to do what I need it to do.Â  That would look something like this.&lt;br /&gt;
&lt;br /&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Monaco;}}{\colortbl;??\red255\green128\blue0;\red40\green40\blue40;\red255\green255\blue255;\red255\green255\blue0;\red43\green145\blue175;\red0\green255\blue0;}??\fs16 \cf1\cb2\highlight2 namespace\cf3  UnitySamples\par ??\{\par ??    \cf1 public\cf3  \cf1 class\cf3  \cf4 Customer\par ??\cf3     \{\par ??        \cf1 public\cf3  \cf1 string\cf3  CustomerId \{ \cf1 get\cf3 ; \cf1 set\cf3 ; \}\par ??\par ??        \cf1 public\cf3  \cf1 string\cf3  FirstName \{ \cf1 get\cf3 ; \cf1 set\cf3 ; \}\par ??\par ??        \cf1 public\cf3  \cf1 string\cf3  MiddleName \{ \cf1 get\cf3 ; \cf1 set\cf3 ; \}\par ??\par ??        \cf1 public\cf3  \cf1 string\cf3  LastName \{ \cf1 get\cf3 ; \cf1 set\cf3 ; \}\par ??    \}\par ??\par ??    \cf1 public\cf3  \cf1 class\cf3  \cf4 CustomerTasks\par ??\cf3     \{\par ??        \cf1 private\cf3  \cf1 readonly\cf3  \cf5 IContext\cf3  context;\par ??\par ??        \cf1 public\cf3  CustomerTasks(\cf5 IContext\cf3  context)\par ??        \{\par ??            \cf1 this\cf3 .context = context;\par ??        \}\par ??\par ??        \cf1 public\cf3  \cf1 void\cf3  SaveCustomer(\cf4 Customer\cf3  customer)\par ??        \{\par ??            \cf6 // Save customer\par ??\cf3             context.Logger.LogEvent(\cf6 "Saving customer"\cf3 , \cf5 LogLevel\cf3 .Information);\par ??        \}\par ??    \}\par ??\}\par ??}
--&gt;
&lt;div style="background: rgb(40, 40, 40) none repeat scroll 0% 50%; font-family: Monaco; font-size: 8pt; color: white; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;namespace&lt;/span&gt; UnitySamples&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;class&lt;/span&gt; &lt;span style="color: yellow;"&gt;Customer&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;string&lt;/span&gt; CustomerId { &lt;span style="color: rgb(255, 128, 0);"&gt;get&lt;/span&gt;; &lt;span style="color: rgb(255, 128, 0);"&gt;set&lt;/span&gt;; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;string&lt;/span&gt; FirstName { &lt;span style="color: rgb(255, 128, 0);"&gt;get&lt;/span&gt;; &lt;span style="color: rgb(255, 128, 0);"&gt;set&lt;/span&gt;; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;string&lt;/span&gt; MiddleName { &lt;span style="color: rgb(255, 128, 0);"&gt;get&lt;/span&gt;; &lt;span style="color: rgb(255, 128, 0);"&gt;set&lt;/span&gt;; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;string&lt;/span&gt; LastName { &lt;span style="color: rgb(255, 128, 0);"&gt;get&lt;/span&gt;; &lt;span style="color: rgb(255, 128, 0);"&gt;set&lt;/span&gt;; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;class&lt;/span&gt; &lt;span style="color: yellow;"&gt;CustomerTasks&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;readonly&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IContext&lt;/span&gt; context;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; CustomerTasks(&lt;span style="color: rgb(43, 145, 175);"&gt;IContext&lt;/span&gt; context)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;this&lt;/span&gt;.context = context;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;void&lt;/span&gt; SaveCustomer(&lt;span style="color: yellow;"&gt;Customer&lt;/span&gt; customer)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  &lt;span style="color: lime;"&gt;// Save customer&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  context.Logger.LogEvent(&lt;span style="color: lime;"&gt;"Saving customer"&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;LogLevel&lt;/span&gt;.Information);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;br /&gt;
I'm not doing anything special, instead, just showing how this pattern might apply.Â  And then just tying it all together is my console application (sometimes my favorite UI for quick spikes).&lt;br /&gt;
&lt;br /&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Monaco;}}{\colortbl;??\red255\green128\blue0;\red40\green40\blue40;\red255\green255\blue255;\red255\green255\blue0;\red0\green255\blue0;}??\fs16 \cf1\cb2\highlight2 namespace\cf3  UnitySamples\par ??\{\par ??    \cf1 class\cf3  \cf4 Program\par ??\cf3     \{\par ??        \cf1 static\cf3  \cf1 void\cf3  Main(\cf1 string\cf3 [] args)\par ??        \{\par ??            \cf4 IoC\cf3 .Initialize(\cf1 new\cf3  \cf4 UnityDependencyResolver\cf3 ());\par ??            \cf4 CustomerTasks\cf3  tasks = \cf4 IoC\cf3 .Resolve&amp;lt;\cf4 CustomerTasks\cf3 &amp;gt;();\par ??            tasks.SaveCustomer(\cf1 new\cf3  \cf4 Customer\cf3 \{ CustomerId = \cf5 "12345"\cf3 , FirstName = \cf5 "Joe"\cf3 , LastName = \cf5 "Smith"\cf3 , MiddleName = \cf5 "Frank"\cf3 \});\par ??        \}\par ??    \}\par ??\}\par ??}
--&gt;
&lt;div style="background: rgb(40, 40, 40) none repeat scroll 0% 50%; font-family: Monaco; font-size: 8pt; color: white; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;namespace&lt;/span&gt; UnitySamples&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;class&lt;/span&gt; &lt;span style="color: yellow;"&gt;Program&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;void&lt;/span&gt; Main(&lt;span style="color: rgb(255, 128, 0);"&gt;string&lt;/span&gt;[] args)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  &lt;span style="color: yellow;"&gt;IoC&lt;/span&gt;.Initialize(&lt;span style="color: rgb(255, 128, 0);"&gt;new&lt;/span&gt; &lt;span style="color: yellow;"&gt;UnityDependencyResolver&lt;/span&gt;());&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  &lt;span style="color: yellow;"&gt;CustomerTasks&lt;/span&gt; tasks = &lt;span style="color: yellow;"&gt;IoC&lt;/span&gt;.Resolve&amp;lt;&lt;span style="color: yellow;"&gt;CustomerTasks&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  tasks.SaveCustomer(&lt;span style="color: rgb(255, 128, 0);"&gt;new&lt;/span&gt; &lt;span style="color: yellow;"&gt;Customer&lt;/span&gt;{ CustomerId = &lt;span style="color: lime;"&gt;"12345"&lt;/span&gt;, FirstName = &lt;span style="color: lime;"&gt;"Joe"&lt;/span&gt;, LastName = &lt;span style="color: lime;"&gt;"Smith"&lt;/span&gt;, MiddleName = &lt;span style="color: lime;"&gt;"Frank"&lt;/span&gt;});&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;br /&gt;
But the more interesting part about this is the XML configuration.Â  I know many people such as Ayende have declared war on XML configuration, but I think for this quick example it does quite well.Â  If we start talking about complex object graphs, then I'd certainly agree and I'd rather do it programmatically.Â  But, let's first look at how I'd wire up the whole thing in the app.config file.&lt;br /&gt;
&lt;br /&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Monaco;}}{\colortbl;??\red255\green255\blue255;\red40\green40\blue40;\red255\green128\blue0;\red0\green255\blue0;}??\fs16 \cf1\cb2\highlight2 &amp;lt;?xml \cf3 version\cf1 ="\cf4 1.0\cf1 " \cf3 encoding\cf1 ="\cf4 utf-8\cf1 " ?&amp;gt;\par ??&amp;lt;configuration&amp;gt;\par ??\tab &amp;lt;configSections&amp;gt;\par ??\tab \tab &amp;lt;section \cf3 name\cf1 ="\cf4 unity\cf1 " \cf3 type\cf1 ="\cf4 Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration\cf1 " /&amp;gt;\par ??\tab &amp;lt;/configSections&amp;gt;\par ??\par ??\tab &amp;lt;unity&amp;gt;\par ??\tab \tab &amp;lt;typeAliases&amp;gt;\par ??\tab \tab \tab &amp;lt;typeAlias \cf3 alias\cf1 ="\cf4 string\cf1 " \cf3 type\cf1 ="\cf4 System.String, mscorlib\cf1 " /&amp;gt;\par ??\tab \tab \tab &amp;lt;typeAlias \cf3 alias\cf1 ="\cf4 ILogger\cf1 " \cf3 type\cf1 ="\cf4 UnitySamples.ILogger, UnitySamples\cf1 " /&amp;gt;\par ??\tab \tab \tab &amp;lt;typeAlias \cf3 alias\cf1 ="\cf4 ConsoleLogger\cf1 " \cf3 type\cf1 ="\cf4 UnitySamples.ConsoleLogger, UnitySamples\cf1 " /&amp;gt;\par ??\tab \tab \tab &amp;lt;typeAlias \cf3 alias\cf1 ="\cf4 DebugLogger\cf1 " \cf3 type\cf1 ="\cf4 UnitySamples.DebugLogger, UnitySamples\cf1 " /&amp;gt;\par ??\tab \tab \tab &amp;lt;typeAlias \cf3 alias\cf1 ="\cf4 IContext\cf1 " \cf3 type\cf1 ="\cf4 UnitySamples.IContext, UnitySamples\cf1 " /&amp;gt;\par ??\tab \tab \tab &amp;lt;typeAlias \cf3 alias\cf1 ="\cf4 UnityContext\cf1 " \cf3 type\cf1 ="\cf4 UnitySamples.UnityContext, UnitySamples\cf1 " /&amp;gt;\par ??\tab \tab \tab &amp;lt;typeAlias \cf3 alias\cf1 ="\cf4 CustomerTasks\cf1 " \cf3 type\cf1 ="\cf4 UnitySamples.CustomerTasks, UnitySamples\cf1 " /&amp;gt;\par ??\tab \tab &amp;lt;/typeAliases&amp;gt;\par ??\tab \tab &amp;lt;containers&amp;gt;\par ??\tab \tab \tab &amp;lt;container&amp;gt;\par ??\tab \tab \tab \tab &amp;lt;types&amp;gt;\par ??\tab \tab \tab \tab \tab &amp;lt;type \cf3 type\cf1 ="\cf4 ILogger\cf1 " \cf3 mapTo\cf1 ="\cf4 ConsoleLogger\cf1 " \cf3 name\cf1 ="\cf4 defaultLogger\cf1 "/&amp;gt;\par ??\tab \tab \tab \tab \tab &amp;lt;type \cf3 type\cf1 ="\cf4 ILogger\cf1 " \cf3 mapTo\cf1 ="\cf4 DebugLogger\cf1 " \cf3 name\cf1 ="\cf4 debugLogger\cf1 "/&amp;gt;\par ??\tab \tab \tab \tab \tab &amp;lt;type \cf3 type\cf1 ="\cf4 IContext\cf1 " \cf3 mapTo\cf1 ="\cf4 UnityContext\cf1 "&amp;gt;\par ??\tab \tab \tab \tab \tab \tab &amp;lt;typeConfig \cf3 extensionType\cf1 ="\cf4 Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration\cf1 "&amp;gt;\par ??\tab \tab \tab \tab \tab \tab \tab &amp;lt;constructor&amp;gt;\par ??\tab \tab \tab \tab \tab \tab \tab \tab &amp;lt;param \cf3 name\cf1 ="\cf4 logger\cf1 " \cf3 parameterType\cf1 ="\cf4 ILogger\cf1 "&amp;gt;\par ??\tab \tab \tab \tab \tab \tab \tab \tab \tab &amp;lt;dependency \cf3 name\cf1 ="\cf4 debugLogger\cf1 "/&amp;gt;\par ??\tab \tab \tab \tab \tab \tab \tab \tab &amp;lt;/param&amp;gt;\par ??\tab \tab \tab \tab \tab \tab \tab &amp;lt;/constructor&amp;gt;\par ??\tab \tab \tab \tab \tab \tab &amp;lt;/typeConfig&amp;gt;\par ??\tab \tab \tab \tab \tab &amp;lt;/type&amp;gt;\par ??\tab \tab \tab \tab \tab &amp;lt;type \cf3 type\cf1 ="\cf4 CustomerTasks\cf1 "&amp;gt;\par ??\tab \tab \tab \tab \tab \tab &amp;lt;typeConfig \cf3 extensionType\cf1 ="\cf4 Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration\cf1 "&amp;gt;\par ??\tab \tab \tab \tab \tab \tab \tab &amp;lt;constructor&amp;gt;\par ??\tab \tab \tab \tab \tab \tab \tab \tab &amp;lt;param \cf3 name\cf1 ="\cf4 context\cf1 " \cf3 parameterType\cf1 ="\cf4 IContext\cf1 "&amp;gt;\par ??\tab \tab \tab \tab \tab \tab \tab \tab \tab &amp;lt;dependency/&amp;gt;\par ??\tab \tab \tab \tab \tab \tab \tab \tab &amp;lt;/param&amp;gt;\par ??\tab \tab \tab \tab \tab \tab \tab &amp;lt;/constructor&amp;gt;\par ??\tab \tab \tab \tab \tab \tab &amp;lt;/typeConfig&amp;gt;\par ??\tab \tab \tab \tab \tab &amp;lt;/type&amp;gt;\par ??\tab \tab \tab \tab &amp;lt;/types&amp;gt;\par ??\tab \tab \tab &amp;lt;/container&amp;gt;\par ??\tab \tab &amp;lt;/containers&amp;gt;\par ??\tab &amp;lt;/unity&amp;gt;\par ??&amp;lt;/configuration&amp;gt;}
--&gt;
&lt;div style="background: rgb(40, 40, 40) none repeat scroll 0% 50%; font-family: Monaco; font-size: 8pt; color: white; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;
&lt;p style="margin: 0px;"&gt;&amp;lt;?xml &lt;span style="color: rgb(255, 128, 0);"&gt;version&lt;/span&gt;="&lt;span style="color: lime;"&gt;1.0&lt;/span&gt;" &lt;span style="color: rgb(255, 128, 0);"&gt;encoding&lt;/span&gt;="&lt;span style="color: lime;"&gt;utf-8&lt;/span&gt;" ?&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;lt;configuration&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  &amp;lt;configSections&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &amp;lt;section &lt;span style="color: rgb(255, 128, 0);"&gt;name&lt;/span&gt;="&lt;span style="color: lime;"&gt;unity&lt;/span&gt;" &lt;span style="color: rgb(255, 128, 0);"&gt;type&lt;/span&gt;="&lt;span style="color: lime;"&gt;Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration&lt;/span&gt;" /&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  &amp;lt;/configSections&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  &amp;lt;unity&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &amp;lt;typeAliases&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  &amp;lt;typeAlias &lt;span style="color: rgb(255, 128, 0);"&gt;alias&lt;/span&gt;="&lt;span style="color: lime;"&gt;string&lt;/span&gt;" &lt;span style="color: rgb(255, 128, 0);"&gt;type&lt;/span&gt;="&lt;span style="color: lime;"&gt;System.String, mscorlib&lt;/span&gt;" /&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  &amp;lt;typeAlias &lt;span style="color: rgb(255, 128, 0);"&gt;alias&lt;/span&gt;="&lt;span style="color: lime;"&gt;ILogger&lt;/span&gt;" &lt;span style="color: rgb(255, 128, 0);"&gt;type&lt;/span&gt;="&lt;span style="color: lime;"&gt;UnitySamples.ILogger, UnitySamples&lt;/span&gt;" /&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  &amp;lt;typeAlias &lt;span style="color: rgb(255, 128, 0);"&gt;alias&lt;/span&gt;="&lt;span style="color: lime;"&gt;ConsoleLogger&lt;/span&gt;" &lt;span style="color: rgb(255, 128, 0);"&gt;type&lt;/span&gt;="&lt;span style="color: lime;"&gt;UnitySamples.ConsoleLogger, UnitySamples&lt;/span&gt;" /&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  &amp;lt;typeAlias &lt;span style="color: rgb(255, 128, 0);"&gt;alias&lt;/span&gt;="&lt;span style="color: lime;"&gt;DebugLogger&lt;/span&gt;" &lt;span style="color: rgb(255, 128, 0);"&gt;type&lt;/span&gt;="&lt;span style="color: lime;"&gt;UnitySamples.DebugLogger, UnitySamples&lt;/span&gt;" /&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  &amp;lt;typeAlias &lt;span style="color: rgb(255, 128, 0);"&gt;alias&lt;/span&gt;="&lt;span style="color: lime;"&gt;IContext&lt;/span&gt;" &lt;span style="color: rgb(255, 128, 0);"&gt;type&lt;/span&gt;="&lt;span style="color: lime;"&gt;UnitySamples.IContext, UnitySamples&lt;/span&gt;" /&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  &amp;lt;typeAlias &lt;span style="color: rgb(255, 128, 0);"&gt;alias&lt;/span&gt;="&lt;span style="color: lime;"&gt;UnityContext&lt;/span&gt;" &lt;span style="color: rgb(255, 128, 0);"&gt;type&lt;/span&gt;="&lt;span style="color: lime;"&gt;UnitySamples.UnityContext, UnitySamples&lt;/span&gt;" /&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  &amp;lt;typeAlias &lt;span style="color: rgb(255, 128, 0);"&gt;alias&lt;/span&gt;="&lt;span style="color: lime;"&gt;CustomerTasks&lt;/span&gt;" &lt;span style="color: rgb(255, 128, 0);"&gt;type&lt;/span&gt;="&lt;span style="color: lime;"&gt;UnitySamples.CustomerTasks, UnitySamples&lt;/span&gt;" /&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &amp;lt;/typeAliases&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &amp;lt;containers&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  &amp;lt;container&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  &amp;lt;types&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  &amp;lt;type &lt;span style="color: rgb(255, 128, 0);"&gt;type&lt;/span&gt;="&lt;span style="color: lime;"&gt;ILogger&lt;/span&gt;" &lt;span style="color: rgb(255, 128, 0);"&gt;mapTo&lt;/span&gt;="&lt;span style="color: lime;"&gt;ConsoleLogger&lt;/span&gt;" &lt;span style="color: rgb(255, 128, 0);"&gt;name&lt;/span&gt;="&lt;span style="color: lime;"&gt;defaultLogger&lt;/span&gt;"/&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  &amp;lt;type &lt;span style="color: rgb(255, 128, 0);"&gt;type&lt;/span&gt;="&lt;span style="color: lime;"&gt;ILogger&lt;/span&gt;" &lt;span style="color: rgb(255, 128, 0);"&gt;mapTo&lt;/span&gt;="&lt;span style="color: lime;"&gt;DebugLogger&lt;/span&gt;" &lt;span style="color: rgb(255, 128, 0);"&gt;name&lt;/span&gt;="&lt;span style="color: lime;"&gt;debugLogger&lt;/span&gt;"/&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  &amp;lt;type &lt;span style="color: rgb(255, 128, 0);"&gt;type&lt;/span&gt;="&lt;span style="color: lime;"&gt;IContext&lt;/span&gt;" &lt;span style="color: rgb(255, 128, 0);"&gt;mapTo&lt;/span&gt;="&lt;span style="color: lime;"&gt;UnityContext&lt;/span&gt;"&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  &amp;lt;typeConfig &lt;span style="color: rgb(255, 128, 0);"&gt;extensionType&lt;/span&gt;="&lt;span style="color: lime;"&gt;Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration&lt;/span&gt;"&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  &amp;lt;constructor&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  &amp;lt;param &lt;span style="color: rgb(255, 128, 0);"&gt;name&lt;/span&gt;="&lt;span style="color: lime;"&gt;logger&lt;/span&gt;" &lt;span style="color: rgb(255, 128, 0);"&gt;parameterType&lt;/span&gt;="&lt;span style="color: lime;"&gt;ILogger&lt;/span&gt;"&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  &amp;lt;dependency &lt;span style="color: rgb(255, 128, 0);"&gt;name&lt;/span&gt;="&lt;span style="color: lime;"&gt;debugLogger&lt;/span&gt;"/&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  &amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  &amp;lt;/constructor&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  &amp;lt;/typeConfig&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  &amp;lt;/type&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  &amp;lt;type &lt;span style="color: rgb(255, 128, 0);"&gt;type&lt;/span&gt;="&lt;span style="color: lime;"&gt;CustomerTasks&lt;/span&gt;"&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  &amp;lt;typeConfig &lt;span style="color: rgb(255, 128, 0);"&gt;extensionType&lt;/span&gt;="&lt;span style="color: lime;"&gt;Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration&lt;/span&gt;"&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  &amp;lt;constructor&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  &amp;lt;param &lt;span style="color: rgb(255, 128, 0);"&gt;name&lt;/span&gt;="&lt;span style="color: lime;"&gt;context&lt;/span&gt;" &lt;span style="color: rgb(255, 128, 0);"&gt;parameterType&lt;/span&gt;="&lt;span style="color: lime;"&gt;IContext&lt;/span&gt;"&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  &amp;lt;dependency/&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  &amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  &amp;lt;/constructor&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  &amp;lt;/typeConfig&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  &amp;lt;/type&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  &amp;lt;/types&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  &amp;lt;/container&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &amp;lt;/containers&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  &amp;lt;/unity&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;lt;/configuration&amp;gt;&lt;/p&gt;
&lt;/div&gt;
&lt;br /&gt;
As you may notice from above, Unity gives us the ability to give type aliases so that we can reference the short names instead of the long ugly fully qualified type names by using the &amp;lt;typeAliases&amp;gt;&amp;lt;typeAlias&amp;gt; nodes in the configuration.Â  Much as before, we still register our types with a name, type and so on.Â  &lt;br /&gt;
&lt;br /&gt;
But, what's interesting is that we now have the ability to do type injection through a Unity extension called the TypeInjectionElement.Â  This allows us to inject into the constructor and put in our parameters as need be.Â  But we could also replace that with &amp;lt;method&amp;gt; or &amp;lt;property&amp;gt; in order to do method injection and property setter injection respectively.Â  &lt;br /&gt;
Within the &amp;lt;param&amp;gt; element, we can specify our given injection element parameters.Â  This allows us to specify the name of our given parameter, the type, but also for the dependency check, we can specify the name and type to alias the other &amp;lt;type&amp;gt; elements that we wish to get a reference of.Â  We can also specify the values if we so desire of strings, integers and so on.Â  Below is a simple example of using both dependency references and values.&lt;br /&gt;
&lt;br /&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Monaco;}}{\colortbl;??\red255\green255\blue255;\red40\green40\blue40;\red255\green128\blue0;\red0\green255\blue0;}??\fs16 \cf1\cb2\highlight2 &amp;lt;constructor&amp;gt;\par ??\tab &amp;lt;param \cf3 name\cf1 ="\cf4 logger\cf1 " \cf3 parameterType\cf1 ="\cf4 ILogger\cf1 "&amp;gt;\par ??\tab \tab &amp;lt;dependency \cf3 name\cf1 ="\cf4 debugLogger\cf1 "/&amp;gt;\par ??\tab &amp;lt;/param&amp;gt;\par ??\tab &amp;lt;param \cf3 name\cf1 ="\cf4 dbName\cf1 " \cf3 parameterType\cf1 ="\cf4 string\cf1 "&amp;gt;\par ??\tab \tab &amp;lt;value \cf3 value\cf1 ="\cf4 AdventureWorks\cf1 " /&amp;gt;\par ??\tab &amp;lt;/param&amp;gt;\par ??&amp;lt;/constructor&amp;gt;}
--&gt;
&lt;div style="background: rgb(40, 40, 40) none repeat scroll 0% 50%; font-family: Monaco; font-size: 8pt; color: white; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;
&lt;p style="margin: 0px;"&gt;&amp;lt;constructor&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  &amp;lt;param &lt;span style="color: rgb(255, 128, 0);"&gt;name&lt;/span&gt;="&lt;span style="color: lime;"&gt;logger&lt;/span&gt;" &lt;span style="color: rgb(255, 128, 0);"&gt;parameterType&lt;/span&gt;="&lt;span style="color: lime;"&gt;ILogger&lt;/span&gt;"&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &amp;lt;dependency &lt;span style="color: rgb(255, 128, 0);"&gt;name&lt;/span&gt;="&lt;span style="color: lime;"&gt;debugLogger&lt;/span&gt;"/&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  &amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  &amp;lt;param &lt;span style="color: rgb(255, 128, 0);"&gt;name&lt;/span&gt;="&lt;span style="color: lime;"&gt;dbName&lt;/span&gt;" &lt;span style="color: rgb(255, 128, 0);"&gt;parameterType&lt;/span&gt;="&lt;span style="color: lime;"&gt;string&lt;/span&gt;"&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &amp;lt;value &lt;span style="color: rgb(255, 128, 0);"&gt;value&lt;/span&gt;="&lt;span style="color: lime;"&gt;AdventureWorks&lt;/span&gt;" /&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  &amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;lt;/constructor&amp;gt;&lt;/p&gt;
&lt;/div&gt;
&lt;br /&gt;
I find the best way to learn about these features usually isn't through documentation, although it's a nice thing to do, but instead the tests.Â  I look for the functionality that I'm interested in and go deep.Â  A project this size without good unit tests == &lt;span style="font-weight: bold;"&gt;FAIL &lt;/span&gt;in my opinion.Â  Anyhow, this is better looking than it used to be and much more intuitive.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt; Interception Revisited&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
As Chris Tavares, the developer of Unity noted on my blog before was the fact that he could easily put in the interceptors from the ObjectBuilder2 since ObjectBuilder2 is modular and Unity is built upon it.Â  It shouldn't have been news, because I've been working on that myself to get lightweight interception to work on it.Â  Maybe when I get it fully implemented I'll share it.Â  But in the mean time, it's just a spike.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt; Conclusion&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
I think this approach has helped in the XML configuration goo that had been a bit confusing.Â  I didn't want to clutter my domain models and processing code with excess attributes stating intent, and would rather keep it clean.Â  This makes a step in that direction.Â  In the next installation of looking at IoC will revolve around Spring.NET and AOP in the Enterprise, so stay tuned...Â  Until next time... &lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://geekswithblogs.net/Podwysocki/archive/2008/03/27/120808.aspx"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://geekswithblogs.net/Podwysocki/archive/2008/03/27/120808.aspx" /&gt;&lt;/a&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=120808"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=120808" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/Podwysocki/aggbug/120808.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Matthew Podwysocki</dc:creator>
            <guid>http://geekswithblogs.net/Podwysocki/archive/2008/03/27/120808.aspx</guid>
            <pubDate>Thu, 27 Mar 2008 23:52:01 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/Podwysocki/comments/120808.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/Podwysocki/archive/2008/03/27/120808.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/Podwysocki/comments/commentRss/120808.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/Podwysocki/services/trackbacks/120808.aspx</trackback:ping>
        </item>
        <item>
            <title>IoC and Unity - The Basics and InterceptionIoC and Unity - The Basics and Interception</title>
            <link>http://geekswithblogs.net/Podwysocki/archive/2008/03/26/120764.aspx</link>
            <description>I realize it's been a while since my last post on &lt;a href="http://en.wikipedia.org/wiki/Inversion_of_control"&gt;Inversion of Control&lt;/a&gt; containers and looking at Unity as one of them.Â  Since that time, &lt;a href="http://www.hanselman.com/blog/" mce_href="http://www.hanselman.com/blog/"&gt;Scott Hanselman&lt;/a&gt; linked to some of the comparisons that I did for IoC containers &lt;a href="http://www.hanselman.com/blog/ListOfNETDependencyInjectionContainersIOC.aspx" mce_href="http://www.hanselman.com/blog/ListOfNETDependencyInjectionContainersIOC.aspx"&gt;here&lt;/a&gt;.Â  I'll be the first to admit that the look was a bit naive, but to get you all interested in looking at IoC container and how they can improve your applications.Â  It was suggested &lt;a href="http://www.ayende.com/Blog/archive/2008/03/14/Evaluating-IoC-containers.aspx" mce_href="http://www.ayende.com/Blog/archive/2008/03/14/Evaluating-IoC-containers.aspx"&gt;here &lt;/a&gt;that my posts weren't a complete comparison, although in my previous posts I covered a lot of those topics.Â  Even so, after talking with &lt;a href="http://www.ayende.com/"&gt;said individual&lt;/a&gt;, I need to cover more ground on the basics and dive deeper into the details.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt; Where We Are&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Before we begin today, let's see what we've already covered in the past:&lt;br /&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;a href="http://weblogs.asp.net/podwysocki/archive/2008/03/07/ioc-container-unity-and-breaking-changes-galore.aspx"&gt;IoC Container, Unity and Breaking Changes Galore&lt;/a&gt;&lt;br /&gt;
    Covers the breaking changes made from the old Unity drop to the new one&lt;br /&gt;
    &lt;a href="http://weblogs.asp.net/podwysocki/archive/2008/03/04/ioc-containers-unity-and-objectbuilder2-the-saga-continues.aspx"&gt;&lt;br /&gt;
    &lt;/a&gt;     &lt;/li&gt;
    &lt;li&gt;&lt;a href="javascript:void(0);/*1206509408817*/"&gt;IoC Containers, Unity and ObjectBuilder2 - The Saga Continues&lt;/a&gt;&lt;br /&gt;
    Managing instances and parameter mapping resolution&lt;br /&gt;
    &lt;a href="http://geekswithblogs.net/Podwysocki/archive/2008/02/26/120002.aspx"&gt;&lt;br /&gt;
    &lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://weblogs.asp.net/podwysocki/archive/2008/02/26/ioc-and-the-unity-application-block-once-again.aspx"&gt;IoC and the Unity Application Block Once Again&lt;/a&gt;&lt;br /&gt;
    Setter Injection versus Constructor Injection and PostSharp4Unity&lt;br /&gt;
    &lt;br /&gt;
    &lt;/li&gt;
    &lt;li&gt;&lt;a href="http://weblogs.asp.net/podwysocki/archive/2008/02/22/ioc-and-the-unity-application-block-going-deeper.aspx"&gt;IoC and the Unity Application Block - Going Deeper&lt;/a&gt;&lt;br /&gt;
    Constructor Injection and comparing Unity with Castle Windsor&lt;br /&gt;
    &lt;br /&gt;
    &lt;/li&gt;
    &lt;li&gt;&lt;a href="http://weblogs.asp.net/podwysocki/archive/2008/02/18/ioc-and-the-unity-application-block.aspx"&gt;IoC and the Unity Application Block&lt;/a&gt;&lt;br /&gt;
    Covering ObjectBuilder and Unity Application Block&lt;/li&gt;
&lt;/ul&gt;
So, we have a bit of back history, but I think I dived too far into things without giving some of the back story.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt; DI Frameworks Galore&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
As Scott Hanselman noted, there are a number of IoC containers in the .NET Space.Â  Most of these I was already aware of and played with, so I'm only going to list the ones I've played with.Â  Here they are in no particular order:&lt;br /&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;a href="http://www.castleproject.org/container/index.html"&gt;Castle Windsor&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://structuremap.sourceforge.net/"&gt;StructureMap&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://www.springframework.net/"&gt;Spring.NET&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://www.codeplex.com/ObjectBuilder/SourceControl/DownloadSourceCode.aspx?changeSetId=29957"&gt;ObjectBuilder2 CodePlex Container&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://codeplex.com/unity"&gt;Unity Application Block&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://www.codeproject.com/KB/cs/LinFuPart1.aspx"&gt;LinFu&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://ninject.org/"&gt;NInject&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://code.google.com/p/autofac/"&gt;AutoFac&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://www.puzzleframework.com/"&gt;Puzzle.NET&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://s2container.net.seasar.org/en/index.html"&gt;S2Container.NET&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
So, as you can see, there are plenty to choose from.Â  They all serve basic IoC needs and I'll talk about that a little bit more later.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt; Getting Back to Basics&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
When we talk about Inversion of Control and &lt;a href="http://en.wikipedia.org/wiki/Dependency_inversion_principle"&gt;dependency inversion principle&lt;/a&gt;, we're talking about loosely coupled applications.Â  It follows the &lt;a href="http://en.wikipedia.org/wiki/Hollywood_Principle"&gt;Hollywood Principle&lt;/a&gt; which is the trite "Don't call use, we'll call you" mantra.Â  In a way it means that those dependencies that you have inside your class, say your Logger and its associated formatter.Â  In the tightly coupled world, you would have a private member of a Logger.Â  Instead, what you would have in the other case is an interface or abstract class that would represent the functionality of that logger, and that instance given to your class.Â  Throw on top of that a container to manage those dependencies to those interfaces and mappings to concrete classes.Â  Also, your container can manage the object lifetimes as well.Â Â  &lt;a href="http://www.ayende.com/"&gt;Ayende&lt;/a&gt; shows how easy it is to &lt;a href="http://www.ayende.com/Blog/archive/2007/10/20/Building-an-IoC-container-in-15-lines-of-code.aspx"&gt;create a simple container&lt;/a&gt;, although it misses an important point about lifetime management.&lt;br /&gt;
&lt;br /&gt;
To put it succinctly, to be a container should satisfy the following requirements:&lt;br /&gt;
&lt;ul&gt;
    &lt;li&gt;Configuration - Registering types and mappings through code, XML or script&lt;br /&gt;
    &lt;/li&gt;
    &lt;li&gt;Lifetime Management - How the lifetime of the objects are managed, singleton, transient, etc&lt;/li&gt;
    &lt;li&gt;Resolution - Resolve dependencies and instantiate&lt;/li&gt;
    &lt;li&gt;Extensibility - Be able to add new facilities for additional features (interception, AOP, and so on)&lt;/li&gt;
&lt;/ul&gt;
Before we get into any advanced topics, let's get into more detail.Â  &lt;a href="http://codebetter.com/blogs/james.kovacs/default.aspx"&gt;James Kovacs&lt;/a&gt; wrote in the March 2008 edition of MSDN Magazine called &lt;a href="http://msdn2.microsoft.com/en-ca/magazine/cc337885.aspx"&gt;"Loosen Up - Tame Your Software Dependencies For More Flexible Apps"&lt;/a&gt;.Â Â  This article is a great start for those trying to understand dependency inversion and dependency injection.Â  James walks through a simple application with tight coupling, and then works to loosen it.Â  Once that is complete, Castle Windsor is introduced and walks through the XML configuration or using Binsor to auto-wire the container.&lt;br /&gt;
&lt;br /&gt;
One of the more key points in this article and it came from Ayende is to have an anti-corruption layer for your IoC container.Â  Yes, that's the same Domain Driven Design term in which we isolate things that don't conform to our given architecture and can translate from one context to another.Â  Let's throw up a simple example of using this.Â  Remember it's just a quick spike of an anti-corruption layer.&lt;br /&gt;
&lt;br /&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Monaco;}}{\colortbl;??\red255\green128\blue0;\red40\green40\blue40;\red255\green255\blue255;\red255\green255\blue0;\red43\green145\blue175;}??\fs16 \cf1\cb2\highlight2 namespace\cf3  UnitySamples\par ??\{\par ??    \cf1 public\cf3  \cf1 static\cf3  \cf1 class\cf3  \cf4 IoC\par ??\cf3     \{\par ??        \cf1 private\cf3  \cf1 static\cf3  \cf5 IDependencyResolver\cf3  resolver;\par ??\par ??        \cf1 public\cf3  \cf1 static\cf3  \cf1 void\cf3  Initialize(\cf5 IDependencyResolver\cf3  resolver)\par ??        \{\par ??            \cf4 IoC\cf3 .resolver = resolver;\par ??        \}\par ??\par ??        \cf1 public\cf3  \cf1 static\cf3  T Resolve&amp;lt;T&amp;gt;()\par ??        \{\par ??            \cf1 return\cf3  resolver.Resolve&amp;lt;T&amp;gt;();\par ??        \}\par ??\par ??        \cf1 public\cf3  \cf1 static\cf3  T Resolve&amp;lt;T&amp;gt;(\cf1 string\cf3  name)\par ??        \{\par ??            \cf1 return\cf3  resolver.Resolve&amp;lt;T&amp;gt;(name);\par ??        \}\par ??    \}\par ??\}\par ??}
--&gt;
&lt;div style="background: rgb(40, 40, 40) none repeat scroll 0% 50%; font-family: Monaco; font-size: 8pt; color: white; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;namespace&lt;/span&gt; UnitySamples&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;class&lt;/span&gt; &lt;span style="color: yellow;"&gt;IoC&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IDependencyResolver&lt;/span&gt; resolver;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;void&lt;/span&gt; Initialize(&lt;span style="color: rgb(43, 145, 175);"&gt;IDependencyResolver&lt;/span&gt; resolver)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  &lt;span style="color: yellow;"&gt;IoC&lt;/span&gt;.resolver = resolver;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;static&lt;/span&gt; T Resolve&amp;lt;T&amp;gt;()&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;return&lt;/span&gt; resolver.Resolve&amp;lt;T&amp;gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;static&lt;/span&gt; T Resolve&amp;lt;T&amp;gt;(&lt;span style="color: rgb(255, 128, 0);"&gt;string&lt;/span&gt; name)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;return&lt;/span&gt; resolver.Resolve&amp;lt;T&amp;gt;(name);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;br /&gt;
So, what we have is an IDependencyResolver given to the singleton IoC container.Â  Like I said, this isn't best practice as I haven't done any checking on whether the resolver was initialized or not.Â  But, what we want is to expose the way of resolving dependencies.Â  Now let's take a look at the IDependencyResolver interface and how that's used.&lt;br /&gt;
&lt;br /&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Monaco;}}{\colortbl;??\red255\green128\blue0;\red40\green40\blue40;\red255\green255\blue255;\red43\green145\blue175;}??\fs16 \cf1\cb2\highlight2 namespace\cf3  UnitySamples\par ??\{\par ??    \cf1 public\cf3  \cf1 interface\cf3  \cf4 IDependencyResolver\par ??\cf3     \{\par ??        T Resolve&amp;lt;T&amp;gt;(\cf1 string\cf3  name);\par ??\par ??        T Resolve&amp;lt;T&amp;gt;();\par ??    \}\par ??\}\par ??}
--&gt;
&lt;div style="background: rgb(40, 40, 40) none repeat scroll 0% 50%; font-family: Monaco; font-size: 8pt; color: white; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;namespace&lt;/span&gt; UnitySamples&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;interface&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IDependencyResolver&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  T Resolve&amp;lt;T&amp;gt;(&lt;span style="color: rgb(255, 128, 0);"&gt;string&lt;/span&gt; name);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  T Resolve&amp;lt;T&amp;gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;br /&gt;
Not much to this but gives us the ability to resolve our dependencies by type, or by type and name.Â  Pretty simple code here once again.Â  But, if I wanted to suddenly use Unity as the backing store, I'd simply implement it as the following:&lt;br /&gt;
&lt;br /&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Monaco;}}{\colortbl;??\red255\green128\blue0;\red40\green40\blue40;\red255\green255\blue255;\red255\green255\blue0;\red43\green145\blue175;\red0\green255\blue0;}??\fs16 \cf1\cb2\highlight2 using\cf3  System.Configuration;\par ??\cf1 using\cf3  Microsoft.Practices.Unity;\par ??\cf1 using\cf3  Microsoft.Practices.Unity.Configuration;\par ??\par ??\cf1 namespace\cf3  UnitySamples\par ??\{\par ??    \cf1 public\cf3  \cf1 class\cf3  \cf4 UnityDependencyResolver\cf3 : \cf5 IDependencyResolver\par ??\cf3     \{\par ??        \cf1 private\cf3  \cf5 IUnityContainer\cf3  container;\par ??\par ??        \cf1 public\cf3  UnityDependencyResolver()\par ??        \{\par ??            container = \cf1 new\cf3  \cf4 UnityContainer\cf3 ();\par ??            \cf4 UnityConfigurationSection\cf3  section = (\cf4 UnityConfigurationSection\cf3 )\cf4 ConfigurationManager\cf3 .GetSection(\cf6 "unity"\cf3 );\par ??            section.Containers.Default.GetConfigCommand().Configure(container);\par ??        \}\par ??\par ??        \cf1 public\cf3  T Resolve&amp;lt;T&amp;gt;()\par ??        \{\par ??            \cf1 return\cf3  container.Resolve&amp;lt;T&amp;gt;();\par ??        \}\par ??\par ??        \cf1 public\cf3  T Resolve&amp;lt;T&amp;gt;(\cf1 string\cf3  name)\par ??        \{\par ??            \cf1 return\cf3  container.Resolve&amp;lt;T&amp;gt;(name);\par ??        \}\par ??    \}\par ??\}\par ??}
--&gt;
&lt;div style="background: rgb(40, 40, 40) none repeat scroll 0% 50%; font-family: Monaco; font-size: 8pt; color: white; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;using&lt;/span&gt; System.Configuration;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;using&lt;/span&gt; Microsoft.Practices.Unity;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;using&lt;/span&gt; Microsoft.Practices.Unity.Configuration;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: rgb(255, 128, 0);"&gt;namespace&lt;/span&gt; UnitySamples&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;class&lt;/span&gt; &lt;span style="color: yellow;"&gt;UnityDependencyResolver&lt;/span&gt;: &lt;span style="color: rgb(43, 145, 175);"&gt;IDependencyResolver&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IUnityContainer&lt;/span&gt; container;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; UnityDependencyResolver()&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  container = &lt;span style="color: rgb(255, 128, 0);"&gt;new&lt;/span&gt; &lt;span style="color: yellow;"&gt;UnityContainer&lt;/span&gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  &lt;span style="color: yellow;"&gt;UnityConfigurationSection&lt;/span&gt; section = (&lt;span style="color: yellow;"&gt;UnityConfigurationSection&lt;/span&gt;)&lt;span style="color: yellow;"&gt;ConfigurationManager&lt;/span&gt;.GetSection(&lt;span style="color: lime;"&gt;"unity"&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  section.Containers.Default.GetConfigCommand().Configure(container);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; T Resolve&amp;lt;T&amp;gt;()&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;return&lt;/span&gt; container.Resolve&amp;lt;T&amp;gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; T Resolve&amp;lt;T&amp;gt;(&lt;span style="color: rgb(255, 128, 0);"&gt;string&lt;/span&gt; name)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;return&lt;/span&gt; container.Resolve&amp;lt;T&amp;gt;(name);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;br /&gt;
So, all I'd have to do to switch from one container to another is just swap out the IDependencyResolver and I'm finished.Â  But, unfortunately, there are some techniques that are partial to one container or another, so there may be some things I'd lose there.Â  But for most cases, this pattern works and works well at that.&lt;br /&gt;
&lt;br /&gt;
Of course loose coupling, dependency injection and using IoC containers are nice in terms of making unit tests easier, but as you can see, there's a bit more to it than that, including the lifetime management, interception, the flexibility of changing implementations and so on.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt; Interception and Aspect Oriented Programming&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://en.wikipedia.org/wiki/Aspect_oriented_programming"&gt;Aspect Oriented Programming (AOP)&lt;/a&gt; and basic interception are interesting pieces of some containers.Â  This technique allows us to handle cross-cutting concerns such as logging, transaction management, security and so on at a different level instead of littering our code and domain model with checks everywhere.Â  Instead, we centralize those concerns and have them intercept calls and do their work.Â  Let's get some jargon straight here before we go any further.Â  An &lt;span style="font-weight: bold;"&gt;aspect &lt;/span&gt;is the part that encapsulates the cross-cutting concern such as logging.Â  These aspects can give advice, which is to alter the behavior of a given program.Â  The &lt;span style="font-weight: bold;"&gt;advice &lt;/span&gt;is given at certain &lt;span style="font-weight: bold;"&gt;join points&lt;/span&gt; which is where the aspect advice and the program intersect.Â  A group of these join points are called a &lt;span style="font-weight: bold;"&gt;pointcut&lt;/span&gt;. &lt;br /&gt;
&lt;br /&gt;
Interception is a feature of some of the IoC containers out there.Â  A few come to mind such as Castle Windsor, Spring.NET, LinFu, S2Container, Puzzle.NET and even ObjectBuilder2.Â  These containers provide at least a basic level of interception, meaning that you can intercept calls to methods, analyze the contents of the method call, proceed with the call and even modify the return value.Â  Since most containers support simple interception, I haven't found it more useful than logging or enforcing security on various things.Â  However, it can be extended to support more robust Policy Injection style of programming such as you would find in the &lt;a href="http://msdn2.microsoft.com/en-us/library/bb410104.aspx"&gt;Policy Injection Application Block&lt;/a&gt;.&lt;br /&gt;
&lt;br style="font-weight: bold;" /&gt;
&lt;span style="font-weight: bold;"&gt; Interception in Unity?&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Quite a bit has changed with Unity since I last blogged about it in terms of the configuration.Â  You can grab the latest bits &lt;a href="http://www.codeplex.com/unity/Release/ProjectReleases.aspx?ReleaseId=11956"&gt;here&lt;/a&gt;.Â  It would be nice to have changesets and such checked into CodePlex for things like this instead of zip files or MSIs, but oh well.Â  Anyhow, most of the changes revolve around managing lifetimes and handling the parameters for constructor injection.Â  You can read the tests and figure it out.&lt;br /&gt;
&lt;br /&gt;
As I've covered before, right now simple injection in Unity is not supported at the moment.Â  The intention is to have the Policy Injection Application Block fill that role.Â  There is also PostSharp4Unity which can also fit that bill for quick fixes, but I want deep down support for simple interception.Â  Conversations with &lt;a href="http://bradwilson.typepad.com/"&gt;Brad Wilson&lt;/a&gt; led me to ObjectBuilder2 and some spikes that they had with a customer DependencyContainer.Â  So parts of me wondered why that wasn't leveraged at all.Â  Let's take a look at what was implemented in the base of ObjectBuilder2.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt; Interception in ObjectBuilder2&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
ObjectBuilder2 was a project by Brad Wilson and &lt;a href="http://blogs.msdn.com/scottdensmore/"&gt;Scott Densmore&lt;/a&gt; which took the original ObjectBuilder, which was really meant for CAB and not a real container, and made some changes and improvements.Â  Such improvements also came with a few samples of things you can do with it such as a DependencyContainer sample.Â  In here, there are a few interesting pieces.Â  Most of which lie in the interception pieces.Â  Let's take a look at each type below of the ones supported.&lt;br /&gt;
&lt;br /&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Monaco;}}{\colortbl;??\red255\green255\blue255;\red40\green40\blue40;\red255\green128\blue0;\red255\green255\blue0;\red43\green145\blue175;}??\fs16 \cf1\cb2\highlight2         \cf3 public\cf1  \cf3 void\cf1  InterceptInterface&amp;lt;T&amp;gt;(\cf4 MethodInfo\cf1  interfaceMethod,\par ??                                          \cf3 params\cf1  \cf5 IInterceptionHandler\cf1 [] handlers)\par ??        \{\par ??            Intercept&amp;lt;\cf4 InterfaceInterceptionPolicy\cf1 , \cf5 IInterfaceInterceptionPolicy\cf1 &amp;gt;(\cf3 typeof\cf1 (T), interfaceMethod, handlers);\par ??        \}\par ??\par ??        \cf3 public\cf1  \cf3 void\cf1  InterceptInterface(\cf4 Type\cf1  typeToIntercept,\par ??                                       \cf4 MethodInfo\cf1  interfaceMethod,\par ??                                       \cf3 params\cf1  \cf5 IInterceptionHandler\cf1 [] handlers)\par ??        \{\par ??            Intercept&amp;lt;\cf4 InterfaceInterceptionPolicy\cf1 , \cf5 IInterfaceInterceptionPolicy\cf1 &amp;gt;(typeToIntercept, interfaceMethod, handlers);\par ??        \}\par ??\par ??        \cf3 public\cf1  \cf3 void\cf1  InterceptRemoting&amp;lt;T&amp;gt;(\cf4 MethodInfo\cf1  method,\par ??                                         \cf3 params\cf1  \cf5 IInterceptionHandler\cf1 [] handlers)\par ??        \{\par ??            Intercept&amp;lt;\cf4 RemotingInterceptionPolicy\cf1 , \cf5 IRemotingInterceptionPolicy\cf1 &amp;gt;(\cf3 typeof\cf1 (T), method, handlers);\par ??        \}\par ??\par ??        \cf3 public\cf1  \cf3 void\cf1  InterceptRemoting(\cf4 Type\cf1  typeToIntercept,\par ??                                      \cf4 MethodInfo\cf1  method,\par ??                                      \cf3 params\cf1  \cf5 IInterceptionHandler\cf1 [] handlers)\par ??        \{\par ??            Intercept&amp;lt;\cf4 RemotingInterceptionPolicy\cf1 , \cf5 IRemotingInterceptionPolicy\cf1 &amp;gt;(typeToIntercept, method, handlers);\par ??        \}\par ??\par ??        \cf3 public\cf1  \cf3 void\cf1  InterceptVirtual&amp;lt;T&amp;gt;(\cf4 MethodInfo\cf1  method,\par ??                                        \cf3 params\cf1  \cf5 IInterceptionHandler\cf1 [] handlers)\par ??        \{\par ??            Intercept&amp;lt;\cf4 VirtualInterceptionPolicy\cf1 , \cf5 IVirtualInterceptionPolicy\cf1 &amp;gt;(\cf3 typeof\cf1 (T), method, handlers);\par ??        \}\par ??\par ??        \cf3 public\cf1  \cf3 void\cf1  InterceptVirtual(\cf4 Type\cf1  typeToIntercept,\par ??                                     \cf4 MethodInfo\cf1  method,\par ??                                     \cf3 params\cf1  \cf5 IInterceptionHandler\cf1 [] handlers)\par ??        \{\par ??            Intercept&amp;lt;\cf4 VirtualInterceptionPolicy\cf1 , \cf5 IVirtualInterceptionPolicy\cf1 &amp;gt;(typeToIntercept, method, handlers);\par ??        \}}
--&gt;
&lt;div style="background: rgb(40, 40, 40) none repeat scroll 0% 50%; font-family: Monaco; font-size: 8pt; color: white; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;void&lt;/span&gt; InterceptInterface&amp;lt;T&amp;gt;(&lt;span style="color: yellow;"&gt;MethodInfo&lt;/span&gt; interfaceMethod,&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â  &lt;span style="color: rgb(255, 128, 0);"&gt;params&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IInterceptionHandler&lt;/span&gt;[] handlers)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;void&lt;/span&gt; InterceptInterface(&lt;span style="color: yellow;"&gt;Type&lt;/span&gt; typeToIntercept,&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â  &lt;span style="color: yellow;"&gt;MethodInfo&lt;/span&gt; interfaceMethod,&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;params&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IInterceptionHandler&lt;/span&gt;[] handlers)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;void&lt;/span&gt; InterceptRemoting&amp;lt;T&amp;gt;(&lt;span style="color: yellow;"&gt;MethodInfo&lt;/span&gt; method,&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â   &lt;span style="color: rgb(255, 128, 0);"&gt;params&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IInterceptionHandler&lt;/span&gt;[] handlers)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;void&lt;/span&gt; InterceptRemoting(&lt;span style="color: yellow;"&gt;Type&lt;/span&gt; typeToIntercept,&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â  &lt;span style="color: yellow;"&gt;MethodInfo&lt;/span&gt; method,&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â  &lt;span style="color: rgb(255, 128, 0);"&gt;params&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IInterceptionHandler&lt;/span&gt;[] handlers)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;void&lt;/span&gt; InterceptVirtual&amp;lt;T&amp;gt;(&lt;span style="color: yellow;"&gt;MethodInfo&lt;/span&gt; method,&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;params&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IInterceptionHandler&lt;/span&gt;[] handlers)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;void&lt;/span&gt; InterceptVirtual(&lt;span style="color: yellow;"&gt;Type&lt;/span&gt; typeToIntercept,&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â   &lt;span style="color: yellow;"&gt;MethodInfo&lt;/span&gt; method,&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â   &lt;span style="color: rgb(255, 128, 0);"&gt;params&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IInterceptionHandler&lt;/span&gt;[] handlers)&lt;/p&gt;
&lt;/div&gt;
&lt;br /&gt;
Now with this, you can see there are three different ways of intercepting, through interfaces, virtual methods or through remoting.Â  We also have the ability to intercept through attributes or through code.Â  Let's walk through a test to see how intercepting through attributes might work.Â  &lt;br /&gt;
&lt;br /&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Monaco;}}{\colortbl;??\red255\green255\blue255;\red40\green40\blue40;\red255\green255\blue0;\red255\green128\blue0;\red43\green145\blue175;\red0\green255\blue0;}??\fs16 \cf1\cb2\highlight2             [\cf3 Fact\cf1 ]\par ??            \cf4 public\cf1  \cf4 void\cf1  InterceptViaAttributes()\par ??            \{\par ??                \cf3 Recorder\cf1 .Records.Clear();\par ??                \cf3 DependencyContainer\cf1  container = \cf4 new\cf1  \cf3 DependencyContainer\cf1 ();\par ??                container.RegisterTypeMapping&amp;lt;\cf5 ISpy\cf1 , \cf3 SpyInterfaceAttributes\cf1 &amp;gt;();\par ??\par ??                \cf5 ISpy\cf1  obj = container.Get&amp;lt;\cf5 ISpy\cf1 &amp;gt;();\par ??                obj.InterceptedMethod();\par ??\par ??                \cf3 Assert\cf1 .Equal(3, \cf3 Recorder\cf1 .Records.Count);\par ??                \cf3 Assert\cf1 .Equal(\cf6 "Before Method"\cf1 , \cf3 Recorder\cf1 .Records[0]);\par ??                \cf3 Assert\cf1 .Equal(\cf6 "In Method"\cf1 , \cf3 Recorder\cf1 .Records[1]);\par ??                \cf3 Assert\cf1 .Equal(\cf6 "After Method"\cf1 , \cf3 Recorder\cf1 .Records[2]);\par ??            \}}
--&gt;
&lt;div style="background: rgb(40, 40, 40) none repeat scroll 0% 50%; font-family: Monaco; font-size: 8pt; color: white; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  [&lt;span style="color: yellow;"&gt;Fact&lt;/span&gt;]&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;void&lt;/span&gt; InterceptViaAttributes()&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  &lt;span style="color: yellow;"&gt;Recorder&lt;/span&gt;.Records.Clear();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  &lt;span style="color: yellow;"&gt;DependencyContainer&lt;/span&gt; container = &lt;span style="color: rgb(255, 128, 0);"&gt;new&lt;/span&gt; &lt;span style="color: yellow;"&gt;DependencyContainer&lt;/span&gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  container.RegisterTypeMapping&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;ISpy&lt;/span&gt;, &lt;span style="color: yellow;"&gt;SpyInterfaceAttributes&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  &lt;span style="color: rgb(43, 145, 175);"&gt;ISpy&lt;/span&gt; obj = container.Get&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;ISpy&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  obj.InterceptedMethod();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  &lt;span style="color: yellow;"&gt;Assert&lt;/span&gt;.Equal(3, &lt;span style="color: yellow;"&gt;Recorder&lt;/span&gt;.Records.Count);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  &lt;span style="color: yellow;"&gt;Assert&lt;/span&gt;.Equal(&lt;span style="color: lime;"&gt;"Before Method"&lt;/span&gt;, &lt;span style="color: yellow;"&gt;Recorder&lt;/span&gt;.Records[0]);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  &lt;span style="color: yellow;"&gt;Assert&lt;/span&gt;.Equal(&lt;span style="color: lime;"&gt;"In Method"&lt;/span&gt;, &lt;span style="color: yellow;"&gt;Recorder&lt;/span&gt;.Records[1]);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  &lt;span style="color: yellow;"&gt;Assert&lt;/span&gt;.Equal(&lt;span style="color: lime;"&gt;"After Method"&lt;/span&gt;, &lt;span style="color: yellow;"&gt;Recorder&lt;/span&gt;.Records[2]);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  }&lt;/p&gt;
&lt;/div&gt;
&lt;br /&gt;
Of course there wouldn't be tests in here without using &lt;a href="http://www.codeplex.com/xunit"&gt;xUnit.NET&lt;/a&gt; of course.Â  Hope to cover that soon enough.Â  Anyhow, as you can see, it's pretty simple to register types in here and then check the expectations through the recorder to see whether the actions happened.Â  To find out what the ISpy and SpyInterfaceAttribtues are actually doing, let's dig through the code.Â  &lt;br /&gt;
&lt;br /&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Monaco;}}{\colortbl;??\red255\green255\blue255;\red40\green40\blue40;\red255\green128\blue0;\red43\green145\blue175;\red255\green255\blue0;\red0\green255\blue0;}??\fs16 \cf1\cb2\highlight2             \cf3 public\cf1  \cf3 interface\cf1  \cf4 ISpy\par ??\cf1             \{\par ??                \cf3 void\cf1  InterceptedMethod();\par ??                \cf3 void\cf1  ThrowsException();\par ??            \}\par ??\par ??            \cf3 internal\cf1  \cf3 sealed\cf1  \cf3 class\cf1  \cf5 SpyInterface\cf1  : \cf4 ISpy\par ??\cf1             \{\par ??                \cf3 public\cf1  \cf3 void\cf1  InterceptedMethod()\par ??                \{\par ??                    \cf5 Recorder\cf1 .Records.Add(\cf6 "In Method"\cf1 );\par ??                \}\par ??\par ??                \cf3 public\cf1  \cf3 void\cf1  ThrowsException()\par ??                \{\par ??                    \cf5 Recorder\cf1 .Records.Add(\cf6 "In Method"\cf1 );\par ??                    \cf3 throw\cf1  \cf3 new\cf1  \cf5 Exception\cf1 ();\par ??                \}\par ??            \}\par ??\par ??            \cf3 internal\cf1  \cf3 sealed\cf1  \cf3 class\cf1  \cf5 SpyInterfaceAttributes\cf1  : \cf4 ISpy\par ??\cf1             \{\par ??                [\cf5 InterfaceIntercept\cf1 (\cf3 typeof\cf1 (\cf5 RecordingHandler\cf1 ))]\par ??                \cf3 public\cf1  \cf3 void\cf1  InterceptedMethod()\par ??                \{\par ??                    \cf5 Recorder\cf1 .Records.Add(\cf6 "In Method"\cf1 );\par ??                \}\par ??\par ??                \cf3 public\cf1  \cf3 void\cf1  ThrowsException()\par ??                \{\par ??                    \cf3 throw\cf1  \cf3 new\cf1  \cf5 Exception\cf1 ();\par ??                \}\par ??            \}}
--&gt;
&lt;div style="background: rgb(40, 40, 40) none repeat scroll 0% 50%; font-family: Monaco; font-size: 8pt; color: white; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(255, 128, 0);"&gt;interface&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ISpy&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â  Â Â Â  Â Â Â  &lt;span style="color: rgb(255, 128, 0);"&gt;void&lt;/span&gt; InterceptedMethod();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;Â Â Â  Â Â Â