<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:blogChannel="http://backend.userland.com/blogChannelModule" 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/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">
  <channel>
    <title>Ben Hart</title>
    <description>Another C# developer and architect</description>
    <link>http://blog.benhartonline.com/</link>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <generator>BlogEngine.NET 1.4.5.0</generator>
    <language>en-US</language>
    <blogChannel:blogRoll>http://blog.benhartonline.com/opml.axd</blogChannel:blogRoll>
    <dc:creator>Ben Hart</dc:creator>
    <dc:title>Ben Hart</dc:title>
    <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/BenHart" type="application/rss+xml" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item>
      <title>I digg kicking that link into the zone of votes</title>
      <description>&lt;p&gt;
For years I&amp;#39;ve been aware of &lt;a href="http://www.dotnetkicks.com/"&gt;DotNetKicks&lt;/a&gt; (I believe the stream of consciousness at the time was, &amp;quot;Jeepers,&amp;nbsp; this guy&amp;#39;s put all this effort into a blog post, and someone&amp;#39;s kicked him. Oh, it&amp;#39;s a good thing. Oh, this site aggregates .net related links, and spreads those that are popular. That&amp;#39;s cool!&amp;quot;)
&lt;/p&gt;
&lt;p&gt;
I only really got into it recently (shamefacedly, most likely since I started coveting those kicks as a blogger myself) and I must confess it&amp;#39;s a struggle to keep up. What with the blog roll, the mailing lists I monitor, occasional forum posting, required daily research, links sent around the office, having more required reading so well shared via DNK can be overwhelming. Especially considering I&amp;#39;ve got to get some actual work in too.
&lt;/p&gt;
&lt;p&gt;
Keeping up to date with trends and continuous improvement is an essential part of my job, so I make the time for most of it. I do sometimes wish I&amp;#39;d chosen a less progressive profession, though. Something like Phrenology, those guys haven&amp;#39;t had any advances for &lt;em&gt;ages&lt;/em&gt;...
&lt;/p&gt;
&lt;p&gt;
The nifty links BlogEngine.NET had by default (&lt;em&gt;note to self: one of these days I must customise this thing further&lt;/em&gt;) exposed me to &lt;a href="http://www.dzone.com/"&gt;DZone&lt;/a&gt; (&amp;quot;Oh great, more fresh links for developers&amp;quot;), previously unknown to me probably because they don&amp;#39;t do trackbacks. DZone is awesome, the most polished of interfaces, and a great, wide variety of content. You can even be a bitch, and vote down. Nice.
&lt;/p&gt;
&lt;p&gt;
Then today for the first time I noticed some other trackbacks appearing in blogs I read. Suddenly I noticed that people were being &lt;a href="http://webdevvote.com/"&gt;voted (great)&lt;/a&gt; on the &lt;a href="http://webdevvote.com/"&gt;Web Development Community&lt;/a&gt;, and &lt;a href="http://www.dotnetlinks.net/"&gt;linked&lt;/a&gt; on &lt;a href="http://www.dotnetlinks.net/"&gt;DotNetLinks&lt;/a&gt;. Not sure how long they&amp;#39;ve been around, but they&amp;#39;re new to me.
&lt;/p&gt;
&lt;p&gt;
Oh my. Come on guys, some of us are desperately trying to do some work over here :)
&lt;/p&gt;
&lt;p&gt;
In the meantime though, I think I&amp;#39;ll publish this to all those sites I&amp;#39;ve mentioned. Maybe I&amp;#39;ll even discover some more...
&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BenHart/~4/0Z9UNbHqTDQ" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/BenHart/~3/0Z9UNbHqTDQ/post.aspx</link>
      <author>benjamin.hart.nospam@nospam.gmail.com (ben)</author>
      <comments>http://blog.benhartonline.com/post/2008/11/26/I-digg-kicking-that-link-into-the-zone-of-votes.aspx#comment</comments>
      <guid isPermaLink="false">http://blog.benhartonline.com/post.aspx?id=39732eda-6f94-4c24-a576-0e4c4df8e1ea</guid>
      <pubDate>Wed, 26 Nov 2008 14:14:00 -0500</pubDate>
      <category>Blogging</category>
      <category>Life</category>
      <category>Software Development</category>
      <dc:publisher>ben</dc:publisher>
      <pingback:server>http://blog.benhartonline.com/pingback.axd</pingback:server>
      <pingback:target>http://blog.benhartonline.com/post.aspx?id=39732eda-6f94-4c24-a576-0e4c4df8e1ea</pingback:target>
      <slash:comments>191</slash:comments>
      <trackback:ping>http://blog.benhartonline.com/trackback.axd?id=39732eda-6f94-4c24-a576-0e4c4df8e1ea</trackback:ping>
      <wfw:comment>http://blog.benhartonline.com/post/2008/11/26/I-digg-kicking-that-link-into-the-zone-of-votes.aspx#comment</wfw:comment>
      <wfw:commentRss>http://blog.benhartonline.com/syndication.axd?post=39732eda-6f94-4c24-a576-0e4c4df8e1ea</wfw:commentRss>
    <feedburner:origLink>http://blog.benhartonline.com/post.aspx?id=39732eda-6f94-4c24-a576-0e4c4df8e1ea</feedburner:origLink></item>
    <item>
      <title>ASP.NET MVC SelectList selectedValue Gotcha</title>
      <description>&lt;p&gt;
A recent &lt;a href="http://ayende.com/Blog/archive/2008/11/11/and-yet-another-asp.net-mvc-bug.aspx"&gt;post&lt;/a&gt; from &lt;a href="http://ayende.com/Blog/"&gt;Ayende&lt;/a&gt; was declaring a another bug in ASP.NET MVC, specifically referring to a select not rendering with the option selected. I remembered that we had had some issues some time ago, and had made our own helper to render selects, and took the opportunity to see whether we had made the same incorrect assumption - passing in the whole object, when only the value field was required.
&lt;/p&gt;
&lt;p&gt;
I think we might have, but chatting to the team some other problems were experienced. It seemed the selected value was working in some situations, but not all. To keep as simple as possible, I used a test class similar to Ayende&amp;#39;s:
&lt;/p&gt;
&lt;div class="csharpcode-wrapper"&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; IdName
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
{
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Id { get; set; }
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Name { get; set; }
&lt;/pre&gt;
&lt;pre class="alt"&gt;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
And built a SelectList as duly instructed by the commentators on the post:
&lt;/p&gt;
&lt;div class="csharpcode-wrapper"&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;
var list = &lt;span class="kwrd"&gt;new&lt;/span&gt;[] 
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
{   
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    &lt;span class="kwrd"&gt;new&lt;/span&gt; IdName { Id = 1, Name = &lt;span class="str"&gt;&amp;quot;Name1&amp;quot;&lt;/span&gt; }, 
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    &lt;span class="kwrd"&gt;new&lt;/span&gt; IdName { Id = 2, Name = &lt;span class="str"&gt;&amp;quot;Name2&amp;quot;&lt;/span&gt; }, 
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    &lt;span class="kwrd"&gt;new&lt;/span&gt; IdName { Id = 3, Name = &lt;span class="str"&gt;&amp;quot;Name3&amp;quot;&lt;/span&gt; } 
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
};
&lt;/pre&gt;
&lt;pre class="alt"&gt;
var selectList = &lt;span class="kwrd"&gt;new&lt;/span&gt; SelectList(list, &lt;span class="str"&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;, &lt;span class="str"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;, 2);
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
ViewData[&lt;span class="str"&gt;&amp;quot;TestClasses&amp;quot;&lt;/span&gt;] = selectList;
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;span class="kwrd"&gt;Using the following extension method on the view&lt;/span&gt;
&lt;/p&gt;
&lt;div class="csharpcode-wrapper"&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;
Html.DropDownList(&lt;span class="str"&gt;&amp;quot;TestClass&amp;quot;&lt;/span&gt;, (SelectList)ViewData[&lt;span class="str"&gt;&amp;quot;TestClasses&amp;quot;&lt;/span&gt;])
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;span class="kwrd"&gt;this all works as expected:&lt;/span&gt;
&lt;/p&gt;
&lt;div class="csharpcode-wrapper"&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;
&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;select&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;TestClass&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;TestClass&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;option&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;1&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Name1&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;option&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;option&lt;/span&gt; &lt;span class="attr"&gt;selected&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;selected&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;2&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Name2&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;option&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;option&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;3&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Name3&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;option&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;pre class="alt"&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;select&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;
I noticed, though, that this was not working for all our views. Despite exactly the same pattern, many of our views weren&amp;#39;t rendering the selected attribute.
&lt;/p&gt;
&lt;p&gt;
At some point long ago we built our own binding framework that reflect items and passes found values through. Thus often we have a referenced object which is passed through to the view, and we there have a few helpers that get id&amp;#39;s and appropriate &amp;#39;names&amp;#39;. At the time this seemed fine, in retrospect we&amp;#39;ve painted ourselves into a corner. If we modified the above snippet to something along the lines of:
&lt;/p&gt;
&lt;div class="csharpcode-wrapper"&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;
var list = &lt;span class="kwrd"&gt;new&lt;/span&gt;[] 
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
{   
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    &lt;span class="kwrd"&gt;new&lt;/span&gt; IdName { Id = 1, Name = &lt;span class="str"&gt;&amp;quot;Name1&amp;quot;&lt;/span&gt; }, 
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    &lt;span class="kwrd"&gt;new&lt;/span&gt; IdName { Id = 2, Name = &lt;span class="str"&gt;&amp;quot;Name2&amp;quot;&lt;/span&gt; }, 
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    &lt;span class="kwrd"&gt;new&lt;/span&gt; IdName { Id = 3, Name = &lt;span class="str"&gt;&amp;quot;Name3&amp;quot;&lt;/span&gt; } 
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
};
&lt;/pre&gt;
&lt;pre class="alt"&gt;
var selectList = &lt;span class="kwrd"&gt;new&lt;/span&gt; SelectList(list, &lt;span class="str"&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;, &lt;span class="str"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;, 2);
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
ViewData[&lt;span class="str"&gt;&amp;quot;TestClasses&amp;quot;&lt;/span&gt;] = selectList;
&lt;/pre&gt;
&lt;pre class="alt"&gt;
ViewData[&lt;span class="str"&gt;&amp;quot;TestClass&amp;quot;&lt;/span&gt;] = 3;
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
The select does not render with the option passed in to the SelectList as the selectedValue, but rather uses what is found in ViewData with the same name as the select.
&lt;/p&gt;
&lt;div class="csharpcode-wrapper"&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;
&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;select&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;TestClass&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;TestClass&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;option&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;1&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Name1&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;option&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;option&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;2&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Name2&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;option&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;option&lt;/span&gt; &lt;span class="attr"&gt;selected&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;selected&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;3&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Name3&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;option&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;pre class="alt"&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;select&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;
If what is in ViewData does not correspond to the value, nothing is selected. I played around with this for much longer than I should have before investigating the beta source, which has the following block in SelectExtensions.SelectInternal():
&lt;/p&gt;
&lt;div class="csharpcode-wrapper"&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;
&lt;span class="rem"&gt;// If we haven&amp;#39;t already used ViewData to get the entire list of items then we need to&lt;/span&gt;
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
&lt;span class="rem"&gt;// use the ViewData-supplied value before using the parameter-supplied value.&lt;/span&gt;
&lt;/pre&gt;
&lt;pre class="alt"&gt;
&lt;span class="kwrd"&gt;if&lt;/span&gt; (!usedViewData) {
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    &lt;span class="kwrd"&gt;object&lt;/span&gt; defaultValue;
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (htmlHelper.ViewData.TryGetValue(name, &lt;span class="kwrd"&gt;out&lt;/span&gt; defaultValue)) {
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
        selectList = &lt;span class="kwrd"&gt;new&lt;/span&gt; MultiSelectList(selectList.Items, selectList.DataValueField, selectList.DataTextField,
&lt;/pre&gt;
&lt;pre class="alt"&gt;
            (allowMultiple) ? defaultValue &lt;span class="kwrd"&gt;as&lt;/span&gt; IEnumerable : &lt;span class="kwrd"&gt;new&lt;/span&gt;[] { defaultValue });
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    }
&lt;/pre&gt;
&lt;pre class="alt"&gt;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
The RenderDropDown above eventually calls this method passing in false for usedViewData, and as such the above is always executed. Thus no matter what is passed in to the selectedValue of the SelectList, it will always be overridden by any object in ViewData with the same name as the select.
&lt;/p&gt;
&lt;p&gt;
Now that I understand what&amp;#39;s happening it&amp;#39;s a little easier to bear. I also acknowledge that one could (even should) adopt a practice of ensuring that the object in ViewData with the same key as the desired name of a select should contain value to be marked selected.
&lt;/p&gt;
&lt;p&gt;
But still, if I&amp;#39;ve gone to the effort to set the selectedValue in the SelectList, I&amp;#39;d prefer it to take precedence.
&lt;/p&gt;
&lt;div id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:ea96f717-4ea3-42ab-8388-f06babb7cce3" class="wlWriterSmartContent" style="margin: 0px; padding: 0px; display: inline"&gt;
Technorati Tags: &lt;a rel="tag" href="http://technorati.com/tags/ASP.NET%20MVC"&gt;ASP.NET MVC&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/BenHart/~4/XUplvK3lwmg" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/BenHart/~3/XUplvK3lwmg/post.aspx</link>
      <author>benjamin.hart.nospam@nospam.gmail.com (ben)</author>
      <comments>http://blog.benhartonline.com/post/2008/11/24/ASPNET-MVC-SelectList-selectedValue-Gotcha.aspx#comment</comments>
      <guid isPermaLink="false">http://blog.benhartonline.com/post.aspx?id=fda0c298-3475-42ca-b1f1-b910143c7582</guid>
      <pubDate>Mon, 24 Nov 2008 13:14:00 -0500</pubDate>
      <category>ASP.NET MVC</category>
      <dc:publisher>ben</dc:publisher>
      <pingback:server>http://blog.benhartonline.com/pingback.axd</pingback:server>
      <pingback:target>http://blog.benhartonline.com/post.aspx?id=fda0c298-3475-42ca-b1f1-b910143c7582</pingback:target>
      <slash:comments>341</slash:comments>
      <trackback:ping>http://blog.benhartonline.com/trackback.axd?id=fda0c298-3475-42ca-b1f1-b910143c7582</trackback:ping>
      <wfw:comment>http://blog.benhartonline.com/post/2008/11/24/ASPNET-MVC-SelectList-selectedValue-Gotcha.aspx#comment</wfw:comment>
      <wfw:commentRss>http://blog.benhartonline.com/syndication.axd?post=fda0c298-3475-42ca-b1f1-b910143c7582</wfw:commentRss>
    <feedburner:origLink>http://blog.benhartonline.com/post.aspx?id=fda0c298-3475-42ca-b1f1-b910143c7582</feedburner:origLink></item>
    <item>
      <title>Dependency Inversion for Dummies: Unity and StructureMap</title>
      <description>&lt;p&gt;
This is it. The moment you&amp;#39;ve been waiting for. The series that has you periodically checking back to see if there is another instalment continues (since the frequency of my posts has you questioning whether the feed is working). &lt;em&gt;Dependency Inversion for Dummies&lt;/em&gt; is back...
&lt;/p&gt;
&lt;h4&gt;Previously...&lt;/h4&gt; 
&lt;p&gt;
When we started on this journey, early on, we were empowered with the knowledge of how &lt;a href="http://blog.benhartonline.com/post/2008/09/24/Dependency-Inversion-for-Dummies-Constructor-Injection.aspx"&gt;insidious dependencies can cripple an application&amp;#39;s ability to change&lt;/a&gt;. Despite this alarming revelation, in the spirit of developers knowing that the time of failed software projects must change, we read on. Yes we can.
&lt;/p&gt;
&lt;p&gt;
Rather than despair at this new knowledge we considered that a simple means to reduce this restrictive coupling is constructor injection. We struggled through references to teen chick-rock, arrogant comparisons to blockbuster TV-Series, bad examples, always seeking this better way. Yes we can.
&lt;/p&gt;
&lt;p&gt;
When we then discovered that inverting our dependencies can make for more work in object creation, rather than walking away and dismissing this as a fad, we took the high road to find out &lt;a href="http://blog.benhartonline.com/post/2008/10/12/Dependency-Inversion-for-Dummies-Factories-and-Service-Locators.aspx"&gt;how centralising object creation can help&lt;/a&gt;. Yes we can.
&lt;/p&gt;
&lt;p&gt;
When all seemed lost, when the temptation to ignore the breakthroughs of those that came before us began to overwhelm our new sensibilities, we learned that factories or service locators are not only simple concepts, but remarkably easy to implement and use. Yes we can.
&lt;/p&gt;
&lt;p&gt;
And, when promises of a follow-up were not kept, and when a week became a fortnight, a month, more, did we lose faith in the value of this blog, or did we stand up, with resolve and patience, and understand that Ben Hart is an African, and that in Africa, we work on African Time?
&lt;/p&gt;
&lt;h4&gt;&lt;a href="http://blog.benhartonline.com/image.axd?picture=WindowsLiveWriter/DependencyInversionforDummiesUnityandStr_11DD1/Unity%20and%20StructureMap_2.jpg"&gt;&lt;img style="border: 0px none ; margin: 0px 0px 0px 10px" src="http://blog.benhartonline.com/image.axd?picture=WindowsLiveWriter/DependencyInversionforDummiesUnityandStr_11DD1/Unity%20and%20StructureMap_thumb.jpg" border="0" alt="Unity and StructureMap" width="364" height="484" align="right" /&gt;&lt;/a&gt;Yes we did&lt;/h4&gt; 
&lt;p&gt;
Today we use some tools, and compare them with the simplest of all requirements. This will not be a comparison of features, nor an evaluation of any sort. Don&amp;#39;t leave this settled on an IoC container without investigating them yourself. Take a look at least at &lt;a href="http://code.google.com/p/autofac/"&gt;Autofac&lt;/a&gt;, &lt;a href="http://ninject.org/"&gt;Ninject&lt;/a&gt;, &lt;a href="http://www.castleproject.org/container/index.html"&gt;Windsor&lt;/a&gt; and &lt;a href="http://www.springframework.net/"&gt;Spring.NET&lt;/a&gt; before you make up your mind. Choose the one that works for you, while all similar in the basics, they all bring something different to the party (some bring the whole bar!).
&lt;/p&gt;
&lt;p&gt;
Today we&amp;#39;re just going to look at the tool I use against the one released by Microsoft. The Apache 2 OSS against the MPL. The industry veteran against the newcomer. Hulk Hogan against Toa.
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;&lt;em&gt;[While I generally don&amp;#39;t care much for what people think of me, the thought that anyone might assume that I knew who Toa was before this blog post is too much to bear. This was merely the first image result that had an oldie of WWF (in my time, the kids call it WWE these days) with a newcomer. That&amp;#39;s my story, and I&amp;#39;m sticking to it.]&lt;/em&gt;
&lt;/p&gt;
&lt;h4&gt;Where&amp;#39;s the code?&lt;/h4&gt; 
&lt;p&gt;
Remember last time we had centralised object creation into a simple service locator. It wasn&amp;#39;t all that pretty. It was called by a simple console application that requested an instance of the ITagService in order to retrieve tags. The whole application fits into this tiny box:
&lt;/p&gt;
&lt;div class="csharpcode-wrapper"&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;
&lt;span class="kwrd"&gt;class&lt;/span&gt; Program
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
{
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    &lt;span class="kwrd"&gt;static&lt;/span&gt; void Main(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] args)
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    {
&lt;/pre&gt;
&lt;pre class="alt"&gt;
        ITagService tagService = ServiceLocator.GetInstance&amp;lt;ITagService&amp;gt;();
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
        foreach (&lt;span class="kwrd"&gt;string&lt;/span&gt; tag &lt;span class="kwrd"&gt;in&lt;/span&gt; tagService.GetUniqueTags())
&lt;/pre&gt;
&lt;pre class="alt"&gt;
        {
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
            Console.WriteLine(tag);
&lt;/pre&gt;
&lt;pre class="alt"&gt;
        }
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
        Console.ReadLine();
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    }
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
}
&lt;/pre&gt;
&lt;pre class="alt"&gt;
&amp;nbsp;
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; ServiceLocator
&lt;/pre&gt;
&lt;pre class="alt"&gt;
{
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; T GetInstance&amp;lt;T&amp;gt;()
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    {
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
        &lt;span class="kwrd"&gt;return&lt;/span&gt; (T)GetInstance(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(T));
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    }
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt; GetInstance(Type type)
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    {
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
        &lt;span class="kwrd"&gt;return&lt;/span&gt; GetInstance(type.Name);
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    }
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt; GetInstance(&lt;span class="kwrd"&gt;string&lt;/span&gt; name)
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    {
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
        switch (name)
&lt;/pre&gt;
&lt;pre class="alt"&gt;
        {
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
            &lt;span class="kwrd"&gt;case&lt;/span&gt; &lt;span class="str"&gt;&amp;quot;ITagService&amp;quot;&lt;/span&gt;:
&lt;/pre&gt;
&lt;pre class="alt"&gt;
                &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; UniqueTagService(&lt;span class="kwrd"&gt;new&lt;/span&gt; TagDatabase());
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
        }
&lt;/pre&gt;
&lt;pre class="alt"&gt;
        &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; ArgumentOutOfRangeException(&lt;span class="str"&gt;&amp;quot;name&amp;quot;&lt;/span&gt;, &lt;span class="kwrd"&gt;string&lt;/span&gt;.Format(&lt;span class="str"&gt;&amp;quot;{0} is not a registered type.&amp;quot;&lt;/span&gt;, name));
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    }
&lt;/pre&gt;
&lt;pre class="alt"&gt;
}
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
&amp;nbsp;
&lt;/pre&gt;
&lt;pre class="alt"&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; ITagService
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
{
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    IList&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt; GetUniqueTags();
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    int TagCount();
&lt;/pre&gt;
&lt;pre class="alt"&gt;
}
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
&amp;nbsp;
&lt;/pre&gt;
&lt;pre class="alt"&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; UniqueTagService : ITagService
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
{
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    &lt;span class="kwrd"&gt;private&lt;/span&gt; ITagData _db;
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
&amp;nbsp;
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; UniqueTagService(ITagData database)
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    {
&lt;/pre&gt;
&lt;pre class="alt"&gt;
        _db = database;
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    }
&lt;/pre&gt;
&lt;pre class="alt"&gt;
&amp;nbsp;
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; IList&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt; GetUniqueTags()
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    {
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt;(_db.GetTags().Distinct());
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    }
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
&amp;nbsp;
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; int TagCount()
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    {
&lt;/pre&gt;
&lt;pre class="alt"&gt;
        &lt;span class="kwrd"&gt;return&lt;/span&gt; _db.GetTags().Length;
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    }
&lt;/pre&gt;
&lt;pre class="alt"&gt;
}
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
&amp;nbsp;
&lt;/pre&gt;
&lt;pre class="alt"&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; ITagData
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
{
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    &lt;span class="kwrd"&gt;string&lt;/span&gt;[] GetTags();
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
}
&lt;/pre&gt;
&lt;pre class="alt"&gt;
&amp;nbsp;
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; TagDatabase : ITagData
&lt;/pre&gt;
&lt;pre class="alt"&gt;
{
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt;[] _tags = &lt;span class="kwrd"&gt;new&lt;/span&gt;[] { &lt;span class="str"&gt;&amp;quot;Word1&amp;quot;&lt;/span&gt;, &lt;span class="str"&gt;&amp;quot;Word2&amp;quot;&lt;/span&gt;, &lt;span class="str"&gt;&amp;quot;Word3&amp;quot;&lt;/span&gt;, &lt;span class="str"&gt;&amp;quot;Word1&amp;quot;&lt;/span&gt; };
&lt;/pre&gt;
&lt;pre class="alt"&gt;
&amp;nbsp;
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt;[] GetTags()
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    {
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
        &lt;span class="kwrd"&gt;return&lt;/span&gt; _tags;
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    }
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
Rather than change much else, we&amp;#39;re simply going to extend the service locator to use the new tools in our belt. Notice that our client code is only calling the generic GetInstance&amp;lt;T&amp;gt; method, the overloads were simply to confuse you. So, without any further ado, let&amp;#39;s meet our contestants.
&lt;/p&gt;
&lt;p&gt;
First into the ring: the drum beater, the system cheater, the broadcaster, the drummer... can a get a shout out fooooooorrrrrrrr.......
&lt;/p&gt;
&lt;h4&gt;StructureMap&lt;/h4&gt;
&lt;p&gt;
First up you&amp;#39;ll need to download the latest binaries from the &lt;a href="http://sourceforge.net/projects/structuremap"&gt;sourceforge site&lt;/a&gt; and add a reference to StructureMap.dll. While there are other means to configure, the easiest has to be the internal dsl. The simplest means to use this is the creation of a registry file, and the override of the configure() method.
&lt;/p&gt;
&lt;div class="csharpcode-wrapper"&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; MyRegistry : Registry
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
{
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    &lt;span class="kwrd"&gt;protected&lt;/span&gt; override void configure()
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    {
&lt;/pre&gt;
&lt;pre class="alt"&gt;
        ForRequestedType&amp;lt;ITagService&amp;gt;().TheDefaultIsConcreteType&amp;lt;UniqueTagService&amp;gt;();
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
        ForRequestedType&amp;lt;ITagData&amp;gt;().TheDefaultIsConcreteType&amp;lt;TagDatabase&amp;gt;();
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    }
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
All we&amp;#39;ve really done here is mapped the implementation we desire to the type that will be requested. Next thing is to let StructureMap know about the registry, and wire it into our service locator.
&lt;/p&gt;
&lt;div class="csharpcode-wrapper"&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; ServiceLocator
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
{
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    &lt;span class="kwrd"&gt;static&lt;/span&gt; ServiceLocator()
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    {
&lt;/pre&gt;
&lt;pre class="alt"&gt;
        ObjectFactory.Initialize(c =&amp;gt; c.AddRegistry(&lt;span class="kwrd"&gt;new&lt;/span&gt; MyRegistry()));
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    }
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; T GetInstance&amp;lt;T&amp;gt;()
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    {
&lt;/pre&gt;
&lt;pre class="alt"&gt;
        &lt;span class="kwrd"&gt;return&lt;/span&gt; ObjectFactory.GetInstance&amp;lt;T&amp;gt;();
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    }
&lt;/pre&gt;
&lt;pre class="alt"&gt;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
For simplicities sake I&amp;#39;ve called ObjectFactory.Initialize() from the static constructor of our already static service locator. You&amp;#39;d generally only want to call this once in an application, and this achieves this. Note the lambda adds the registry to the object factory, and that our GetInstance&amp;lt;T&amp;gt; defers to the factory.&amp;nbsp; That&amp;#39;s all that&amp;#39;s required. (Note also that having the service locator wrapping the IoC container is not something I&amp;#39;d recommend, rather just a means to continue without changing any more code.)
&lt;/p&gt;
&lt;p&gt;
More astute readers may wonder HTF this works? All we&amp;#39;ve requested is the ITagService. ITagService has a dependency to ITagData, previously we had to instantiate the TagDataBase and pass it in. WTF is going on here?
&lt;/p&gt;
&lt;p&gt;
That, my friends, is the beauty of auto-wiring. StructureMap takes it upon itself to examine the requested type for dependencies, and creates and passes those in when requested. Just don&amp;#39;t forget to say thank you.
&lt;/p&gt;
&lt;p&gt;
Well that was pretty simple. Now the one I&amp;#39;ve been &lt;em&gt;l&amp;#39;ve&lt;/em&gt; been waiting for. The one I&amp;#39;ve been meaning to look at for ages. The one that at first appalled me as a senseless waste of resources when the existing alternatives were so plentiful. The one that will likely be the most adopted despite its relative youth. Can I get a whoop-whoop foooooorrrrrrr.....
&lt;/p&gt;
&lt;h4&gt;Unity Application Block&lt;/h4&gt;
&lt;p&gt;
First up you need to track down the download of the binaries. If StructureMap is a little modest with its download link, Unity can only be described as coy. Following the links you&amp;#39;ll likely end up &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=2C8B79E7-AE56-4F90-822E-A1E43C49D12E&amp;amp;displaylang=en"&gt;here&lt;/a&gt;. Install the msi, and add a reference to Microsoft.Practices.Unity.dll.
&lt;/p&gt;
&lt;p&gt;
The container in Unity is called UnityContainer (ObjectFactory in StructureMap provides static methods that wrap the Container, but it&amp;#39;s still there). Unity is a breeze to get started with. All we need to do is create the container in our service locator, and register the types.
&lt;/p&gt;
&lt;div class="csharpcode-wrapper"&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; ServiceLocator
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
{
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; IUnityContainer _container;
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
&amp;nbsp;
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    &lt;span class="kwrd"&gt;static&lt;/span&gt; ServiceLocator()
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    {
&lt;/pre&gt;
&lt;pre class="alt"&gt;
        _container = &lt;span class="kwrd"&gt;new&lt;/span&gt; UnityContainer();
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
        _container.RegisterType&amp;lt;ITagService, UniqueTagService&amp;gt;();
&lt;/pre&gt;
&lt;pre class="alt"&gt;
        _container.RegisterType&amp;lt;ITagData, TagDatabase&amp;gt;();
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    }
&lt;/pre&gt;
&lt;pre class="alt"&gt;
&amp;nbsp;
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; T GetInstance&amp;lt;T&amp;gt;()
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    {
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
        &lt;span class="kwrd"&gt;return&lt;/span&gt; _container.Resolve&amp;lt;T&amp;gt;();
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    }
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
F*ck me, that was easy. Finding the download took longer! So easy that I had to find a similar implementation in StructureMap...
&lt;/p&gt;
&lt;div class="csharpcode-wrapper"&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; ServiceLocator
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
{
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Container _container;
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    &lt;span class="kwrd"&gt;static&lt;/span&gt; ServiceLocator()
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    {
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
        _container = &lt;span class="kwrd"&gt;new&lt;/span&gt; Container(x =&amp;gt; 
&lt;/pre&gt;
&lt;pre class="alt"&gt;
        { 
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
            x.ForRequestedType&amp;lt;ITagService&amp;gt;().TheDefaultIsConcreteType&amp;lt;UniqueTagService&amp;gt;();
&lt;/pre&gt;
&lt;pre class="alt"&gt;
            x.ForRequestedType&amp;lt;ITagData&amp;gt;().TheDefaultIsConcreteType&amp;lt;TagDatabase&amp;gt;();
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
        });
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    }
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; T GetInstance&amp;lt;T&amp;gt;()
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    {
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
        &lt;span class="kwrd"&gt;return&lt;/span&gt; _container.GetInstance&amp;lt;T&amp;gt;();
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    }
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
Here we&amp;#39;re accessing the container directly, configured through the constructor. A few more lines of code, but as simple. I&amp;#39;ll likely stick to using registries, but options are useful. (Note this would have taken me longer to discover if not for &lt;a href="http://codebetter.com/blogs/jeremy.miller/default.aspx"&gt;Jeremy D. Miller&lt;/a&gt;&amp;#39;s epic weekend series of posts documenting StructureMap 2.5, this &lt;a href="http://codebetter.com/blogs/jeremy.miller/archive/2008/10/18/using-the-structuremap-container-independent-of-objectfactory.aspx"&gt;one&lt;/a&gt; in particular.)
&lt;/p&gt;
&lt;h4&gt;Wrapping up&lt;/h4&gt;
&lt;p&gt;
Remember that the above is the simplest of uses for a IoC container, but already I hope the value is clear. Next time we&amp;#39;ll look at some of the more advanced uses, and, since I&amp;#39;ve enjoyed my introduction to Unity, maybe even get to know Autofac a little better...
&lt;/p&gt;
&lt;div id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:35e09207-ee3f-43f4-96a1-020a742858e8" class="wlWriterSmartContent" style="margin: 0px; padding: 0px; display: inline"&gt;
Technorati Tags: &lt;a rel="tag" href="http://technorati.com/tags/Design%20Patterns"&gt;Design Patterns&lt;/a&gt;,&lt;a rel="tag" href="http://technorati.com/tags/C#"&gt;C#&lt;/a&gt;,&lt;a rel="tag" href="http://technorati.com/tags/Dependency%20Inversion"&gt;Dependency Inversion&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/BenHart/~4/82UOjBjS3_k" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/BenHart/~3/82UOjBjS3_k/post.aspx</link>
      <author>benjamin.hart.nospam@nospam.gmail.com (ben)</author>
      <comments>http://blog.benhartonline.com/post/2008/11/20/Dependency-Inversion-for-Dummies-Unity-and-StructureMap.aspx#comment</comments>
      <guid isPermaLink="false">http://blog.benhartonline.com/post.aspx?id=d7b2eefa-1055-4937-b5a3-72a8c9b9033f</guid>
      <pubDate>Thu, 20 Nov 2008 14:55:00 -0500</pubDate>
      <category>C#</category>
      <category>Design Patterns</category>
      <dc:publisher>ben</dc:publisher>
      <pingback:server>http://blog.benhartonline.com/pingback.axd</pingback:server>
      <pingback:target>http://blog.benhartonline.com/post.aspx?id=d7b2eefa-1055-4937-b5a3-72a8c9b9033f</pingback:target>
      <slash:comments>886</slash:comments>
      <trackback:ping>http://blog.benhartonline.com/trackback.axd?id=d7b2eefa-1055-4937-b5a3-72a8c9b9033f</trackback:ping>
      <wfw:comment>http://blog.benhartonline.com/post/2008/11/20/Dependency-Inversion-for-Dummies-Unity-and-StructureMap.aspx#comment</wfw:comment>
      <wfw:commentRss>http://blog.benhartonline.com/syndication.axd?post=d7b2eefa-1055-4937-b5a3-72a8c9b9033f</wfw:commentRss>
    <feedburner:origLink>http://blog.benhartonline.com/post.aspx?id=d7b2eefa-1055-4937-b5a3-72a8c9b9033f</feedburner:origLink></item>
    <item>
      <title>NHibernate Starter Kit Enhanced</title>
      <description>&lt;p&gt;
I&amp;#39;ve spent some time today enhancing the NHibernate Starter Kit. My previous &lt;a href="http://blog.benhartonline.com/post/2008/10/21/NHibernate-Starter-Kit.aspx" title="NHibernate Starter Kit"&gt;post describes what the kit is about in greater detail&lt;/a&gt;, so please give it a read. To recap, though, the kit is intended to be a means to get started with NHibernate within minutes. Once installed you&amp;#39;ll have a new project template within Visual Studio that contains all NHibernate binaries, a simple domain project demonstrating some common mappings, a console application that generates a database and some test data, and some example unit tests that tie it all together.
&lt;/p&gt;
&lt;p&gt;
It is not intended to be a demonstration of advanced features of NHibernate, nor a demonstration of any best practices. Once this kit has got you started there are many better resources out there to teach you those. I&amp;#39;ve intentionally kept things as simple as possible.
&lt;/p&gt;
&lt;p&gt;
A common objection to NHibernate is that it&amp;#39;s too complicated. I want this kit to prove that objection wrong.
&lt;/p&gt;
&lt;h4&gt;So what&amp;#39;s changed?&lt;/h4&gt; 
&lt;p&gt;
I wasn&amp;#39;t too delighted about the PersistentEntity class to begin with. I didn&amp;#39;t want anyone to assume that this was a requirement, nor did I want to have to create an enterprise ready class like that found in the &lt;a href="http://code.google.com/p/sharp-architecture/"&gt;S#arp Architecture&lt;/a&gt;. Having a base class for entities in NHibernate does help though, so I tried to keep it as simple as possible.
&lt;/p&gt;
&lt;p&gt;
Unfortunately my class had some issues that would result in confusing bugs. The previous override of object.Equals() looked more or less like this:
&lt;/p&gt;
&lt;div class="csharpcode-wrapper"&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;
PersistentEntity other = obj &lt;span class="kwrd"&gt;as&lt;/span&gt; PersistentEntity;
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
&amp;nbsp;
&lt;/pre&gt;
&lt;pre class="alt"&gt;
&lt;span class="kwrd"&gt;if&lt;/span&gt; (this == other)
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
{
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;true&lt;/span&gt;;
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
}
&lt;/pre&gt;
&lt;pre class="alt"&gt;
&amp;nbsp;
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
&lt;span class="kwrd"&gt;if&lt;/span&gt; (other == null)
&lt;/pre&gt;
&lt;pre class="alt"&gt;
{
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;false&lt;/span&gt;;
&lt;/pre&gt;
&lt;pre class="alt"&gt;
}
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
&amp;nbsp;
&lt;/pre&gt;
&lt;pre class="alt"&gt;
&lt;span class="kwrd"&gt;return&lt;/span&gt; this.Id == other.Id;
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
This was problematic since two transient entities (newly created, and not yet saved to the database) would evaluate as equal. The id of the entity is only assigned once the object has been saved, and thus that last statement would be true for any two unsaved entities! &amp;quot;Thanks for that great starter kit, Ben...&amp;quot;
&lt;/p&gt;
&lt;p&gt;
The next problem was with the naive override of object.GetHashCode():
&lt;/p&gt;
&lt;div class="csharpcode-wrapper"&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; override int GetHashCode()
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
{
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    &lt;span class="kwrd"&gt;return&lt;/span&gt; Id;
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
This works for many situations, but falls flat in others. Firstly (and most seriously), this means that two transient entities will have the same hash code, and once saved they will suddenly have a different hash! Secondly persistent entities of different types have the same id, they too will both have the same hash code.
&lt;/p&gt;
&lt;p&gt;
The default mapping used a hilo generator for id&amp;#39;s. In a nutshell, hilo uses a sequence of unique ids to assign to entities as they are saved. The sequence is tracked using a separate table in the database, and this results in every entity being given a unique id. I like hilo since it gives the flexibility of guids (don&amp;#39;t have to worry about the autoincrement quirks of various databases, for one, nor the constraint of hitting the database to get an id), with the usability of integers (if you ever need to manage the database by hand integers are a lot easier to work with than guids). If anyone changed the mapping away from hilo to one that gave different types the same id, GetHashCode() would start causing problems.
&lt;/p&gt;
&lt;p&gt;
So I&amp;#39;ve reluctantly changed the id of the base class from an integer to an guid. PersistentEntity now has the following implementations (courtesy of Gabriel Schenker&amp;#39;s good &lt;a href="http://nhforge.org/blogs/nhibernate/archive/2008/09/06/identity-field-equality-and-hash-code.aspx" title="Identity Field, Equality and Hash Code"&gt;article up on nhforge&lt;/a&gt;):
&lt;/p&gt;
&lt;div class="csharpcode-wrapper"&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; override bool Equals(&lt;span class="kwrd"&gt;object&lt;/span&gt; obj)
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
{
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    PersistentEntity other = obj &lt;span class="kwrd"&gt;as&lt;/span&gt; PersistentEntity;
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
&amp;nbsp;
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (this == other)
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    {
&lt;/pre&gt;
&lt;pre class="alt"&gt;
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;true&lt;/span&gt;;
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    }
&lt;/pre&gt;
&lt;pre class="alt"&gt;
&amp;nbsp;
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (other == null)
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    {
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;false&lt;/span&gt;;
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    }
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    //Transient entities will both have the same id, &lt;span class="kwrd"&gt;and&lt;/span&gt; &lt;span class="kwrd"&gt;as&lt;/span&gt; such we must check &lt;span class="kwrd"&gt;for&lt;/span&gt; reference equality.
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (this.Id == Guid.Empty || other.Id == Guid.Empty)
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    {
&lt;/pre&gt;
&lt;pre class="alt"&gt;
        &lt;span class="kwrd"&gt;return&lt;/span&gt; ReferenceEquals(this, other);
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    }
&lt;/pre&gt;
&lt;pre class="alt"&gt;
&amp;nbsp;
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    &lt;span class="kwrd"&gt;return&lt;/span&gt; this.Id == other.Id;
&lt;/pre&gt;
&lt;pre class="alt"&gt;
}
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
&amp;nbsp;
&lt;/pre&gt;
&lt;pre class="alt"&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; override int GetHashCode()
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
{
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    // Once we have a hash code we&lt;span class="rem"&gt;&amp;#39;ll never change it&lt;/span&gt;
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (_oldHashCode.HasValue)
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    {
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
        &lt;span class="kwrd"&gt;return&lt;/span&gt; _oldHashCode.Value;
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    }
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
&amp;nbsp;
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    // &lt;span class="kwrd"&gt;When&lt;/span&gt; this instance &lt;span class="kwrd"&gt;is&lt;/span&gt; transient, we use the base GetHashCode()
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    // &lt;span class="kwrd"&gt;and&lt;/span&gt; remember it, so an instance can NEVER change its hash code.
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (Id == Guid.Empty)
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    {
&lt;/pre&gt;
&lt;pre class="alt"&gt;
        _oldHashCode = base.GetHashCode();
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
        &lt;span class="kwrd"&gt;return&lt;/span&gt; _oldHashCode.Value;
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    }
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
&amp;nbsp;
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    &lt;span class="kwrd"&gt;return&lt;/span&gt; Id.GetHashCode();
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
This was the right choice. It&amp;#39;s kept PersistentEntity as simple as possible, at the small cost of making some manual DBA work a little harder.
&lt;/p&gt;
&lt;h4&gt;Some more mappings&lt;/h4&gt;
&lt;p&gt;
In the interests of getting started more quickly, I&amp;#39;ve added some more entities and mappings to the starter kit.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.benhartonline.com/image.axd?picture=WindowsLiveWriter/NHibernateStarterKitEnhanced_DE0E/Domain_2.png"&gt;&lt;img style="border: 0px none ; margin: 0px 10px 0px 0px" src="http://blog.benhartonline.com/image.axd?picture=WindowsLiveWriter/NHibernateStarterKitEnhanced_DE0E/Domain_thumb.png" border="0" alt="Domain" width="407" height="341" align="left" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
I&amp;#39;ve thrown together the classes as seen on the left. Again nothing fancy, just a means to illustrate common mappings.
&lt;/p&gt;
&lt;p&gt;
A venue has a one-to-many to courses, which is mapped to cascade=&amp;quot;all&amp;quot;. 
&lt;/p&gt;
&lt;p&gt;
A course has a many-to-one back to the venue, and well as a many-to-many to the students in the course.
&lt;/p&gt;
&lt;p&gt;
A student has a many-to-many to courses.
&lt;/p&gt;
&lt;p&gt;
There are some simple tests that verify some of this behaviour. By no means conclusive, but enough to get started with. 
&lt;/p&gt;
&lt;p&gt;
Picking up the theme yet?
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
I&amp;#39;ve also realised that the template will not work without some effort in Visual Studio 2005. I might address this in future, but for now have limited the vsi to only install to Visual Studio 2008.
&lt;/p&gt;
&lt;p&gt;
To remove the previous version, simply delete the old NHibernateStarterKit.zip file from &amp;quot;[My Documents]\Visual Studio 2008\Templates\ProjectTemplates\Visual C#&amp;quot;, and the corresponding 2005 if applicable.
&lt;/p&gt;
&lt;p&gt;
As before, if you have any suggestions or problems, please contact me any which way you please. 
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.benhartonline.com/file.axd?file=2008%2f11%2fNHibernateStarterKit.vsi"&gt;NHibernateStarterKit.vsi (738.18 kb)&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;div id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:e43d82b3-0071-4549-b9e3-30dbe843427e" class="wlWriterSmartContent" style="margin: 0px; padding: 0px; display: inline"&gt;
Technorati Tags: &lt;a rel="tag" href="http://technorati.com/tags/NHibernate"&gt;NHibernate&lt;/a&gt;,&lt;a rel="tag" href="http://technorati.com/tags/C#"&gt;C#&lt;/a&gt;,&lt;a rel="tag" href="http://technorati.com/tags/Visual%20Studio"&gt;Visual Studio&lt;/a&gt;,&lt;a rel="tag" href="http://technorati.com/tags/VS2008"&gt;VS2008&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/BenHart/~4/PLsp995hb_I" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/BenHart/~3/PLsp995hb_I/post.aspx</link>
      <author>benjamin.hart.nospam@nospam.gmail.com (ben)</author>
      <comments>http://blog.benhartonline.com/post/2008/11/15/NHibernate-Starter-Kit-Enhanced.aspx#comment</comments>
      <guid isPermaLink="false">http://blog.benhartonline.com/post.aspx?id=cf111bef-1908-487a-bb4f-118ed5d905f9</guid>
      <pubDate>Sat, 15 Nov 2008 11:32:00 -0500</pubDate>
      <category>C#</category>
      <category>NHibernate</category>
      <dc:publisher>ben</dc:publisher>
      <pingback:server>http://blog.benhartonline.com/pingback.axd</pingback:server>
      <pingback:target>http://blog.benhartonline.com/post.aspx?id=cf111bef-1908-487a-bb4f-118ed5d905f9</pingback:target>
      <slash:comments>259</slash:comments>
      <trackback:ping>http://blog.benhartonline.com/trackback.axd?id=cf111bef-1908-487a-bb4f-118ed5d905f9</trackback:ping>
      <wfw:comment>http://blog.benhartonline.com/post/2008/11/15/NHibernate-Starter-Kit-Enhanced.aspx#comment</wfw:comment>
      <wfw:commentRss>http://blog.benhartonline.com/syndication.axd?post=cf111bef-1908-487a-bb4f-118ed5d905f9</wfw:commentRss>
    <feedburner:origLink>http://blog.benhartonline.com/post.aspx?id=cf111bef-1908-487a-bb4f-118ed5d905f9</feedburner:origLink></item>
    <item>
      <title>Generic Variance Baby!</title>
      <description>&lt;p&gt;
I&amp;#39;ve been pretty busy lately. The day job is getting interesting, as are a few side projects. A lot has happened the last few weeks, both in my world, and the larger .NET&amp;#39;o&amp;#39;sphere. I haven&amp;#39;t had much time to write to this blog, but I&amp;#39;ve made an effort to keep up with others.
&lt;/p&gt;
&lt;p&gt;
What I&amp;#39;m &lt;em&gt;still&lt;/em&gt; most excited by (yes, it&amp;#39;s been a few weeks of heightened heart-rate) is the introduction of generic variance in C#. I don&amp;#39;t care to admit how many times I&amp;#39;ve written something along the lines of the following:
&lt;/p&gt;
&lt;div class="csharpcode-wrapper"&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; void DoSomething()
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
{
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    IList&amp;lt;Child&amp;gt; children = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;Child&amp;gt;();
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    IList&amp;lt;Parent&amp;gt; parents = children;
&lt;/pre&gt;
&lt;pre class="alt"&gt;
}
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
&lt;span class="kwrd"&gt;class&lt;/span&gt; Parent{}
&lt;/pre&gt;
&lt;pre class="alt"&gt;
&lt;span class="kwrd"&gt;class&lt;/span&gt; Child : Parent{}
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
The above invalidity is obvious (after the first time, at least), but more subtle variations continue to catch me (lambdas spring to mind). It seems so intuitive to be able to assign a generic type of a subclass to that of a superclass. I&amp;#39;ve occasionally refactored to a point relying on something along these lines, before pulling up the handbrake (or is that the shift to reverse?) when the compiler reminds me it&amp;#39;s not valid.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.benhartonline.com/image.axd?picture=WindowsLiveWriter/GenericVarianceBaby_14CE9/GVAvatar_2.png"&gt;&lt;img style="border: 0px none ; margin: 0px 10px 0px 0px" src="http://blog.benhartonline.com/image.axd?picture=WindowsLiveWriter/GenericVarianceBaby_14CE9/GVAvatar_thumb.png" border="0" alt="GVAvatar" width="244" height="216" align="left" /&gt;&lt;/a&gt;But no longer. Well, at least, soon no longer... C# 4.0 introduces generic variance.
&lt;/p&gt;
&lt;p&gt;
I&amp;#39;m not going to add anything to what&amp;#39;s already been written. My only contribution is my little friend on the left here - lovingly created to celebrate the occasion. He represents the wonderful diversity in code that will soon be possible. (Except the t-shirt. That&amp;#39;s chosen because a friend of mine played with &lt;em&gt;My Little Ponies&lt;/em&gt; as a boy, so that&amp;#39;s for him.)
&lt;/p&gt;
&lt;p&gt;
If you want to learn more about generic variance, and be casually able to drop the terms &lt;em&gt;covariant&lt;/em&gt; and &lt;em&gt;contravariant &lt;/em&gt;into the standup tomorrow morning, &lt;a href="http://kohari.org/"&gt;Nate Kohari&lt;/a&gt; has a great summary &lt;a href="http://kohari.org/2008/10/28/generic-variance-in-c-40/"&gt;in this post&lt;/a&gt;. Alternatively, &lt;a href="http://www.microsoft.com/downloads/details.aspx?familyid=922b4655-93d0-4476-bda4-94cf5f8d4814&amp;amp;displaylang=en&amp;amp;tm"&gt;download the CTP of VS2010&lt;/a&gt;, and shake up that invariance...
&lt;/p&gt;
&lt;div id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:c7a79dad-33e4-4281-9b84-62c97cc9e95d" class="wlWriterSmartContent" style="margin: 0px; padding: 0px; display: inline"&gt;
Technorati Tags: &lt;a rel="tag" href="http://technorati.com/tags/C#4.0"&gt;C#4.0&lt;/a&gt;,&lt;a rel="tag" href="http://technorati.com/tags/C#"&gt;C#&lt;/a&gt;,&lt;a rel="tag" href="http://technorati.com/tags/.NET"&gt;.NET&lt;/a&gt;,&lt;a rel="tag" href="http://technorati.com/tags/VS2010"&gt;VS2010&lt;/a&gt;,&lt;a rel="tag" href="http://technorati.com/tags/Generic%20Variance"&gt;Generic Variance&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/BenHart/~4/ofCb1ZPgTUo" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/BenHart/~3/ofCb1ZPgTUo/post.aspx</link>
      <author>benjamin.hart.nospam@nospam.gmail.com (ben)</author>
      <comments>http://blog.benhartonline.com/post/2008/11/13/Generic-Variance-Baby!.aspx#comment</comments>
      <guid isPermaLink="false">http://blog.benhartonline.com/post.aspx?id=ac3e6870-af70-4eb4-966d-428835ee21db</guid>
      <pubDate>Thu, 13 Nov 2008 15:40:00 -0500</pubDate>
      <category>.NET</category>
      <category>C#</category>
      <dc:publisher>ben</dc:publisher>
      <pingback:server>http://blog.benhartonline.com/pingback.axd</pingback:server>
      <pingback:target>http://blog.benhartonline.com/post.aspx?id=ac3e6870-af70-4eb4-966d-428835ee21db</pingback:target>
      <slash:comments>67</slash:comments>
      <trackback:ping>http://blog.benhartonline.com/trackback.axd?id=ac3e6870-af70-4eb4-966d-428835ee21db</trackback:ping>
      <wfw:comment>http://blog.benhartonline.com/post/2008/11/13/Generic-Variance-Baby!.aspx#comment</wfw:comment>
      <wfw:commentRss>http://blog.benhartonline.com/syndication.axd?post=ac3e6870-af70-4eb4-966d-428835ee21db</wfw:commentRss>
    <feedburner:origLink>http://blog.benhartonline.com/post.aspx?id=ac3e6870-af70-4eb4-966d-428835ee21db</feedburner:origLink></item>
    <item>
      <title>SPAM!!!</title>
      <description>&lt;p&gt;I feel like my blog has come of age.&lt;/p&gt; &lt;p&gt;While I've been too busy on personal life to write even so much as an apology post, some sneaky bot managed to get some medication advertising as a comment on an old post.&lt;/p&gt; &lt;p&gt;Nice. And here I was thinking my readers were getting a rise from my good writing alone...&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BenHart/~4/kdoIqENgIsY" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/BenHart/~3/kdoIqENgIsY/post.aspx</link>
      <author>benjamin.hart.nospam@nospam.gmail.com (ben)</author>
      <comments>http://blog.benhartonline.com/post/2008/11/11/SPAM!!!.aspx#comment</comments>
      <guid isPermaLink="false">http://blog.benhartonline.com/post.aspx?id=9b3ff546-76ce-4f44-97a5-5196512da87a</guid>
      <pubDate>Tue, 11 Nov 2008 15:19:21 -0500</pubDate>
      <category>Blogging</category>
      <dc:publisher>ben</dc:publisher>
      <pingback:server>http://blog.benhartonline.com/pingback.axd</pingback:server>
      <pingback:target>http://blog.benhartonline.com/post.aspx?id=9b3ff546-76ce-4f44-97a5-5196512da87a</pingback:target>
      <slash:comments>58</slash:comments>
      <trackback:ping>http://blog.benhartonline.com/trackback.axd?id=9b3ff546-76ce-4f44-97a5-5196512da87a</trackback:ping>
      <wfw:comment>http://blog.benhartonline.com/post/2008/11/11/SPAM!!!.aspx#comment</wfw:comment>
      <wfw:commentRss>http://blog.benhartonline.com/syndication.axd?post=9b3ff546-76ce-4f44-97a5-5196512da87a</wfw:commentRss>
    <feedburner:origLink>http://blog.benhartonline.com/post.aspx?id=9b3ff546-76ce-4f44-97a5-5196512da87a</feedburner:origLink></item>
    <item>
      <title>Repeat after me, "check yourself before giving bad advice"</title>
      <description>&lt;p&gt;
Some time ago I started to give some advice to someone on the &lt;a href="http://tech.groups.yahoo.com/group/altdotnet/"&gt;alt.net mailing list&lt;/a&gt; regarding converting an ASP.NET 1.1 web site to ASP.NET 2.0. I&amp;#39;d gone through this some time before that, and thought I could share some pointers. Another reader had suggested he skip 2.0, and bring it into 3.5, which made sense.
&lt;/p&gt;
 
&lt;p&gt;
I don&amp;#39;t really use the Web Site template (or is it a model?). Having come from the early days, Web Application was a lot more familiar, and I (thankfully) bridged into 2.0 after the initial release of the separate installer. I&amp;#39;ve since played around a little with the Web Site, but the Web Application will remain the only one I&amp;#39;d choose. Not sure if that&amp;#39;s resistance to change (since I&amp;#39;ve never really taken the time to understand the real differences) or just my natural knack to pick the best model.
&lt;/p&gt;
 
&lt;p&gt;
To cut a long story short, I couldn&amp;#39;t understand the problems he was having converting the now Web Site (he&amp;#39;d rolled into VS 2008) into a Web Application. Dude, just right-click the site, and select &amp;quot;Convert to Web Application&amp;quot;. No, really, dude, while it was a while ago, I&amp;#39;ve been through this. Just right-click the site, and select &amp;quot;Convert to Web Application&amp;quot;. This carried on for a few more mails, before I presumed he&amp;#39;d finally seen the context menu item. It turns out his silence was more likely his giving up in frustration (or disgust).
&lt;/p&gt;
 
&lt;p&gt;
I&amp;#39;m currently playing around with the &lt;a href="http://forums.asp.net/t/1318180.aspx"&gt;Flixon site generator&lt;/a&gt; (which has a home elsewhere, the forum announcement seems to pip it in Google page rank, though), which generates a ASP.NET Web Site. Being more comfortable with Web Applications (and not wanting to learn how to reference Web Site code from a separate assembly for testing), I thought I&amp;#39;d just convert it to a Web Application. So just right-click, and select &amp;quot;Convert to Web Application&amp;quot;. Hmm. Ok. Just right-click and select &amp;quot;Convert to Web Application&amp;quot;. Hmmmmmmmm, that&amp;#39;s weird. That option doesn&amp;#39;t exist. 
&lt;/p&gt;
 
&lt;p&gt;
Turns out you can only convert a Web Application to a Web Application (as ridiculous as that might sound). The first step is to copy and paste all files from the Web Site to a freshly created Web Application. The rest of the steps are best described by &lt;a href="http://weblogs.asp.net/meligy/"&gt;Mohamed Meligy&lt;/a&gt; in this &lt;a href="http://weblogs.asp.net/meligy/archive/2008/08/03/converting-vs-2008-website-to-web-application.aspx"&gt;post&lt;/a&gt;. I think (but I&amp;#39;m not certain) that the same applies to 2.0, but who knows.
&lt;/p&gt;
 
&lt;p&gt;
Sorry about the bad advice, guy. Next time I promise I&amp;#39;ll check before harping on.
&lt;/p&gt;
 
&lt;div id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:c6f4cf1a-b3a1-42bb-976c-4c181ab7c4b3" class="wlWriterSmartContent" style="margin: 0px; padding: 0px; display: inline"&gt;
Technorati Tags: &lt;a rel="tag" href="http://technorati.com/tags/ASP.NET"&gt;ASP.NET&lt;/a&gt;,&lt;a rel="tag" href="http://technorati.com/tags/VS2008"&gt;VS2008&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/BenHart/~4/U5OF-YnWNFI" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/BenHart/~3/U5OF-YnWNFI/post.aspx</link>
      <author>benjamin.hart.nospam@nospam.gmail.com (ben)</author>
      <comments>http://blog.benhartonline.com/post/2008/11/03/Repeat-after-me-check-yourself-before-giving-bad-advice.aspx#comment</comments>
      <guid isPermaLink="false">http://blog.benhartonline.com/post.aspx?id=f8f3b957-3a0f-4e92-b796-b0518fe82d39</guid>
      <pubDate>Mon, 03 Nov 2008 12:59:00 -0500</pubDate>
      <category>.NET</category>
      <category>ASP.NET</category>
      <dc:publisher>ben</dc:publisher>
      <pingback:server>http://blog.benhartonline.com/pingback.axd</pingback:server>
      <pingback:target>http://blog.benhartonline.com/post.aspx?id=f8f3b957-3a0f-4e92-b796-b0518fe82d39</pingback:target>
      <slash:comments>72</slash:comments>
      <trackback:ping>http://blog.benhartonline.com/trackback.axd?id=f8f3b957-3a0f-4e92-b796-b0518fe82d39</trackback:ping>
      <wfw:comment>http://blog.benhartonline.com/post/2008/11/03/Repeat-after-me-check-yourself-before-giving-bad-advice.aspx#comment</wfw:comment>
      <wfw:commentRss>http://blog.benhartonline.com/syndication.axd?post=f8f3b957-3a0f-4e92-b796-b0518fe82d39</wfw:commentRss>
    <feedburner:origLink>http://blog.benhartonline.com/post.aspx?id=f8f3b957-3a0f-4e92-b796-b0518fe82d39</feedburner:origLink></item>
    <item>
      <title>IEC Website WTF</title>
      <description>&lt;p&gt;
Here in sunny South Africa we&amp;#39;re also gearing up for elections. Granted, not quite with the international significance as those over in the US, but it could be argued that my local government is just as likely to affect my day to day existence.
&lt;/p&gt;
 
&lt;p&gt;
I&amp;#39;m obviously registered as a voter, but I wanted to check on Friday where, having recently moved district. Conveniently our local &lt;a href="http://www.elections.org.za/"&gt;Independent Electoral Commission has a site&lt;/a&gt; you can enter your details, and check your registration status. I popped on over, and got this message:
&lt;/p&gt;
 
&lt;p&gt;
&lt;a href="http://blog.benhartonline.com/image.axd?picture=WindowsLiveWriter/IECWebsiteWTF_E17A/IECHome_2.png"&gt;&lt;img style="border: 0px none " src="http://blog.benhartonline.com/image.axd?picture=WindowsLiveWriter/IECWebsiteWTF_E17A/IECHome_thumb.png" border="0" alt="IECHome" width="644" height="245" /&gt;&lt;/a&gt; 
&lt;/p&gt;
 
&lt;p&gt;
I haven&amp;#39;t seen that for quite some time. Brought me right back to the turn of the millennium. It&amp;#39;s clearly been updated recently (notice the mention of Chrome), but hell&amp;#39;s teeth. I luckily use windows and have the option of IE (in fairness, they do mention IE 4 and greater, so in theory some adventurous mac users could have a go at it). But we&amp;#39;re in a country whose &lt;a href="http://www.oss.gov.za/"&gt;government has openly embraced open source software&lt;/a&gt;. We&amp;#39;re the birthplace and oftentimes home of he who is called &lt;a href="http://en.wikipedia.org/wiki/Mark_Shuttleworth" title="Mark Shuttleworth"&gt;Mark&lt;/a&gt;, arguably the one most likely to take &lt;a href="http://www.ubuntu.com/" title="Ubuntu"&gt;Linux to the masses&lt;/a&gt;.
&lt;/p&gt;
 
&lt;p&gt;
I took a look at the output html, and, in and amongst all the scary ASP.NET-and-Web-Forms-Designer-bastard-child HTML I think I found the reason non-IE browsers are not supported, called on load of the body:
&lt;/p&gt;
 
&lt;div class="csharpcode-wrapper"&gt;
 
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;
&amp;lt;script language=&lt;span class="str"&gt;&amp;quot;vbscript&amp;quot;&lt;/span&gt;&amp;gt; 
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
&lt;span class="kwrd"&gt;Sub&lt;/span&gt; ShowMsg()
&lt;/pre&gt;
&lt;pre class="alt"&gt;
        
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    &lt;span class="kwrd"&gt;if&lt;/span&gt; len(trim(&lt;span class="str"&gt;&amp;quot;&amp;quot;&lt;/span&gt;)) &amp;gt; 1 &lt;span class="kwrd"&gt;then&lt;/span&gt; 
&lt;/pre&gt;
&lt;pre class="alt"&gt;
        msgbox(&lt;span class="str"&gt;&amp;quot;&amp;quot;&lt;/span&gt;)
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    &lt;span class="kwrd"&gt;end&lt;/span&gt; &lt;span class="kwrd"&gt;if&lt;/span&gt; 
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
    
&lt;/pre&gt;
&lt;pre class="alt"&gt;
    
&lt;/pre&gt;
&lt;pre class="alteven"&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;sub&lt;/span&gt; 
&lt;/pre&gt;
&lt;pre class="alt"&gt;
&amp;lt;/script&amp;gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
Now I&amp;#39;m no expert in vbscript (I missed that bus, and its route seems to have been cancelled), but my powers of deduction tell me that this one doesn&amp;#39;t do much. Did anyone think to check the output html? Did anyone peruse the code that resulted in this? Perhaps a code review is too much to ask for, but having a developer on the team who leaves this in all the way to production is more a liability than an asset.
&lt;/p&gt;
&lt;p&gt;
Come on guys, sort it out. This is important. I&amp;#39;m happy to lend a hand - drop me a line, I&amp;#39;ll happily donate a few hours a week.
&lt;/p&gt;
&lt;div id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:7ebcf276-02d0-4f23-b5f4-57be57eb89be" class="wlWriterSmartContent" style="margin: 0px; padding: 0px; display: inline"&gt;
Technorati Tags: &lt;a rel="tag" href="http://technorati.com/tags/IEC"&gt;IEC&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/BenHart/~4/pt5M_Pd0vQA" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/BenHart/~3/pt5M_Pd0vQA/post.aspx</link>
      <author>benjamin.hart.nospam@nospam.gmail.com (ben)</author>
      <comments>http://blog.benhartonline.com/post/2008/11/02/IEC-Website-WTF.aspx#comment</comments>
      <guid isPermaLink="false">http://blog.benhartonline.com/post.aspx?id=c3b26535-c973-42a0-985f-2a2f91b06542</guid>
      <pubDate>Sun, 02 Nov 2008 08:02:00 -0500</pubDate>
      <category>.NET</category>
      <category>Life</category>
      <category>South Africa</category>
      <dc:publisher>ben</dc:publisher>
      <pingback:server>http://blog.benhartonline.com/pingback.axd</pingback:server>
      <pingback:target>http://blog.benhartonline.com/post.aspx?id=c3b26535-c973-42a0-985f-2a2f91b06542</pingback:target>
      <slash:comments>68</slash:comments>
      <trackback:ping>http://blog.benhartonline.com/trackback.axd?id=c3b26535-c973-42a0-985f-2a2f91b06542</trackback:ping>
      <wfw:comment>http://blog.benhartonline.com/post/2008/11/02/IEC-Website-WTF.aspx#comment</wfw:comment>
      <wfw:commentRss>http://blog.benhartonline.com/syndication.axd?post=c3b26535-c973-42a0-985f-2a2f91b06542</wfw:commentRss>
    <feedburner:origLink>http://blog.benhartonline.com/post.aspx?id=c3b26535-c973-42a0-985f-2a2f91b06542</feedburner:origLink></item>
    <item>
      <title>NHibernate Starter Kit</title>
      <description>&lt;p&gt;
It took me a while to get into NHibernate. I knew it was an awesome tool, with an active community, but it always seemed simpler to just roll-my-own before taking the time to learn a proper ORM. I think part of the problem was accessibility. Coming from a Microsoft development world one gets used to running installers and adding templates to try new products out.
&lt;/p&gt;
&lt;p&gt;
Despite the wealth of &lt;a href="http://www.google.com/search?q=nhibernate+tutorial"&gt;tutorials&lt;/a&gt;, &lt;a href="http://ayende.com/hibernating-rhinos.aspx"&gt;screen&lt;/a&gt; &lt;a href="http://www.summerofnhibernate.com/"&gt;casts&lt;/a&gt;, a pretty thorough &lt;a href="http://nhforge.org/doc/nh/en/index.html"&gt;reference&lt;/a&gt;, easily available &lt;a href="http://sourceforge.net/projects/nhibernate"&gt;source code&lt;/a&gt;, and a vibrant &lt;a href="http://nhforge.org/"&gt;community&lt;/a&gt;, many people still have the impression that NHibernate is hard. It seems that those extra 4 steps to get started (download the latest binary package, extract and add references to NHibernate, add &amp;quot;nhibernate.cfg.xml&amp;quot;, and add entity mappings) is enough to deter people, myself included for quite some time.
&lt;/p&gt;
&lt;h4&gt;But why go through this?&lt;/h4&gt; 
&lt;p&gt;
Once I broke through the initial resistance, I&amp;#39;m not turning back. It&amp;#39;s a great ORM, the first that&amp;#39;s entered my world that gets me close to modelling my domain how I&amp;#39;d like it, without much compromise. The ability to throw together some C# classes, test these until I&amp;#39;m happy with their shape and behaviour, and then have a database generated which supports their persistence has liberated me. Most ORMs in the .NET space place too much emphasis on the database, and the temptation to use the tools and wizards has me back in SQL Server Management Studio far more often than I&amp;#39;d like. While I&amp;#39;m not a zealot (at least I try to mention the alternatives when I encourage NHibernate), I do feel more people need this liberation from the database. As a great man once said, 
&lt;/p&gt;
&lt;blockquote&gt;
	&lt;p&gt;
	I have a dream that one day the majority of .NET developers will emancipate themselves from the binds of an inherently cumbersome environment. 
	&lt;/p&gt;
	&lt;p&gt;
	I have a dream that one day these developers will join me in dropping the notion that &amp;quot;business logic classes&amp;quot; are simply filters between the user interface and the database. 
	&lt;/p&gt;
	&lt;p&gt;
	I have a dream that one day even the most hardened of stored procedure advocates will concede that the ability to refactor with ease and unit test in isolation is compelling enough a reason to forever rid themselves of TSQL and its ilk. 
	&lt;/p&gt;
	&lt;p&gt;
	Today I took a small step towards that dream.
	&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h4&gt;Nah, I&amp;#39;m just lazy.&lt;/h4&gt; 
&lt;p&gt;
I often want to create a quick NHibernate based application, generally for a proof of concept, and I&amp;#39;m tired of finding those dll&amp;#39;s on my bloated drive. I&amp;#39;m also tired of copying and pasting the config file, and the plumbing I need to generate a schema.
&lt;/p&gt;
&lt;p&gt;
So I thought I&amp;#39;d save myself some time and create a Visual Studio template that does this for me. In fairness it was a little more of an adventure than I was bargaining for, but in about 20 proofs of concept I should break even. It was interesting to get to know Visual Studio templates, and some of the limitations thereof, so never any time wasted.
&lt;/p&gt;
&lt;p&gt;
When installed (I created a .vsi which is just a zip in disguise, so feel free to unzip it and butcher) a new project template becomes available under Visual C#, &amp;quot;NHibernate Starter Kit&amp;quot;.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.benhartonline.com/image.axd?picture=WindowsLiveWriter/NHibernateStarterKit_14A4/VSProjects_2.png"&gt;&lt;img style="border: 0px none " src="http://blog.benhartonline.com/image.axd?picture=WindowsLiveWriter/NHibernateStarterKit_14A4/VSProjects_thumb.png" border="0" alt="VSProjects" width="584" height="426" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.benhartonline.com/image.axd?picture=WindowsLiveWriter/NHibernateStarterKit_14A4/NHSKSolution_2.png"&gt;&lt;img style="border: 0px none ; margin: 0px 10px 10px 0px" src="http://blog.benhartonline.com/image.axd?picture=WindowsLiveWriter/NHibernateStarterKit_14A4/NHSKSolution_thumb.png" border="0" alt="NHSKSolution" width="172" height="327" align="left" /&gt;&lt;/a&gt;Adding this project creates a solution with three projects, Domain, SchemaGenerator, and Tests. Domain contains a simple base class for entities (by no means required for NHibernate), and an example entity and corresponding mapping.
&lt;/p&gt;
&lt;p&gt;
SchemaGenerator is a console application that uses these mappings to generate a database schema and insert some test data. Rather than create the database from scratch, it assumes that the database as defined in the App.config has already been created.
&lt;/p&gt;
&lt;p&gt;
Tests has an example integration test: creating a new entity, saving to the database, and retrieving it.
&lt;/p&gt;
&lt;p&gt;
I&amp;#39;ve scattered a few comments around the code for luck.
&lt;/p&gt;
&lt;p&gt;
One of the limitations of a multi-project template is the ability to include files that aren&amp;#39;t in a project. Typically we&amp;#39;d all have a lib or dependencies folder that contains common 3rd party libraries, and that was my initial intention. Rather than go the whole hog to get this (which involved implementing IWizard in a class library, signing, GAC&amp;#39;ing, jumping around in frustration), I&amp;#39;ve cheated by placing them in the bin folder, included in the project. Not ideal, but works fine. (Incidentally, the template includes the latest released version of NHibernate, 4.0.1, and &lt;a href="http://nunit.org"&gt;NUnit&lt;/a&gt; 2.4.8. I&amp;#39;m actually not sure whether redistributing these is a problem, so drop me a line if I need to take down).
&lt;/p&gt;
&lt;p&gt;
In theory, you should simply be able to install the template, create a database (simply an empty database), match this to the connection string, run the schema generator and marvel how that table is created for you. You might even want to add a few properties to MyEntity, jack out the corresponding mapping, run that schema generator, and marvel some more how the table changes without that pesky designer. Run the NUnit test for luck too.
&lt;/p&gt;
&lt;p&gt;
But that&amp;#39;s theory, at this stage all I know is that it works on my machine (Visual Studio 2008, SQL Server 2005 Express). Let me know if it works on yours.
&lt;/p&gt;
&lt;p&gt;
I intend to enhance the kit over time, adding more to the sample entity as a reference for mappings, demonstrating common patterns, and so on.
&lt;/p&gt;
&lt;p&gt;
In the meantime, &lt;a href="http://blog.benhartonline.com/file.axd?file=2008%2f10%2fNHibernateStarterKit.vsi"&gt;download the kit&lt;/a&gt;, and free yourself from the database. 
&lt;/p&gt;
&lt;p&gt;
&lt;em&gt;Update: I&amp;#39;ve since updated the kit to include a few more mappings, and sorted out some issues with the persistent entity base class. Please see the updated version &lt;a href="http://blog.benhartonline.com/post/2008/11/15/NHibernate-Starter-Kit-Enhanced.aspx"&gt;here&lt;/a&gt;.&lt;/em&gt;
&lt;/p&gt;
&lt;div id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:81872a09-058b-41b6-9aea-44b11c2ed65c" class="wlWriterSmartContent" style="margin: 0px; padding: 0px; display: inline"&gt;
Technorati Tags: &lt;a rel="tag" href="http://technorati.com/tags/NHibernate"&gt;NHibernate&lt;/a&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;a href="http://blog.benhartonline.com/file.axd?file=2008%2f10%2fNHibernateStarterKit.vsi"&gt;NHibernateStarterKit.vsi (737.61 kb)&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BenHart/~4/Ok4f560loEk" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/BenHart/~3/Ok4f560loEk/post.aspx</link>
      <author>benjamin.hart.nospam@nospam.gmail.com (ben)</author>
      <comments>http://blog.benhartonline.com/post/2008/10/21/NHibernate-Starter-Kit.aspx#comment</comments>
      <guid isPermaLink="false">http://blog.benhartonline.com/post.aspx?id=37616d65-d50b-40a7-89b3-f6c86df801f2</guid>
      <pubDate>Tue, 21 Oct 2008 17:28:00 -0500</pubDate>
      <category>NHibernate</category>
      <dc:publisher>ben</dc:publisher>
      <pingback:server>http://blog.benhartonline.com/pingback.axd</pingback:server>
      <pingback:target>http://blog.benhartonline.com/post.aspx?id=37616d65-d50b-40a7-89b3-f6c86df801f2</pingback:target>
      <slash:comments>181</slash:comments>
      <trackback:ping>http://blog.benhartonline.com/trackback.axd?id=37616d65-d50b-40a7-89b3-f6c86df801f2</trackback:ping>
      <wfw:comment>http://blog.benhartonline.com/post/2008/10/21/NHibernate-Starter-Kit.aspx#comment</wfw:comment>
      <wfw:commentRss>http://blog.benhartonline.com/syndication.axd?post=37616d65-d50b-40a7-89b3-f6c86df801f2</wfw:commentRss>
    <feedburner:origLink>http://blog.benhartonline.com/post.aspx?id=37616d65-d50b-40a7-89b3-f6c86df801f2</feedburner:origLink></item>
    <item>
      <title>Quitting the game</title>
      <description>&lt;p&gt;
I subscribe to &lt;a href="http://www.fourhourworkweek.com/blog/"&gt;Tim Ferris&amp;#39;s&lt;/a&gt; blog for a fresh perspective and different read to my usual development related blogs. He writes on a variety of topics, summarised, I suppose, to living the best life you can with the short time you have allotted. He&amp;#39;s the author of &lt;a href="http://www.amazon.com/4-Hour-Workweek-Escape-Live-Anywhere/dp/0307353133"&gt;The 4-Hour Workweek&lt;/a&gt;, and has often had me questioning the value of the traditional work environment, and the games we play therein.
&lt;/p&gt;
 
&lt;p&gt;
His most recent &lt;a href="http://www.fourhourworkweek.com/blog/2008/10/18/investment-series-preview-the-good-bye-and-f__k-you-letter/"&gt;post&lt;/a&gt; (in fairness a cross-post, I didn&amp;#39;t bother finding the source) is great. In summary, it quotes Andrew Lahde&amp;#39;s (a hedge fund manager) farewell letter, calling it quits after reaping the rewards of the current financial &amp;#39;crisis&amp;#39; (in which he reaped an 866% return in one year). 
&lt;/p&gt;
 
&lt;blockquote&gt;
	 
	&lt;p&gt;
	I will no longer manage money for other people or institutions. I have enough of my own wealth to manage. Some people, who think they have arrived at a reasonable estimate of my net worth, might be surprised that I would call it quits with such a small war chest. That is fine; I am content with my rewards. Moreover, I will let others try to amass nine, ten or eleven figure net worths. Meanwhile, their lives suck. Appointments back to back, booked solid for the next three months, they look forward to their two week vacation in January during which they will likely be glued to their Blackberries or other such devices. What is the point? They will all be forgotten in fifty years anyway. Steve Balmer, Steven Cohen, and Larry Ellison will all be forgotten. I do not understand the legacy thing. Nearly everyone will be forgotten. Give up on leaving your mark. Throw the Blackberry away and enjoy life.
	&lt;/p&gt;
&lt;/blockquote&gt;
Sage advice.&lt;img src="http://feeds.feedburner.com/~r/BenHart/~4/CUekTfywwBY" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/BenHart/~3/CUekTfywwBY/post.aspx</link>
      <author>benjamin.hart.nospam@nospam.gmail.com (ben)</author>
      <comments>http://blog.benhartonline.com/post/2008/10/18/Quitting-the-game.aspx#comment</comments>
      <guid isPermaLink="false">http://blog.benhartonline.com/post.aspx?id=0952d9e9-3a74-4f7f-9772-e4cb150fe66a</guid>
      <pubDate>Sat, 18 Oct 2008 08:25:00 -0500</pubDate>
      <category>Life</category>
      <dc:publisher>ben</dc:publisher>
      <pingback:server>http://blog.benhartonline.com/pingback.axd</pingback:server>
      <pingback:target>http://blog.benhartonline.com/post.aspx?id=0952d9e9-3a74-4f7f-9772-e4cb150fe66a</pingback:target>
      <slash:comments>66</slash:comments>
      <trackback:ping>http://blog.benhartonline.com/trackback.axd?id=0952d9e9-3a74-4f7f-9772-e4cb150fe66a</trackback:ping>
      <wfw:comment>http://blog.benhartonline.com/post/2008/10/18/Quitting-the-game.aspx#comment</wfw:comment>
      <wfw:commentRss>http://blog.benhartonline.com/syndication.axd?post=0952d9e9-3a74-4f7f-9772-e4cb150fe66a</wfw:commentRss>
    <feedburner:origLink>http://blog.benhartonline.com/post.aspx?id=0952d9e9-3a74-4f7f-9772-e4cb150fe66a</feedburner:origLink></item>
  </channel>
</rss>
