<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" version="2.0">
  <channel>
    <title>Eleutian SpeakENG Development Blog - emit</title>
    <link>http://blog.eleutian.com/</link>
    <description />
    <language>en-us</language>
    <copyright>Eleutian Technology</copyright>
    <lastBuildDate>Sun, 18 Nov 2007 23:49:46 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.0.7226.0</generator>
    <managingEditor>aaron@eleutian.com</managingEditor>
    <webMaster>aaron@eleutian.com</webMaster>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=9ae72631-5839-4026-ae1c-26277219b004</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,9ae72631-5839-4026-ae1c-26277219b004.aspx</pingback:target>
      <dc:creator>Jacob</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,9ae72631-5839-4026-ae1c-26277219b004.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=9ae72631-5839-4026-ae1c-26277219b004</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I haven't posted in a while and decided to write a post about one of the little side
projects I've worked on recently. I've been lurking around on the ALT.NET mailing
list and one of the discussions I take time to keep up on is on Dependency Injection.
Something I worked on a few weeks ago has some applications in that area and so I
decided to see what others thought.
</p>
        <p>
I was talking with Aaron several months ago about how many problems constructors in
static languages cause. Ruby people are fortunate in that their constructors are much
more flexible than their C# counterparts. Want something to be a singleton? Just return
the same instance from the constructor! Lifecycle management and dependencies can
be the responsibility of the constructor itself, rather than a function of a container.
</p>
        <p>
Over the weekend a few weeks ago I wrote a library and MsBuild task that factors constructor
calls out of code and into a pluggable factory system. You write code like this:
</p>
        <pre class="csharpcode">
          <span class="kwrd">public</span>
          <span class="kwrd">class</span> EmailSender
{ <span class="kwrd">public</span><span class="kwrd">void</span> TellEveryone(<span class="kwrd">string</span> message)
{ TemplateRenderer renderer = <span class="kwrd">new</span> TemplateRenderer(); Console.WriteLine(renderer.Render(message));
} } <span class="kwrd">public</span><span class="kwrd">class</span> EmailSenderFactory
: IObjectFactory&lt;EmailSender&gt; { <span class="kwrd">public</span> EmailSender
Create() { <span class="kwrd">return</span><span class="kwrd">new</span> EmailSender();
} } EmailSender emailer = <span class="kwrd">new</span> EmailSender(); emailer.TellEveryone(<span class="str">"Hello,
World!"</span>);</pre>
        <p>
What the MsBuild task will do is this:
</p>
        <ol>
          <li>
Load the target Assemblies using Mono.Cecil 
</li>
          <li>
Find implementations of IObjectFactory&lt;T&gt; and add them to the FactoryMap. 
</li>
          <li>
Find all instances of new T() for each of those factories and replace them with a
call to the Factories.Create&lt;T&gt;(), ignoring calls inside of factories themselves. 
</li>
          <li>
Weave the FactoryMap (just the Factory Type and its Object Type) into the Assembly. 
</li>
        </ol>
        <p>
So during runtime we can use the FactoryMap that was serialized in step #4 to create
the factories and use them to create instances as they're demanded. It is important
to note that your objects need default constructors, even though they may never be
called after the code is weaved. One can also replace the factory implementations
with IoC backed versions, etc... 
</p>
        <p>
          <strong>Why don't you just create a New.Instance&lt;T&gt; method and use that instead
of new T() and save yourself the pain of weaving and having a task and all that, moron?</strong>
        </p>
        <p>
Good question! I mostly did this as an exercise in code weaving using Cecil. It's
a great library, although testing code using it can be a little tedious. I'm tossing
around some other projects that I'd love to give a shot and this was a good introduction.
You could achieve the same thing using reflection to build your factory map and remember
to use New.Instance&lt;T&gt;() instead of the new operator in your code. Granted,
the manual approach plasters the concern all over the place and weaving keeps the
original code free from that. Which may be an OK trade-off for the simplified process.
</p>
        <p>
Another thing to consider, is that perhaps the weaving approach has its applications
elsewhere? Use it specifically for creating domain classes to transparently provide
domain services? Another idea I've been tossing around is if you're writing code for
platforms where garbage collection isn't as performant (xbox for example) you may
be able to transparently add object pooling and other patterns after the fact and
not clutter up the original code. 
</p>
        <p>
I want to release the code anyway, but I'm tired of uploading zip files. All my future
source releases will probably be via Google Code or some public svn repository. Stay
tuned.... In the meantime, feel free to opine.
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=9ae72631-5839-4026-ae1c-26277219b004" />
      </body>
      <title>A Weird Code Weaving Thing I Wrote</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,9ae72631-5839-4026-ae1c-26277219b004.aspx</guid>
      <link>http://blog.eleutian.com/2007/11/18/AWeirdCodeWeavingThingIWrote.aspx</link>
      <pubDate>Sun, 18 Nov 2007 23:49:46 GMT</pubDate>
      <description>&lt;p&gt;
I haven't posted in a while and decided to write a post about one of the little side
projects I've worked on recently. I've been lurking around on the ALT.NET mailing
list and one of the discussions I take time to keep up on is on Dependency Injection.
Something I worked on a few weeks ago has some applications in that area and so I
decided to see what others thought.
&lt;/p&gt;
&lt;p&gt;
I was talking with Aaron several months ago about how many problems constructors in
static languages cause. Ruby people are fortunate in that their constructors are much
more flexible than their C# counterparts. Want something to be a singleton? Just return
the same instance from the constructor! Lifecycle management and dependencies can
be the responsibility of the constructor itself, rather than a function of a container.
&lt;/p&gt;
&lt;p&gt;
Over the weekend a few weeks ago I wrote a library and MsBuild task that factors constructor
calls out of code and into a pluggable factory system. You write code like this:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; EmailSender
{ &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; TellEveryone(&lt;span class="kwrd"&gt;string&lt;/span&gt; message)
{ TemplateRenderer renderer = &lt;span class="kwrd"&gt;new&lt;/span&gt; TemplateRenderer(); Console.WriteLine(renderer.Render(message));
} } &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; EmailSenderFactory
: IObjectFactory&amp;lt;EmailSender&amp;gt; { &lt;span class="kwrd"&gt;public&lt;/span&gt; EmailSender
Create() { &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; EmailSender();
} } EmailSender emailer = &lt;span class="kwrd"&gt;new&lt;/span&gt; EmailSender(); emailer.TellEveryone(&lt;span class="str"&gt;&amp;quot;Hello,
World!&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;p&gt;
What the MsBuild task will do is this:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Load the target Assemblies using Mono.Cecil 
&lt;/li&gt;
&lt;li&gt;
Find implementations of IObjectFactory&amp;lt;T&amp;gt; and add them to the FactoryMap. 
&lt;/li&gt;
&lt;li&gt;
Find all instances of new T() for each of those factories and replace them with a
call to the Factories.Create&amp;lt;T&amp;gt;(), ignoring calls inside of factories themselves. 
&lt;/li&gt;
&lt;li&gt;
Weave the FactoryMap (just the Factory Type and its Object Type) into the Assembly. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
So during runtime we can use the FactoryMap that was serialized in step #4 to create
the factories and use them to create instances as they're demanded. It is important
to note that your objects need default constructors, even though they may never be
called after the code is weaved. One can also replace the factory implementations
with IoC backed versions, etc... 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Why don't you just create a New.Instance&amp;lt;T&amp;gt; method and use that instead
of new T() and save yourself the pain of weaving and having a task and all that, moron?&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Good question! I mostly did this as an exercise in code weaving using Cecil. It's
a great library, although testing code using it can be a little tedious. I'm tossing
around some other projects that I'd love to give a shot and this was a good introduction.
You could achieve the same thing using reflection to build your factory map and remember
to use New.Instance&amp;lt;T&amp;gt;() instead of the new operator in your code. Granted,
the manual approach plasters the concern all over the place and weaving keeps the
original code free from that. Which may be an OK trade-off for the simplified process.
&lt;/p&gt;
&lt;p&gt;
Another thing to consider, is that perhaps the weaving approach has its applications
elsewhere? Use it specifically for creating domain classes to transparently provide
domain services? Another idea I've been tossing around is if you're writing code for
platforms where garbage collection isn't as performant (xbox for example) you may
be able to transparently add object pooling and other patterns after the fact and
not clutter up the original code. 
&lt;/p&gt;
&lt;p&gt;
I want to release the code anyway, but I'm tired of uploading zip files. All my future
source releases will probably be via Google Code or some public svn repository. Stay
tuned.... In the meantime, feel free to opine.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=9ae72631-5839-4026-ae1c-26277219b004" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,9ae72631-5839-4026-ae1c-26277219b004.aspx</comments>
      <category>development</category>
      <category>emit</category>
      <category>inversion of control</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=e6b41de9-34c0-48e7-b440-72a7f0ca48de</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,e6b41de9-34c0-48e7-b440-72a7f0ca48de.aspx</pingback:target>
      <dc:creator>Jacob</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,e6b41de9-34c0-48e7-b440-72a7f0ca48de.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=e6b41de9-34c0-48e7-b440-72a7f0ca48de</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Hello everyone. I just wanted to let those who responded to the string literal posts know
what was "going on". Basically, we're still trying to find a way that we can release
the source to some of our tools. I apologize for dragging my feet. I manage
to keep pretty busy. Heh.
</p>
        <p>
I was catching up on the castle-project's mailing list and found <a href="http://groups.google.com/group/castle-project-devel/browse_frm/thread/aa65cfb118250109">this
post</a> by Lee Henson. He's tossed together an implementation of a PropertyBag
wrapper generator and posted the source. It's very similar to our approach. One main
difference is that we do our generation at runtime so we don't have to generate and
reference another assembly. We have an IViewFactory interface and a PropertyBagViewFactory
implementation that hands them out. In tests we have an IViewFactory that pulls the
views out of the MockRepository. 
</p>
        <p>
Incidentally, our code that does this lies with some other code for generating
Control.Invoke proxies for our System.Windows.Forms view interfaces. It seems we're
not the only people who do that particular type of generation. Rüdiger Klaehn
has an article <a href="http://www.codeproject.com/csharp/threadsafeforms.asp?print=true">here</a> about
it. I strongly suggest reading it if you do any kind of SWF work.
</p>
        <p>
In the thread I found a link to <a href="http://www.brianromanko.com/2007/02/monorail-code-generation.html">Brian
Romanko's</a> post about his implementation of the controller action/view SiteMap,
as well as the PropertyBag wrapper. So just in case anybody gets impatient they can
find something to tinker with over there. While his implementation is different from
ours, the end result is very similar. I like his idea of generating a property for
actions to refer to them without parameters.
</p>
        <p>
I mentioned that for the SiteMap code generation we used CodeDom. CodeDom is
great and means we don't have to use StringBuilder to construct C#. It also abstracts
the language away. For those who are interested in this kind of code inspection/generation,
I strongly suggest taking a look at <a href="http://wiki.sharpdevelop.net/default.aspx/SharpDevelop.NRefactory">N
Refactory</a>, it's a great library for parsing C#/VB.NET source. It's what we use
and I've been incredibly happy with it. The only real hurdle when doing that kind
or work is type resolution. I'll try and make a post about that in the near future. 
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=e6b41de9-34c0-48e7-b440-72a7f0ca48de" />
      </body>
      <title>We're Glad Everybody Hates String Literals</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,e6b41de9-34c0-48e7-b440-72a7f0ca48de.aspx</guid>
      <link>http://blog.eleutian.com/2007/02/19/WereGladEverybodyHatesStringLiterals.aspx</link>
      <pubDate>Mon, 19 Feb 2007 03:04:16 GMT</pubDate>
      <description>&lt;p&gt;
Hello everyone. I just wanted to let those who responded to the string literal posts&amp;nbsp;know
what was "going on". Basically, we're still trying to find a way that we can release
the source to some of our tools.&amp;nbsp;I apologize for dragging&amp;nbsp;my feet. I manage
to keep pretty busy. Heh.
&lt;/p&gt;
&lt;p&gt;
I was catching up on the castle-project's mailing list and found &lt;a href="http://groups.google.com/group/castle-project-devel/browse_frm/thread/aa65cfb118250109"&gt;this
post&lt;/a&gt;&amp;nbsp;by Lee Henson. He's tossed together an implementation of a PropertyBag
wrapper generator and posted the source. It's very similar to our approach. One main
difference is that we do our generation at runtime so we don't have to generate and
reference another assembly. We have an IViewFactory interface and a PropertyBagViewFactory
implementation that hands them out. In tests we have an IViewFactory that pulls the
views out of the MockRepository. 
&lt;/p&gt;
&lt;p&gt;
Incidentally, our code that does&amp;nbsp;this lies with some other code for generating
Control.Invoke proxies for our System.Windows.Forms view interfaces. It seems&amp;nbsp;we're
not the only&amp;nbsp;people who do that particular type of generation. Rüdiger Klaehn
has an article &lt;a href="http://www.codeproject.com/csharp/threadsafeforms.asp?print=true"&gt;here&lt;/a&gt; about
it. I strongly suggest reading&amp;nbsp;it if you do any kind of SWF work.
&lt;/p&gt;
&lt;p&gt;
In the thread I found a link to &lt;a href="http://www.brianromanko.com/2007/02/monorail-code-generation.html"&gt;Brian
Romanko's&lt;/a&gt;&amp;nbsp;post about his implementation of the controller&amp;nbsp;action/view&amp;nbsp;SiteMap,
as well as the PropertyBag wrapper. So just in case anybody gets impatient they can
find something to tinker with over there. While his implementation is different from
ours, the end result is very similar. I like his idea of generating a property for
actions to refer to them without parameters.
&lt;/p&gt;
&lt;p&gt;
I mentioned that for the SiteMap code&amp;nbsp;generation we used CodeDom. CodeDom is
great and means we don't have to use StringBuilder to construct C#. It also abstracts
the language away. For those who are interested in this kind of code inspection/generation,
I strongly suggest taking a look at &lt;a href="http://wiki.sharpdevelop.net/default.aspx/SharpDevelop.NRefactory"&gt;N
Refactory&lt;/a&gt;, it's a great library for parsing C#/VB.NET source. It's what we use
and I've been incredibly happy with it. The only real hurdle when doing that kind
or work is type resolution. I'll try and make a post about that in the near future. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=e6b41de9-34c0-48e7-b440-72a7f0ca48de" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,e6b41de9-34c0-48e7-b440-72a7f0ca48de.aspx</comments>
      <category>code generation</category>
      <category>development</category>
      <category>emit</category>
      <category>source</category>
    </item>
    <item>
      <trackback:ping>http://blog.eleutian.com/Trackback.aspx?guid=ba5b573d-9863-456f-8e0d-be8871883eca</trackback:ping>
      <pingback:server>http://blog.eleutian.com/pingback.aspx</pingback:server>
      <pingback:target>http://blog.eleutian.com/PermaLink,guid,ba5b573d-9863-456f-8e0d-be8871883eca.aspx</pingback:target>
      <dc:creator>Jacob</dc:creator>
      <wfw:comment>http://blog.eleutian.com/CommentView,guid,ba5b573d-9863-456f-8e0d-be8871883eca.aspx</wfw:comment>
      <wfw:commentRss>http://blog.eleutian.com/SyndicationService.asmx/GetEntryCommentsRss?guid=ba5b573d-9863-456f-8e0d-be8871883eca</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I figured I might as well continue on the string literal kick I started with my last
post and talk about another situation where we've eliminated string literals. Take
the following code from inside a MonoRail action method:
</p>
        <pre class="csharpcode">PropertyBag[<span class="str">"User"</span>] = ourUser; PropertyBag[<span class="str">"TwoStates"</span>]
= <span class="kwrd">new</span><span class="kwrd">string</span>[] { <span class="str">"WA"</span>, <span class="str">"CA"</span> };
PropertyBag[<span class="str">"IsAdmin"</span>] = <span class="kwrd">true</span>;</pre>
        <p>
String literals, used as keys into dictionaries carry a cetain degree of code smell
(for us) and we try to avoid them. It reminds me too much code that does something
similar, but in the opposite direction:
</p>
        <pre class="csharpcode">
          <span class="kwrd">int</span> id = Int32.Parse(<span class="kwrd">this</span>.Params[<span class="str">"id"</span>])</pre>
        <p>
Thankfully, this kind of code is eliminated when using MonoRail and its SmartDispatchController.
We found ourselves thinking of how the same code would look in a SWF application.
We would be populating views, only those views would be interfaces that the various
forms/controls implemented. Well, this is exactly what we wanted in MonoRail, to wrap
the PropertyBag with an interface!
</p>
        <p>
After a few hours playing with Reflection.Emit, I had a code generator that would
take an interface:
</p>
        <pre class="csharpcode">
          <span class="kwrd">public</span>
          <span class="kwrd">interface</span> IEditMyProfileView
{ <span class="kwrd">string</span> Name { get; set; } DateTime Birthday { get; set;
} IList&lt;TimeZone&gt; TimeZones { get; set; } } </pre>
        <p>
And produce a class like the following:
</p>
        <pre class="csharpcode">
          <span class="kwrd">public</span>
          <span class="kwrd">class</span> EditMyProfileViewPropertyBagManipulator
: IEditMyProfileView { <span class="kwrd">private</span> IDictionary _bag; <span class="kwrd">public</span> EditMyProfileViewPropertyBagManipulator(IDictionary
bag) { _bag = bag; } <span class="kwrd">public</span><span class="kwrd">string</span> Name
{ get { <span class="kwrd">return</span> (<span class="kwrd">string</span>)_bag[<span class="str">"Name"</span>];
} set { _bag[<span class="str">"Name"</span>] = <span class="kwrd">value</span>; }
} <span class="kwrd">public</span> DateTime Birthday { get { <span class="kwrd">return</span> (DateTime)_bag[<span class="str">"Birthday"</span>];
} set { _bag[<span class="str">"Birthday"</span>] = <span class="kwrd">value</span>;
} } <span class="kwrd">public</span> IList&lt;TimeZone&gt; TimeZones { get { <span class="kwrd">return</span> (IList&lt;TimeZone&gt;)_bag[<span class="str">"TimeZones"</span>];
} set { _bag[<span class="str">"TimeZones"</span>] = <span class="kwrd">value</span>;
} } } </pre>
        <p>
This has the advantage of keeping our interactions with the PropertyBag as type safe
as they can be. Changing the interface, breaks the build. Where as changing the type
of a value inserted into the PropertyBag will (hopefully) only break tests if even
that. This is another example of us trying to turn run-time failures into compile-time
failures.
</p>
        <p>
This kind of thing also makes tests more elegant. Instead of:
</p>
        <pre class="csharpcode">Assert.Equals(<span class="str">"Jacob"</span>, PropertyBag[<span class="str">"Name"</span>]);</pre>
        <p>
In our controller tests, we can continue to leverage the use of RhinoMocks to ensure
the views are properly initialized:
</p>
        <pre class="csharpcode">
          <span class="kwrd">using</span> (_mocks.Unordered()) { _view.Name
= <span class="str">"Jacob"</span>; } _mocks.ReplayAll(); _controller.Action(); _mocks.Verify(_view);</pre>
        <p>
All we've done here is created a mock from the view interface, rather than emitting
the wrapper class. Again, another added benefit is if the view changes, compiling
the tests will also break. The sooner things break after a change the better, and
the build is pretty soon. Although, with ReSharper, it's nice to see red squiggles
appear as that's even sooner.
</p>
        <img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=ba5b573d-9863-456f-8e0d-be8871883eca" />
      </body>
      <title>We Still Hate String Literals</title>
      <guid isPermaLink="false">http://blog.eleutian.com/PermaLink,guid,ba5b573d-9863-456f-8e0d-be8871883eca.aspx</guid>
      <link>http://blog.eleutian.com/2007/01/31/WeStillHateStringLiterals.aspx</link>
      <pubDate>Wed, 31 Jan 2007 04:31:07 GMT</pubDate>
      <description>&lt;p&gt;
I figured I might as well continue on the string literal kick I started with my last
post and talk about another situation where we've eliminated string literals. Take
the following code from inside a MonoRail action method:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;PropertyBag[&lt;span class="str"&gt;"User"&lt;/span&gt;] = ourUser; PropertyBag[&lt;span class="str"&gt;"TwoStates"&lt;/span&gt;]
= &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt;[] { &lt;span class="str"&gt;"WA"&lt;/span&gt;, &lt;span class="str"&gt;"CA"&lt;/span&gt; };
PropertyBag[&lt;span class="str"&gt;"IsAdmin"&lt;/span&gt;] = &lt;span class="kwrd"&gt;true&lt;/span&gt;;&lt;/pre&gt;
&lt;p&gt;
String literals, used as keys into dictionaries carry a cetain degree of code smell
(for us) and we try to avoid them. It reminds me too much code that does something
similar, but in the opposite direction:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;int&lt;/span&gt; id = Int32.Parse(&lt;span class="kwrd"&gt;this&lt;/span&gt;.Params[&lt;span class="str"&gt;"id"&lt;/span&gt;])&lt;/pre&gt;
&lt;p&gt;
Thankfully, this kind of code is eliminated when using MonoRail and its SmartDispatchController.
We found ourselves thinking of how the same code would look in a SWF application.
We would be populating views, only those views would be interfaces that the various
forms/controls implemented. Well, this is exactly what we wanted in MonoRail, to wrap
the PropertyBag with an interface!
&lt;/p&gt;
&lt;p&gt;
After a few hours playing with Reflection.Emit, I had a code generator that would
take an interface:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; IEditMyProfileView
{ &lt;span class="kwrd"&gt;string&lt;/span&gt; Name { get; set; } DateTime Birthday { get; set;
} IList&amp;lt;TimeZone&amp;gt; TimeZones { get; set; } } &lt;/pre&gt;
&lt;p&gt;
And produce a class like the following:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; EditMyProfileViewPropertyBagManipulator
: IEditMyProfileView { &lt;span class="kwrd"&gt;private&lt;/span&gt; IDictionary _bag; &lt;span class="kwrd"&gt;public&lt;/span&gt; EditMyProfileViewPropertyBagManipulator(IDictionary
bag) { _bag = bag; } &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Name
{ get { &lt;span class="kwrd"&gt;return&lt;/span&gt; (&lt;span class="kwrd"&gt;string&lt;/span&gt;)_bag[&lt;span class="str"&gt;"Name"&lt;/span&gt;];
} set { _bag[&lt;span class="str"&gt;"Name"&lt;/span&gt;] = &lt;span class="kwrd"&gt;value&lt;/span&gt;; }
} &lt;span class="kwrd"&gt;public&lt;/span&gt; DateTime Birthday { get { &lt;span class="kwrd"&gt;return&lt;/span&gt; (DateTime)_bag[&lt;span class="str"&gt;"Birthday"&lt;/span&gt;];
} set { _bag[&lt;span class="str"&gt;"Birthday"&lt;/span&gt;] = &lt;span class="kwrd"&gt;value&lt;/span&gt;;
} } &lt;span class="kwrd"&gt;public&lt;/span&gt; IList&amp;lt;TimeZone&amp;gt; TimeZones { get { &lt;span class="kwrd"&gt;return&lt;/span&gt; (IList&amp;lt;TimeZone&amp;gt;)_bag[&lt;span class="str"&gt;"TimeZones"&lt;/span&gt;];
} set { _bag[&lt;span class="str"&gt;"TimeZones"&lt;/span&gt;] = &lt;span class="kwrd"&gt;value&lt;/span&gt;;
} } } &lt;/pre&gt;
&lt;p&gt;
This has the advantage of keeping our interactions with the PropertyBag as type safe
as they can be. Changing the interface, breaks the build. Where as changing the type
of a value inserted into the PropertyBag will (hopefully) only break tests if even
that. This is another example of us trying to turn run-time failures into compile-time
failures.
&lt;/p&gt;
&lt;p&gt;
This kind of thing also makes tests more elegant. Instead of:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;Assert.Equals(&lt;span class="str"&gt;"Jacob"&lt;/span&gt;, PropertyBag[&lt;span class="str"&gt;"Name"&lt;/span&gt;]);&lt;/pre&gt;
&lt;p&gt;
In our controller tests, we can continue to leverage the use of RhinoMocks to ensure
the views are properly initialized:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; (_mocks.Unordered()) { _view.Name
= &lt;span class="str"&gt;"Jacob"&lt;/span&gt;; } _mocks.ReplayAll(); _controller.Action(); _mocks.Verify(_view);&lt;/pre&gt;
&lt;p&gt;
All we've done here is created a mock from the view interface, rather than emitting
the wrapper class. Again, another added benefit is if the view changes, compiling
the tests will also break. The sooner things break after a change the better, and
the build is pretty soon. Although, with ReSharper, it's nice to see red squiggles
appear as that's even sooner.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.eleutian.com/aggbug.ashx?id=ba5b573d-9863-456f-8e0d-be8871883eca" /&gt;</description>
      <comments>http://blog.eleutian.com/CommentView,guid,ba5b573d-9863-456f-8e0d-be8871883eca.aspx</comments>
      <category>code generation</category>
      <category>development</category>
      <category>monorail</category>
      <category>emit</category>
    </item>
  </channel>
</rss>