<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>Code</title>
        <link>http://blog.peterlesliemorris.com/category/2.aspx</link>
        <description>Code</description>
        <language>en-GB</language>
        <copyright>Peter Morris</copyright>
        <generator>Subtext Version 2.1.1.1</generator>
        <item>
            <title>Delphi Audio Compression components updated to Delphi 2009</title>
            <link>http://blog.peterlesliemorris.com/archive/2009/09/01/delphi-audio-compression-components-updated-to-delphi-2009.aspx</link>
            <description>Diego has upgraded my deACM source to work with Delphi 2009.&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://unitonedev.blogspot.com/2009/08/deaudio-convertido-para-delphi-2009.html"&gt;http://unitonedev.blogspot.com/2009/08/deaudio-convertido-para-delphi-2009.html&lt;/a&gt;&lt;img src="http://blog.peterlesliemorris.com/aggbug/16.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Peter Morris</dc:creator>
            <guid>http://blog.peterlesliemorris.com/archive/2009/09/01/delphi-audio-compression-components-updated-to-delphi-2009.aspx</guid>
            <pubDate>Tue, 01 Sep 2009 12:04:51 GMT</pubDate>
            <wfw:comment>http://blog.peterlesliemorris.com/comments/16.aspx</wfw:comment>
            <comments>http://blog.peterlesliemorris.com/archive/2009/09/01/delphi-audio-compression-components-updated-to-delphi-2009.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://blog.peterlesliemorris.com/comments/commentRss/16.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Partial declarations of must not specify different base classes</title>
            <link>http://blog.peterlesliemorris.com/archive/2009/07/23/partial-declarations-of-must-not-specify-different-base-classes.aspx</link>
            <description>&lt;p&gt;I wanted to use a common base class for a set of UserControls in my WPF application, but when I changed the class’s ancestor in the code file and compiled I would get the error&lt;/p&gt;  &lt;p&gt;“Partial declarations of must not specify different base classes”&lt;/p&gt;  &lt;p&gt;This is because when you compile a WPF application Visual Studio generates a partial class in a code-behind file automatically, the base type specified is always “UserControl”.  To solve this problem change your XAML from this&lt;/p&gt;  &lt;div class="csharpcode"&gt;   &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;UserControl&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;  &lt;span class="attr"&gt;x:Class&lt;/span&gt;&lt;span class="kwrd"&gt;="MyApp.MyControl"&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;To this&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;local:SomeBaseTypeYouWantToUse&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;  &lt;span class="attr"&gt;x:Class&lt;/span&gt;&lt;span class="kwrd"&gt;="MyApp.MyControl"&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;  &lt;span class="attr"&gt;xmlns:local&lt;/span&gt;&lt;span class="kwrd"&gt;="clr-namespace:NameSpace.To.Your.BaseClass"&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;&lt;img src="http://blog.peterlesliemorris.com/aggbug/10.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Peter Morris</dc:creator>
            <guid>http://blog.peterlesliemorris.com/archive/2009/07/23/partial-declarations-of-must-not-specify-different-base-classes.aspx</guid>
            <pubDate>Thu, 23 Jul 2009 11:08:31 GMT</pubDate>
            <wfw:comment>http://blog.peterlesliemorris.com/comments/10.aspx</wfw:comment>
            <comments>http://blog.peterlesliemorris.com/archive/2009/07/23/partial-declarations-of-must-not-specify-different-base-classes.aspx#feedback</comments>
            <slash:comments>7</slash:comments>
            <wfw:commentRss>http://blog.peterlesliemorris.com/comments/commentRss/10.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Models, views, controllers</title>
            <link>http://blog.peterlesliemorris.com/archive/2009/07/17/models-views-controllers.aspx</link>
            <description>&lt;p&gt;I’m working on a new app which will have WPF as its user interface.  With the slight nag in the back of my mind that someone might say “No, we want WinForms” (I hope not) I decided there should be as little code in the UI as possible so I opted for a view/controller approach where my controller will perform all of the logic and the view will only do “viewy” things.&lt;/p&gt;  &lt;p&gt;One of the features of this app is that some of the views should appear as modal windows (such as a splash window during start-up) but others should appear as tabs in a client area so that the user can switch between different tasks.  So I created a UserControl which data bound to a list of IView, there were two problems with this approach.&lt;/p&gt;  &lt;p&gt;Firstly I wanted my IView implementers to be UserControls so that when I data bind to the List&amp;lt;IView&amp;gt; the content of the TabItem would show the correct control.  The problem was that I needed the header of the TabItem to be data bound too…&lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;div class="csharpcode"&gt;   &lt;div class="csharpcode"&gt;     &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;TabControl.ItemTemplate&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

    &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;DataTemplate&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;TextBlock&lt;/span&gt; &lt;span class="attr"&gt;Text&lt;/span&gt;&lt;span class="kwrd"&gt;="{Binding ViewTitle}"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

    &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;DataTemplate&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;TabControl.ItemTemplate&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;So all I need is a string property on IView named ViewTitle and all would be well.  Or so you would think!  Data binding a TabControl to a list of objects which happen to be UserControl descendants doesn’t work very well, as a result the ItemTemplate was unable to read my property.  Even if I converted it to a DependencyProperty it still didn’t work.  It seems it is a problem with the way the WPF TabControl was written.&lt;/p&gt;

&lt;p&gt;Secondly there was a problem when it came to wizard like views.  When one of my tabs needs multiple steps how can I change the view?  I can’t!&lt;/p&gt;

&lt;p&gt;The solution was instead to data bind the TabControl to a list of controllers instead.  Seeing as I already have IController for controllers which have modal views I went for List&amp;lt;IEmbeddedController&amp;gt; instead.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; IController : INotifyPropertyChanged&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    IView CurrentView { get; }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;}&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; IEmbeddedController : IController&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;    &lt;span class="kwrd"&gt;string&lt;/span&gt; ViewTitle { get; }&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;Now I can bind my TabItem.ItemTemplate to ViewTitle with no problem because it is not binding to a UserControl but a plain object instead.  In addition the IEmbeddedController can set its CurrentView property at any point in order to show a different presentation to the user.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;TabControl.ContentTemplate&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;DataTemplate&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ContentPresenter&lt;/span&gt; &lt;span class="attr"&gt;Content&lt;/span&gt;&lt;span class="kwrd"&gt;="{Binding CurrentView}"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;DataTemplate&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;TabControl.ContentTemplate&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;Now my tabbed views function correctly.  I see a title on the tab item and the contents of the view.  The next thing to address is how does the controller know which view to set CurrentView to?  I do that using Unity dependency injection.  So, for example, if I were to create a controller named “WizardTest” with two steps I would have the following items…&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;IStep1View &lt;/li&gt;

  &lt;li&gt;Step1Control (implements IStep1View) &lt;/li&gt;

  &lt;li&gt;IStep2View &lt;/li&gt;

  &lt;li&gt;Step2Control (implements IStep2View) &lt;/li&gt;

  &lt;li&gt;IWizardTestController &lt;/li&gt;

  &lt;li&gt;WizardTestController (implements IWizardTestController) &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;In the start-up phase of my application I would register which classes implement those interfaces…&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; RegisterWizardTestViewAndController(IUnityContainer container)&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    container.RegisterType&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;        &amp;lt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;            MyApp.Views.WizardTest.IWizardTestController,&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;            MyApp.Views.WizardTest.WizardTestController&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;        &amp;gt;(&lt;span class="kwrd"&gt;new&lt;/span&gt; TransientLifetimeManager());&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;    container.RegisterType&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;        &amp;lt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;            MyApp.Views.WizardTest.Steps.IStep1View,&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;            MyApp.Views.WizardTest.Steps.Step1&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;        &amp;gt;(&lt;span class="kwrd"&gt;new&lt;/span&gt; TransientLifetimeManager());&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;    container.RegisterType&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;        &amp;lt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;            MyApp.Views.WizardTest.Steps.IStep2View,&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;            MyApp.Views.WizardTest.Steps.Step2&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;        &amp;gt;(&lt;span class="kwrd"&gt;new&lt;/span&gt; TransientLifetimeManager());&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;When I need to show one of the states I write code something like this…&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;IStep1View Step1View;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SetStep1View()&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;if&lt;/span&gt; (Step1View == &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    {&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;        Step1View = ViewFactory.CreateView&amp;lt;IStep1View&amp;gt;();&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;        Step1View.Initialize(SomeModelObjectToEdit);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;    }&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;    CurrentView = Step1View;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;

&lt;p /&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;I can update the view on the screen simply by changing the CurrentView property.  Let’s say that the Step1Control needs a NEXT button.  This is easily achieved like so…&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; IStep1View : IView&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    ICommand NextCommand { get; set; }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;

&lt;p /&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;ICommand is an interface that WPF uses to bind UI actions to, for example in Step1Control I implement the property required by IStep1View and in the XAML I add a button like this…&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Button&lt;/span&gt; &lt;span class="attr"&gt;Content&lt;/span&gt;&lt;span class="kwrd"&gt;="Go to step 2"&lt;/span&gt; &lt;span class="attr"&gt;Command&lt;/span&gt;&lt;span class="kwrd"&gt;="{Binding CurrentView.NextCommand}"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;p&gt; &lt;/p&gt;

  &lt;p&gt;CurrentView because (remember) the control is bound to the IEmbeddedController so we need the NextCommand of the CurrentView.  When the user clicks this button it will execute the ICommand.Execute.  This means that I don’t need a Button.Click event in the XAML, I don’t need to handle the event in the code-behind class, and I don’t need some kind of NextButtonClicked event on IStep1View - although I do instead need a NextCommand property instead, but all in all there’s less work…&lt;/p&gt;

  &lt;p&gt; &lt;/p&gt;

  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;IStep2View Step1View;&lt;/pre&gt;

    &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SetStep1View()&lt;/pre&gt;

    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;{&lt;/pre&gt;

    &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;if&lt;/span&gt; (Step1View == &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;/pre&gt;

    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    {&lt;/pre&gt;

    &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;        Step1View = ViewFactory.CreateView&amp;lt;IStep1View&amp;gt;();&lt;/pre&gt;

    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;        Step1View.Initialize(SomeModelObjectToEdit);&lt;/pre&gt;

    &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;        Step1View.NextCommand = &lt;span class="kwrd"&gt;new&lt;/span&gt; ActionCommand(p =&amp;gt; State = States.Step2);&lt;/pre&gt;

    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;    }&lt;/pre&gt;

    &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;    CurrentView = Step1View;&lt;/pre&gt;

    &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;}&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;ViewFactory is a very simple class which asks Unity to Resolve&amp;lt;&amp;gt; the type passed.  This not only gives me simple construction but also injects any dependencies.  ActionCommand is also a simple class which uses a lambda expression as its action to execute:&lt;/p&gt;

&lt;div class="csharpcode"&gt; &lt;/div&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; ActionCommand : ICommand&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;readonly&lt;/span&gt; Action&amp;lt;&lt;span class="kwrd"&gt;object&lt;/span&gt;&amp;gt; Action;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;event&lt;/span&gt; EventHandler CanExecuteChanged;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; ActionCommand(Action&amp;lt;&lt;span class="kwrd"&gt;object&lt;/span&gt;&amp;gt; action)&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    {&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;        Action = action;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;        Enabled = &lt;span class="kwrd"&gt;true&lt;/span&gt;;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;    }&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;    &lt;span class="kwrd"&gt;bool&lt;/span&gt; enabled;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; Enabled&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;    {&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;        get { &lt;span class="kwrd"&gt;return&lt;/span&gt; enabled; }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;        set&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;        {&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;            enabled = &lt;span class="kwrd"&gt;value&lt;/span&gt;;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;            var canExecuteChanged = CanExecuteChanged;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (canExecuteChanged != &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;                canExecuteChanged(&lt;span class="kwrd"&gt;this&lt;/span&gt;, EventArgs.Empty);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;        }&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;    }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  24:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  25:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; CanExecute(&lt;span class="kwrd"&gt;object&lt;/span&gt; parameter)&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt;    {&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  27:  &lt;/span&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; Enabled;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  28:  &lt;/span&gt;    }&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  29:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  30:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Execute(&lt;span class="kwrd"&gt;object&lt;/span&gt; parameter)&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  31:  &lt;/span&gt;    {&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  32:  &lt;/span&gt;        Action(parameter);&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  33:  &lt;/span&gt;    }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  34:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;Clicking the button in the UserControl executes the NextCommand.Execute method, this is a lambda which sets State = States.Step2.  The property “State” has a switch statement and calls SetStep1View, SetStep2View etc depending on the state that has been set.&lt;/p&gt;

&lt;p&gt;Finally the management of the tabbed views, and how to show new views from other controllers.  I did this with a service I called “IEmbeddedControllerPresenterController” because it is a controller which presents Embedded Controllers.  &lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; IEmbeddedControllerPresenterController&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;void&lt;/span&gt; AddController(IEmbeddedController controller);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;void&lt;/span&gt; RemoveController(IEmbeddedController controller);&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    &lt;span class="kwrd"&gt;void&lt;/span&gt; FocusController(IEmbeddedController controller);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    UserControl UserControl { get; }&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;I’m going to change the “UserControl UserControl” property to “IView View'” very soon.  But the idea is that this is registered in Unity with a ContainerControlledLifetimeManager (effectively a singleton).  I can add and remove controllers from the list which my TabControl is bound to, and also specify which one should be focused.  &lt;/p&gt;

&lt;p&gt;Now when it comes to creating new views I use an IEmbeddedControllerRepository.  It’s a repository because I don’t always create the embedded controller.  When asking for a controller I pass a unique “InstanceID” which could be something simple like “NewCustomer” / “NewSupplier”, or when editing an instance of a user it could be the primary key of that user instance.  This is to ensure that when you want a controller to edit “User A” and the interface already has a view editing that object the existing view will focus, if there is no view present for this object a new one will be created by calling a factory (simply delegates to Unity).&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; IEmbeddedControllerRepository&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    TController GetController&amp;lt;TController&amp;gt;(&lt;span class="kwrd"&gt;string&lt;/span&gt; instanceID) &lt;span class="kwrd"&gt;where&lt;/span&gt; TController : IEmbeddedController;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;void&lt;/span&gt; ReleaseController(&lt;span class="kwrd"&gt;string&lt;/span&gt; instanceID);&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;Two examples of using this might be…&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;var repository = EmbeddedUserRepository;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;&lt;span class="rem"&gt;//Example 1 - only create 1 new user at a time&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;repository.GetController&amp;lt;INewUserController&amp;gt;(&lt;span class="str"&gt;"NewUser"&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;&lt;span class="rem"&gt;//Example 2 - edit any number of users, but never the same one twice&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;User user = .........;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;var controller = repository.GetController&amp;lt;IEditUserController&amp;gt;(user.UniqueID);&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;controller.Initialize(user);&lt;/pre&gt;
&lt;/div&gt;

&lt;p /&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;All of this is really just a slight variation of an approach I took for a wizard driven application I once wrote for the Pocket PC.  The only difference is that the views are tabbed (whereas the PPC they were stacked and only 1 was visible at a time), and in this I am using Unity so that I can modularise the code a bit and potentially get some mocked objects in there via the interface definitions so that I can unit test more easily.&lt;/p&gt;

&lt;p&gt;I’ve read (&lt;a href="http://tech.groups.yahoo.com/group/domaindrivendesign/message/13738"&gt;http://tech.groups.yahoo.com/group/domaindrivendesign/message/13738&lt;/a&gt;) that you should only have one controller per application.  I simply don’t think this is true.  While I do believe it is sometimes necessary to have a single controller responsible for multiple views I don’t think there are many apps which require only a single controller.  E.g. if you are modelling a 3D scene and want to see a live preview of the scene in one window and sliders etc to control the camera angle you would use one controller, but if you are editing a customer, a supplier, and an employee you will need  three different types of controller, and one instance of a controller per object you are editing in isolation. &lt;/p&gt;&lt;img src="http://blog.peterlesliemorris.com/aggbug/9.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Peter Morris</dc:creator>
            <guid>http://blog.peterlesliemorris.com/archive/2009/07/17/models-views-controllers.aspx</guid>
            <pubDate>Fri, 17 Jul 2009 15:38:52 GMT</pubDate>
            <wfw:comment>http://blog.peterlesliemorris.com/comments/9.aspx</wfw:comment>
            <comments>http://blog.peterlesliemorris.com/archive/2009/07/17/models-views-controllers.aspx#feedback</comments>
            <wfw:commentRss>http://blog.peterlesliemorris.com/comments/commentRss/9.aspx</wfw:commentRss>
        </item>
        <item>
            <title>TaskSmart</title>
            <link>http://blog.peterlesliemorris.com/archive/2009/06/27/tasksmart.aspx</link>
            <description>&lt;p&gt;Carlo Kok from &lt;a href="http://www.RemObjects.com" target="_blank"&gt;RemObjects&lt;/a&gt; suggested a book to me “&lt;a href="http://www.amazon.co.uk/Getting-Things-Done-Stress-free-Productivity/dp/0749922648/ref=sr_1_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1246106393&amp;amp;sr=8-1" target="_blank"&gt;Getting things done&lt;/a&gt;” a while back (I’d recommend it too) – since then I have been using &lt;a href="http://getontracks.org/" target="_blank"&gt;Get On Tracks&lt;/a&gt;, which is a web based app based on the suggestions in the book.  It’s a pretty good app but I find it is very slow, painfully slow.  So, due to the relentless part of my personality which compels me to constantly learn new things I have started an open source project on sourceforge.net – &lt;a href="http://sourceforge.net/projects/tasksmart/" target="_blank"&gt;TaskSmart&lt;/a&gt;.  This will be a web app I can use as a replacement for Get On Tracks, so I will get a faster app (hopefully) and also get to play with some new things along the way.&lt;/p&gt;  &lt;p&gt;The ingredients for this application will be&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://www.asp.net/" target="_blank"&gt;ASP MVC&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;AJAX – Not used it before, possibly using jQuery&lt;/li&gt;    &lt;li&gt;Domain Driven Design – Only a few items I liked from the book&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.codeplex.com/unity" target="_blank"&gt;UNITY&lt;/a&gt; – Dependency injection container&lt;/li&gt;    &lt;li&gt;&lt;a href="https://www.hibernate.org/343.html" target="_blank"&gt;NHibernate&lt;/a&gt; – Not used this before either&lt;/li&gt;    &lt;li&gt;&lt;a href="http://ayende.com/projects/rhino-mocks.aspx" target="_blank"&gt;Rhino Mocks&lt;/a&gt; – Used this quite a lot&lt;/li&gt;    &lt;li&gt;Test Driven Design – A must have!&lt;/li&gt;    &lt;li&gt;&lt;a href="http://automapper.codeplex.com/" target="_blank"&gt;AutoMapper&lt;/a&gt; – To map domains to view model&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;a href="http://peterlesliemorris.com/BlogFiles/TaskSmart_C0BE/Architecture.png"&gt;&lt;img title="Architecture" style="border-right: 0px; border-top: 0px; display: block; float: none; margin-left: auto; border-left: 0px; margin-right: auto; border-bottom: 0px" height="318" alt="Architecture" src="http://peterlesliemorris.com/BlogFiles/TaskSmart_C0BE/Architecture_thumb.png" width="136" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The web interface will be handled by ASP MVC controllers.&lt;/li&gt;    &lt;li&gt;These controllers will present a View Model to the interface.&lt;/li&gt;    &lt;li&gt;The controller will talk to the Application Layer using View Model instances as parameters.&lt;/li&gt;    &lt;li&gt;I think I will use AutoMapper to help the Application Layer to translate between Domain Classes and View Model.&lt;/li&gt;    &lt;li&gt;The Application Layer will act as a facade to the Domain classes.&lt;/li&gt;    &lt;li&gt;The Application Layer will manage all persistence using NHibernate.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;In addition all dependencies will be injected using UNITY from Microsoft, this will make testing easier and also make it easier to compartmentalise the source code into smaller chunks which are easier to understand.  &lt;/p&gt;  &lt;p&gt;I have decided to manage constraints as individual objects, for example&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;StringIsNotNullConstraint&lt;/li&gt;    &lt;li&gt;StringIsNotTooLongConstraint&lt;/li&gt;    &lt;li&gt;StringIsNotTooShortConstraint&lt;/li&gt;    &lt;li&gt;StringIsValidInternetEmailAddressConstraint&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Previously I would have used object constraint language expressions and return a list of broken constraints as a collection of strings.  This would mean that when I validate user input I would either have to manually reproduce the business constraints in the user interface (which is a pain when they need to change) or not validate on the client at all and leave it all to the server (which results in a wasted server request when we know for a fact that the user input will be rejected).  So my idea here is that if my constraints are objects then I will be able to determine two things about the constraints.&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;The way in which the constraint logically checks a value, such as “This string must be at least X characters in length”.&lt;/li&gt;    &lt;li&gt;The relevant properties of the constraint, such as exactly what the minimum number of characters is.&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;What I am hoping to do with this additional information is then to&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Define the constraints on my domain classes.&lt;/li&gt;    &lt;li&gt;AutoMap the relevant constraints to the View Model.&lt;/li&gt;    &lt;li&gt;Automatically convert these constraints into JavaScript.&lt;/li&gt;    &lt;li&gt;Have the client validate input in the web browser before it is submitted to the server for final validation.&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;This should give me the advantage of not having to waste server resources on a certainly invalid request, and also save me from having to define constraints in more than one place; updating on the domain class will automatically update the validation on the page.&lt;/p&gt;  &lt;p&gt;It’s all a pretty tall order, but it should be interesting to say the least!&lt;/p&gt;&lt;img src="http://blog.peterlesliemorris.com/aggbug/7.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Peter Morris</dc:creator>
            <guid>http://blog.peterlesliemorris.com/archive/2009/06/27/tasksmart.aspx</guid>
            <pubDate>Sat, 27 Jun 2009 16:56:17 GMT</pubDate>
            <wfw:comment>http://blog.peterlesliemorris.com/comments/7.aspx</wfw:comment>
            <comments>http://blog.peterlesliemorris.com/archive/2009/06/27/tasksmart.aspx#feedback</comments>
            <wfw:commentRss>http://blog.peterlesliemorris.com/comments/commentRss/7.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>