<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type='text/xsl' href='http://wekempf.spaces.live.com/mmm2008-07-24_12.50/rsspretty.aspx?rssquery=en-US;http%3a%2f%2fwekempf.spaces.live.com%2ffeed.rss' version='1.0'?><rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:msn="http://schemas.microsoft.com/msn/spaces/2005/rss" xmlns:live="http://schemas.microsoft.com/live/spaces/2006/rss" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:cf="http://www.microsoft.com/schemas/rss/core/2005" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>So long, and thanks for all the chips</title><description /><link>http://wekempf.spaces.live.com/</link><language>en-US</language><pubDate>Sat, 06 Sep 2008 07:48:45 GMT</pubDate><lastBuildDate>Sat, 06 Sep 2008 07:48:45 GMT</lastBuildDate><generator>Microsoft Spaces v1.1</generator><docs>http://www.rssboard.org/rss-specification</docs><ttl>60</ttl><live:identity><live:id>-3347231426831879729</live:id><live:alias>wekempf</live:alias></live:identity><image><title>So long, and thanks for all the chips</title><url>http://blufiles.storage.live.com/y1pO_TkCUDo1_Lu_hWAZU2l93oP50j58SW9RsordwqQP4hrdsNCo6oSz2mzfLDGLCRj</url><link>http://wekempf.spaces.live.com/</link></image><cf:listinfo><cf:group ns="http://schemas.microsoft.com/live/spaces/2006/rss" element="typelabel" label="Type" /><cf:group ns="http://schemas.microsoft.com/live/spaces/2006/rss" element="tag" label="Tag" /><cf:group element="category" label="Category" /><cf:sort element="pubDate" label="Date" data-type="date" default="true" /><cf:sort element="title" label="Title" data-type="string" /><cf:sort ns="http://purl.org/rss/1.0/modules/slash/" element="comments" label="Comments" data-type="number" /></cf:listinfo><item><title>Background Task in WPF</title><link>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!541.entry</link><description>&lt;p&gt;Writing multi-threaded code is hard.  Very hard.  Writing multi-threaded UI code is even harder.  Much harder.  See, user interface components are generally not thread safe.  You can't modify properties or call methods on user interface components safely, unless you're in the same thread on which the component was created.  This has been true on every UI framework I've ever dealt with, and it's true in WPF.  This makes it very difficult to create multi-threaded UI code, because you have to constantly be marshalling operations across thread boundaries, which is both more complicated to code, and leads to the potential for race conditions and deadlocks. &lt;p&gt;WPF has helped to make the marshalling of operations across thread boundaries a little easier with the DispatcherObject class.  DispatcherObject is generally used as a base class for other types, and all of the WPF UI components derive from DispatcherObject.  When a DispatcherObject is constructed, it associates itself with the currently running thread.  A couple of methods, CheckAccess and VerifyAccess, allow you to determine if the current thread is the same thread that the DispatcherObject was created on.  CheckAccess simply returns a boolean, while VerifyAccess will throw an exception if the current thread isn't the associated thread on which the DispatcherObject was created.  By convention, any property or method that must be called only when running on the associated thread will call VerifyAccess internally, thus ensuring they can't be called except from the thread on which the DispatcherObject was created. &lt;p&gt;So, we understand how DispatcherObject ensures that operations occur only on the associated thread.  How does it help us to marshal operations onto this thread?  DispatcherObject has a Dispatcher property that includes various overloads of Invoke and BeginInvoke that marshal operations onto the DispatcherObject's associated thread.  All of this greatly simplifies the coding required to create objects with thread affinity and to marshal operations across thread boundaries.  You should be aware that this isn't a silver bullet solution.  The marshalling can still lead to deadlock.  The general idea behind marshalling operations across thread boundaries is to bundle up the operation and put it in a queue where the other thread can extract it and run the operation.  This requires the other thread to actually participate by checking the queue and running the operation.  If that thread is busy doing something else, it won't be checking the queue and the operation will be sitting there.  So, if one thread is marshalling to another, and that thread is try to marshal to the first, deadlock can ensue.  So, you must be careful here, even though the DispatcherObject has made it easier to coordinate between threads. &lt;p&gt;WinForms had a somewhat similar concept to DispatcherObject in the SynchronizationContext class, though the DispatcherObject is a cleaner and easier to use design.  Score one for WPF. &lt;p&gt;So, with the DispatcherObject or SynchronizationContext classes, it's possible to create background operations that communicate with our UI by marshalling operations onto the &amp;quot;UI thread&amp;quot; (the thread on which the UI components are created).  However, there's still a lot of dirty little details you have to take into account when creating such a background process.  So, WinForms provided us with a BackgroundWorker class.  This class would greatly simplify the code required to create a background task, allow it to be cancelled and report progress on the UI thread.  WPF doesn't have a similar concept, so score one for WinForms. &lt;p&gt;Other people have pointed out that you can use the BackgroundWorker in a WPF application, and there's plenty of examples of how to do this.  It's not rocket science, really, so I'm not going to cover that.  What I am going to do, and the whole purpose of the post, is tell you that BackgroundWorker is a sub-optimal solution in WPF, and provide you with the basis of a better solution. &lt;p&gt;Why is BackgroundWorker sub-optimal?  Because the design requires you to use an event handler in order to update your UI on the progress of the operation.  This is WPF.  We don't want to have to write an event handler that will update our UI.  We want to use data binding and no code at all! &lt;p&gt;This is fairly easy to achieve.  I'm not going to provide code for this here, because I'm still working on the concept.  But it should be easy enough for you to take the idea I'm about to describe and run with it. &lt;p&gt;What I'm working on is what I'm calling a BackgroundTask.  This is a class that works very similarly to the way the BackgroundWorker does.  However, there is no ProgressChanged event on the BackgroundTask.  Instead, there's a PercentProgress property which the UI can data bind to.  BackgroundTask is a DispatcherObject, so it has thread affinity, and by convention should be created on the &amp;quot;UI thread&amp;quot;.  The background process will then use the magic of DispatcherObject to marshal changes to the PercentProgress property onto the UI thread, where INotifyPropertyChanged can notify the data binding that it needs to be updated. &lt;p&gt;This is a very simple concept, but the result is a much easier to use component in WPF.  More importantly, this is just the tip of the iceberg for what's possible with the idea.  Imagine we give these BackgroundTask objects a Title property that can also be used in data binding to display on the UI.  Then imagine a BackgroundTaskCollection that holds running BackgroundTask instances (which would be automatically removed from the collection when the task completes).  Now our UI can display information about multiple running background tasks through simple WPF data binding magic.  Now imagine we have other properties on the BackgroundTask that can also be used in data binding to display even richer information about our background process.  Doesn't this concept start to sound very exciting? &lt;p&gt;Now, here's another huge bonus to this design.  Let's imagine we have some domain model (data model, what ever name you wish to use) objects which our UI displays through data binding.  Imagine that these models are not thread safe (they probably shouldn't be).  Then imagine we need to add some long running process that's going to modify these domain objects.  We don't want to run this process on the UI thread, because it would cause our UI to become unresponsive for an extended period of time.  We don't want to, or can't, modify the domain object, however, in order to make it thread safe. So, what do we do?  Well, you don't have to do much if you're using a BackgroundTask concept like I just described.  The operation you're running in the background can use the BackgroundTask to marshal operations that modify the domain object onto the &amp;quot;UI thread&amp;quot;.  Your UI will update itself through the magic of data binding.  Problem solved with a minimal amount of code, and no impact to your current domain object design.&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=-3347231426831879729&amp;page=RSS%3a+Background+Task+in+WPF&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=wekempf.spaces.live.com&amp;amp;GT1=wekempf"&gt;</description><comments>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!541.entry#comment</comments><guid isPermaLink="true">http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!541.entry</guid><pubDate>Thu, 04 Sep 2008 16:16:31 GMT</pubDate><slash:comments>0</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://wekempf.spaces.live.com/blog/cns!D18C3EC06EA971CF!541/comments/feed.rss</wfw:commentRss><wfw:comment>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!541.entry#comment</wfw:comment><dcterms:modified>2008-09-04T16:16:31Z</dcterms:modified></item><item><title>The first rule of WPF</title><link>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!508.entry</link><description>&lt;p&gt;Avoid control inheritance. &lt;p&gt;I knew this rule.  I've read it several times from many different sources.  I've even given this advice to others.  However, I'm also guilty of forgetting this rule. &lt;p&gt;Recently, I had the need for a MaskedTextBox in WPF.  Marlon Grech, a fellow WPF Disciple, has a great &lt;a href="http://marlongrech.wordpress.com/2007/10/28/masked-textbox/"&gt;article&lt;/a&gt; about how he created such a control for his AvalonControlsLibrary.  Having read that post, my first inclination was to create my own MaskedTextBox (not a matter of NIH syndrome, but the lesser evil of simply not wanting to deal with the legal paperwork necessary to use Marlon's code at work).  I spent a few days perfecting this control (there's a lot of corner cases you have to deal with, and I wanted full MaskedTextBox functionality, which makes it even more difficult).  Once I was done, a coworker pointed out to me that we didn't pick up the Aero styling that our application uses.  It's a simple fix at an application level, but not so easy to fix at a library level.  Not a huge deal, but this got me to thinking about how to address the problem.  Then it dawned on me... I shouldn't have created a MaskedTextBox at all! &lt;p&gt;You see, if I'd followed the &amp;quot;first rule of WPF&amp;quot;, I wouldn't have had the styling issue. &lt;p&gt;So, what's the better solution here, then?  Well, you simply use attached properties and behaviors instead!  Here's a naive example of this concept.  Please beware that this is NOT a complete solution, and won't begin to work for you as is.  It only is here for illustrative purposes.  I can't publish a complete solution since I developed this during work hours.  However, with this illustration, it shouldn't be too difficult to develop a complete solution.  Anyway, on to the code.&lt;pre&gt;&lt;span style="color:blue"&gt;public class &lt;/span&gt;&lt;span style="color:#2b91af"&gt;MaskedText &lt;/span&gt;: &lt;span style="color:#2b91af"&gt;IDisposable
&lt;/span&gt;{
    &lt;span style="color:blue"&gt;private readonly &lt;/span&gt;&lt;span style="color:#2b91af"&gt;TextBox &lt;/span&gt;_target;
    &lt;span style="color:blue"&gt;private &lt;/span&gt;&lt;span style="color:#2b91af"&gt;MaskedTextProvider &lt;/span&gt;_provider;

    &lt;span style="color:blue"&gt;private &lt;/span&gt;MaskedText(&lt;span style="color:#2b91af"&gt;TextBox &lt;/span&gt;target)
    {
        _target = target;
        _target.PreviewTextInput += OnPreviewTextInput;
        CreateProvider();
    }

    &lt;span style="color:blue"&gt;public void &lt;/span&gt;Dispose()
    {
        _target.PreviewTextInput -= OnPreviewTextInput;
    }

    &lt;span style="color:blue"&gt;private void &lt;/span&gt;OnPreviewTextInput(&lt;span style="color:blue"&gt;object &lt;/span&gt;sender, System.Windows.Input.&lt;span style="color:#2b91af"&gt;TextCompositionEventArgs &lt;/span&gt;e)
    {
        e.Handled = &lt;span style="color:blue"&gt;true&lt;/span&gt;;
        ReplaceText(e.Text);
    }

    &lt;span style="color:blue"&gt;private void &lt;/span&gt;CreateProvider()
    {
        &lt;span style="color:blue"&gt;string &lt;/span&gt;text = _target.Text;
        &lt;span style="color:blue"&gt;if &lt;/span&gt;(_provider != &lt;span style="color:blue"&gt;null&lt;/span&gt;)
        {
            text = _provider.ToString(&lt;span style="color:blue"&gt;false&lt;/span&gt;, &lt;span style="color:blue"&gt;false&lt;/span&gt;);
        }

        _provider = &lt;span style="color:blue"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af"&gt;MaskedTextProvider&lt;/span&gt;(
            GetMask(_target),
            &lt;span style="color:#2b91af"&gt;CultureInfo&lt;/span&gt;.CurrentCulture,
            &lt;span style="color:blue"&gt;true&lt;/span&gt;, &lt;span style="color:green"&gt;// allow prompt as input
            &lt;/span&gt;GetPromptChar(_target),
            &lt;span style="color:#a31515"&gt;'\0'&lt;/span&gt;,
            &lt;span style="color:blue"&gt;false&lt;/span&gt;); &lt;span style="color:green"&gt;// Ascii only

        &lt;/span&gt;&lt;span style="color:blue"&gt;int &lt;/span&gt;testPosition;
        &lt;span style="color:#2b91af"&gt;MaskedTextResultHint &lt;/span&gt;hint;
        _provider.Replace(text, 0, _provider.Length, &lt;span style="color:blue"&gt;out &lt;/span&gt;testPosition, &lt;span style="color:blue"&gt;out &lt;/span&gt;hint);
        SetText();
    }

    &lt;span style="color:blue"&gt;private void &lt;/span&gt;SetText()
    {
        &lt;span style="color:blue"&gt;int &lt;/span&gt;start = _target.SelectionStart;
        _target.Text = GetFormattedDisplayString();
        _target.SelectionStart = start;
        _target.SelectionLength = 0;
    }

    &lt;span style="color:blue"&gt;private string &lt;/span&gt;GetFormattedDisplayString()
    {
        &lt;span style="color:blue"&gt;bool &lt;/span&gt;flag;
        &lt;span style="color:blue"&gt;if &lt;/span&gt;(_target.IsReadOnly)
        {
            flag = &lt;span style="color:blue"&gt;false&lt;/span&gt;;
        }
        &lt;span style="color:blue"&gt;else if &lt;/span&gt;(&lt;span style="color:#2b91af"&gt;DesignerProperties&lt;/span&gt;.GetIsInDesignMode(_target))
        {
            flag = &lt;span style="color:blue"&gt;true&lt;/span&gt;;
        }
        &lt;span style="color:blue"&gt;else
        &lt;/span&gt;{
            flag = !GetHidePromptOnLeave(_target) || _target.IsFocused;
        }

        &lt;span style="color:blue"&gt;return &lt;/span&gt;_provider.ToString(&lt;span style="color:blue"&gt;false&lt;/span&gt;, flag, &lt;span style="color:blue"&gt;true&lt;/span&gt;, 0, _provider.Length);
    }

    &lt;span style="color:blue"&gt;private void &lt;/span&gt;ReplaceText(&lt;span style="color:blue"&gt;string &lt;/span&gt;text)
    {
        &lt;span style="color:blue"&gt;int &lt;/span&gt;start = _target.SelectionStart;
        &lt;span style="color:blue"&gt;int &lt;/span&gt;end = start + _target.SelectionLength - 1;

        &lt;span style="color:blue"&gt;int &lt;/span&gt;testPosition;
        &lt;span style="color:#2b91af"&gt;MaskedTextResultHint &lt;/span&gt;hint;
        &lt;span style="color:blue"&gt;if &lt;/span&gt;(end &amp;gt;= start)
        {
            _provider.Replace(text, start, end, &lt;span style="color:blue"&gt;out &lt;/span&gt;testPosition, &lt;span style="color:blue"&gt;out &lt;/span&gt;hint);
        }
        &lt;span style="color:blue"&gt;else
        &lt;/span&gt;{
            _provider.InsertAt(text, start, &lt;span style="color:blue"&gt;out &lt;/span&gt;testPosition, &lt;span style="color:blue"&gt;out &lt;/span&gt;hint);
        }

        &lt;span style="color:blue"&gt;if &lt;/span&gt;(hint == &lt;span style="color:#2b91af"&gt;MaskedTextResultHint&lt;/span&gt;.Success ||
            hint == &lt;span style="color:#2b91af"&gt;MaskedTextResultHint&lt;/span&gt;.CharacterEscaped ||
            hint == &lt;span style="color:#2b91af"&gt;MaskedTextResultHint&lt;/span&gt;.NoEffect ||
            hint == &lt;span style="color:#2b91af"&gt;MaskedTextResultHint&lt;/span&gt;.SideEffect)
        {
            SetText();
            _target.SelectionStart = testPosition + text.Length;
            _target.SelectionLength = 0;
        }
        &lt;span style="color:blue"&gt;else
        &lt;/span&gt;{
            &lt;span style="color:green"&gt;//RaiseMaskInputRejectedEvent(hint, start);
        &lt;/span&gt;}
    }

    &lt;span style="color:blue"&gt;#region &lt;/span&gt;Instance

    &lt;span style="color:blue"&gt;private static readonly &lt;/span&gt;&lt;span style="color:#2b91af"&gt;DependencyProperty &lt;/span&gt;InstanceProperty =
        &lt;span style="color:#2b91af"&gt;DependencyProperty&lt;/span&gt;.RegisterAttached(&lt;span style="color:#a31515"&gt;&amp;quot;Instance&amp;quot;&lt;/span&gt;, &lt;span style="color:blue"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af"&gt;MaskedText&lt;/span&gt;), &lt;span style="color:blue"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af"&gt;MaskedText&lt;/span&gt;),
            &lt;span style="color:blue"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af"&gt;FrameworkPropertyMetadata&lt;/span&gt;((&lt;span style="color:#2b91af"&gt;MaskedText&lt;/span&gt;)&lt;span style="color:blue"&gt;null&lt;/span&gt;));

    &lt;span style="color:blue"&gt;private static &lt;/span&gt;&lt;span style="color:#2b91af"&gt;MaskedText &lt;/span&gt;GetInstance(&lt;span style="color:#2b91af"&gt;DependencyObject &lt;/span&gt;d)
    {
        &lt;span style="color:blue"&gt;return &lt;/span&gt;(&lt;span style="color:#2b91af"&gt;MaskedText&lt;/span&gt;)d.GetValue(InstanceProperty);
    }

    &lt;span style="color:blue"&gt;private static void &lt;/span&gt;SetInstance(&lt;span style="color:#2b91af"&gt;DependencyObject &lt;/span&gt;d, &lt;span style="color:#2b91af"&gt;MaskedText &lt;/span&gt;value)
    {
        d.SetValue(InstanceProperty, value);
    }

    &lt;span style="color:blue"&gt;#endregion

    #region &lt;/span&gt;Mask

    &lt;span style="color:blue"&gt;public static readonly &lt;/span&gt;&lt;span style="color:#2b91af"&gt;DependencyProperty &lt;/span&gt;MaskProperty =
        &lt;span style="color:#2b91af"&gt;DependencyProperty&lt;/span&gt;.RegisterAttached(
            &lt;span style="color:#a31515"&gt;&amp;quot;Mask&amp;quot;&lt;/span&gt;,
            &lt;span style="color:blue"&gt;typeof&lt;/span&gt;(&lt;span style="color:blue"&gt;string&lt;/span&gt;),
            &lt;span style="color:blue"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af"&gt;MaskedText&lt;/span&gt;),
            &lt;span style="color:blue"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af"&gt;FrameworkPropertyMetadata&lt;/span&gt;((&lt;span style="color:blue"&gt;string&lt;/span&gt;)&lt;span style="color:blue"&gt;null&lt;/span&gt;, &lt;span style="color:blue"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af"&gt;PropertyChangedCallback&lt;/span&gt;(OnMaskChanged), &lt;span style="color:blue"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af"&gt;CoerceValueCallback&lt;/span&gt;(CoerceMaskValue)),
            &lt;span style="color:blue"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af"&gt;ValidateValueCallback&lt;/span&gt;(IsMaskValid));

    &lt;span style="color:blue"&gt;public static string &lt;/span&gt;GetMask(&lt;span style="color:#2b91af"&gt;DependencyObject &lt;/span&gt;d)
    {
        &lt;span style="color:blue"&gt;return &lt;/span&gt;(&lt;span style="color:blue"&gt;string&lt;/span&gt;)d.GetValue(MaskProperty);
    }

    &lt;span style="color:blue"&gt;public static void &lt;/span&gt;SetMask(&lt;span style="color:#2b91af"&gt;DependencyObject &lt;/span&gt;d, &lt;span style="color:blue"&gt;string &lt;/span&gt;value)
    {
        d.SetValue(MaskProperty, value);
    }

    &lt;span style="color:blue"&gt;private static void &lt;/span&gt;OnMaskChanged(&lt;span style="color:#2b91af"&gt;DependencyObject &lt;/span&gt;d, &lt;span style="color:#2b91af"&gt;DependencyPropertyChangedEventArgs &lt;/span&gt;e)
    {
        &lt;span style="color:blue"&gt;if &lt;/span&gt;(e.NewValue == &lt;span style="color:blue"&gt;null&lt;/span&gt;)
        {
            Unattach(d);
        }
        &lt;span style="color:blue"&gt;else
        &lt;/span&gt;{
            Attach(d);
        }
    }

    &lt;span style="color:blue"&gt;private static object &lt;/span&gt;CoerceMaskValue(&lt;span style="color:#2b91af"&gt;DependencyObject &lt;/span&gt;d, &lt;span style="color:blue"&gt;object &lt;/span&gt;value)
    {
        &lt;span style="color:blue"&gt;string &lt;/span&gt;mask = (&lt;span style="color:blue"&gt;string&lt;/span&gt;)value;
        &lt;span style="color:blue"&gt;if &lt;/span&gt;(&lt;span style="color:blue"&gt;string&lt;/span&gt;.IsNullOrEmpty(mask))
        {
            &lt;span style="color:blue"&gt;return null&lt;/span&gt;;
        }
        &lt;span style="color:blue"&gt;return &lt;/span&gt;value;
    }

    &lt;span style="color:blue"&gt;private static bool &lt;/span&gt;IsMaskValid(&lt;span style="color:blue"&gt;object &lt;/span&gt;value)
    {
        &lt;span style="color:blue"&gt;string &lt;/span&gt;mask = (&lt;span style="color:blue"&gt;string&lt;/span&gt;)value;
        &lt;span style="color:blue"&gt;if &lt;/span&gt;(&lt;span style="color:blue"&gt;string&lt;/span&gt;.IsNullOrEmpty(mask))
        {
            &lt;span style="color:blue"&gt;return true&lt;/span&gt;;
        }

        &lt;span style="color:blue"&gt;foreach &lt;/span&gt;(&lt;span style="color:blue"&gt;char &lt;/span&gt;ch &lt;span style="color:blue"&gt;in &lt;/span&gt;mask)
        {
            &lt;span style="color:blue"&gt;if &lt;/span&gt;(!&lt;span style="color:#2b91af"&gt;MaskedTextProvider&lt;/span&gt;.IsValidMaskChar(ch))
            {
                &lt;span style="color:blue"&gt;return false&lt;/span&gt;;
            }
        }

        &lt;span style="color:blue"&gt;return true&lt;/span&gt;;
    }

    &lt;span style="color:blue"&gt;#endregion

    #region &lt;/span&gt;PromptChar

    &lt;span style="color:blue"&gt;public static readonly &lt;/span&gt;&lt;span style="color:#2b91af"&gt;DependencyProperty &lt;/span&gt;PromptCharProperty =
        &lt;span style="color:#2b91af"&gt;DependencyProperty&lt;/span&gt;.RegisterAttached(
            &lt;span style="color:#a31515"&gt;&amp;quot;PromptChar&amp;quot;&lt;/span&gt;,
            &lt;span style="color:blue"&gt;typeof&lt;/span&gt;(&lt;span style="color:blue"&gt;char&lt;/span&gt;),
            &lt;span style="color:blue"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af"&gt;MaskedText&lt;/span&gt;),
            &lt;span style="color:blue"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af"&gt;FrameworkPropertyMetadata&lt;/span&gt;(&lt;span style="color:#a31515"&gt;'_'&lt;/span&gt;, &lt;span style="color:#2b91af"&gt;FrameworkPropertyMetadataOptions&lt;/span&gt;.None, &lt;span style="color:blue"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af"&gt;PropertyChangedCallback&lt;/span&gt;(OnPromptCharChanged)),
            &lt;span style="color:blue"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af"&gt;ValidateValueCallback&lt;/span&gt;(IsPromptCharValid));

    &lt;span style="color:blue"&gt;public static char &lt;/span&gt;GetPromptChar(&lt;span style="color:#2b91af"&gt;DependencyObject &lt;/span&gt;d)
    {
        &lt;span style="color:blue"&gt;return &lt;/span&gt;(&lt;span style="color:blue"&gt;char&lt;/span&gt;)d.GetValue(PromptCharProperty);
    }

    &lt;span style="color:blue"&gt;public static void &lt;/span&gt;SetPromptChar(&lt;span style="color:#2b91af"&gt;DependencyObject &lt;/span&gt;d, &lt;span style="color:blue"&gt;char &lt;/span&gt;value)
    {
        d.SetValue(PromptCharProperty, value);
    }

    &lt;span style="color:blue"&gt;private static void &lt;/span&gt;OnPromptCharChanged(&lt;span style="color:#2b91af"&gt;DependencyObject &lt;/span&gt;d, &lt;span style="color:#2b91af"&gt;DependencyPropertyChangedEventArgs &lt;/span&gt;e)
    {
        &lt;span style="color:#2b91af"&gt;MaskedText &lt;/span&gt;maskedText = GetInstance(d);
        &lt;span style="color:blue"&gt;if &lt;/span&gt;(maskedText != &lt;span style="color:blue"&gt;null&lt;/span&gt;)
        {
            maskedText._provider.PromptChar = (&lt;span style="color:blue"&gt;char&lt;/span&gt;)e.NewValue;
            maskedText.SetText();
        }
    }

    &lt;span style="color:blue"&gt;private static bool &lt;/span&gt;IsPromptCharValid(&lt;span style="color:blue"&gt;object &lt;/span&gt;value)
    {
        &lt;span style="color:blue"&gt;char &lt;/span&gt;ch = (&lt;span style="color:blue"&gt;char&lt;/span&gt;)value;
        &lt;span style="color:blue"&gt;return &lt;/span&gt;&lt;span style="color:#2b91af"&gt;MaskedTextProvider&lt;/span&gt;.IsValidPasswordChar(ch);
    }

    &lt;span style="color:blue"&gt;#endregion

    #region &lt;/span&gt;IncludePrompt

    &lt;span style="color:blue"&gt;public static readonly &lt;/span&gt;&lt;span style="color:#2b91af"&gt;DependencyProperty &lt;/span&gt;IncludePromptProperty =
        &lt;span style="color:#2b91af"&gt;DependencyProperty&lt;/span&gt;.RegisterAttached(
            &lt;span style="color:#a31515"&gt;&amp;quot;IncludePrompt&amp;quot;&lt;/span&gt;,
            &lt;span style="color:blue"&gt;typeof&lt;/span&gt;(&lt;span style="color:blue"&gt;bool&lt;/span&gt;),
            &lt;span style="color:blue"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af"&gt;MaskedText&lt;/span&gt;),
            &lt;span style="color:blue"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af"&gt;FrameworkPropertyMetadata&lt;/span&gt;(&lt;span style="color:blue"&gt;false&lt;/span&gt;, &lt;span style="color:blue"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af"&gt;PropertyChangedCallback&lt;/span&gt;(OnIncludePromptChanged)));

    &lt;span style="color:blue"&gt;public static bool &lt;/span&gt;GetIncludePrompt(&lt;span style="color:#2b91af"&gt;DependencyObject &lt;/span&gt;d)
    {
        &lt;span style="color:blue"&gt;return &lt;/span&gt;(&lt;span style="color:blue"&gt;bool&lt;/span&gt;)d.GetValue(IncludePromptProperty);
    }

    &lt;span style="color:blue"&gt;public static void &lt;/span&gt;SetIncludePrompt(&lt;span style="color:#2b91af"&gt;DependencyObject &lt;/span&gt;d, &lt;span style="color:blue"&gt;bool &lt;/span&gt;value)
    {
        d.SetValue(IncludePromptProperty, value);
    }

    &lt;span style="color:blue"&gt;private static void &lt;/span&gt;OnIncludePromptChanged(&lt;span style="color:#2b91af"&gt;DependencyObject &lt;/span&gt;d, &lt;span style="color:#2b91af"&gt;DependencyPropertyChangedEventArgs &lt;/span&gt;e)
    {
        &lt;span style="color:#2b91af"&gt;MaskedText &lt;/span&gt;maskedText = GetInstance(d);
        &lt;span style="color:blue"&gt;if &lt;/span&gt;(maskedText != &lt;span style="color:blue"&gt;null&lt;/span&gt;)
        {
            maskedText.CreateProvider();
        }
    }

    &lt;span style="color:blue"&gt;#endregion

    #region &lt;/span&gt;HidePromptOnLeave

    &lt;span style="color:blue"&gt;public static readonly &lt;/span&gt;&lt;span style="color:#2b91af"&gt;DependencyProperty &lt;/span&gt;HidePromptOnLeaveProperty =
        &lt;span style="color:#2b91af"&gt;DependencyProperty&lt;/span&gt;.RegisterAttached(
            &lt;span style="color:#a31515"&gt;&amp;quot;HidePromptOnLeave&amp;quot;&lt;/span&gt;,
            &lt;span style="color:blue"&gt;typeof&lt;/span&gt;(&lt;span style="color:blue"&gt;bool&lt;/span&gt;),
            &lt;span style="color:blue"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af"&gt;MaskedText&lt;/span&gt;),
            &lt;span style="color:blue"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af"&gt;FrameworkPropertyMetadata&lt;/span&gt;(&lt;span style="color:blue"&gt;false&lt;/span&gt;, &lt;span style="color:blue"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af"&gt;PropertyChangedCallback&lt;/span&gt;(OnHidePromptOnLeaveChanged)));

    &lt;span style="color:blue"&gt;public static bool &lt;/span&gt;GetHidePromptOnLeave(&lt;span style="color:#2b91af"&gt;DependencyObject &lt;/span&gt;d)
    {
        &lt;span style="color:blue"&gt;return &lt;/span&gt;(&lt;span style="color:blue"&gt;bool&lt;/span&gt;)d.GetValue(HidePromptOnLeaveProperty);
    }

    &lt;span style="color:blue"&gt;public static void &lt;/span&gt;SetHidePromptOnLeave(&lt;span style="color:#2b91af"&gt;DependencyObject &lt;/span&gt;d, &lt;span style="color:blue"&gt;bool &lt;/span&gt;value)
    {
        d.SetValue(HidePromptOnLeaveProperty, value);
    }

    &lt;span style="color:blue"&gt;private static void &lt;/span&gt;OnHidePromptOnLeaveChanged(&lt;span style="color:#2b91af"&gt;DependencyObject &lt;/span&gt;d, &lt;span style="color:#2b91af"&gt;DependencyPropertyChangedEventArgs &lt;/span&gt;e)
    {
        &lt;span style="color:#2b91af"&gt;MaskedText &lt;/span&gt;maskedText = GetInstance(d);
        &lt;span style="color:blue"&gt;if &lt;/span&gt;(maskedText != &lt;span style="color:blue"&gt;null&lt;/span&gt;)
        {
            maskedText.SetText();
        }
    }

    &lt;span style="color:blue"&gt;#endregion

    private static void &lt;/span&gt;Attach(&lt;span style="color:#2b91af"&gt;DependencyObject &lt;/span&gt;d)
    {
        &lt;span style="color:#2b91af"&gt;TextBox &lt;/span&gt;textBox = d &lt;span style="color:blue"&gt;as &lt;/span&gt;&lt;span style="color:#2b91af"&gt;TextBox&lt;/span&gt;;
        &lt;span style="color:blue"&gt;if &lt;/span&gt;(textBox != &lt;span style="color:blue"&gt;null&lt;/span&gt;)
        {
            &lt;span style="color:#2b91af"&gt;MaskedText &lt;/span&gt;maskedText = GetInstance(d);
            &lt;span style="color:blue"&gt;if &lt;/span&gt;(maskedText == &lt;span style="color:blue"&gt;null&lt;/span&gt;)
            {
                maskedText = &lt;span style="color:blue"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af"&gt;MaskedText&lt;/span&gt;(textBox);
                SetInstance(d, maskedText);
            }
            &lt;span style="color:blue"&gt;else
            &lt;/span&gt;{
                maskedText.CreateProvider();
            }
        }
    }

    &lt;span style="color:blue"&gt;private static void &lt;/span&gt;Unattach(&lt;span style="color:#2b91af"&gt;DependencyObject &lt;/span&gt;d)
    {
        &lt;span style="color:#2b91af"&gt;MaskedText &lt;/span&gt;maskedText = GetInstance(d);
        &lt;span style="color:blue"&gt;if &lt;/span&gt;(maskedText != &lt;span style="color:blue"&gt;null&lt;/span&gt;)
        {
            maskedText.Dispose();
            SetInstance(d, &lt;span style="color:blue"&gt;null&lt;/span&gt;);
        }
    }
}
&lt;/pre&gt;
&lt;p&gt;Usage is quite simple.&lt;pre&gt;        &lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;TextBox &lt;/span&gt;&lt;span style="color:red"&gt;Margin&lt;/span&gt;&lt;span style="color:blue"&gt;=&amp;quot;2&amp;quot; &lt;/span&gt;&lt;span style="color:red"&gt;local&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:red"&gt;MaskedText.Mask&lt;/span&gt;&lt;span style="color:blue"&gt;=&amp;quot;00/00/0000&amp;quot; &lt;/span&gt;&lt;span style="color:red"&gt;local&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:red"&gt;MaskedText.PromptChar&lt;/span&gt;&lt;span style="color:blue"&gt;=&amp;quot;?&amp;quot;/&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;We're no longer using a MaskedTextBox, but instead we use attached properties and behaviors to make existing TextBox controls behave appropriately.
&lt;p&gt;With Styles, Templates, Decorators (check out the SpinDecorator by &lt;a href="http://www.mindscape.co.nz/Products/WpfElements/controls/SpinControls.aspx"&gt;MindScape&lt;/a&gt;, for example) and attached properties and behaviors you can accomplish most of the things that you used to have to roll a new control for.  In the end, what you get is usually a more reusable piece of functionality than what you could get through inheritance.  For instance, the MaskedText class could be modified to support more than just a TextBox, giving you the same behavior for, say, a ComboBox, while inheritance limits how you can reuse the functionality.
&lt;p&gt;WPF sure is powerful.&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=-3347231426831879729&amp;page=RSS%3a+The+first+rule+of+WPF&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=wekempf.spaces.live.com&amp;amp;GT1=wekempf"&gt;</description><comments>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!508.entry#comment</comments><guid isPermaLink="true">http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!508.entry</guid><pubDate>Wed, 20 Aug 2008 16:28:38 GMT</pubDate><slash:comments>1</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://wekempf.spaces.live.com/blog/cns!D18C3EC06EA971CF!508/comments/feed.rss</wfw:commentRss><wfw:comment>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!508.entry#comment</wfw:comment><dcterms:modified>2008-08-20T16:28:38Z</dcterms:modified></item><item><title>VisualStudio 2008 and .NET Framework SP1</title><link>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!491.entry</link><description>&lt;p&gt;These are now available, and there's much new in here. I'm going to provide some links to the pertinent stuff. &lt;p&gt;VS 2008 Service Pack 1:  &lt;ul&gt; &lt;li&gt;Executable: &lt;u&gt;&lt;a href="http://go.microsoft.com/fwlink/?LinkId=122094"&gt;http://go.microsoft.com/fwlink/?LinkId=122094&lt;/a&gt;&lt;/u&gt; (&lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=FBEE1648-7106-44A7-9649-6D9F6D58056E&amp;amp;displaylang=en"&gt;Direct Link&lt;/a&gt;)  &lt;li&gt;Iso: &lt;u&gt;&lt;a href="http://go.microsoft.com/fwlink/?LinkId=122095"&gt;http://go.microsoft.com/fwlink/?LinkId=122095&lt;/a&gt;&lt;/u&gt; (&lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=27673C47-B3B5-4C67-BD99-84E525B5CE61&amp;amp;displaylang=en"&gt;Direct Link&lt;/a&gt;) &lt;/ul&gt; &lt;p&gt;VS 2008 Express Editions with Service Pack 1:  &lt;ul&gt; &lt;li&gt;Executable: &lt;u&gt;&lt;a href="http://go.microsoft.com/fwlink/?LinkId=123679"&gt;http://go.microsoft.com/fwlink/?LinkId=123679&lt;/a&gt;&lt;/u&gt;  &lt;li&gt;ISO: &lt;u&gt;&lt;a href="http://go.microsoft.com/fwlink/?LinkId=123680"&gt;http://go.microsoft.com/fwlink/?LinkId=123680&lt;/a&gt;&lt;/u&gt;&lt;/ul&gt; &lt;p&gt;VS Team System 2008 Team Foundation SP 1:  &lt;ul&gt; &lt;li&gt;&lt;u&gt;&lt;a href="http://go.microsoft.com/fwlink/?LinkId=124829"&gt;http://go.microsoft.com/fwlink/?LinkId=124829&lt;/a&gt;&lt;/u&gt;&lt;/ul&gt; &lt;p&gt;.NET Framework 3.5 Service Pack 1  &lt;ul&gt; &lt;li&gt;&lt;u&gt;&lt;a href="http://go.microsoft.com/fwlink/?LinkId=124150"&gt;http://go.microsoft.com/fwlink/?LinkId=124150&lt;/a&gt;&lt;/u&gt;&lt;/ul&gt; &lt;p&gt;Team Foundation Server  &lt;ul&gt; &lt;li&gt;&lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=9e40a5b6-da41-43a2-a06d-3cee196bfe3d&amp;amp;DisplayLang=en"&gt;Team Foundation Server 2008 SP1&lt;/a&gt; (&lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=FF12844F-398C-4FE9-8B0D-9E84181D9923&amp;amp;displaylang=en"&gt;TFS Installation Guide&lt;/a&gt;) &lt;/ul&gt; &lt;p&gt;A general description of the release can be found &lt;a href="http://msdn.microsoft.com/en-us/vstudio/products/cc533447.aspx"&gt;here&lt;/a&gt;. &lt;p&gt;Here's a list of some of the new WPF features, with links to information about them. &lt;ul&gt; &lt;li&gt;Item Container recycling: &lt;a title="http://msdn.microsoft.com/en-us/vstudio/products/cc533447.aspx" href="http://msdn.microsoft.com/en-us/vstudio/products/cc533447.aspx"&gt;http://msdn.microsoft.com/en-us/vstudio/products/cc533447.aspx&lt;/a&gt;  &lt;li&gt;TreeView virtualization: &lt;a title="http://phizzpop.visitmix.com/blogs/techniques/archive/2008/07/06/improving-your-wpf-3-5-sp1-controls-performance.aspx" href="http://phizzpop.visitmix.com/blogs/techniques/archive/2008/07/06/improving-your-wpf-3-5-sp1-controls-performance.aspx"&gt;http://phizzpop.visitmix.com/blogs/techniques/archive/2008/07/06/improving-your-wpf-3-5-sp1-controls-performance.aspx&lt;/a&gt;  &lt;li&gt;Writable bitmap: &lt;a title="http://channel9.msdn.com/posts/AdamKinney/WPF-35-SP1-Graphics-with-David-Teitlebaum/?Page=3" href="http://channel9.msdn.com/posts/AdamKinney/WPF-35-SP1-Graphics-with-David-Teitlebaum/?Page=3"&gt;http://channel9.msdn.com/posts/AdamKinney/WPF-35-SP1-Graphics-with-David-Teitlebaum/?Page=3&lt;/a&gt;  &lt;li&gt;StringFormat: &lt;a title="http://blogs.msdn.com/llobo/archive/2008/05/19/wpf-3-5-sp1-feature-stringformat.aspx" href="http://blogs.msdn.com/llobo/archive/2008/05/19/wpf-3-5-sp1-feature-stringformat.aspx"&gt;http://blogs.msdn.com/llobo/archive/2008/05/19/wpf-3-5-sp1-feature-stringformat.aspx&lt;/a&gt;  &lt;li&gt;RenderMode and SoftwareOnly: &lt;a title="http://blogs.msdn.com/jgoldb/archive/2007/10/10/performance-improvements-in-wpf-in-net-3-5-3-0-sp1.aspx" href="http://blogs.msdn.com/jgoldb/archive/2007/10/10/performance-improvements-in-wpf-in-net-3-5-3-0-sp1.aspx"&gt;http://blogs.msdn.com/jgoldb/archive/2007/10/10/performance-improvements-in-wpf-in-net-3-5-3-0-sp1.aspx&lt;/a&gt;  &lt;li&gt;Deferred scrolling: &lt;a title="http://dotnet.org.za/rudi/archive/2008/05/22/scrolling-performance-in-wpf.aspx" href="http://dotnet.org.za/rudi/archive/2008/05/22/scrolling-performance-in-wpf.aspx"&gt;http://dotnet.org.za/rudi/archive/2008/05/22/scrolling-performance-in-wpf.aspx&lt;/a&gt;  &lt;li&gt;Alternating row style support: &lt;a title="http://blogs.msdn.com/vinsibal/archive/2008/05/28/wpf-3-5-sp1-feature-alternating-rows.aspx" href="http://blogs.msdn.com/vinsibal/archive/2008/05/28/wpf-3-5-sp1-feature-alternating-rows.aspx"&gt;http://blogs.msdn.com/vinsibal/archive/2008/05/28/wpf-3-5-sp1-feature-alternating-rows.aspx&lt;/a&gt;  &lt;li&gt;Null value conversion and handling  &lt;li&gt;Item-level validation: &lt;a title="http://blogs.msdn.com/vinsibal/archive/2008/08/11/wpf-3-5-sp1-feature-bindinggroups-with-item-level-validation.aspx" href="http://blogs.msdn.com/vinsibal/archive/2008/08/11/wpf-3-5-sp1-feature-bindinggroups-with-item-level-validation.aspx"&gt;http://blogs.msdn.com/vinsibal/archive/2008/08/11/wpf-3-5-sp1-feature-bindinggroups-with-item-level-validation.aspx&lt;/a&gt;  &lt;li&gt;MultiSelector support  &lt;li&gt;IEditableCollectionView support: &lt;a title="http://blogs.msdn.com/vinsibal/archive/2008/05/20/wpf-3-5-sp1-feature-ieditablecollectionview.aspx" href="http://blogs.msdn.com/vinsibal/archive/2008/05/20/wpf-3-5-sp1-feature-ieditablecollectionview.aspx"&gt;http://blogs.msdn.com/vinsibal/archive/2008/05/20/wpf-3-5-sp1-feature-ieditablecollectionview.aspx&lt;/a&gt;  &lt;li&gt;Extensible Shader Effects: &lt;a title="http://runtothehills.org/rob/archives/180" href="http://runtothehills.org/rob/archives/180"&gt;http://runtothehills.org/rob/archives/180&lt;/a&gt;  &lt;li&gt;D3DImage: &lt;a title="http://www.codeproject.com/KB/WPF/D3DImage.aspx" href="http://www.codeproject.com/KB/WPF/D3DImage.aspx"&gt;http://www.codeproject.com/KB/WPF/D3DImage.aspx&lt;/a&gt;  &lt;li&gt;SplashScreen: &lt;a title="http://tozon.info/blogs/andrej/archive/2008/08/11/Splash-Screen-in-WPF.aspx" href="http://tozon.info/blogs/andrej/archive/2008/08/11/Splash-Screen-in-WPF.aspx"&gt;http://tozon.info/blogs/andrej/archive/2008/08/11/Splash-Screen-in-WPF.aspx&lt;/a&gt;&lt;/ul&gt; &lt;p&gt;There were some things left out of SP1 that we had been under the impression were &amp;quot;in the works&amp;quot;.  Amongst these were a DateTimePicker and a DataGrid.  These did not make it into SP1, but it looks like we'll be getting them &amp;quot;out of band&amp;quot; prior to their inclusion in some future WPF update.  The &lt;a href="http://www.codeplex.com/wpf"&gt;DataGrid&lt;/a&gt; has already been released to CodePlex in what's being called the &amp;quot;WPF Toolkit&amp;quot;, with the promise of this being the mechanism for introducing several other WPF features &amp;quot;out of band&amp;quot;. &lt;p&gt;If I've missed any features, or if links are available for any of the features I didn't provide links for, let me know and I'll update this post.&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=-3347231426831879729&amp;page=RSS%3a+VisualStudio+2008+and+.NET+Framework+SP1&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=wekempf.spaces.live.com&amp;amp;GT1=wekempf"&gt;</description><comments>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!491.entry#comment</comments><guid isPermaLink="true">http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!491.entry</guid><pubDate>Tue, 12 Aug 2008 14:06:14 GMT</pubDate><slash:comments>0</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://wekempf.spaces.live.com/blog/cns!D18C3EC06EA971CF!491/comments/feed.rss</wfw:commentRss><wfw:comment>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!491.entry#comment</wfw:comment><dcterms:modified>2008-08-12T14:06:14Z</dcterms:modified></item><item><title>Randomize Extension</title><link>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!470.entry</link><description>&lt;p&gt;I'm not sure how useful this is, but it's a fun little thought experiment at the very least.  The goal here is to randomize the order of an IEnumerable&amp;lt;T&amp;gt; via an extension method.  It's actually a rather simple one liner (well, two if you count the creation of the Random instance).  This specific implementation isn't optimal, and it suffers all the same issues as the pseudo random number generator Random, since it's built on that.  It does illustrate the power of functional programming and LINQ, though.&lt;pre&gt;&lt;span style="color:blue"&gt;public static class &lt;/span&gt;&lt;span style="color:#2b91af"&gt;RandomExtension
&lt;/span&gt;{
    &lt;span style="color:blue"&gt;public static &lt;/span&gt;&lt;span style="color:#2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; Randomize&amp;lt;T&amp;gt;(&lt;span style="color:blue"&gt;this &lt;/span&gt;&lt;span style="color:#2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; self)
    {
        &lt;span style="color:blue"&gt;return &lt;/span&gt;self.OrderBy(i =&amp;gt; random.Next());
    }

    &lt;span style="color:blue"&gt;private static &lt;/span&gt;&lt;span style="color:#2b91af"&gt;Random &lt;/span&gt;random = &lt;span style="color:blue"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af"&gt;Random&lt;/span&gt;();
}
&lt;/pre&gt;
&lt;p&gt;Now you can easily iterate randomly over any enumerable.  LINQ/functional programming is quite fun, IMHO.
&lt;p&gt;&lt;strong&gt;Edit 8/28:&lt;/strong&gt; Greatly simplified the implementation of this extension method. The previous implementation was clever, but just not as efficient.  Looking at how simple this implementation is, you could argue there's little reason to wrap it up in an extension method.&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=-3347231426831879729&amp;page=RSS%3a+Randomize+Extension&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=wekempf.spaces.live.com&amp;amp;GT1=wekempf"&gt;</description><comments>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!470.entry#comment</comments><guid isPermaLink="true">http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!470.entry</guid><pubDate>Fri, 01 Aug 2008 17:57:13 GMT</pubDate><slash:comments>1</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://wekempf.spaces.live.com/blog/cns!D18C3EC06EA971CF!470/comments/feed.rss</wfw:commentRss><wfw:comment>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!470.entry#comment</wfw:comment><dcterms:modified>2008-08-27T19:33:35Z</dcterms:modified></item><item><title>Attached Read-only Collection Dependency Properties</title><link>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!468.entry</link><description>&lt;p&gt;Oh wow!  This is one I've been wanting almost since the day I started learning WPF.  It seemed like such a huge thing missing from WPF.  How in the heck do you manage to make an attached read-only dependency property with a collection type? 
&lt;p&gt;At first blush, many of you are thinking &amp;quot;What's the big deal?  Attached read-only dependency properties are clearly documented and easy to implement.&amp;quot;  Very true.  But if you're thinking that, you're glossing over the critical bit here: the type of this property is supposed to be a collection.  &amp;quot;So what?&amp;quot; is your response?  Well, smarty, how do you initialize that property?  See, if we rely on the dependency property's &amp;quot;default value&amp;quot; mechanism, since this is an attached property &lt;strong&gt;every instance will have that value&lt;/strong&gt;.  Now, what you need to be able to do here is lazily create the collection &lt;strong&gt;on first access&lt;/strong&gt;.  However, WPF doesn't provide you with any callbacks for retrieving the value, only for setting the value, and the XAML parser bypasses the CLR &amp;quot;get wrapper&amp;quot;.  So, there's no place to lazily instantiate the collection.  I searched high and low for a solution here.  I even talked directly with several Microsoft WPF developers.  I think I may have even made an official request for an event to hook into for this purpose.  In the end, though, I was left wanting. 
&lt;p&gt;That is, until today.  &lt;a href="http://blogs.msdn.com/johngossman/"&gt;John Gossman&lt;/a&gt; e-mailed me today with a solution to this problem that works with the current WPF system.  Holy smokes! 
&lt;p&gt;The key to his solution lies with the fact that I didn't fully understand WPF (because this something that's implied and not spelled out in the documentation).  Remember when I said the XAML parser bypasses the CLR &amp;quot;get wrapper&amp;quot;?  Well, that's not always true.  It seems that if you register the dependency property with a &amp;quot;name&amp;quot; that differs from the CLR name, the XAML parser will call your CLR &amp;quot;get wrapper&amp;quot; instead of bypassing it and going directly to GetValue.  This may not make a lot of sense with out an example.  Here's such an example (I'm not including the code for the Foo type, as it's not really relevant):&lt;pre&gt;&lt;span style="color:blue"&gt;public class &lt;/span&gt;&lt;span style="color:#2b91af"&gt;FooCollection &lt;/span&gt;: &lt;span style="color:#2b91af"&gt;ObservableCollection&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af"&gt;Foo&lt;/span&gt;&amp;gt;
{
    &lt;span style="color:blue"&gt;public static readonly &lt;/span&gt;&lt;span style="color:#2b91af"&gt;DependencyPropertyKey &lt;/span&gt;InstancePropertyKey =
        &lt;span style="color:#2b91af"&gt;DependencyProperty&lt;/span&gt;.RegisterAttachedReadOnly(&lt;span style="color:#a31515"&gt;&amp;quot;InstanceInternal&amp;quot;&lt;/span&gt;,
        &lt;span style="color:blue"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af"&gt;FooCollection&lt;/span&gt;),
        &lt;span style="color:blue"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af"&gt;FooCollection&lt;/span&gt;),
        &lt;span style="color:blue"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af"&gt;UIPropertyMetadata&lt;/span&gt;(&lt;span style="color:blue"&gt;null&lt;/span&gt;));

    &lt;span style="color:blue"&gt;public static readonly &lt;/span&gt;&lt;span style="color:#2b91af"&gt;DependencyProperty &lt;/span&gt;InstanceProperty = InstancePropertyKey.DependencyProperty;

    &lt;span style="color:blue"&gt;private static void &lt;/span&gt;SetInstance(&lt;span style="color:#2b91af"&gt;DependencyObject &lt;/span&gt;obj, &lt;span style="color:#2b91af"&gt;FooCollection &lt;/span&gt;value)
    {
        obj.SetValue(&lt;span style="color:#2b91af"&gt;FooCollection&lt;/span&gt;.InstancePropertyKey, value);
    }

    &lt;span style="color:blue"&gt;public static &lt;/span&gt;&lt;span style="color:#2b91af"&gt;FooCollection &lt;/span&gt;GetInstance(&lt;span style="color:#2b91af"&gt;DependencyObject &lt;/span&gt;obj)
    {
        &lt;span style="color:blue"&gt;if &lt;/span&gt;(obj == &lt;span style="color:blue"&gt;null&lt;/span&gt;) &lt;span style="color:blue"&gt;throw new &lt;/span&gt;&lt;span style="color:#2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color:#a31515"&gt;&amp;quot;obj&amp;quot;&lt;/span&gt;);
        &lt;span style="color:#2b91af"&gt;FooCollection &lt;/span&gt;foos = obj.GetValue(&lt;span style="color:#2b91af"&gt;FooCollection&lt;/span&gt;.InstanceProperty) &lt;span style="color:blue"&gt;as &lt;/span&gt;&lt;span style="color:#2b91af"&gt;FooCollection&lt;/span&gt;;
        &lt;span style="color:blue"&gt;if &lt;/span&gt;(foos == &lt;span style="color:blue"&gt;null&lt;/span&gt;)
        {
            foos = &lt;span style="color:blue"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af"&gt;FooCollection&lt;/span&gt;();
            SetInstance(obj, foos);
        }
        &lt;span style="color:blue"&gt;return &lt;/span&gt;foos;
    }
}
&lt;/pre&gt;
&lt;p&gt;The key above is the first parameter passed to RegisterAttachedReadOnly: &amp;quot;InstanceInternal&amp;quot;.  Since the name is different than the implied CLR name (&amp;quot;Instance&amp;quot;), the XAML parser will now call our GetInstance method, which does the lazy creation.  With this in place, the following XAML code actually works (it doesn't do anything useful, mind you, as this isn't a meaningful example beyond illustrating the concept of lazily creating the read-only attached collection dependency property).&lt;pre&gt;&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Window &lt;/span&gt;&lt;span style="color:red"&gt;x&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:red"&gt;Class&lt;/span&gt;&lt;span style="color:blue"&gt;=&amp;quot;WpfAttachedCollection.Window1&amp;quot;
    &lt;/span&gt;&lt;span style="color:red"&gt;xmlns&lt;/span&gt;&lt;span style="color:blue"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&amp;quot;
    &lt;/span&gt;&lt;span style="color:red"&gt;xmlns&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:red"&gt;x&lt;/span&gt;&lt;span style="color:blue"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml&amp;quot;
    &lt;/span&gt;&lt;span style="color:red"&gt;xmlns&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:red"&gt;ui&lt;/span&gt;&lt;span style="color:blue"&gt;=&amp;quot;clr-namespace:WpfAttachedCollection&amp;quot;
    &lt;/span&gt;&lt;span style="color:red"&gt;Title&lt;/span&gt;&lt;span style="color:blue"&gt;=&amp;quot;Window1&amp;quot; &lt;/span&gt;&lt;span style="color:red"&gt;Height&lt;/span&gt;&lt;span style="color:blue"&gt;=&amp;quot;300&amp;quot; &lt;/span&gt;&lt;span style="color:red"&gt;Width&lt;/span&gt;&lt;span style="color:blue"&gt;=&amp;quot;300&amp;quot;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;ui&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:#a31515"&gt;FooCollection.Instance&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;ui&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:#a31515"&gt;Foo &lt;/span&gt;&lt;span style="color:red"&gt;Name&lt;/span&gt;&lt;span style="color:blue"&gt;=&amp;quot;Bar&amp;quot;/&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;ui&lt;/span&gt;&lt;span style="color:blue"&gt;:&lt;/span&gt;&lt;span style="color:#a31515"&gt;FooCollection.Instance&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Grid&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;
        
    &amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Grid&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Window&lt;/span&gt;&lt;span style="color:blue"&gt;&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;This is one case where the VisualStudio designer fails me.  I get an error &amp;quot;The attachable property 'Instance' was not found in type 'FooCollection'.&amp;quot;  I'm not sure what the cause of the failure here is, but when I running the executable, it actually does work.  A little more work and I can add a button that displays the added Foo objects in a MessageBox.Show, just to prove to myself that everything is working. 
&lt;p&gt;Like I said, this isn't exactly documented in the MSDN.  It's only implied.  The relevant documentation given to me by Mr. Gossman is this: 
&lt;blockquote&gt;
&lt;p&gt;Implications for Custom Dependency Properties &lt;br&gt;Because the current WPF implementation of the XAML processor behavior for property setting bypasses the wrappers entirely, you should not put any additional logic into the set definitions of the wrapper for your custom dependency property. If you put such logic in the set definition, then the logic will not be executed when the property is set in XAML rather than in code.&lt;br&gt;Similarly, other aspects of the XAML processor that obtain property values from XAML processing also use GetValue rather than using the wrapper. Therefore, you should also avoid any additional implementation in the get definition beyond the GetValue call.&lt;br&gt;The following example is a recommended dependency property definition with wrappers, where the property identifier is stored as a public static readonly field, and the get and set definitions contain no code beyond the necessary property system methods that define the dependency property backing.&lt;/blockquote&gt;
&lt;p&gt;Mr. Gossman assures me the behavior here won't change, because it's relied upon by too much existing code.  So, maybe it will be documented in the future, but for now you can feel safe in make use of this &amp;quot;hack&amp;quot;.  I owe Mr. Gossman a &lt;strong&gt;huge&lt;/strong&gt; thank you for providing this to me, and I'm very happy to provide a post here for future Googlers.
&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt;  Looks like Mr. Gossman &lt;a href="http://blogs.gotdotnet.com/johngossman/archive/2008/07/28/how-to-initialize-an-attached-dependencyproperty-of-type-collection.aspx"&gt;blogged&lt;/a&gt; about this as well.&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=-3347231426831879729&amp;page=RSS%3a+Attached+Read-only+Collection+Dependency+Properties&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=wekempf.spaces.live.com&amp;amp;GT1=wekempf"&gt;</description><comments>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!468.entry#comment</comments><guid isPermaLink="true">http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!468.entry</guid><pubDate>Mon, 28 Jul 2008 20:54:47 GMT</pubDate><slash:comments>2</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://wekempf.spaces.live.com/blog/cns!D18C3EC06EA971CF!468/comments/feed.rss</wfw:commentRss><wfw:comment>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!468.entry#comment</wfw:comment><dcterms:modified>2008-07-29T13:09:34Z</dcterms:modified></item><item><title>Rant: $%!#(* Grid</title><link>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!463.entry</link><description>&lt;p&gt;I'm a tad frustrated by the default templates used by VisualStudio and Expression Blend for WPF.  The XAML always starts you out with a Grid.  WTF? 
&lt;p&gt;The Grid panel is the most inefficient of the panels.  It's also not all that easy to use for most layouts.  In other words, its the layout I would expect most developers to use the second least often (Canvas takes the bottom spot). 
&lt;p&gt;Don't get me wrong.  There's plenty of scenarios in which Grid is your best layout choice.  However, there's even more scenarios where it's not.  Rarely is it the panel I'll want my Window/Page to use by default.  I almost always rip that Grid out of the generated XAML and replace it with either a DockPanel or a StackPanel. 
&lt;p&gt;So why the harsh tone to this rant?  After all, it's not that big of a deal to replace it, if it's not appropriate. 
&lt;p&gt;The tone is harsh, because having Grid as the default panel generated by tools is leading the unwashed developers into some bad habits.  Most developers don't understand why Grid isn't as appropriate as other panels.  When they see it being used by default, they think it's the panel they should fall back on by default.  This leads to an overuse of the panel.  That really bothers me.  How many times have you seen a Grid used when there's a single column and/or row, for instance? 
&lt;p&gt;Sorry, this is partially a religious debate.  The performance of a Grid isn't so bad as to matter most of the time.  But I still have the hairs on the back of my neck stand on end the majority of the time I see a Grid used in WPF.  The fact that the generated XAML encourages this doesn't help.
&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt;  Here's a classic example.  The recently released &lt;a href="http://www.codeplex.com/CompositeWPF"&gt;Prism&lt;/a&gt; contains a &amp;quot;view&amp;quot; called OrdersToolbar.  The OrdersToolbar.xaml file contains some lovely code in which the default template's Grid element was left in the source.  It's only child element is a horizontal StackPanel that really defines the layout for the user control.  Remove the Grid and the resulting user control is functionally identical, with easier to understand XAML markup and theoretically better performance (yeah, the perf hit here is not noticeable to the user and probably very difficult to measure, so I don't want to make too much out of that, but it is still there).  Maybe the template shouldn't include anything but the Window/Page/UserControl, but it certainly shouldn't include a Grid, IMHO.&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=-3347231426831879729&amp;page=RSS%3a+Rant%3a+%24%25!%23(*+Grid&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=wekempf.spaces.live.com&amp;amp;GT1=wekempf"&gt;</description><comments>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!463.entry#comment</comments><guid isPermaLink="true">http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!463.entry</guid><pubDate>Mon, 21 Jul 2008 14:07:18 GMT</pubDate><slash:comments>1</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://wekempf.spaces.live.com/blog/cns!D18C3EC06EA971CF!463/comments/feed.rss</wfw:commentRss><wfw:comment>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!463.entry#comment</wfw:comment><dcterms:modified>2008-07-23T14:14:41Z</dcterms:modified></item><item><title>An object you can depend on</title><link>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!444.entry</link><description>&lt;p&gt;Sorry for that.  I needed a catchy title ;). &lt;p&gt;Well, now that I'm a &lt;a href="http://wpfdisciples.wordpress.com/"&gt;WPF Disciple&lt;/a&gt; I should probably blog more about WPF.  What's interesting though, is despite the fact that I'm now working on a WPF project, most of the coding I've been doing lately has not been UI related.  So, not much to blog about WPF. &lt;p&gt;Well, over lunch today my mind was wandering over the stuff I have worked on lately.  Among them was the IoC container I've been tooling about with.  One of the things I did with the container was to extend the functionality through extension methods.  Worked great, but occasionally your extensions needed new state information that didn't exist on the object.  Well now, this ties back to WPF after all. &lt;p&gt;See, there's this concept in WPF that directly addresses this.  It's called an &amp;quot;attached property&amp;quot;.  An attached property is a property associated with an object instance, but the backing store isn't the object itself.  This is accomplished through DependencyObject and DependencyProperty classes.  A DependencyObject has GetValue() and SetValue() methods that take a DependencyProperty as a parameter, and get/set values for that dependency property through some magical backing store that's not actually part of the object (basically the storage is some giant HashMap).  An attached property is a special type of DependencyProperty that allows you to attach values to any DependencyObject, not just the object on which the DependencyProperty is defined. &lt;p&gt;So, by turning my ObjectContainer into a DependencyObject, I can allow extension methods to maintain their own state information on the container they are extending!  Such a simple concept, and yet it didn't dawn on me to do this in my initial implementations.  I rolled all of the necessary plumbing to track associated external state information by hand, and did it every time I needed new state information in a new extension method.  What a waste of time and effort! :) &lt;p&gt;I think I'm starting to fall in love all over again with DependencyObjects.  I've avoided using them anywhere but at the UI layer.  I thought it was wrong to bring in all of this baggage at the data/domain layer.  After all, INotifyPropertyChanged gave me everything I need at that level, &lt;a href="http://www.pluralsight.com/blogs/dbox/archive/2007/05/10/47283.aspx"&gt;and Don Box agreed with me&lt;/a&gt;.  Well, now I think Don and I were wrong. &lt;p&gt;You see, even at the domain layer, it's not that unusual to need an &amp;quot;extension mechanism&amp;quot;.  Some way for a known domain object to be extended with more features under certain circumstances.  We've all rolled specialized mechanisms for allowing this in the past.  However, with a DependencyObject this functionality is baked in, using a mechanism that's well supported by the rest of the framework.  I used to think that dependency properties were too much of a PITA to deal with unless you absolutely had to... but I think I'm changing my mind.  The flexibility and functionality you gain by deriving from DependencyObject is probably enough to outweigh the development cost, especially if you use a good set of &lt;a href="http://drwpf.com/blog/Home/tabid/36/EntryID/22/Default.aspx"&gt;snippets&lt;/a&gt;. &lt;p&gt;I've done a lot of programming in dynamic languages.  The ability to add functions and data to an existing object always proved invaluable in those languages.  Of course, the lack of compile time checks on all of that made maintaining those code bases a bit of a PITA.  It's kind of exciting to realize now that I have most of the capability to do this but in a statically checked at compile time nature.&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=-3347231426831879729&amp;page=RSS%3a+An+object+you+can+depend+on&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=wekempf.spaces.live.com&amp;amp;GT1=wekempf"&gt;</description><comments>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!444.entry#comment</comments><guid isPermaLink="true">http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!444.entry</guid><pubDate>Wed, 21 May 2008 17:21:22 GMT</pubDate><slash:comments>3</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://wekempf.spaces.live.com/blog/cns!D18C3EC06EA971CF!444/comments/feed.rss</wfw:commentRss><wfw:comment>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!444.entry#comment</wfw:comment><dcterms:modified>2008-05-21T17:21:22Z</dcterms:modified></item><item><title>Newest WPF Disciple</title><link>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!435.entry</link><description>&lt;p&gt;You have &lt;strong&gt;GOT&lt;/strong&gt; to check out the newest &lt;a href="http://wpfdisciples.wordpress.com/bios/count-xamula/"&gt;WPF Disciple&lt;/a&gt;.  I'm not sure I can &lt;em&gt;count&lt;/em&gt; all of the contributions he's made to the community.&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=-3347231426831879729&amp;page=RSS%3a+Newest+WPF+Disciple&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=wekempf.spaces.live.com&amp;amp;GT1=wekempf"&gt;</description><comments>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!435.entry#comment</comments><guid isPermaLink="true">http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!435.entry</guid><pubDate>Fri, 02 May 2008 18:27:30 GMT</pubDate><slash:comments>1</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://wekempf.spaces.live.com/blog/cns!D18C3EC06EA971CF!435/comments/feed.rss</wfw:commentRss><wfw:comment>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!435.entry#comment</wfw:comment><dcterms:modified>2008-05-02T18:27:30Z</dcterms:modified></item><item><title>OCD - Oh is that Code Disgusting!</title><link>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!434.entry</link><description>&lt;p&gt;This is a little rant.  If you don't like those, move along. &lt;p&gt;I think coders have been slowly becoming lazier, and now it's really started to get on my nerves.  I've seen SOOO much public code lately that is just frightening!  I'm not referring to the algorithms or other technical aspects of the code here, but rather to the lack of formatting. &lt;p&gt;I'm not starting a religious war here.  I don't care if you put your opening brace on the same line or the next.  I don't care if the braces are indented or not.  I don't care if you use tabs or spaces.  I don't care if indents are 3 spaces or 12.  I don't care if you mark members with an &amp;quot;_&amp;quot;, an &amp;quot;m_&amp;quot; or any other wart.  (Well, to be fair, I &lt;strong&gt;do&lt;/strong&gt; have opinions on all of those, I just don't care if your opinion differs.) &lt;p&gt;No, what I care about is consistency.  It's the lack of consistency that I've been seeing lately that's lead to this rant.  In fact, it goes beyond a lack of consistency.  Developers aren't even TRYING to make their code readable or maintainable.  Want an example of what I'm ranting about?  Check out the published code in this &lt;a href="http://www.codeproject.com/KB/architecture/designing_application.aspx"&gt;article&lt;/a&gt; on CodeProject.  I don't even know how hard you have to work to get code this unformatted, considering all editors today at the very least auto-indent for you.  VisualStudio will even indent pasted code that originally had a different indent properly.  And one of my bigger pet peeves is with extraneous blank lines.  A &lt;strong&gt;single&lt;/strong&gt; blank line in logical places to call out sections of code is very appropriate.  I use a lot of them in my own code.  But in that first code snippet there's &lt;strong&gt;no&lt;/strong&gt; consistency in the way blank lines are used, and I count at least 8 lines that have no purpose existing.  Worse, some of those are consecutive. &lt;p&gt;Are we really becoming to lazy to bother making our code look professional, instead of looking like it was mashed out on a keyboard by a monkey with 3 missing fingers? &lt;p&gt;&lt;em&gt;Disclaimer: I don't mean to pick on the author of that CodeProject article.  I've not read the article yet, and it may be of high quality, ignoring this glaring issue.&lt;/em&gt;&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=-3347231426831879729&amp;page=RSS%3a+OCD+-+Oh+is+that+Code+Disgusting!&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=wekempf.spaces.live.com&amp;amp;GT1=wekempf"&gt;</description><comments>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!434.entry#comment</comments><guid isPermaLink="true">http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!434.entry</guid><pubDate>Fri, 02 May 2008 13:40:04 GMT</pubDate><slash:comments>2</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://wekempf.spaces.live.com/blog/cns!D18C3EC06EA971CF!434/comments/feed.rss</wfw:commentRss><wfw:comment>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!434.entry#comment</wfw:comment><dcterms:modified>2008-05-02T13:40:04Z</dcterms:modified></item><item><title>The "var" controversy</title><link>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!428.entry</link><description>&lt;p&gt;There's some blog buzz going on right now about the appropriateness of using the new C# &amp;quot;var&amp;quot; keyword.  I first ran across the meme from Jean-Paul S. Boodhoo's blog, with &lt;a href="http://codebetter.com/blogs/jean-paul_boodhoo/archive/2008/04/22/got-var.aspx"&gt;this&lt;/a&gt; post.  He later linked to a &lt;a href="http://resharper.blogspot.com/2008/03/varification-using-implicitly-typed.html"&gt;post&lt;/a&gt; by Ilya Ryzhenkov on the same subject.  One of the responses on Ilya's blog read: &lt;blockquote&gt; &lt;p&gt;&lt;i&gt;&amp;quot;The upshot here is that vars generate some serious code - all for good reason when using LINQ. But NOT for a good reason if you’re being lazy - which is the point of this whole post. If you find yourself using “var” anywhere that’s not within a LINQ statement, it’s probably not a good idea.&amp;quot;&lt;/i&gt;&lt;/blockquote&gt; &lt;p&gt;This response was quoting a &lt;a href="http://blog.wekeroad.com/2007/08/29/the-c-makeover/"&gt;post&lt;/a&gt; by Rob Conery.  Let me first say, I have not read all of Rob's post (mostly because the formatting is so bad, it makes it hard to read the post, and I don't have the time to spend on the effort).  Maybe this quote is taken out of context, so take what I say next with a grain of salt.  This quote is utter hogwash.  The &amp;quot;var&amp;quot; keyword produces &lt;strong&gt;no&lt;/strong&gt; extra code.  Prove it to yourself.&lt;pre&gt;&lt;span style="color:blue"&gt;public class &lt;/span&gt;&lt;span style="color:#2b91af"&gt;Foo
&lt;/span&gt;{
}

&lt;span style="color:blue"&gt;class &lt;/span&gt;&lt;span style="color:#2b91af"&gt;Program
&lt;/span&gt;{
    &lt;span style="color:blue"&gt;static void &lt;/span&gt;Main(&lt;span style="color:blue"&gt;string&lt;/span&gt;[] args)
    {
        &lt;span style="color:#2b91af"&gt;Foo &lt;/span&gt;explicitFoo = &lt;span style="color:blue"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af"&gt;Foo&lt;/span&gt;();
        &lt;span style="color:blue"&gt;var &lt;/span&gt;inferredFoo = &lt;span style="color:blue"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af"&gt;Foo&lt;/span&gt;();
    }
}
&lt;/pre&gt;
&lt;p&gt;The resultant IL that's generated is this.&lt;pre&gt;.method private hidebysig static &lt;a href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:2.0.0.0:b77a5c561934e089/System.Void"&gt;void&lt;/a&gt; &lt;b&gt;&lt;a href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Playground:1.0.0.0/Playground.Program/Main(String[])"&gt;Main&lt;/a&gt;&lt;/b&gt;(&lt;a href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:2.0.0.0:b77a5c561934e089/System.String"&gt;string&lt;/a&gt;[] args) cil managed
{
    .entrypoint
    .maxstack 1
    .locals init (
        [0] class &lt;a href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Playground:1.0.0.0/Playground.Foo"&gt;Playground.Foo&lt;/a&gt; &lt;b&gt;explicitFoo&lt;/b&gt;,
        [1] class &lt;a href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Playground:1.0.0.0/Playground.Foo"&gt;Playground.Foo&lt;/a&gt; &lt;b&gt;inferredFoo&lt;/b&gt;)
    L_0000: &lt;a&gt;nop&lt;/a&gt; 
    L_0001: &lt;a&gt;newobj&lt;/a&gt; instance &lt;a href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:2.0.0.0:b77a5c561934e089/System.Void"&gt;void&lt;/a&gt; &lt;a href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Playground:1.0.0.0/Playground.Foo"&gt;Playground.Foo&lt;/a&gt;::&lt;a href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Playground:1.0.0.0/Playground.Foo/.ctor()"&gt;.ctor&lt;/a&gt;()
    L_0006: &lt;a&gt;stloc.0&lt;/a&gt; 
    L_0007: &lt;a&gt;newobj&lt;/a&gt; instance &lt;a href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:2.0.0.0:b77a5c561934e089/System.Void"&gt;void&lt;/a&gt; &lt;a href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Playground:1.0.0.0/Playground.Foo"&gt;Playground.Foo&lt;/a&gt;::&lt;a href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Playground:1.0.0.0/Playground.Foo/.ctor()"&gt;.ctor&lt;/a&gt;()
    L_000c: &lt;a&gt;stloc.1&lt;/a&gt; 
    L_000d: &lt;a&gt;ret&lt;/a&gt; 
}
&lt;/pre&gt;
&lt;p&gt;The code for the explicitly declared variable and the inferred though &amp;quot;var&amp;quot; variable is identical.  Do &lt;strong&gt;NOT&lt;/strong&gt; fear using &amp;quot;var&amp;quot; because of performance concerns, as there is none.
&lt;p&gt;With that out of the way, where do I fall in opinion on this subject?  Well, reading the various posts in this meme, there seems to be two camps.  I think both are extremes.  The first extreme is the &amp;quot;Microsoft Camp&amp;quot;.
&lt;blockquote&gt;
&lt;p&gt;“Overuse of var can make source code less readable for others. It is recommended to use var only when it is necessary, that is, when the variable will be used to store an anonymous type or a collection of anonymous types.”&lt;/blockquote&gt;
&lt;p&gt;I simply can't agree with this extreme viewpoint.  Tell me how the following code can possibly be considered less readable for others?&lt;pre&gt;&lt;span style="color:blue"&gt;var &lt;/span&gt;inferredFoo = &lt;span style="color:blue"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af"&gt;Foo&lt;/span&gt;();
&lt;/pre&gt;
&lt;p&gt;The other camp, which I'll call the Boodhoo camp, though I don't have proof that Mr. Boodhoo specifically takes this extreme point of view, believe that you should always use &amp;quot;var&amp;quot;.  I can't agree with that extreme either.  Can anyone tell me what the type of the following declaration is?&lt;pre&gt;&lt;span style="color:blue"&gt;var &lt;/span&gt;current = &lt;span style="color:#2b91af"&gt;Foo&lt;/span&gt;.Current;
&lt;/pre&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;We can have arguments until we're blue in the face about how better naming would have prevented this confusion.  I don't buy the argument, though.  First, names aren't always under your control.  Second, even with better naming it's still possible to find yourself in situations where you don't have enough type information available to you in situations like this.  C# is still a strongly typed language, and knowing the exact type your dealing with is important.  Relying on the IDE is a no go for me, and relying on naming isn't always possible.
&lt;p&gt;So, what do I think?  Out of habit, I'm still not using &amp;quot;var&amp;quot; that frequently, but I see no harm in using it for your typical &amp;quot;new&amp;quot; statements like the first example.  I don't know if I'll get into the habit of doing that or not, but I see no reason to try and talk anyone out of doing so.  For other declarations like the second example, unless the type is anonymous, I'd probably favor the Microsoft guideline of not using &amp;quot;var&amp;quot; here.  You can probably get away with it 80% of the time and I won't care, but that other 20% is enough reason for me to not recommend getting into this habit.
&lt;p&gt;&lt;strong&gt;Edit:&lt;/strong&gt;  From a reply to Ilya's post by &amp;quot;Simon&amp;quot; we get a list of rules much closer to what I think makes sense.
&lt;ul&gt;
&lt;li&gt;Do use &lt;em&gt;var&lt;/em&gt; for anonymous types
&lt;li&gt;Do use &lt;em&gt;var&lt;/em&gt; for initialization from constructors (var list = new List();)
&lt;li&gt;Do use &lt;em&gt;var&lt;/em&gt; for casts (var list = (IList)list;)
&lt;li&gt;Consider using &lt;em&gt;var&lt;/em&gt; where naming implies the type of the variable (var xmlSerializer = GetXmlSerializer();)&lt;/ul&gt;
&lt;p&gt;The last bullet point is the most controversial, but I can agree with it as long as developers are consciously considering the choice.  For the rest, I can see no reason to recommend not using &amp;quot;var&amp;quot; in any of those situations.&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=-3347231426831879729&amp;page=RSS%3a+The+%22var%22+controversy&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=wekempf.spaces.live.com&amp;amp;GT1=wekempf"&gt;</description><category>Programming</category><comments>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!428.entry#comment</comments><guid isPermaLink="true">http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!428.entry</guid><pubDate>Thu, 24 Apr 2008 14:44:48 GMT</pubDate><slash:comments>2</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://wekempf.spaces.live.com/blog/cns!D18C3EC06EA971CF!428/comments/feed.rss</wfw:commentRss><wfw:comment>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!428.entry#comment</wfw:comment><dcterms:modified>2008-04-24T15:00:51Z</dcterms:modified></item><item><title>IoC I can live with</title><link>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!418.entry</link><description>&lt;p&gt;I've been working on that IoC container I've blogged about.  You know, the ultra-lightweight, delegate based at the root with reflection as optional, simple container.  While doing so, I've also been learning about some of the other capabilities in other containers.  One of the things I discovered was Guice and the &amp;quot;annotation&amp;quot; (that would be an attribute for us .NET developers) based configuration it uses.  By itself, not all that exciting as I'd already expected to use attributes for specifying what to inject.  But then I noticed that they allow you to annotate an interface.  Wow.  Now the following is working with my container.&lt;pre&gt;ObjectContainer container = &lt;span style="color:blue"&gt;new &lt;/span&gt;ObjectContainer();
container.RegisterDynamicConstruction();
IFoo foo = container.Resolve&amp;lt;IFoo&amp;gt;();
&lt;/pre&gt;
&lt;p&gt;No, nothing was left out there (other than the definition of IFoo and some other class that implements it).  I've done &lt;strong&gt;no&lt;/strong&gt; configuration.  I just registered dynamic construction which allows the container to create types that haven't been registered by using reflection.  Then I instantiated an IFoo.  Resolve looked for a registered factory for IFoo, and when it didn't find one it looked at the attributes on IFoo to discover a &amp;quot;default&amp;quot; implementation type and instantiated that.  This eliminates 90% of what I dislike about IoC containers.  You don't have to specify a long list of configurations (in code or in some config file, doesn't really matter) to register types with the container even though 90% of the time you'll want the same type.  Why should I configure a container, just because I may want to mock a service out during testing?
&lt;p&gt;Granted, I'm still specifying what types to create.  However, it's done in code, when defining the interface, and is applied any time I use that interface (unless overriden) in every application with out doing any further configuration.  Nearly nirvana.
&lt;p&gt;Unity (as well as other containers) may have this capability... I haven't looked that close.  It does allow you to instantiate concrete types through reflection, which will still do injection, so it wouldn't surprise me too much if they went the extra few inches to this.  If not, I believe it could be trivially added through an extension. So, the only major difference with what I'm building is the complexity/size.  So there's probably not a lot of interest in what I'm building to other folks.  But I want it, and it's been a fun project for discovery.&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=-3347231426831879729&amp;page=RSS%3a+IoC+I+can+live+with&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=wekempf.spaces.live.com&amp;amp;GT1=wekempf"&gt;</description><comments>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!418.entry#comment</comments><guid isPermaLink="true">http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!418.entry</guid><pubDate>Wed, 26 Mar 2008 23:31:46 GMT</pubDate><slash:comments>0</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://wekempf.spaces.live.com/blog/cns!D18C3EC06EA971CF!418/comments/feed.rss</wfw:commentRss><wfw:comment>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!418.entry#comment</wfw:comment><dcterms:modified>2008-03-26T23:31:46Z</dcterms:modified></item><item><title>Build Your Own IoC Container</title><link>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!415.entry</link><description>&lt;p&gt;A while ago, I wrote a blog &lt;a href="http://wekempf.spaces.live.com/blog/cns!D18C3EC06EA971CF!399.entry"&gt;that&lt;/a&gt; was critical of the &lt;a href="http://www.codeplex.com/unity"&gt;Unity&lt;/a&gt; IoC container.  A lot of what I wrote I think still stands.  One complaint, though, does need a (partial) retraction.  I complained that Unity was reflection based instead of being delegate based.  Well, this is true of Unity proper, but the Unity project includes a StaticFactoryExtension which provides an extension to do delegate based construction.  This can be used like this:&lt;pre&gt;container.Configure&amp;lt;IStaticFactoryConfiguration&amp;gt;().RegisterFactory&amp;lt;SqlConnection&amp;gt;(() =&amp;gt; &lt;span style="color:blue"&gt;new &lt;/span&gt;SqlConnection());&lt;/pre&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;This is very flexible, and does provide full functionality.  It's disappointing (to me) that the base functionality is reflection based, and an extension is required to use delegates (after all, the above syntax is a bit ugly), but at least the functionality is there.  Still not what I'd consider a &amp;quot;light weight&amp;quot; IoC container, and I'm still hoping to see some IoC interfaces in the BCL, but this isn't a bad container.
&lt;p&gt;Anyway, because it's still not my ideal container and since I need a container for the &lt;a href="http://www.drwpf.com/Blog/Default.aspx?tabid=36&amp;amp;EntryID=27"&gt;M-V-Poo&lt;/a&gt; library I'm working on, I've been working on my own container.  I have a long way to go, to ensure the proper balance of features while maintaining a very lightweight code base, but I thought I'd share some of what I've come up with.  So, in this blog post I'm going to provide the basis for a lightweight container that you can extend into a container that fits your needs.  Before we begin, I'll point out that there are a &lt;a href="http://www.ayende.com/Blog/archive/2007/10/20/Building-an-IoC-container-in-15-lines-of-code.aspx"&gt;couple&lt;/a&gt; of &lt;a href="http://www.kenegozi.com/Blog/2008/01/17/its-my-turn-to-build-an-ioc-container-in-15-minutes-and-33-lines.aspx"&gt;other&lt;/a&gt; simple containers you can find in other blogs.  This one is slightly different in design (and heavier), because I'm focusing on providing a lightweight container while still providing all of the plumbing necessary to extend it until it provides all of the same functionality as Unity and other larger containers.
&lt;p&gt;I'll start with the container interface.  As I said in my critic of Unity, I'd prefer separate interfaces for both the locator and the container.  However, I'm keeping things simple here and providing only a single interface.  I'm also following some of the naming conventions from Unity, though this is &lt;strong&gt;not&lt;/strong&gt; a clone of the Unity APIs.  When designing the interface for the container, I provided only the most verbose version of each function.  The other overloads are just convenience versions, and so I'm keeping them out of the interface.  That might sound odd right now, but you'll see in a moment that this is actually a fairly flexible design.  That said, here's the interface.&lt;pre&gt;&lt;span style="color:blue"&gt;public delegate object &lt;/span&gt;&lt;span style="color:#2b91af"&gt;ObjectFactoryCallback&lt;/span&gt;(&lt;span style="color:#2b91af"&gt;IObjectContainer &lt;/span&gt;container);

&lt;span style="color:blue"&gt;public interface &lt;/span&gt;&lt;span style="color:#2b91af"&gt;IObjectContainer
&lt;/span&gt;{
    &lt;span style="color:#2b91af"&gt;IObjectContainer &lt;/span&gt;RegisterType(&lt;span style="color:#2b91af"&gt;Type &lt;/span&gt;type, &lt;span style="color:blue"&gt;string &lt;/span&gt;name, &lt;span style="color:#2b91af"&gt;ILifetimePolicy &lt;/span&gt;liftetime, &lt;span style="color:#2b91af"&gt;ObjectFactoryCallback &lt;/span&gt;callback);
    &lt;span style="color:blue"&gt;object &lt;/span&gt;Resolve(&lt;span style="color:#2b91af"&gt;Type &lt;/span&gt;type, &lt;span style="color:blue"&gt;string &lt;/span&gt;name);
    &lt;span style="color:#2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color:blue"&gt;object&lt;/span&gt;&amp;gt; ResolveAll(&lt;span style="color:#2b91af"&gt;Type &lt;/span&gt;type);
}
&lt;/pre&gt;
&lt;p&gt;Oh, there's an ILifetimePolicy type in there that we've not defined yet.  Let's look at it.&lt;pre&gt;&lt;span style="color:blue"&gt;public interface &lt;/span&gt;&lt;span style="color:#2b91af"&gt;ILifetimePolicy
&lt;/span&gt;{
    &lt;span style="color:blue"&gt;object &lt;/span&gt;GetValue(&lt;span style="color:#2b91af"&gt;IObjectContainer &lt;/span&gt;conatiner, &lt;span style="color:#2b91af"&gt;ObjectFactoryCallback &lt;/span&gt;callback);
}
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;If you've seen Unity, most of what's here should be obvious.  This is a minimalist design, with all of the fluff removed.  Further, you'll notice that RegisterType is delegate based instead of reflection based by the lack of a second Type parameter and the addition of an ObjectFactoryCallback delegate parameter.  So, we have a very simple interface design, now let's provide an implementation.&lt;pre&gt;&lt;span style="color:blue"&gt;public sealed class &lt;/span&gt;&lt;span style="color:#2b91af"&gt;ObjectContainer &lt;/span&gt;: &lt;span style="color:#2b91af"&gt;IObjectContainer
&lt;/span&gt;{
    &lt;span style="color:blue"&gt;private class &lt;/span&gt;&lt;span style="color:#2b91af"&gt;Registration
    &lt;/span&gt;{
        &lt;span style="color:blue"&gt;public &lt;/span&gt;Registration(&lt;span style="color:#2b91af"&gt;ObjectFactoryCallback &lt;/span&gt;callback, &lt;span style="color:#2b91af"&gt;ILifetimePolicy &lt;/span&gt;lifetime)
        {
            Callback = callback;
            LifetimePolicy = lifetime;
        }

        &lt;span style="color:blue"&gt;public &lt;/span&gt;&lt;span style="color:#2b91af"&gt;ObjectFactoryCallback &lt;/span&gt;Callback { &lt;span style="color:blue"&gt;get&lt;/span&gt;; &lt;span style="color:blue"&gt;set&lt;/span&gt;; }
        &lt;span style="color:blue"&gt;public &lt;/span&gt;&lt;span style="color:#2b91af"&gt;ILifetimePolicy &lt;/span&gt;LifetimePolicy { &lt;span style="color:blue"&gt;get&lt;/span&gt;; &lt;span style="color:blue"&gt;set&lt;/span&gt;; }
    }

    &lt;span style="color:blue"&gt;private &lt;/span&gt;&lt;span style="color:#2b91af"&gt;ObjectContainer &lt;/span&gt;_parent;
    &lt;span style="color:blue"&gt;private &lt;/span&gt;&lt;span style="color:#2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af"&gt;Type&lt;/span&gt;, &lt;span style="color:#2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color:blue"&gt;string&lt;/span&gt;, &lt;span style="color:#2b91af"&gt;Registration&lt;/span&gt;&amp;gt;&amp;gt; _registry = &lt;span style="color:blue"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af"&gt;Type&lt;/span&gt;, &lt;span style="color:#2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color:blue"&gt;string&lt;/span&gt;, &lt;span style="color:#2b91af"&gt;Registration&lt;/span&gt;&amp;gt;&amp;gt;();

    &lt;span style="color:blue"&gt;public &lt;/span&gt;ObjectContainer()
    {
    }

    &lt;span style="color:blue"&gt;public &lt;/span&gt;&lt;span style="color:#2b91af"&gt;ObjectContainer &lt;/span&gt;CreateChildContainer()
    {
        &lt;span style="color:#2b91af"&gt;ObjectContainer &lt;/span&gt;child = &lt;span style="color:blue"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af"&gt;ObjectContainer&lt;/span&gt;();
        child._parent = &lt;span style="color:blue"&gt;this&lt;/span&gt;;
        &lt;span style="color:blue"&gt;return &lt;/span&gt;child;
    }

    &lt;span style="color:blue"&gt;#region &lt;/span&gt;IObjectContainer Members

    &lt;span style="color:blue"&gt;public &lt;/span&gt;&lt;span style="color:#2b91af"&gt;IObjectContainer &lt;/span&gt;RegisterType(&lt;span style="color:#2b91af"&gt;Type &lt;/span&gt;type, &lt;span style="color:blue"&gt;string &lt;/span&gt;name, &lt;span style="color:#2b91af"&gt;ILifetimePolicy &lt;/span&gt;liftetime, &lt;span style="color:#2b91af"&gt;ObjectFactoryCallback &lt;/span&gt;callback)
    {
        &lt;span style="color:#2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color:blue"&gt;string&lt;/span&gt;, &lt;span style="color:#2b91af"&gt;Registration&lt;/span&gt;&amp;gt; registrations;
        &lt;span style="color:blue"&gt;if &lt;/span&gt;(!_registry.TryGetValue(type, &lt;span style="color:blue"&gt;out &lt;/span&gt;registrations))
        {
            registrations = &lt;span style="color:blue"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color:blue"&gt;string&lt;/span&gt;, &lt;span style="color:#2b91af"&gt;Registration&lt;/span&gt;&amp;gt;();
            _registry[type] = registrations;
        }

        &lt;span style="color:#2b91af"&gt;Registration &lt;/span&gt;registration = &lt;span style="color:blue"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af"&gt;Registration&lt;/span&gt;(callback, liftetime);
        registrations[name ?? &lt;span style="color:blue"&gt;string&lt;/span&gt;.Empty] = registration;

        &lt;span style="color:blue"&gt;return this&lt;/span&gt;;
    }

    &lt;span style="color:blue"&gt;public object &lt;/span&gt;Resolve(&lt;span style="color:#2b91af"&gt;Type &lt;/span&gt;type, &lt;span style="color:blue"&gt;string &lt;/span&gt;name)
    {
        &lt;span style="color:blue"&gt;object &lt;/span&gt;result = TryResolve(type, name);
        &lt;span style="color:blue"&gt;if &lt;/span&gt;(result == &lt;span style="color:blue"&gt;null &lt;/span&gt;&amp;amp;&amp;amp; _parent != &lt;span style="color:blue"&gt;null&lt;/span&gt;)
            result = _parent.Resolve(type, name);
        &lt;span style="color:blue"&gt;return &lt;/span&gt;result;
    }

    &lt;span style="color:blue"&gt;public &lt;/span&gt;&lt;span style="color:#2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color:blue"&gt;object&lt;/span&gt;&amp;gt; ResolveAll(&lt;span style="color:#2b91af"&gt;Type &lt;/span&gt;type)
    {
        &lt;span style="color:#2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color:blue"&gt;string&lt;/span&gt;, &lt;span style="color:#2b91af"&gt;Registration&lt;/span&gt;&amp;gt; registrations;
        &lt;span style="color:blue"&gt;if &lt;/span&gt;(_registry.TryGetValue(type, &lt;span style="color:blue"&gt;out &lt;/span&gt;registrations))
        {
            &lt;span style="color:blue"&gt;foreach &lt;/span&gt;(&lt;span style="color:#2b91af"&gt;Registration &lt;/span&gt;registration &lt;span style="color:blue"&gt;in &lt;/span&gt;registrations.Values)
            {
                &lt;span style="color:blue"&gt;yield return &lt;/span&gt;Resolve(registration);
            }
        }
    }

    &lt;span style="color:blue"&gt;#endregion

    private object &lt;/span&gt;TryResolve(&lt;span style="color:#2b91af"&gt;Type &lt;/span&gt;type, &lt;span style="color:blue"&gt;string &lt;/span&gt;name)
    {
        &lt;span style="color:#2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color:blue"&gt;string&lt;/span&gt;, &lt;span style="color:#2b91af"&gt;Registration&lt;/span&gt;&amp;gt; registrations;
        &lt;span style="color:blue"&gt;if &lt;/span&gt;(!_registry.TryGetValue(type, &lt;span style="color:blue"&gt;out &lt;/span&gt;registrations))
            &lt;span style="color:blue"&gt;return null&lt;/span&gt;;

        &lt;span style="color:#2b91af"&gt;Registration &lt;/span&gt;registration;
        &lt;span style="color:blue"&gt;if &lt;/span&gt;(!registrations.TryGetValue(name ?? &lt;span style="color:blue"&gt;string&lt;/span&gt;.Empty, &lt;span style="color:blue"&gt;out &lt;/span&gt;registration))
            &lt;span style="color:blue"&gt;return null&lt;/span&gt;;

        &lt;span style="color:blue"&gt;return &lt;/span&gt;Resolve(registration);
    }

    &lt;span style="color:blue"&gt;private object &lt;/span&gt;Resolve(&lt;span style="color:#2b91af"&gt;Registration &lt;/span&gt;registration)
    {
        &lt;span style="color:blue"&gt;if &lt;/span&gt;(registration.LifetimePolicy != &lt;span style="color:blue"&gt;null&lt;/span&gt;)
            &lt;span style="color:blue"&gt;return &lt;/span&gt;registration.LifetimePolicy.GetValue(&lt;span style="color:blue"&gt;this&lt;/span&gt;, registration.Callback);
        &lt;span style="color:blue"&gt;return &lt;/span&gt;registration.Callback(&lt;span style="color:blue"&gt;this&lt;/span&gt;);
    }
}
&lt;/pre&gt;
&lt;p&gt;There you go, a complete IoC container that can be easily extended to have the full capabilities of Unity and other containers.
&lt;p&gt;OK, I won't leave you hanging.  Obviously when given just this, it seems like a very poor implementation.  Even for only a delegate based container, the convenience overloads are missing, which seems like it would make this awfully cumbersome to use.  And I'm sure several of you are confused about how you could possibly extend this container.  There's no built in extension mechanism, like there is in Unity, and the container is sealed, for Pete's sake.  Well, don't worry, I'll provide more.
&lt;p&gt;First, I'll explain how to extend this.  Instead of derivation or a fancy extension mechanism, I'm going to use &lt;a href="http://weblogs.asp.net/scottgu/archive/2007/03/13/new-orcas-language-feature-extension-methods.aspx"&gt;extension methods&lt;/a&gt;.  I created an ObjectContainerExtensions static class where I provided a host of extensions.  First, let's look at how you'd define some of the convenience overloads.&lt;pre&gt;&lt;span style="color:blue"&gt;public static &lt;/span&gt;&lt;span style="color:#2b91af"&gt;IObjectContainer &lt;/span&gt;RegisterType(&lt;span style="color:blue"&gt;this &lt;/span&gt;&lt;span style="color:#2b91af"&gt;IObjectContainer &lt;/span&gt;self, &lt;span style="color:#2b91af"&gt;Type &lt;/span&gt;type, &lt;span style="color:#2b91af"&gt;ObjectFactoryCallback &lt;/span&gt;callback)
{
    &lt;span style="color:blue"&gt;return &lt;/span&gt;self.RegisterType(type, &lt;span style="color:blue"&gt;null&lt;/span&gt;, &lt;span style="color:blue"&gt;null&lt;/span&gt;, callback);
}&lt;/pre&gt;&lt;pre&gt;&lt;span style="color:blue"&gt;public static &lt;/span&gt;&lt;span style="color:#2b91af"&gt;IObjectContainer &lt;/span&gt;RegisterType&amp;lt;T&amp;gt;(&lt;span style="color:blue"&gt;this &lt;/span&gt;&lt;span style="color:#2b91af"&gt;IObjectContainer &lt;/span&gt;self, &lt;span style="color:#2b91af"&gt;ObjectFactoryCallback &lt;/span&gt;callback)
{
    &lt;span style="color:blue"&gt;return &lt;/span&gt;self.RegisterType(&lt;span style="color:blue"&gt;typeof&lt;/span&gt;(T), &lt;span style="color:blue"&gt;null&lt;/span&gt;, &lt;span style="color:blue"&gt;null&lt;/span&gt;, callback);
}
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;Pretty simple, really.  From those two examples, you should be able to implement all of the the other convenience methods.  One very nice benefit here is that any IObjectContainer concrete type will get the added benefit of these methods with out having to implement anything.
&lt;p&gt;OK, now what about instance registration?  We'll write another extension method for that, with a few convenience overloads (I'll leave the overloads as an exercise for the reader).&lt;pre&gt;&lt;span style="color:blue"&gt;public static &lt;/span&gt;&lt;span style="color:#2b91af"&gt;IObjectContainer &lt;/span&gt;RegisterInstance(&lt;span style="color:blue"&gt;this &lt;/span&gt;&lt;span style="color:#2b91af"&gt;IObjectContainer &lt;/span&gt;self, &lt;span style="color:#2b91af"&gt;Type &lt;/span&gt;type, &lt;span style="color:blue"&gt;string &lt;/span&gt;name, &lt;span style="color:blue"&gt;object &lt;/span&gt;instance)
{
    &lt;span style="color:blue"&gt;return &lt;/span&gt;self.RegisterType(type, name, &lt;span style="color:blue"&gt;null&lt;/span&gt;, c =&amp;gt; instance);
}
&lt;/pre&gt;
&lt;p&gt;This starts to show off the power of the delegate based approach.  Our concrete container only knows how to register a delegate, yet we've trivially implemented an extension method that allows you to register an instance.
&lt;p&gt;Great, now what about adding reflection based registration?  How can I possibly do that with only a delegate based container?  Why, with some extension methods, of course.  I'll start with an InjectProperties method that's similar in usage to the BuildUp method in Unity.  It injects properties on an existing object by using reflection and looking for properties marked with an InjectAttribute (left as an exercise for the reader).&lt;pre&gt;&lt;span style="color:blue"&gt;public static object &lt;/span&gt;InjectProperties(&lt;span style="color:blue"&gt;this &lt;/span&gt;&lt;span style="color:#2b91af"&gt;IObjectContainer &lt;/span&gt;self, &lt;span style="color:blue"&gt;string &lt;/span&gt;name, &lt;span style="color:blue"&gt;object &lt;/span&gt;existing)
{
    &lt;span style="color:#2b91af"&gt;Type &lt;/span&gt;type = existing.GetType();
    &lt;span style="color:#2b91af"&gt;PropertyInfo&lt;/span&gt;[] properties = type.GetProperties();
    &lt;span style="color:blue"&gt;foreach &lt;/span&gt;(&lt;span style="color:#2b91af"&gt;PropertyInfo &lt;/span&gt;propertyInfo &lt;span style="color:blue"&gt;in &lt;/span&gt;properties)
    {
        &lt;span style="color:blue"&gt;object&lt;/span&gt;[] inject = propertyInfo.GetCustomAttributes(&lt;span style="color:blue"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af"&gt;InjectAttribute&lt;/span&gt;), &lt;span style="color:blue"&gt;true&lt;/span&gt;);
        &lt;span style="color:blue"&gt;if &lt;/span&gt;(inject != &lt;span style="color:blue"&gt;null &lt;/span&gt;&amp;amp;&amp;amp; inject.Length &amp;gt; 0)
        {
            &lt;span style="color:blue"&gt;object &lt;/span&gt;value = self.Resolve(propertyInfo.PropertyType, name);
            propertyInfo.SetValue(existing, value, &lt;span style="color:blue"&gt;null&lt;/span&gt;);
        }
    }
    &lt;span style="color:blue"&gt;return &lt;/span&gt;existing;
}
&lt;/pre&gt;
&lt;p&gt;Again, you can provide convenience overloads for this.
&lt;p&gt;Now we have the functionality we need to do property injection via reflection, but we still need a way to do constructor injection.  Obviously the Resolve method should do this, but we can't extend that beyond providing special delegates when we register a type with the container.  So, I'll add a couple of private static methods that we'll use as the implementation of our delegates later on.  I'll also provide a private static method that can be used to create an ObjectFactoryDelegate that constructs a Type via reflection.
&lt;p&gt;&lt;span style="color:blue"&gt;private static &lt;/span&gt;&lt;span style="color:#2b91af"&gt;ConstructorInfo &lt;/span&gt;FindConstructor(&lt;span style="color:#2b91af"&gt;Type&lt;/span&gt;type)&lt;br&gt;{&lt;br&gt;    &lt;span style="color:#2b91af"&gt;ConstructorInfo&lt;/span&gt;[] constructors = type.GetConstructors();&lt;br&gt;    &lt;span style="color:blue"&gt;if &lt;/span&gt;(constructors.Length == 0)&lt;br&gt;        &lt;span style="color:blue"&gt;throw new&lt;/span&gt;&lt;span style="color:#2b91af"&gt;InvalidOperationException&lt;/span&gt;();&lt;br&gt;&lt;br&gt;    &lt;span style="color:#2b91af"&gt;ConstructorInfo &lt;/span&gt;constructorInfo = constructors[0];&lt;br&gt;    &lt;span style="color:blue"&gt;if &lt;/span&gt;(constructors.Length &amp;gt; 1)&lt;br&gt;    {&lt;br&gt;        &lt;span style="color:blue"&gt;int &lt;/span&gt;argCount = -1;&lt;br&gt;        &lt;span style="color:blue"&gt;bool &lt;/span&gt;injectFound = &lt;span style="color:blue"&gt;false&lt;/span&gt;;&lt;br&gt;        &lt;span style="color:blue"&gt;foreach &lt;/span&gt;(&lt;span style="color:#2b91af"&gt;ConstructorInfo &lt;/span&gt;cur &lt;span style="color:blue"&gt;in&lt;/span&gt;constructors)&lt;br&gt;        {&lt;br&gt;            &lt;span style="color:blue"&gt;object&lt;/span&gt;[] inject = cur.GetCustomAttributes(&lt;span style="color:blue"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af"&gt;InjectAttribute&lt;/span&gt;), &lt;span style="color:blue"&gt;false&lt;/span&gt;);&lt;br&gt;            &lt;span style="color:blue"&gt;if &lt;/span&gt;(inject != &lt;span style="color:blue"&gt;nul l&lt;/span&gt;&amp;amp;&amp;amp; inject.Length &amp;gt; 0)&lt;br&gt;            {&lt;br&gt;                &lt;span style="color:blue"&gt;if &lt;/span&gt;(injectFound)&lt;br&gt;                    &lt;span style="color:blue"&gt;throw new&lt;/span&gt;&lt;span style="color:#2b91af"&gt;InvalidOperationException&lt;/span&gt;();&lt;br&gt;                constructorInfo = cur;&lt;br&gt;                injectFound = &lt;span style="color:blue"&gt;true&lt;/span&gt;;&lt;br&gt;            }&lt;br&gt;            &lt;span style="color:blue"&gt;if &lt;/span&gt;(!injectFound)&lt;br&gt;            {&lt;br&gt;                &lt;span style="color:#2b91af"&gt;ParameterInfo&lt;/span&gt;[] parameters = cur.GetParameters();&lt;br&gt;                &lt;span style="color:blue"&gt;if &lt;/span&gt;(parameters.Length &amp;gt; argCount)&lt;br&gt;                {&lt;br&gt;                    constructorInfo = cur;&lt;br&gt;                    argCount = parameters.Length;&lt;br&gt;                }&lt;br&gt;            }&lt;br&gt;        }&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    &lt;span style="color:blue"&gt;return &lt;/span&gt;constructorInfo;&lt;br&gt;}&lt;br&gt;&lt;br&gt;&lt;span style="color:blue"&gt;private static object &lt;/span&gt;Create(&lt;span style="color:#2b91af"&gt;IObjectContainer &lt;/span&gt;container, &lt;span style="color:#2b91af"&gt;ConstructorInfo &lt;/span&gt;constructorInfo)&lt;br&gt;{&lt;br&gt;    &lt;span style="color:#2b91af"&gt;ParameterInfo&lt;/span&gt;[] parameters = constructorInfo.GetParameters();&lt;br&gt;    &lt;span style="color:blue"&gt;if &lt;/span&gt;(parameters.Length == 0)&lt;br&gt;        &lt;span style="color:blue"&gt;return &lt;/span&gt;constructorInfo.Invoke(&lt;span style="color:blue"&gt;null&lt;/span&gt;);&lt;br&gt;&lt;br&gt;    &lt;span style="color:blue"&gt;object&lt;/span&gt;[] parameterValues = &lt;span style="color:blue"&gt;new object&lt;/span&gt;[parameters.Length];&lt;br&gt;    &lt;span style="color:blue"&gt;for &lt;/span&gt;(&lt;span style="color:blue"&gt;int &lt;/span&gt;i = 0; i &amp;lt; parameters.Length; ++i)&lt;br&gt;    {&lt;br&gt;        parameterValues[i] = container.Resolve(parameters[i].ParameterType);&lt;br&gt;    }&lt;br&gt;    &lt;span style="color:blue"&gt;return &lt;/span&gt;constructorInfo.Invoke(parameterValues);&lt;br&gt;}&lt;br&gt;&lt;br&gt;&lt;span style="color:blue"&gt;private static &lt;/span&gt;&lt;span style="color:#2b91af"&gt;ObjectFactoryCallback &lt;/span&gt;TypeFactoryAdapter(&lt;span style="color:#2b91af"&gt;Type&lt;/span&gt;type)&lt;br&gt;{&lt;br&gt;    &lt;span style="color:#2b91af"&gt;ConstructorInfo &lt;/span&gt;constructorInfo = FindConstructor(type);&lt;br&gt;    &lt;span style="color:blue"&gt;if &lt;/span&gt;(constructorInfo == &lt;span style="color:blue"&gt;null&lt;/span&gt;)&lt;br&gt;        &lt;span style="color:blue"&gt;throw new &lt;/span&gt;&lt;span style="color:#2b91af"&gt;InvalidOperationException&lt;/span&gt;();&lt;br&gt;&lt;br&gt;    &lt;span style="color:blue"&gt;return &lt;/span&gt;c =&amp;gt;&lt;br&gt;    {&lt;br&gt;        &lt;span style="color:blue"&gt;object &lt;/span&gt;result = Create(c, constructorInfo);&lt;br&gt;        &lt;span style="color:blue"&gt;return &lt;/span&gt;c.InjectProperties(result);&lt;br&gt;    };&lt;br&gt;}&lt;br&gt;
&lt;p&gt;Given this private implementation, we can now provide the RegisterType overloads for reflection based construction (again, most of the overloads are left for the reader to do).&lt;pre&gt;&lt;span style="color:blue"&gt;public static &lt;/span&gt;&lt;span style="color:#2b91af"&gt;IObjectContainer &lt;/span&gt;RegisterType(&lt;span style="color:blue"&gt;this &lt;/span&gt;&lt;span style="color:#2b91af"&gt;IObjectContainer &lt;/span&gt;self, &lt;span style="color:#2b91af"&gt;Type &lt;/span&gt;from, &lt;span style="color:#2b91af"&gt;Type &lt;/span&gt;to, &lt;span style="color:blue"&gt;string &lt;/span&gt;name, &lt;span style="color:#2b91af"&gt;ILifetimePolicy &lt;/span&gt;lifetime)
{
    &lt;span style="color:blue"&gt;return &lt;/span&gt;self.RegisterType(from, name, lifetime, TypeFactoryAdapter(to));
}
&lt;/pre&gt;
&lt;p&gt;There you go, a lightweight container that provides a good portion of the functionality found in Unity.  There's plenty of room for improvement.  The code provided is not production worthy yet, and there's still functionality missing.  For instance, Unity will construct a type through reflection even if the type isn't registered with the container.  All you do is call Resolve with a concrete class type, and the container will construct it and do appropriate injections.  With the current code, it's non-trivial to do this, because we've hard-coded the reflection into our ObjectContainerExtensions class.  To keep this lightweight, I'd move that functionality out into it's own extension class, and put it into a different namespace.  This allows anyone to use the functionality when implementing extensions, but keeps them out of the normal view for consumers.
&lt;p&gt;Oh, there's one final thing to illustrate.  You've seen the ILifetimePolicy interface, but how do you implement a policy?  Here's the requisite SingletonLifetimePolicy.&lt;pre&gt;&lt;span style="color:blue"&gt;public class &lt;/span&gt;&lt;span style="color:#2b91af"&gt;SingletonLifetimePolicy &lt;/span&gt;: &lt;span style="color:#2b91af"&gt;ILifetimePolicy
&lt;/span&gt;{
    &lt;span style="color:blue"&gt;private object &lt;/span&gt;lockObject = &lt;span style="color:blue"&gt;new object&lt;/span&gt;();
    &lt;span style="color:blue"&gt;private object &lt;/span&gt;value;

    &lt;span style="color:blue"&gt;#region &lt;/span&gt;ILifetimePolicy Members

    &lt;span style="color:blue"&gt;public object &lt;/span&gt;GetValue(&lt;span style="color:#2b91af"&gt;IObjectContainer &lt;/span&gt;container, &lt;span style="color:#2b91af"&gt;ObjectFactoryCallback &lt;/span&gt;callback)
    {
        &lt;span style="color:blue"&gt;if &lt;/span&gt;(value == &lt;span style="color:blue"&gt;null&lt;/span&gt;)
        {
            &lt;span style="color:blue"&gt;lock &lt;/span&gt;(lockObject)
            {
                &lt;span style="color:blue"&gt;if &lt;/span&gt;(value == &lt;span style="color:blue"&gt;null&lt;/span&gt;)
                {
                    value = callback(container);
                }
            }
        }
        &lt;span style="color:blue"&gt;return &lt;/span&gt;value;
    }

    &lt;span style="color:blue"&gt;#endregion
&lt;/span&gt;}
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;I considered other designs here.  Initially, I just provided delegate adapters instead of providing an ILifetimePolicy.  This works great, but I think the usage would be a little foreign to a lot of C# developers who've never done any functional programming.  I also considered designing the ILifetimePolicy interface so that it had an Adapt method instead of a GetValue method.  This makes the API familiar to the user, but the policy developer still has to understand functional programming.  I'm thinking that might be the more appropriate compromise, since it removes the concern about user's reusing a policy instance in multiple RegisterType calls, which as you can see from the implementation above would simply not work.  With a functional design, it's not too difficult to provide implementations that are safe from this.  I'm pretty sure I'll go with that design in my own library, but I kept the implementation here simple, in order to not detract from the lessons meant to be learned.&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=-3347231426831879729&amp;page=RSS%3a+Build+Your+Own+IoC+Container&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=wekempf.spaces.live.com&amp;amp;GT1=wekempf"&gt;</description><comments>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!415.entry#comment</comments><guid isPermaLink="true">http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!415.entry</guid><pubDate>Tue, 18 Mar 2008 01:43:16 GMT</pubDate><slash:comments>0</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://wekempf.spaces.live.com/blog/cns!D18C3EC06EA971CF!415/comments/feed.rss</wfw:commentRss><wfw:comment>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!415.entry#comment</wfw:comment><dcterms:modified>2008-03-18T01:43:16Z</dcterms:modified></item><item><title>Prism Drop 03-12-2008</title><link>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!413.entry</link><description>&lt;p&gt;In case you haven't heard, Prism has it's &lt;a href="http://www.codeplex.com/prism"&gt;first drop&lt;/a&gt; available.  Prism is the Pattern's &amp;amp; Practices next take on &amp;quot;composite applications&amp;quot;, which are a very specific form of &lt;a href="http://www.drwpf.com/Blog/Default.aspx?tabid=36&amp;amp;EntryID=27"&gt;M-V-Poo&lt;/a&gt;.  Obviously, I'm interested in M-V-Poo, especially as the patterns apply to WPF.  I've been tracking Prism for a while now, and I've commented on the spikes and other information leaked to us in the past, so some of you have some idea what to expect out of this post ;).  Well, I might surprise you a little, actually. &lt;p&gt;This drop of Prism, though not with out serious design problems from my point of view, actually is fairly promising.  One of the most important design criteria I had for a framework such as this was actually a &lt;a href="http://codebetter.com/blogs/glenn.block/archive/2008/03/12/prism-launches-on-codeplex.aspx"&gt;design goal&lt;/a&gt; of Prism: that it be lightweight (not an all-imposing framework, limited &amp;quot;Prism stuff&amp;quot; footprint in your applications and limited number of abstractions with a small API surface area).  Looking at the StockTrader reference application's source, I'd say they've done a fair job with it.  In my opinion, the code for this application is a bit of a spaghetti mess.  It took me a while to understand the flow and relationships/responsibilities of the various classes.  I simply would have structured the code differently.  However, the lightweight nature of the emerging framework (it's not yet a framework) looks like it would allow for using the concepts in an application structure more like I would prefer (specifics one what I'd prefer aren't important here). &lt;p&gt;Currently, the reference application is leaning very heavily on the &lt;a href="http://www.codeplex.com/unity"&gt;UnityContainer&lt;/a&gt; and Dependency Injection.  For those who have their own favorite DI container, don't fear, you're not locked into UnityContainer.  However, I still strongly believe that DI is overused, and there are certainly applications that would prefer to not have to deal with this concept at all.  So, I hope the emphasis on this pattern can be reduced.  At the very least, there should be no requirements for DI within the framework, even if the reference applications rely on it.  Since there's no framework yet, I can't say for sure wether or not there currently is such a dependency. &lt;p&gt;My one major complaint so far is that there's currently no solution to my one big complaint with WPF in general: event handling requires codebehind.  I'm a very firm believer that the declarative approach with strong data binding is the most important aspect of WPF and other competing technologies, such as XUL.  This enables very rapid UI development, and allows the UIs to be done by designers (using design tools) instead of developers (using code editors).  Not that I don't believe many people will fill both roles, mind you.  The one major failing with this in WPF from my point of view is that we don't have a useful equivalent to data binding for events.  In XAML, event handlers (and this includes command handlers) can only specify methods that exist in the codebehind.  There's a couple of problems with this.  First, the codebehind is very tightly coupled to the XAML (they are part of the same partial class!), which makes it awkward to maintain the separation between design and development.  In fact, it &lt;strong&gt;almost&lt;/strong&gt; requires the designer to be enough of a developer to be able to create the handlers and implement them to call into the business objects.  Otherwise, the developer and designer can wind up in a situation where they step on each other's toes.  This isn't the end of the world, as we're still in a &lt;strong&gt;much&lt;/strong&gt; better situation here than we've ever been in the past, but I want full separation!  Then there's the other problem with this, namely that a good portion of the time you want events to be handled by other objects and it's awkward even for one person to have to create little stub handlers in the codebehind just to bridge between the two. &lt;p&gt;The reference application punts here.  All the Views create stub event handlers in their codebehinds, which then raise specific events published from the View that the Presenters (or anyone else) can subscribe to.  Obviously this works, and is the easiest (and a very flexible) solution.  But the amount of tedious code required is depressing, even if you don't mind using codebehind, like I do.  This is the &lt;strong&gt;ONE &lt;/strong&gt;thing I absolutely demand from an M-V-Poo framework.  (Actually, I'd much prefer a solution in WPF proper for this, but as long as we don't have a solution there, any M-V-Poo framework I'd choose to use would need to supply a solution.)  There were a few concepts that were included in the spikes, that aren't used in the reference application, that seemed to be attempts to address this issue.  In particular, I'm referring to the CommandDispatcher stuff.  Unfortunately, I found that to be a very poor answer.  First, it wasn't general enough, only allowing you to handle Commands (and RoutedUICommands at that, due to a reliance on the Name property in a way that I don't think is reliable, since I'm not aware of any requirements for this property to contain a unique value).  I demand a solution that will work with any event. &lt;p&gt;Wouldn't it be ideal if WPF proper included an EventBinding markup extension with capabilities similar to the Binding markup extension, but which created event bindings instead of data bindings?&lt;pre&gt;&lt;span style="color:blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Button &lt;/span&gt;&lt;span style="color:red"&gt;Click&lt;/span&gt;&lt;span style="color:blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:blue"&gt;{EventBinding Source={StaticResource MyBusinessObject}, Handler=OnButtonClicked}&lt;/span&gt;&amp;quot;&lt;span style="color:blue"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;With this, M-V-Poo would be trivial to implement any way you decided you wanted to implement it.  Unfortunately, I don't believe it's possible to implement such a markup extension today, and thus it has to be implemented within WPF proper.  But oh how I wish we had this today!!!  If &lt;strong&gt;any&lt;/strong&gt; WPF framework developers at Microsoft read this, &lt;strong&gt;please&lt;/strong&gt; consider something like this and do anything you can to get it into &lt;a href="http://weblogs.asp.net/scottgu/archive/2008/02/19/net-3-5-client-product-roadmap.aspx"&gt;this summer's service release&lt;/a&gt;!  If you want to discuss the concept, please reply here.
&lt;p&gt;Any way, overall, even though Prism isn't the M-V-Poo I'd build, I think they're off to a better start than I originally gave them credit for.  It's very possible this will evolve into something that, even if it's not my M-V-Poo, it might allow me to implement my M-V-Poo.&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=-3347231426831879729&amp;page=RSS%3a+Prism+Drop+03-12-2008&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=wekempf.spaces.live.com&amp;amp;GT1=wekempf"&gt;</description><comments>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!413.entry#comment</comments><guid isPermaLink="true">http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!413.entry</guid><pubDate>Thu, 13 Mar 2008 15:55:06 GMT</pubDate><slash:comments>2</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://wekempf.spaces.live.com/blog/cns!D18C3EC06EA971CF!413/comments/feed.rss</wfw:commentRss><wfw:comment>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!413.entry#comment</wfw:comment><dcterms:modified>2008-03-13T15:55:06Z</dcterms:modified></item><item><title>Unity Project</title><link>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!399.entry</link><description>&lt;p&gt;Speaking about DI/IoC, Microsoft Patterns &amp;amp; Practices has just released an MSPL licensed DI container in the project &lt;a href="http://www.codeplex.com/unity"&gt;Unity&lt;/a&gt;.  I must say, I'm disappointed. &lt;p&gt;Here's a list of things I think Unity did wrong. &lt;ol&gt; &lt;li&gt;It's a P&amp;amp;P project, and not part of the BCL.  I'm not sure we need a full DI container in the BCL, but I do believe a lot of the interfaces should be there.  &lt;li&gt;They call this a &amp;quot;lightweight&amp;quot; container, but it contains two assemblies.  Doesn't sound too light weight to me.  I haven't fully looked into this yet, and it's possible one of the assemblies provides optional features layered on a basic container, in which case I'd have to retract what I just said in this point ;).  &lt;li&gt;The interfaces are designed incorrectly.  There's basically one interface, IUnityContainer, that's the driving interface.  Like mentioned, I'd prefer this in the BCL, with a more generic name, such as IObjectContainer.  Further, this should be split into two interfaces: the APIs to resolve instances should be separated from the APIs to configure a container, such as is done with IServiceProvider and IContainer in the BCL.  &lt;li&gt;Types are resolved through reflection.  At the basic level the container should be operating on &amp;quot;factory delegates&amp;quot; registered with the container.  This is the most flexible design, and also provides the best performance.  The reflective stuff can be built on top of this.  I'd layer it, so if you don't need it you don't have to include it, but I wouldn't argue to strongly if the container (and interfaces) provided both out of the box.  The fact that the factory delegate isn't present in the API at all, though, is a mistake.  Just as an example, this container doesn't provide support for per-thread scope, you only have Singleton (in two varieties... one where the user creates the instance and one where the container creates the instance) and per-call.  There's absolutely no way to change this, short of changing the container implementation.  If we had delegate factory registration, you wouldn't have this problem.  &lt;li&gt;There's still a lot of complexity here.  I want a lighter weight solution.&lt;/ol&gt; &lt;p&gt;Over all, it's not a bad library, but it doesn't deliver what &lt;strong&gt;I&lt;/strong&gt; believe we need.&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=-3347231426831879729&amp;page=RSS%3a+Unity+Project&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=wekempf.spaces.live.com&amp;amp;GT1=wekempf"&gt;</description><comments>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!399.entry#comment</comments><guid isPermaLink="true">http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!399.entry</guid><pubDate>Wed, 13 Feb 2008 16:14:13 GMT</pubDate><slash:comments>6</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://wekempf.spaces.live.com/blog/cns!D18C3EC06EA971CF!399/comments/feed.rss</wfw:commentRss><wfw:comment>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!399.entry#comment</wfw:comment><dcterms:modified>2008-02-13T16:14:13Z</dcterms:modified></item><item><title>Using ServiceContainer</title><link>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!397.entry</link><description>&lt;p&gt;OK, in my last post I promised to show you how to use the ServiceContainer.  I'm going to steal code from the &lt;a href="http://code.google.com/p/autofac/"&gt;Autofac&lt;/a&gt; home page, and rewrite it using ServiceContainer.&lt;pre&gt;IServiceContainer container = new ServiceContainer();

container.AddService(typeof(IEngine),
   (c, t) =&amp;gt; new Straight6TwinTurbo());

container.AddService(typeof(ICar),
   (c, t) =&amp;gt; new Skyline(c.GetService(typeof(IEngine)), Color.Black));

ICar car = container.GetService(typeof(ICar));
car.DriveTo(&amp;quot;Byron Bay&amp;quot;);
&lt;/pre&gt;
&lt;p&gt;Pretty darn similar.  Autofac uses a ContainerBuilder and a declarative coding style, but other than this they are very equivalent.  However, most containers allow you to specify the &amp;quot;scope&amp;quot;, while the BCL ServiceContainer only provides Singleton scope, even when you supply a ServiceCreatorCallback, which is unfortunate.  So, although I'll be using IServiceProvider, I won't be able to use ServiceContainer, and may consider extending both IServiceProvider and IServiceContainer to account for generics and keyed access.&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=-3347231426831879729&amp;page=RSS%3a+Using+ServiceContainer&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=wekempf.spaces.live.com&amp;amp;GT1=wekempf"&gt;</description><comments>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!397.entry#comment</comments><guid isPermaLink="true">http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!397.entry</guid><pubDate>Wed, 06 Feb 2008 19:14:38 GMT</pubDate><slash:comments>1</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://wekempf.spaces.live.com/blog/cns!D18C3EC06EA971CF!397/comments/feed.rss</wfw:commentRss><wfw:comment>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!397.entry#comment</wfw:comment><dcterms:modified>2008-02-06T19:14:38Z</dcterms:modified></item><item><title>IoC Container in the BCL</title><link>http://wekempf.spaces.live.com/Blog/cns!D18C3EC06EA971CF!396.entry</link><description>&lt;p&gt;I recently wrote a &lt;a href="http://wekempf.spaces.live.com/blog/cns!D18C3EC06EA971CF!393.entry"&gt;rant&lt;/a&gt; on here about Dependency Injection and Inversion of Control containers.  This might leave you to believe I'm totally against IoC.  In fact, I'm not.  There is a time and a place for this pattern, I just feel that it's overused, especially in designs where the only purpose it's serving is to allow for unit testing. &lt;p&gt;However, right now I've got a use for it.  The details about why aren't that relevant, so I'm not going to discuss it here.  However, my needs are isolated and still want to take a pragmatic approach to the usage.  I don't expect to need runtime configuration, though I don't want to strictly rule it out, either.  So, I've been researching the available IoC containers for the CLR.  Most of them seem to be sledgehammer solutions, when I need a claw hammer.  The one that seemed to come the closest to what I'd want is &lt;a href="http://code.google.com/p/autofac/"&gt;Autofac&lt;/a&gt;, but there's a lot of features there that I don't necessarily need either.  What I'd really like is to code to an extremely light weight interface, and provide a really simple default implementation, allowing for the ability to use any of the other containers if and/or when my needs grow. &lt;p&gt;Did you know the BCL contains such an interface?  More importantly, it provides a very simple container as well.  If you've done much in WPF, you've actually probably used the interface already.  What am I talking about? &lt;p&gt;IServiceProvider. &lt;p&gt;This simple interface provides a single method: object GetService(Type serviceType).  That's it.  And that's all you need to get services from an IoC container.  Oh, I also mentioned that the BCL provides a container as well.  The