<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>A day in the life...</title>
        <link>http://blog.peterlesliemorris.com/Default.aspx</link>
        <description>public class GeekEarth : Earth { }</description>
        <language>en-GB</language>
        <copyright>Peter Morris</copyright>
        <generator>Subtext Version 2.1.1.1</generator>
        <image>
            <title>A day in the life...</title>
            <url>http://blog.peterlesliemorris.com/images/RSS2Image.gif</url>
            <link>http://blog.peterlesliemorris.com/Default.aspx</link>
            <width>77</width>
            <height>60</height>
        </image>
        <item>
            <title>Embedding IronPython into a C# application</title>
            <link>http://blog.peterlesliemorris.com/archive/2010/05/19/embedding-ironpython-into-a-c-application.aspx</link>
            <description>&lt;p&gt;It’s been a while since I blogged, I’ve been busy :)&lt;/p&gt;  &lt;p&gt;A requirement I have in an app I am writing is to allow the administrator to set up formulas which can be calculated at runtime. The client app is completely disconnected from &lt;a title="" href="http://www.capableobjects.com" rel="" target="_blank"&gt;Enterprise Core Objects&lt;/a&gt;, it uses a (query/command)-&amp;gt;response approach so I didn’t want to expose OCL on the client, I also didn’t want to use OCL because I didn’t want arbitrary browsing of the model.&lt;/p&gt;  &lt;p&gt;So I have been looking at &lt;a href="http://ironpython.codeplex.com/" target="_blank"&gt;IronPython&lt;/a&gt; – and I like it! I’m not going to go into details of what this code does, I’m just going to paste it so that you may take a look. In short the requirement is for the Admin to be able to retrieve values from the DB by name and perform whatever logic they wish in order to return a decimal result.&lt;/p&gt;  &lt;p&gt;You need to reference the following DLLs.&lt;/p&gt;  &lt;p&gt;Microsoft.Scripting   &lt;br /&gt;Microsoft.Scripting.Core    &lt;br /&gt;IronPython&lt;/p&gt;  &lt;p&gt;And here is the source example&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:f93fea9a-8a25-4e68-80d3-90b67d46a2f0" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow: auto;"&gt;&lt;span style="color: #0000FF;"&gt;using&lt;/span&gt;&lt;span style="color: #000000;"&gt; System;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;using&lt;/span&gt;&lt;span style="color: #000000;"&gt; System.Collections;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;using&lt;/span&gt;&lt;span style="color: #000000;"&gt; System.Collections.Generic;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;using&lt;/span&gt;&lt;span style="color: #000000;"&gt; System.IO;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;using&lt;/span&gt;&lt;span style="color: #000000;"&gt; IronPython.Hosting;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;using&lt;/span&gt;&lt;span style="color: #000000;"&gt; IronPython.Runtime;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;using&lt;/span&gt;&lt;span style="color: #000000;"&gt; IronPython;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;using&lt;/span&gt;&lt;span style="color: #000000;"&gt; Microsoft.Scripting.Hosting;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;using&lt;/span&gt;&lt;span style="color: #000000;"&gt; Microsoft.Scripting;

&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;namespace&lt;/span&gt;&lt;span style="color: #000000;"&gt; ConsoleApplication17
{
  &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;class&lt;/span&gt;&lt;span style="color: #000000;"&gt; ConsoleApplication17
  {
    [STAThread]
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;static&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;void&lt;/span&gt;&lt;span style="color: #000000;"&gt; Main(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;string&lt;/span&gt;&lt;span style="color: #000000;"&gt;[] rawArgs)
    {
      var engine &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; Python.CreateEngine();
      var scriptSource &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; engine.CreateScriptSourceFromString(&lt;/span&gt;&lt;span style="color: #800000;"&gt;@"&lt;/span&gt;&lt;span style="color: #800000;"&gt;
def foo(paygroup):
  global retrievedValue
  global factor
  retrievedValue = numbers.GetValue(paygroup)
  if retrievedValue &amp;gt;= 100000:
    factor = 0.75
  elif retrievedValue &amp;gt;= 50000:
    factor = 0.8
  else:
    factor = 0.9
  return retrievedValue * factor&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;
        , SourceCodeKind.Statements);

      &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;Create a variable which can be used in the script
      &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;to retrieve values from the DB&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;      var scope &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; engine.CreateScope();
      scope.SetVariable(&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;numbers&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; Meh());

      &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;Compile the script&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;      scriptSource.Execute(scope);

      &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;Get a reference to the function&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;      Func&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;string&lt;/span&gt;&lt;span style="color: #000000;"&gt;, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;decimal&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; foo &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; scope.GetVariable&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;Func&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;string&lt;/span&gt;&lt;span style="color: #000000;"&gt;, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;decimal&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;foo&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;);

      &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;Execute it&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;      Console.WriteLine(foo(&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;a&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;));
      Console.WriteLine(foo(&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;b&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;));
      Console.WriteLine(foo(&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;c&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;));
      Console.ReadLine();
    }
  }

  &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;interface&lt;/span&gt;&lt;span style="color: #000000;"&gt; IMeh
  {
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; GetValue(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;string&lt;/span&gt;&lt;span style="color: #000000;"&gt; name);
  }

  &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;class&lt;/span&gt;&lt;span style="color: #000000;"&gt; Meh : IMeh
  {
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; GetValue(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;string&lt;/span&gt;&lt;span style="color: #000000;"&gt; name)
    {
      &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;switch&lt;/span&gt;&lt;span style="color: #000000;"&gt; (name)
      {
        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;case&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;a&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;: &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;100000&lt;/span&gt;&lt;span style="color: #000000;"&gt;;
        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;case&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;b&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;: &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;50000&lt;/span&gt;&lt;span style="color: #000000;"&gt;;
        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;default&lt;/span&gt;&lt;span style="color: #000000;"&gt;: &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;36000&lt;/span&gt;&lt;span style="color: #000000;"&gt;;
      }
    }
  }

}&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;&lt;img src="http://blog.peterlesliemorris.com/aggbug/18.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Peter Morris</dc:creator>
            <guid>http://blog.peterlesliemorris.com/archive/2010/05/19/embedding-ironpython-into-a-c-application.aspx</guid>
            <pubDate>Wed, 19 May 2010 14:00:16 GMT</pubDate>
            <wfw:comment>http://blog.peterlesliemorris.com/comments/18.aspx</wfw:comment>
            <comments>http://blog.peterlesliemorris.com/archive/2010/05/19/embedding-ironpython-into-a-c-application.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://blog.peterlesliemorris.com/comments/commentRss/18.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Repositories, and filters</title>
            <link>http://blog.peterlesliemorris.com/archive/2009/09/30/repositories-and-filters.aspx</link>
            <description>&lt;p&gt;I remember a while back some discussions about whether Repositories are a good or a bad thing.  The case given as a bad example was one where the Repository had a large number of methods on it, whether Repositories are good or not was irrelevant, it was crap code :-)&lt;/p&gt;  &lt;p&gt;Personally I DO use Repositories.  Or, to be more accurate, I use A Repository.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:a41b8ed3-ec92-461b-9101-b7fd45f1da66" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:White;overflow: auto;"&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;interface&lt;/span&gt;&lt;span style="color: #000000;"&gt; IDomainRepository
{
  IEnumerable&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;T&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; AllInstances&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;T&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;();
}&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;To get a list of instances of a specific class I can obviously ask the DomainRepository for AllInstances&amp;lt;User&amp;gt; for example.  This is just about as far as my Repository goes, so that in Unit Test code I can control which instances are “retrieved from the database” without having a database.&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:0f8938ad-d300-4b7c-9e63-bb59f7f1f328" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:#FFFFFF;overflow: auto;"&gt;&lt;span style="color: #000000;"&gt;[TestMethod]
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;void&lt;/span&gt;&lt;span style="color: #000000;"&gt; GetErrors_ReturnsError_ForDuplicateUserName()
{
  var user1 &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; User() { UserName &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;A&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt; };
  var user2 &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; User() { UserName &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;A&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt; };
  var allUsers &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; List&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;User&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; { user1, user2 };

  var mockDomainRepository &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; MockRepository.GenerateMock&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;IDomainRepository&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;();
  mockDomainRepository.Stub(x &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; x.AllInstances&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;User&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;()).Return(allUsers);

  var validator &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; UserValidator(mockDomainRepository);
  
  Assert.IsTrue(validator.GetErrors(user1).Any(x &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; x.FieldName &lt;/span&gt;&lt;span style="color: #000000;"&gt;==&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;UserName&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span style="color: #000000;"&gt; x.ErrorMessage &lt;/span&gt;&lt;span style="color: #000000;"&gt;==&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;Already in use&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;));
}&lt;/span&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;I’ll write about filtering data soon.&lt;/p&gt;&lt;img src="http://blog.peterlesliemorris.com/aggbug/17.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Peter Morris</dc:creator>
            <guid>http://blog.peterlesliemorris.com/archive/2009/09/30/repositories-and-filters.aspx</guid>
            <pubDate>Wed, 30 Sep 2009 11:54:39 GMT</pubDate>
            <wfw:comment>http://blog.peterlesliemorris.com/comments/17.aspx</wfw:comment>
            <comments>http://blog.peterlesliemorris.com/archive/2009/09/30/repositories-and-filters.aspx#feedback</comments>
            <wfw:commentRss>http://blog.peterlesliemorris.com/comments/commentRss/17.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Delphi Audio Compression components updated to Delphi 2009</title>
            <category>Code</category>
            <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>I am special to the BNP!</title>
            <link>http://blog.peterlesliemorris.com/archive/2009/08/29/i-am-special-to-the-bnp.aspx</link>
            <description>I got a spam email from the BNP today.  They are obviously desperate for money...

========
Fellow Patriot,
I am delighted to inform you of a very special invitation now open to you: the British National Party 'LIFE MEMBERSHIP' offer.

This honour is being extended to a carefully selected group of British citizens, stalwart BNP members like you, and I would like to be the first to personally congratulate you for being among that special chosen group of people.
========

Wow, I am special to the BNP.  If all you have to do to be special to them is to write and tell them they are idiots, then I am not entirely sure what their "not special" *supporters* are like!  

They must be vegetables or something :-)&lt;img src="http://blog.peterlesliemorris.com/aggbug/15.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Peter Morris</dc:creator>
            <guid>http://blog.peterlesliemorris.com/archive/2009/08/29/i-am-special-to-the-bnp.aspx</guid>
            <pubDate>Sat, 29 Aug 2009 16:05:54 GMT</pubDate>
            <wfw:comment>http://blog.peterlesliemorris.com/comments/15.aspx</wfw:comment>
            <comments>http://blog.peterlesliemorris.com/archive/2009/08/29/i-am-special-to-the-bnp.aspx#feedback</comments>
            <wfw:commentRss>http://blog.peterlesliemorris.com/comments/commentRss/15.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Government IT failures</title>
            <link>http://blog.peterlesliemorris.com/archive/2009/08/19/government-it-failures.aspx</link>
            <description>&lt;p&gt;Why is it that as soon as the word “government” is applied to a software project the cost multiplies massively and suddenly the project costs “millions”, runs over schedule, and usually doesn’t work when delivered.&lt;/p&gt;  &lt;p&gt;A few months ago my wife and I saw a local lad break into a car and steal a wallet.  A couple of weeks ago we were asked to go to the police station to do an identity parade.  When we arrived we were told that unfortunately the “new” identity system had crashed and we wouldn’t be able to take part that day.  &lt;/p&gt;  &lt;p&gt;Today we went for another try.  Again the police apologised and told us that the system was not working and we wouldn’t be able to take part in an identity parade.  I was slightly annoyed by the incompetence of the IT people.  I mean, why wasn’t the old system being used in parallel just in case of situations just like this?  It’s not even as though they need to capture data and keep it in sync’, it’s just a video selection of 8 people + 1 suspect looking forward, left, then right.&lt;/p&gt;  &lt;p&gt;Just as I had finished telling them how rubbish their new IT system is a police officer popped his head around the door with a big happy smile on his face and said “It’s okay, the file has arrived, we can continue!”  After a little rejoicing I went and sat down at the computer in front of the most simple looking software application I think I have ever seen.&lt;/p&gt;  &lt;p&gt;After sitting in front of it watching 9 blonde hair Caucasians looking forward, left, then right I was asked if I was able to identify the culprit.  “No,” I replied, “the guy was Asian with black hair!”.  The head office had obviously sent down the wrong set of videos for this case.  Thankfully in this case the error was obvious, but what if they had sent down videos of 9 young Asian males?  The error wouldn’t have been obvious and the guy would have got away with it!&lt;/p&gt;  &lt;p&gt;Just a simple safeguard such as embedding the case number into the binary file and asking the officer to type in the expected case number for confirmation would have done the trick.  Surely SOMEONE at some point must have considered an officer might be sent the wrong file, or open a file from the wrong email?&lt;/p&gt;  &lt;p&gt;I can’t help but wonder how much they paid for that software.  I could have easily written all of it within a month and it would have worked just fine.  Unbelievable!&lt;/p&gt;&lt;img src="http://blog.peterlesliemorris.com/aggbug/14.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Peter Morris</dc:creator>
            <guid>http://blog.peterlesliemorris.com/archive/2009/08/19/government-it-failures.aspx</guid>
            <pubDate>Wed, 19 Aug 2009 20:47:47 GMT</pubDate>
            <wfw:comment>http://blog.peterlesliemorris.com/comments/14.aspx</wfw:comment>
            <comments>http://blog.peterlesliemorris.com/archive/2009/08/19/government-it-failures.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://blog.peterlesliemorris.com/comments/commentRss/14.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Tightly coupling generic types</title>
            <link>http://blog.peterlesliemorris.com/archive/2009/08/18/tightly-coupling-generic-types.aspx</link>
            <description>&lt;p&gt;My server application works purely on a request/response pattern, like so&lt;/p&gt;  &lt;div class="csharpcode"&gt;   &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;var query = &lt;span class="kwrd"&gt;new&lt;/span&gt; GetCustomerQuery(customerUniqueID);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;var response = &lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;  AppServer.Execute&amp;lt;GetCustomerQuery, GetCustomerQueryResponse&amp;gt;(query);&lt;/pre&gt;
&lt;/div&gt;

&lt;p /&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;What I wanted to avoid though was the possibility that the user (me writing the client app) would do something silly like the following code and use the wrong pair combination&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;var query = &lt;span class="kwrd"&gt;new&lt;/span&gt; GetCustomerQuery(customerUniqueID);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;var response = &lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;  AppServer.Execute&amp;lt;Get&lt;strong&gt;&lt;u&gt;&lt;em&gt;Customer&lt;/em&gt;&lt;/u&gt;&lt;/strong&gt;Query, Get&lt;strong&gt;&lt;u&gt;&lt;em&gt;Employee&lt;/em&gt;&lt;/u&gt;&lt;/strong&gt;QueryResponse&amp;gt;(query);&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;Up until now I had the server interface defined like this, so that I can at least ensure the generic parameters are a Query and Response…&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;TResponse Execute&amp;lt;TRequest, TResponse&amp;gt;(TRequest request)&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;  &lt;span class="kwrd"&gt;where&lt;/span&gt; TRequest : Request&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;  &lt;span class="kwrd"&gt;where&lt;/span&gt; TResponse : Response;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;But a very simple addition ensured that the response type specified is the right type for the request.&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;TResponse Execute&amp;lt;TRequest, TResponse&amp;gt;(TResponse response)&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;  &lt;span class="kwrd"&gt;where&lt;/span&gt; TRequest : Request&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;  &lt;span class="kwrd"&gt;where&lt;/span&gt; TResponse : Response, IResponseFor&amp;lt;TRequest&amp;gt;;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;Then when I create my GetCustomerQueryResponse class I merely need to declare it like so&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;class&lt;/span&gt; GetCustomerQueryResponse : Response, &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;  IResponseFor&amp;lt;GetCustomerQuery&amp;gt;&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="rem"&gt;//etc&lt;/span&gt;&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;Now the client code above won’t compile because GetEmployeeQueryResponse does not implement IResponseFor&amp;lt;GetCustomerQuery&amp;gt;.&lt;/p&gt;&lt;img src="http://blog.peterlesliemorris.com/aggbug/13.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Peter Morris</dc:creator>
            <guid>http://blog.peterlesliemorris.com/archive/2009/08/18/tightly-coupling-generic-types.aspx</guid>
            <pubDate>Tue, 18 Aug 2009 10:44:42 GMT</pubDate>
            <wfw:comment>http://blog.peterlesliemorris.com/comments/13.aspx</wfw:comment>
            <comments>http://blog.peterlesliemorris.com/archive/2009/08/18/tightly-coupling-generic-types.aspx#feedback</comments>
            <wfw:commentRss>http://blog.peterlesliemorris.com/comments/commentRss/13.aspx</wfw:commentRss>
        </item>
        <item>
            <title>ECO Persistence Mapper per connection</title>
            <link>http://blog.peterlesliemorris.com/archive/2009/08/17/eco-persistence-mapper-per-connection.aspx</link>
            <description>&lt;p&gt;The &lt;a title="" href="http://www.capableobjects.com" rel="" target="_blank"&gt;Enterprise Core Objects&lt;/a&gt; Persistence Mapper is a singleton which is used by all EcoSpace instances.  It’s purpose is to load mapping information from the DB when your app starts and to cache it, improving performance.&lt;/p&gt;  &lt;p&gt;I needed a connection per client, all running within a single application.  This was a problem because once the PMP is created its connection string is tied to a single database.  So I had to come up with a new PersistenceMapperDynamicSharer component.  It is used on the EcoSpace to specify the PMapper type; additionally you can specify a connection string to use.&lt;/p&gt;  &lt;p&gt;It works by dynamically creating a descendant class of your PersistenceMapperProvider at runtime, one for each connection string.&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;class&lt;/span&gt; PersistenceMapperDynamicSharer : PersistenceMapperSharer&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;static&lt;/span&gt; ReaderWriterLockSlim Locker = &lt;span class="kwrd"&gt;new&lt;/span&gt; ReaderWriterLockSlim();&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;static&lt;/span&gt; Dictionary&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;, PersistenceMapperProvider&amp;gt; MapperProviders = &lt;span class="kwrd"&gt;new&lt;/span&gt; Dictionary&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;, PersistenceMapperProvider&amp;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;    [Browsable(&lt;span class="kwrd"&gt;false&lt;/span&gt;)]&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; ConnectionString { get; set; }&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;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; IPersistenceMapper GetPersistenceMapper(ITypeSystemService typeSystemService)&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;span class="kwrd"&gt;return&lt;/span&gt; GetPersistenceMapperProvider().GetPersistenceMapper(typeSystemService);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;    }&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; ReturnPersistenceMapper(IPersistenceMapper persistenceMapper)&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;    {&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;      GetPersistenceMapperProvider().ReturnPersistenceMapper(persistenceMapper);&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; &lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;    PersistenceMapperProvider GetPersistenceMapperProvider()&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;    {&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;      &lt;span class="rem"&gt;//No connection string = default mapper provider&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;      &lt;span class="kwrd"&gt;if&lt;/span&gt; (&lt;span class="kwrd"&gt;string&lt;/span&gt;.IsNullOrEmpty(ConnectionString))&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; PersistenceMapperProvider.GetInstance(MapperProviderType);&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;      PersistenceMapperProvider result;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt;      Locker.EnterUpgradeableReadLock();&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  27:  &lt;/span&gt;      &lt;span class="kwrd"&gt;try&lt;/span&gt;&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;span class="kwrd"&gt;if&lt;/span&gt; (MapperProviders.TryGetValue(ConnectionString, &lt;span class="kwrd"&gt;out&lt;/span&gt; result))&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  30:  &lt;/span&gt;          &lt;span class="kwrd"&gt;return&lt;/span&gt; result;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  31:  &lt;/span&gt;        Locker.EnterWriteLock();&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  32:  &lt;/span&gt;        &lt;span class="kwrd"&gt;try&lt;/span&gt;&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;          var mapperType = CreateNewPersistenceMapperProviderType();&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  35:  &lt;/span&gt;          result = (PersistenceMapperProvider)Activator.CreateInstance(mapperType);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  36:  &lt;/span&gt;          ((IDynamicallySharedPersistenceMapper)result).SetConnectionString(ConnectionString);&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  37:  &lt;/span&gt;          MapperProviders[ConnectionString] = result;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  38:  &lt;/span&gt;        }&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  39:  &lt;/span&gt;        &lt;span class="kwrd"&gt;finally&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  40:  &lt;/span&gt;        {&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  41:  &lt;/span&gt;          Locker.ExitWriteLock();&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  42:  &lt;/span&gt;        }&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  43:  &lt;/span&gt;      }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  44:  &lt;/span&gt;      &lt;span class="kwrd"&gt;finally&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  45:  &lt;/span&gt;      {&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  46:  &lt;/span&gt;        Locker.ExitUpgradeableReadLock();&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  47:  &lt;/span&gt;      }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  48:  &lt;/span&gt;      &lt;span class="kwrd"&gt;return&lt;/span&gt; result;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  49:  &lt;/span&gt;    }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  50:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  51:  &lt;/span&gt;    Type CreateNewPersistenceMapperProviderType()&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  52:  &lt;/span&gt;    {&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  53:  &lt;/span&gt;      &lt;span class="kwrd"&gt;string&lt;/span&gt; guid = Guid.NewGuid().ToString();&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  54:  &lt;/span&gt;      var assemblyName = &lt;span class="kwrd"&gt;new&lt;/span&gt; AssemblyName(guid);&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  55:  &lt;/span&gt;      var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  56:  &lt;/span&gt;        assemblyName, &lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  57:  &lt;/span&gt;        System.Reflection.Emit.AssemblyBuilderAccess.Run);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  58:  &lt;/span&gt;      var moduleBuilder = assemblyBuilder.DefineDynamicModule(guid);&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  59:  &lt;/span&gt;      var typeBuilder = moduleBuilder.DefineType(&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  60:  &lt;/span&gt;        guid, &lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  61:  &lt;/span&gt;        TypeAttributes.Class | TypeAttributes.Public, &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  62:  &lt;/span&gt;        MapperProviderType);&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  63:  &lt;/span&gt;      &lt;span class="kwrd"&gt;return&lt;/span&gt; typeBuilder.CreateType();&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  64:  &lt;/span&gt;    }&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  65:  &lt;/span&gt;  }&lt;/pre&gt;
&lt;/div&gt;

&lt;p /&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;On my EcoSpace I have a method called ActivateWithConnectionString which I can use whenever I need to activate the EcoSpace connecting to a connection string OTHER than the default…&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;void&lt;/span&gt; ActivateWithConnectionString(&lt;span class="kwrd"&gt;string&lt;/span&gt; connectionString)&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;      persistenceMapperDynamicSharer1.ConnectionString = connectionString;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;      Active = &lt;span class="kwrd"&gt;true&lt;/span&gt;;&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;And on my PersistenceMapperProvider I need to implement IDynamicallySharedPersistenceMapper like so…&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;void&lt;/span&gt; IDynamicallySharedPersistenceMapper.SetConnectionString(&lt;span class="kwrd"&gt;string&lt;/span&gt; connectionString)&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;      sqlConnection1.ConnectionString = connectionString;&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;Now I can benefit from the performance gains from the singleton-pattern implemented by PersistenceMapperProvider &lt;strong&gt;and&lt;/strong&gt; connect to different databases.&lt;/p&gt;&lt;img src="http://blog.peterlesliemorris.com/aggbug/12.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Peter Morris</dc:creator>
            <guid>http://blog.peterlesliemorris.com/archive/2009/08/17/eco-persistence-mapper-per-connection.aspx</guid>
            <pubDate>Mon, 17 Aug 2009 09:33:06 GMT</pubDate>
            <wfw:comment>http://blog.peterlesliemorris.com/comments/12.aspx</wfw:comment>
            <comments>http://blog.peterlesliemorris.com/archive/2009/08/17/eco-persistence-mapper-per-connection.aspx#feedback</comments>
            <wfw:commentRss>http://blog.peterlesliemorris.com/comments/commentRss/12.aspx</wfw:commentRss>
        </item>
        <item>
            <title>PropertyChangedEventHandler is not marked as serializable</title>
            <link>http://blog.peterlesliemorris.com/archive/2009/07/24/propertychangedeventhandler-is-not-marked-as-serializable.aspx</link>
            <description>&lt;p&gt;My data-transfer-objects implement INotifyPropertyChanged, which was giving me a problem whenever I tried to serialise them over a Remoting session.  If you try to add [NonSerialized] to the event you get an error informing you that you can only apply this attribute to fields, and not properties.&lt;/p&gt;  &lt;p&gt;The solution is pretty simple, I think the .NET compiler should do this by default.&lt;/p&gt;  &lt;div class="csharpcode"&gt;   &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;[NonSerialized]&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;PropertyChangedEventHandler propertyChanged;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;event&lt;/span&gt; PropertyChangedEventHandler PropertyChanged&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;  add { propertyChanged += &lt;span class="kwrd"&gt;value&lt;/span&gt;; }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;  remove { propertyChanged -= &lt;span class="kwrd"&gt;value&lt;/span&gt;; }&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;&lt;img src="http://blog.peterlesliemorris.com/aggbug/11.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Peter Morris</dc:creator>
            <guid>http://blog.peterlesliemorris.com/archive/2009/07/24/propertychangedeventhandler-is-not-marked-as-serializable.aspx</guid>
            <pubDate>Fri, 24 Jul 2009 09:27:47 GMT</pubDate>
            <wfw:comment>http://blog.peterlesliemorris.com/comments/11.aspx</wfw:comment>
            <comments>http://blog.peterlesliemorris.com/archive/2009/07/24/propertychangedeventhandler-is-not-marked-as-serializable.aspx#feedback</comments>
            <wfw:commentRss>http://blog.peterlesliemorris.com/comments/commentRss/11.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Partial declarations of must not specify different base classes</title>
            <category>Code</category>
            <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>
            <category>Code</category>
            <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>
    </channel>
</rss>