<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jimmy Bogard&#039;s Blog</title>
	<atom:link href="http://lostechies.com/jimmybogard/feed/" rel="self" type="application/rss+xml" />
	<link>http://lostechies.com/jimmybogard</link>
	<description>Strong opinions, weakly held</description>
	<lastBuildDate>Tue, 15 May 2012 13:21:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>Mixing async and sync in distributed systems</title>
		<link>http://lostechies.com/jimmybogard/2012/05/15/mixing-async-and-sync-in-distributed-systems/</link>
		<comments>http://lostechies.com/jimmybogard/2012/05/15/mixing-async-and-sync-in-distributed-systems/#comments</comments>
		<pubDate>Tue, 15 May 2012 13:21:28 +0000</pubDate>
		<dc:creator>Jimmy Bogard</dc:creator>
				<category><![CDATA[Domain-Driven Design]]></category>

		<guid isPermaLink="false">http://lostechies.com/jimmybogard/2012/05/15/mixing-async-and-sync-in-distributed-systems/</guid>
		<description><![CDATA[One of the more difficult transitions when moving from a synchronous UI to an inherently async/CQRS-based UI is the burden of figuring out what to do with all these synchronous operations. Especially when dealing with existing systems, users that expect&#160;&#8230; <a href="http://lostechies.com/jimmybogard/2012/05/15/mixing-async-and-sync-in-distributed-systems/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>One of the more difficult transitions when moving from a synchronous UI to an inherently async/CQRS-based UI is the burden of figuring out what to do with all these synchronous operations. Especially when dealing with existing systems, users that expect everything to be synchronous don’t really appreciate what used to tell them success right away now.</p>
<p>The problem really exacerbates itself when too much was happening at once, and the need to break things out to improve throughput requires a different protocol for integration.</p>
<p>Consider going to the grocery store. When you place an item in your basket, it’s inherently a synchronous operation. Either the jar of pickles makes it into your cart, or it doesn’t, there is no middle ground there. If you drop the jar of pickles onto the ground, you don’t wait for some async process to come by and let you know by email, “sorry, it seems there was a problem with your request to add the item to your shopping cart.”</p>
<h3>Blending async in</h3>
<p>Consider another system that requires to you to register with the site, using a local database for “who is registered” and a web service for downstream email communication:</p>
<p><a href="http://lostechies.com/jimmybogard/files/2012/05/image.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lostechies.com/jimmybogard/files/2012/05/image_thumb.png" width="314" height="340"></a></p>
<p>In this system, we block the registration of the user against both the local database and the 3rd party web service. We need to check the local database to make sure the username/email is not already used, but what about that web service? What happens when that web service is slow, or unavailable?</p>
<p>We’ve coupled the availability of our system with a system we don’t own, which is potentially disastrous. In the real-world system that the above diagram is based on, when that 3rd party system goes into scheduled maintenance, the web application is brought down for maintenance too! Not a good user experience by any means. Are we really going to turn users away based on the availability of a downstream service?</p>
<p>But there’s another way – shifting the protocol of how this system is built. Let’s make sure what needs to be synchronous is, what what doesn’t need to be, isn’t. Instead of coupling the web service and local database calls together, let’s separate the two out with messaging:</p>
<p><a href="http://lostechies.com/jimmybogard/files/2012/05/image1.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lostechies.com/jimmybogard/files/2012/05/image_thumb1.png" width="638" height="340"></a></p>
<p>Instead of performing the web service call in the same thread of the UI request, we instead send an asynchronous command as a message to perform the downstream operation. We’ve now decoupled the operation of calling the web service (what doesn’t need to be synchronous) with what does need (registering the user). The downstream operations of registering the user in the 3rd party email provider doesn’t need to happen at the same time.</p>
<p>To put it another way – does it affect the user’s registration if the email service provider rejects the message? No! Instead, that’s likely an administrative operation to figure out what happened, but the user is still successfully registered.</p>
<h3>Defining the boundaries</h3>
<p>When it comes time to figure out what should be synchronous and what shouldn’t, the key is to figure out what information, operations and behaviors your system owns versus are owned by someone else. If you own the UI and the database, then synchronous is a good possibility. If you <em>don’t</em> own the system (like our web service above), then async is a good possibility.</p>
<p>Next time: sync, async and CQRS.</p>
]]></content:encoded>
			<wfw:commentRss>http://lostechies.com/jimmybogard/2012/05/15/mixing-async-and-sync-in-distributed-systems/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Limits of performance optimization</title>
		<link>http://lostechies.com/jimmybogard/2012/05/03/limits-of-performance-optimization/</link>
		<comments>http://lostechies.com/jimmybogard/2012/05/03/limits-of-performance-optimization/#comments</comments>
		<pubDate>Thu, 03 May 2012 13:31:26 +0000</pubDate>
		<dc:creator>Jimmy Bogard</dc:creator>
				<category><![CDATA[Architecture]]></category>

		<guid isPermaLink="false">http://lostechies.com/jimmybogard/2012/05/03/limits-of-performance-optimization/</guid>
		<description><![CDATA[Back in college, where I was an Electrical Engineering undergrad, I had an especially difficult professor for my microcontrollers course. In this course, we would hand-roll assembly language instructions and upload them to the 68HC12 testing board. (Side-note, I never,&#160;&#8230; <a href="http://lostechies.com/jimmybogard/2012/05/03/limits-of-performance-optimization/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><img style="display: inline; float: right" alt="Datasheet Thumbnail" align="right" src="http://www.datasheetarchive.com/ThumbnailsIndexer/Scans-004/tnScans-0088808.jpg">Back in college, where I was an Electrical Engineering undergrad, I had an especially difficult professor for my microcontrollers course. In this course, we would hand-roll assembly language instructions and upload them to the <a href="http://en.wikipedia.org/wiki/Freescale_68HC12">68HC12</a> testing board. (Side-note, I never, EVER want to hand-roll assembly language again. Or hand-compile C code to assembly).</p>
<p>In microcontrollers, onboard memory is a huge limiting factor. Modern devices have lots of available memory, but for embedded devices, your ROM pretty much decides how complicated your program can be.</p>
<p>As part of the course, we were graded on how <em>compact</em> our code was, rather than merely how <em>correct</em> it was. And this makes sense, since compactness can allow for more features/decisions etc.</p>
<p>So we all made reasonable efforts to compact our code using common tricks like shift-operators for multiplication and so on. However, what we didn’t know is that our professor had spent 20-30 years optimizing assembly code for compactness, and our efforts were being graded against his. Any deviation from his solution was a deduction in our grade. </p>
<p>After receiving poor marks, and seeing why, we all as a class reviewed the (his) solution. And wouldn’t you know that while compact, <strong>code optimized to its maximum is nearly impossible to understand or maintain</strong>. No one in the class, viewing the code for the first time, would be able to decipher what it actually did.</p>
<h3>Long-term maintainability</h3>
<p>Since we change code far more often than we write code, optimizing solely for performance can make it difficult or impossible to change that code in the future. In the case of our college course, we were being held to standards that were nearly impossible to reach, let alone understand. <strong>Performance isn’t an accomplishment, it’s a feature</strong>.</p>
<p>It’s a feature that needs to be balanced against all other constraints, like the ability to maintain the code in the future. Highly optimized code often becomes more difficult to understand or comprehend, making it difficult to tweak or refactor in the future.</p>
<p>So when looking at performance optimization, which is many times a necessary endeavor, always keep an eye on the true goal of the performance optimization. How much more optimized does it need to be? What is the threshold for success?</p>
<p>Performance optimization without a clear definition of success just leads down the path of obfuscation and unmaintainability. Optimization does have an upper limit, not only in terms of gains, but of losses in maintainability.</p>
]]></content:encoded>
			<wfw:commentRss>http://lostechies.com/jimmybogard/2012/05/03/limits-of-performance-optimization/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Persisting enumeration classes with NHibernate</title>
		<link>http://lostechies.com/jimmybogard/2012/05/01/persisting-enumeration-classes-with-nhibernate/</link>
		<comments>http://lostechies.com/jimmybogard/2012/05/01/persisting-enumeration-classes-with-nhibernate/#comments</comments>
		<pubDate>Tue, 01 May 2012 13:31:40 +0000</pubDate>
		<dc:creator>Jimmy Bogard</dc:creator>
				<category><![CDATA[Domain-Driven Design]]></category>
		<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://lostechies.com/jimmybogard/2012/05/01/persisting-enumeration-classes-with-nhibernate/</guid>
		<description><![CDATA[As part of my “Crafting Wicked Domain Models” talk, I walk through the concept of enumeration classes, yanked from Java and on Jon Skeet’s list of biggest C# mistakes (or missing features). In my talk, I leave out how to&#160;&#8230; <a href="http://lostechies.com/jimmybogard/2012/05/01/persisting-enumeration-classes-with-nhibernate/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>As part of my “<a href="http://youtu.be/GubLNJL47K8">Crafting Wicked Domain Models</a>” talk, I walk through the concept of <a href="http://lostechies.com/jimmybogard/2008/08/12/enumeration-classes/">enumeration classes</a>, yanked from Java and on Jon Skeet’s <a href="http://vimeo.com/17151234">list of biggest C# mistakes</a> (or missing features). In my talk, I leave out how to bridge the gap from your domain model to an ORM, simply because it’s just out of scope for that talk to address persistence concerns. Besides, the ORM I use these days to persist relational domain models (NHibernate) handles all the crazy cases an more, so I don’t feel like looking at anything else.</p>
<p>But what I leave out of a talk, I can certainly blog about! Suppose we have one of our enumeration classes (available <a href="http://nuget.org/packages/Enumeration">here on NuGet</a>):</p>
<div id="gist-2567716" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">public</span> <span class="k">class</span> <span class="nc">Color</span> <span class="p">:</span> <span class="n">Enumeration</span><span class="p">&lt;</span><span class="n">Color</span><span class="p">&gt;</span></div><div class='line' id='LC2'><span class="p">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">private</span> <span class="nf">Color</span><span class="p">(</span><span class="kt">int</span> <span class="k">value</span><span class="p">,</span> <span class="kt">string</span> <span class="n">displayName</span><span class="p">)</span> </div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">:</span> <span class="k">base</span><span class="p">(</span><span class="k">value</span><span class="p">,</span> <span class="n">displayName</span><span class="p">)</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC7'><br/></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="k">static</span> <span class="k">readonly</span> <span class="n">Color</span> <span class="n">Red</span> </div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">=</span> <span class="k">new</span> <span class="n">Color</span><span class="p">(</span><span class="m">1</span><span class="p">,</span> <span class="s">&quot;Red&quot;</span><span class="p">);</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="k">static</span> <span class="k">readonly</span> <span class="n">Color</span> <span class="n">Blue</span> </div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">=</span> <span class="k">new</span> <span class="n">Color</span><span class="p">(</span><span class="m">2</span><span class="p">,</span> <span class="s">&quot;Blue&quot;</span><span class="p">);</span></div><div class='line' id='LC12'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/2567716/c59d75ddfe5213eb62f74765e7df719424143d8e/Color.cs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/2567716#file_color.cs" style="float:right;margin-right:10px;color:#666">Color.cs</a>
            <a href="https://gist.github.com/2567716">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>When it comes time to persisting this enumeration class, we want to make sure that the database schema uses the integer value as what gets persisted. When it writes, we want the value to persist, and when it reads, we want the correct enumeration value (Red/Blue) to be hydrated.</p>
<p>To do this in NHibernate, we’ll first need a custom type:</p>
<div id="gist-2567876" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">public</span> <span class="k">class</span> <span class="nc">EnumerationType</span><span class="p">&lt;</span><span class="n">T</span><span class="p">&gt;</span> <span class="p">:</span> <span class="n">PrimitiveType</span> <span class="k">where</span> <span class="n">T</span> <span class="p">:</span> <span class="n">Enumeration</span><span class="p">&lt;</span><span class="n">T</span><span class="p">&gt;</span></div><div class='line' id='LC2'><span class="p">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="nf">EnumerationType</span><span class="p">()</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">:</span> <span class="k">base</span><span class="p">(</span><span class="k">new</span> <span class="n">SqlType</span><span class="p">(</span><span class="n">DbType</span><span class="p">.</span><span class="n">Int32</span><span class="p">))</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC7'><br/></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="k">override</span> <span class="kt">object</span> <span class="nf">Get</span><span class="p">(</span><span class="n">IDataReader</span> <span class="n">rs</span><span class="p">,</span> <span class="kt">int</span> <span class="n">index</span><span class="p">)</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">var</span> <span class="n">o</span> <span class="p">=</span> <span class="n">rs</span><span class="p">[</span><span class="n">index</span><span class="p">];</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">var</span> <span class="k">value</span> <span class="p">=</span> <span class="n">Convert</span><span class="p">.</span><span class="n">ToInt32</span><span class="p">(</span><span class="n">o</span><span class="p">);</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="n">Enumeration</span><span class="p">&lt;</span><span class="n">T</span><span class="p">&gt;.</span><span class="n">FromInt32</span><span class="p">(</span><span class="k">value</span><span class="p">);</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC14'><br/></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="k">override</span> <span class="kt">object</span> <span class="nf">Get</span><span class="p">(</span><span class="n">IDataReader</span> <span class="n">rs</span><span class="p">,</span> <span class="kt">string</span> <span class="n">name</span><span class="p">)</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">var</span> <span class="n">ordinal</span> <span class="p">=</span> <span class="n">rs</span><span class="p">.</span><span class="n">GetOrdinal</span><span class="p">(</span><span class="n">name</span><span class="p">);</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="nf">Get</span><span class="p">(</span><span class="n">rs</span><span class="p">,</span> <span class="n">ordinal</span><span class="p">);</span></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC20'><br/></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="k">override</span> <span class="n">Type</span> <span class="n">ReturnedClass</span></div><div class='line' id='LC22'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC23'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">get</span> <span class="p">{</span> <span class="k">return</span> <span class="k">typeof</span><span class="p">(</span><span class="n">T</span><span class="p">);</span> <span class="p">}</span></div><div class='line' id='LC24'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC25'><br/></div><div class='line' id='LC26'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="k">override</span> <span class="kt">object</span> <span class="nf">FromStringValue</span><span class="p">(</span><span class="kt">string</span> <span class="n">xml</span><span class="p">)</span></div><div class='line' id='LC27'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC28'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="kt">int</span><span class="p">.</span><span class="n">Parse</span><span class="p">(</span><span class="n">xml</span><span class="p">);</span></div><div class='line' id='LC29'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC30'><br/></div><div class='line' id='LC31'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="k">override</span> <span class="kt">string</span> <span class="n">Name</span></div><div class='line' id='LC32'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC33'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">get</span> <span class="p">{</span> <span class="k">return</span> <span class="s">&quot;enumeration&quot;</span><span class="p">;</span> <span class="p">}</span></div><div class='line' id='LC34'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC35'><br/></div><div class='line' id='LC36'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="k">override</span> <span class="k">void</span> <span class="nf">Set</span><span class="p">(</span><span class="n">IDbCommand</span> <span class="n">cmd</span><span class="p">,</span> <span class="kt">object</span> <span class="k">value</span><span class="p">,</span> <span class="kt">int</span> <span class="n">index</span><span class="p">)</span></div><div class='line' id='LC37'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC38'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">var</span> <span class="n">parameter</span> <span class="p">=</span> <span class="p">(</span><span class="n">IDataParameter</span><span class="p">)</span><span class="n">cmd</span><span class="p">.</span><span class="n">Parameters</span><span class="p">[</span><span class="n">index</span><span class="p">];</span></div><div class='line' id='LC39'><br/></div><div class='line' id='LC40'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">var</span> <span class="n">val</span> <span class="p">=</span> <span class="p">(</span><span class="n">Enumeration</span><span class="p">&lt;</span><span class="n">T</span><span class="p">&gt;)</span><span class="k">value</span><span class="p">;</span></div><div class='line' id='LC41'><br/></div><div class='line' id='LC42'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">parameter</span><span class="p">.</span><span class="n">Value</span> <span class="p">=</span> <span class="n">val</span><span class="p">.</span><span class="n">Value</span><span class="p">;</span></div><div class='line' id='LC43'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC44'><br/></div><div class='line' id='LC45'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="k">override</span> <span class="kt">string</span> <span class="nf">ObjectToSQLString</span><span class="p">(</span><span class="kt">object</span> <span class="k">value</span><span class="p">,</span> <span class="n">Dialect</span> <span class="n">dialect</span><span class="p">)</span></div><div class='line' id='LC46'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC47'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="k">value</span><span class="p">.</span><span class="n">ToString</span><span class="p">();</span></div><div class='line' id='LC48'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC49'><br/></div><div class='line' id='LC50'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="k">override</span> <span class="n">Type</span> <span class="n">PrimitiveClass</span></div><div class='line' id='LC51'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC52'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">get</span> <span class="p">{</span> <span class="k">return</span> <span class="k">typeof</span><span class="p">(</span><span class="kt">int</span><span class="p">);</span> <span class="p">}</span></div><div class='line' id='LC53'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC54'><br/></div><div class='line' id='LC55'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="k">override</span> <span class="kt">object</span> <span class="n">DefaultValue</span></div><div class='line' id='LC56'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC57'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">get</span> <span class="p">{</span> <span class="k">return</span> <span class="m">0</span><span class="p">;</span> <span class="p">}</span></div><div class='line' id='LC58'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC59'><span class="p">}</span></div><div class='line' id='LC60'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/2567876/9984e19a10c66d54b1e88d4ce42b5a311fadd4e5/EnumerationType.cs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/2567876#file_enumeration_type.cs" style="float:right;margin-right:10px;color:#666">EnumerationType.cs</a>
            <a href="https://gist.github.com/2567876">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>This class is the bridge between our ORM (NHibernate) and our enumeration class. NHibernate is fantastic in its ability to provide easy ways to bridge to value objects. Value objects help avoid primitive obsession, but is only useful if you can actually use them when you’re mapping to your persistence layer.</p>
<p>To instruct NHibernate to use our custom types when reading/writing, the easiest way to do so is with a <a href="https://github.com/jagregory/fluent-nhibernate/wiki/Conventions">Fluent NHibernate convention</a>:</p>
<div id="gist-2567888" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">public</span> <span class="k">class</span> <span class="nc">EnumerationTypeConvention</span> <span class="p">:</span> <span class="n">IPropertyConvention</span><span class="p">,</span> <span class="n">IPropertyConventionAcceptance</span></div><div class='line' id='LC2'><span class="p">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">private</span> <span class="k">static</span> <span class="k">readonly</span> <span class="n">Type</span> <span class="n">OpenType</span> <span class="p">=</span> <span class="k">typeof</span><span class="p">(</span><span class="n">EnumerationType</span><span class="p">&lt;&gt;);</span></div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="k">void</span> <span class="nf">Apply</span><span class="p">(</span><span class="n">IPropertyInstance</span> <span class="n">instance</span><span class="p">)</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">var</span> <span class="n">closedType</span> <span class="p">=</span> <span class="n">OpenType</span><span class="p">.</span><span class="n">MakeGenericType</span><span class="p">(</span><span class="n">instance</span><span class="p">.</span><span class="n">Property</span><span class="p">.</span><span class="n">PropertyType</span><span class="p">);</span></div><div class='line' id='LC8'><br/></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">instance</span><span class="p">.</span><span class="n">CustomType</span><span class="p">(</span><span class="n">closedType</span><span class="p">);</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC11'><br/></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="k">void</span> <span class="nf">Accept</span><span class="p">(</span><span class="n">IAcceptanceCriteria</span><span class="p">&lt;</span><span class="n">IPropertyInspector</span><span class="p">&gt;</span> <span class="n">criteria</span><span class="p">)</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">criteria</span><span class="p">.</span><span class="n">Expect</span><span class="p">(</span><span class="n">x</span> <span class="p">=&gt;</span> <span class="n">IsEnumerationType</span><span class="p">(</span><span class="n">x</span><span class="p">.</span><span class="n">Property</span><span class="p">.</span><span class="n">PropertyType</span><span class="p">));</span></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC16'><br/></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">private</span> <span class="kt">bool</span> <span class="nf">IsEnumerationType</span><span class="p">(</span><span class="n">Type</span> <span class="n">type</span><span class="p">)</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="nf">GetTypeHierarchy</span><span class="p">(</span><span class="n">type</span><span class="p">)</span></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">.</span><span class="n">Where</span><span class="p">(</span><span class="n">t</span> <span class="p">=&gt;</span> <span class="n">t</span><span class="p">.</span><span class="n">IsGenericType</span><span class="p">)</span></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">.</span><span class="n">Select</span><span class="p">(</span><span class="n">t</span> <span class="p">=&gt;</span> <span class="n">t</span><span class="p">.</span><span class="n">GetGenericTypeDefinition</span><span class="p">())</span></div><div class='line' id='LC22'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">.</span><span class="n">Any</span><span class="p">(</span><span class="n">t</span> <span class="p">=&gt;</span> <span class="n">t</span> <span class="p">==</span> <span class="k">typeof</span><span class="p">(</span><span class="n">Enumeration</span><span class="p">&lt;&gt;));</span></div><div class='line' id='LC23'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC24'><br/></div><div class='line' id='LC25'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">private</span> <span class="n">IEnumerable</span><span class="p">&lt;</span><span class="n">Type</span><span class="p">&gt;</span> <span class="n">GetTypeHierarchy</span><span class="p">(</span><span class="n">Type</span> <span class="n">type</span><span class="p">)</span></div><div class='line' id='LC26'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC27'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">while</span> <span class="p">(</span><span class="n">type</span> <span class="p">!=</span> <span class="k">null</span><span class="p">)</span></div><div class='line' id='LC28'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC29'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">yield</span> <span class="k">return</span> <span class="n">type</span><span class="p">;</span></div><div class='line' id='LC30'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">type</span> <span class="p">=</span> <span class="n">type</span><span class="p">.</span><span class="n">BaseType</span><span class="p">;</span></div><div class='line' id='LC31'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC32'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC33'><span class="p">}</span></div><div class='line' id='LC34'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/2567888/7cff98c6f676a86c49ee09c694444bb9ee44cd8c/EnumerationTypeConvention.cs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/2567888#file_enumeration_type_convention.cs" style="float:right;margin-right:10px;color:#666">EnumerationTypeConvention.cs</a>
            <a href="https://gist.github.com/2567888">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>This convention walks the type hierarchy for each property type given, and checks to see if the property type’s type hierarchy is a generic type that closes the open generic type of our enumeration class. Finally, we just need to hook our convention up to <a href="http://www.fluentnhibernate.org/">Fluent NHibernate</a>, but that really depends on how we have Fluent NHibernate hooked up. We don’t have to use Fluent NHibernate to hook up our custom user type, but it’s <em>much</em> easier this way.</p>
<p>With NHibernate, we get the benefits of using Java-style enumeration classes, and have it seamlessly plug in to our persistence layer, which is the whole point of ORMs, right?</p>
]]></content:encoded>
			<wfw:commentRss>http://lostechies.com/jimmybogard/2012/05/01/persisting-enumeration-classes-with-nhibernate/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Custom errors and error detail policy in ASP.NET Web API</title>
		<link>http://lostechies.com/jimmybogard/2012/04/18/custom-errors-and-error-detail-policy-in-asp-net-web-api/</link>
		<comments>http://lostechies.com/jimmybogard/2012/04/18/custom-errors-and-error-detail-policy-in-asp-net-web-api/#comments</comments>
		<pubDate>Wed, 18 Apr 2012 15:33:53 +0000</pubDate>
		<dc:creator>Jimmy Bogard</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[ASP.NET Web API]]></category>

		<guid isPermaLink="false">http://lostechies.com/jimmybogard/2012/04/18/custom-errors-and-error-detail-policy-in-asp-net-web-api/</guid>
		<description><![CDATA[Today Kurt and I were attempting to debug an Web API service we had deployed to a remote machine. The service was returning 500 errors, and for various reasons, we couldn’t just try to do the requests from that deployed&#160;&#8230; <a href="http://lostechies.com/jimmybogard/2012/04/18/custom-errors-and-error-detail-policy-in-asp-net-web-api/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Today Kurt and I were attempting to debug an Web API service we had deployed to a remote machine. The service was returning 500 errors, and for various reasons, we couldn’t just try to do the requests from that deployed box. We wanted to get the full exception details in the response, but we were just seeing blank 500 errors, with no responses.</p>
<p>We first tried the <a href="http://msdn.microsoft.com/en-us/library/h0hfz6fc.aspx">Web.Config setting for custom errors</a>:</p>
<div id="gist-2414073" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nt">&lt;customErrors</span> <span class="na">mode=</span><span class="s">&quot;Off&quot;</span> <span class="nt">/&gt;</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/2414073/80dcdc9f4147d30593b206d2c31805da5484af46/web.config.xml" style="float:right;">view raw</a>
            <a href="https://gist.github.com/2414073#file_web.config.xml" style="float:right;margin-right:10px;color:#666">web.config.xml</a>
            <a href="https://gist.github.com/2414073">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>But this didn’t affect anything. Digging a little further, we found that ASP.NET Web API uses a different configuration for error details being passed along. This is for a couple of reasons; first, the custom errors element in the Web.Config is an ASP.NET thing. It’s something that ASP.NET uses to determine if that yellow screen of death with additional detail should be shown to users. However, ASP.NET Web API is <a href="http://www.asp.net/web-api/overview/hosting-aspnet-web-api/self-host-a-web-api">designed to be self-hosted</a>, <em>outside</em> of ASP.NET and IIS. While the customErrors element affects requests for ASPX and MVC, it does nothing for Web API.</p>
<p>Instead of relying on a lot of XML configuration, Web API uses a lot of programmatic configuration. This helps self hosting, but for changing policies like error detail, we have to change the code, re-compile and re-deploy. To set the error policy in our application, we need to modify our global Web API configuration:</p>
<div id="gist-2414115" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="n">GlobalConfiguration</span><span class="p">.</span><span class="n">Configuration</span><span class="p">.</span><span class="n">IncludeErrorDetailPolicy</span> </div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">=</span> <span class="n">IncludeErrorDetailPolicy</span><span class="p">.</span><span class="n">Always</span><span class="p">;</span></div><div class='line' id='LC3'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/2414115/0373a00db055dc0098c8becde57e32be3f636d94/IncludeErrorDetailPolicy.cs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/2414115#file_include_error_detail_policy.cs" style="float:right;margin-right:10px;color:#666">IncludeErrorDetailPolicy.cs</a>
            <a href="https://gist.github.com/2414115">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>With this mode, requests from any source will get us full exception detail. It’s likely not something we want in production, but nice that it is available. We have three familiar options for the IncludeErrorDetailPolicy setting:</p>
<ul>
<li>LocalOnly (default)</li>
<li>Always</li>
<li>Off</li>
</ul>
<p>These correspond 1:1 to the customErrror policy in their behavior, but in the reverse. The ASP.NET customErrors setting talks about when to custom errors, where Web API is when to display exception details. Same settings, but approaching from opposite ends.</p>
<p>What if we want to just have Web API use whatever setting that our Web.Config uses? We can just read the ASP.NET setting and apply it to our Web API config:</p>
<div id="gist-2414303" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kt">var</span> <span class="n">config</span> <span class="p">=</span> <span class="p">(</span><span class="n">CustomErrorsSection</span><span class="p">)</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">ConfigurationManager</span><span class="p">.</span><span class="n">GetSection</span><span class="p">(</span><span class="s">&quot;system.web/customErrors&quot;</span><span class="p">);</span></div><div class='line' id='LC3'><br/></div><div class='line' id='LC4'><span class="n">IncludeErrorDetailPolicy</span> <span class="n">errorDetailPolicy</span><span class="p">;</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'><span class="k">switch</span> <span class="p">(</span><span class="n">config</span><span class="p">.</span><span class="n">Mode</span><span class="p">)</span></div><div class='line' id='LC7'><span class="p">{</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">case</span> <span class="n">CustomErrorsMode</span><span class="p">.</span><span class="n">RemoteOnly</span><span class="p">:</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">errorDetailPolicy</span> </div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">=</span> <span class="n">IncludeErrorDetailPolicy</span><span class="p">.</span><span class="n">LocalOnly</span><span class="p">;</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">break</span><span class="p">;</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">case</span> <span class="n">CustomErrorsMode</span><span class="p">.</span><span class="n">On</span><span class="p">:</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">errorDetailPolicy</span> </div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">=</span> <span class="n">IncludeErrorDetailPolicy</span><span class="p">.</span><span class="n">Never</span><span class="p">;</span></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">break</span><span class="p">;</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">case</span> <span class="n">CustomErrorsMode</span><span class="p">.</span><span class="n">Off</span><span class="p">:</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">errorDetailPolicy</span> </div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">=</span> <span class="n">IncludeErrorDetailPolicy</span><span class="p">.</span><span class="n">Always</span><span class="p">;</span></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">break</span><span class="p">;</span></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">default</span><span class="p">:</span></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">throw</span> <span class="k">new</span> <span class="nf">ArgumentOutOfRangeException</span><span class="p">();</span></div><div class='line' id='LC22'><span class="p">}</span></div><div class='line' id='LC23'><br/></div><div class='line' id='LC24'><span class="n">GlobalConfiguration</span><span class="p">.</span><span class="n">Configuration</span><span class="p">.</span><span class="n">IncludeErrorDetailPolicy</span> </div><div class='line' id='LC25'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">=</span> <span class="n">errorDetailPolicy</span><span class="p">;</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/2414303/e06d7c8d2b3bb85bc478fab7f1617a4c1873cdaf/DuplicateCustomErrors.cs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/2414303#file_duplicate_custom_errors.cs" style="float:right;margin-right:10px;color:#666">DuplicateCustomErrors.cs</a>
            <a href="https://gist.github.com/2414303">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>With this approach, we just set one policy for custom errors in our Web.Config file, and both our MVC/WebForms and WebAPI requests in the same application use the exact same policy, and we have the ability to change this policy after we deploy.</p>
]]></content:encoded>
			<wfw:commentRss>http://lostechies.com/jimmybogard/2012/04/18/custom-errors-and-error-detail-policy-in-asp-net-web-api/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>ASP.NET Web API, MVC, ViewModels and Formatters</title>
		<link>http://lostechies.com/jimmybogard/2012/04/10/asp-net-web-api-mvc-viewmodels-and-formatters/</link>
		<comments>http://lostechies.com/jimmybogard/2012/04/10/asp-net-web-api-mvc-viewmodels-and-formatters/#comments</comments>
		<pubDate>Tue, 10 Apr 2012 14:06:32 +0000</pubDate>
		<dc:creator>Jimmy Bogard</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[ASP.NET Web API]]></category>

		<guid isPermaLink="false">http://lostechies.com/jimmybogard/2012/04/10/asp-net-web-api-mvc-viewmodels-and-formatters/</guid>
		<description><![CDATA[There are probably a few more terms I can throw in there, but over the past few days, I’ve been struggling to bridge the gap from how I build applications in ASP.NET MVC and how I see folks building them&#160;&#8230; <a href="http://lostechies.com/jimmybogard/2012/04/10/asp-net-web-api-mvc-viewmodels-and-formatters/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>There are probably a few more terms I can throw in there, but over the past few days, I’ve been struggling to bridge the gap from how I build applications in ASP.NET MVC and how I see folks building them in ASP.NET Web API (and other HTTP-centric frameworks).</p>
<p>These days, the de facto standard for building MVC applications looks something like this for GETs:</p>
<p><a href="http://lostechies.com/jimmybogard/files/2012/04/image4.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lostechies.com/jimmybogard/files/2012/04/image_thumb4.png" width="644" height="101"></a></p>
<p>And for POSTs:</p>
<p><a href="http://lostechies.com/jimmybogard/files/2012/04/image5.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lostechies.com/jimmybogard/files/2012/04/image_thumb5.png" width="644" height="104"></a></p>
<p>We’re using the same ViewModel for both GET and POST, where in one case the ViewModel is used in the View to build a form, and in the other case, the exact same model is used on the POST side to model bind from HTTP variables into something we can reason with.</p>
<p>The ViewModel usually isn’t the <a href="http://lostechies.com/jimmybogard/2009/12/03/persistence-model-and-domain-anemia/">persistence model</a>, as that the concerns of what your persistence layer needs often contradict what your presentation layer needs.</p>
<p>In this model, we have many different pieces at play here:</p>
<ul>
<li><strong>Controller</strong> – responsible for deciding what to do with a request. Show a view? Redirect?
<li><strong>ViewModel – </strong>Represents the information model for the View in GET, and a deserialization target in a POST
<li><strong>View – </strong>Responsible for translating a ViewModel into instructions for generating HTML for the view engines
<li><strong>ModelBinder</strong> – Responsible for supplying the input model to a controller action</li>
</ul>
<p>One key piece here I want to highlight is the View. A ViewModel can’t be translated straight to HTML, and shouldn’t. It’s not an object model suited to act as complete instructions on translating the information model to the HTML media type. In fact, you don’t see really any HTML instructions on ViewModels. There are pieces in the ViewModel that can help with HTML generation (like validation attributes), but you don’t see anything like “make this a REALLY HUGE text box”, nor should you. That’s the view’s responsibility.</p>
<p>What’s missing is that View piece in Web API. The piece that takes the model built by a view (that is Media Type-agnostic) and provides instructions on how to then take that into</p>
<h3>Bridging to ASP.NET Web API</h3>
<p>Coming back to ASP.NET Web API, what objects are in play there? We have some familiar, and not so familiar:</p>
<ul>
<li>Controller – Responsible for deciding what to do with a request, at the HTTP level
<li>Media Type Model – Represents the information needed for a specific media type
<li>Formatter – Responsible for translating a media type model to the specific media type</li>
</ul>
<p>What’s bothering me about this model is that I now don’t have that layer to give instructions to the Formatter on <em>how</em> to serialize. Another thing we’ve done here is conflated the responsibilities of the “ViewModel” of this land with not only the <em>information</em> but also <em>instructions.</em></p>
<p>What do I mean by “instructions”? Putting XML serialization attributes on your model created by your API controller action is mixing the media type in with your model. Having JSON mixed around directly in our controller action. Things like this, that have <a href="http://stephenwalther.com/blog/archive/2012/03/05/introduction-to-the-asp-net-web-api.aspx">Json objects created directly in our actions</a>.</p>
<p>I think the above abstractions are the wrong abstractions. We’re coupling the model built inside the controller action with the needs of a generic formatter. We could build a custom formatter, but that’s like building a custom view engine.</p>
<p>Going back to MVC, a View bridges the gap from Resource to View Engine. I believe that a Formatter in Web API is a View Engine. It is a generic translator of instructions into a specific media type (HTML). Formatters attempt to be intelligent on how to build out other media types (JSON, XML etc.), but ultimately, making them <em>too</em> smart means embedding media-specific information into our model. So what’s my thought? I’d like to see something like:</p>
<ul>
<li><strong>Controller – </strong>Responsible for deciding what to do with a request, at the HTTP level
<li><strong>Resource Model</strong> – Represents informational model of the resource, including links (at a conceptual level, not a technical level)
<li><strong>Media Type Template – </strong>Responsible for translating a Resource Model into instructions for a specific Media Type Formatter
<li><strong>Formatter</strong> – Responsible for using the media type template and resource model to build the response</li>
</ul>
<p>Why the change from Media Type Model to Resource Model? I don’t want to couple the model in my controller action to a specific media type, that’s why! The conneg piece of Web API is what’s supposed to determine this. Imagine if you will that the resource model generated from the controller action is checked against media type templates to see what is <em>possible</em> to return back to the client.</p>
<p>You could use custom formatters in Web API, but looking at examples, they’re at the wrong abstraction layer, talking to things straight to streams etc.</p>
<p>Do we need generic Media Type Templates? <strong>No!</strong> When we were building views for different media types in MVC, we built the <strong>exact same model</strong> from the controller action that fed Telerik reports, PDF reports and HTML, <strong>all from the same controller action and model</strong>. The <em>concept</em> of views bridged the gap in each case to take the information model from the controller action and provide instructions to the “media type generator” on how to generate that PDF, report, or HTML view.</p>
<h3>My ideal world</h3>
<p>In my ideal world, my controller action in Web API would care <strong>nothing</strong> about specific media types. It would know how to build my rich, media-type-ignorant resource model, and that’s it. Web API would then try to line up to figure out what potential formatters are available for my resource model, taking into account accept headers and what media type templates (views) are available for this resource model.</p>
<p>Because wouldn’t it be nice if I could build a single resource model that could <em>actually</em> be used with multiple media types, not just JSON or XML? How about JSON, XML and HTML, and PDF? To do so, the responsibilities of the model generated by the view should only contain the informational model of the resource, and not have media type concerns leaking in.</p>
<p>You could have defaults to make things easier, but what’s really missing here in the Web API landscape is the concept of a view (not the MVC-specific implementation). Something that is responsible for providing (optional) instructions on taking the resource model and telling the formatter how to serialize the model. Looking at Formatters how they are today, you <a href="https://github.com/mamund/HypermediaContacts/blob/master/ContactsMediaTypeFormatterXml.cs">have to take over too much to bridge that gap</a>.</p>
<p><strong>But I could be totally off base here.</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://lostechies.com/jimmybogard/2012/04/10/asp-net-web-api-mvc-viewmodels-and-formatters/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>Working with Forks on GitHub or CodePlex</title>
		<link>http://lostechies.com/jimmybogard/2012/04/02/working-with-forks-on-github-or-codeplex/</link>
		<comments>http://lostechies.com/jimmybogard/2012/04/02/working-with-forks-on-github-or-codeplex/#comments</comments>
		<pubDate>Mon, 02 Apr 2012 18:42:58 +0000</pubDate>
		<dc:creator>Jimmy Bogard</dc:creator>
				<category><![CDATA[git]]></category>

		<guid isPermaLink="false">http://lostechies.com/jimmybogard/2012/04/02/working-with-forks-on-github-or-codeplex/</guid>
		<description><![CDATA[So you want to contribute to a Git project on GitHub or CodePlex. The first thing you always do here is create a fork, which is easy enough: After you create your fork, you’ll likely then clone that forked repository&#160;&#8230; <a href="http://lostechies.com/jimmybogard/2012/04/02/working-with-forks-on-github-or-codeplex/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>So you want to contribute to a Git project on GitHub or CodePlex. The first thing you always do here is create a fork, which is easy enough:</p>
<p><a href="http://lostechies.com/jimmybogard/files/2012/04/image.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lostechies.com/jimmybogard/files/2012/04/image_thumb.png" width="250" height="48"></a></p>
<p><a href="http://lostechies.com/jimmybogard/files/2012/04/image1.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lostechies.com/jimmybogard/files/2012/04/image_thumb1.png" width="605" height="82"></a></p>
<p>After you create your fork, you’ll likely then clone that forked repository locally like:</p>
<pre>git clone git@github.com:jbogard/AutoMapper.git</pre>
<p>That’ll create a remote called “origin” that points up to your fork (in this case, on github). Check out the Git book for <a href="http://progit.org/book/ch2-5.html">more info on remotes</a>.</p>
<p>But we’re not quite done. We’ve created a fork, which is its own entire repository, separate from the original “central” repository:</p>
<p><a href="http://lostechies.com/jimmybogard/files/2012/04/image2.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lostechies.com/jimmybogard/files/2012/04/image_thumb2.png" width="464" height="484"></a></p>
<p>In this new distributed reality, we need to be able to work with those upstream repositories effectively.</p>
<h3>Making changes</h3>
<p>If you want to make changes locally, great! But always make those changes in a branch. What we want to do is still be able to pull in any upstream changes, but isolate any contributions we make to the project. In order to do so, we’ll always create a local AND remote branch for any work we do. Our workflow looks like this:</p>
<pre>git checkout -b MyFeature
**work work work &amp; commit changes locally**
git push origin MyFeature</pre>
<p>The reason we want to isolate our custom work from the master branch is that it makes it much, much easier to integrate and keep up to date with upstream changes. An example in GitHub is:</p>
<p><a href="http://lostechies.com/jimmybogard/files/2012/04/image3.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lostechies.com/jimmybogard/files/2012/04/image_thumb3.png" width="325" height="239"></a></p>
<p>I have two branches – the default “master” branch, and one that I’m going to isolate for a pull request. The nice thing about CodePlex and GitHub is that they both allow you to “add” changes in the form of commits to your pull request. By isolating potential pull requests into a separate, dedicated remote branch, we can ensure that we don’t paint ourselves into a corner where we want to pull in upstream changes or work on something else.</p>
<p>Branches – local and remote – isolate work, and both GitHub and CodePlex work well with branches as pull requests.</p>
<p>When you’re done with any upstream changes, and our pull request is either accepted or rejected – kill your local and remote branch:</p>
<pre>git branch -D MyFeature
git push origin :MyFeature</pre>
<p>Once our changes are integrated (if they are), we want to use the upstream repository’s version of our changes.</p>
<h3>Pulling upstream changes</h3>
<p>When you create a fork, you’ll also want a way to keep up to date with the upstream repository. The first thing to do will be to add a remote that points to the upstream repository (conventionally named &#8220;upstream&#8221;):</p>
<pre>git remote add upstream git://github.com/AutoMapper/AutoMapper.git</pre>
<p>With that remote added, we want to fetch upstream, merge all local branches with that repository, and push that to our upstream:</p>
<pre>git checkout master
git fetch upstream
git merge upstream/master (for each upstream branch)
git push origin</pre>
<p>This workflow:</p>
<ol>
<li>Fetches all upstream commits
<li>Merges our local branches (as fast-forward merges) to the upstream&#8217;s repository&#8217;s versions
<li>Synchronizes our fork with upstream changes by pushing our local repository up.</li>
</ol>
<p>It’s a little weird that we have to work with our local repository to synchronize our fork with the upstream repository, but that’s just because it would be a little hard to try and do that directly in GitHub.</p>
<p>The *first* thing you should do after forking a project and cloning that fork locally is add that “upstream” remote. It’s just much, much easier to have it set up early and get into the flow of fetch, merge, push for incorporating upstream changes.</p>
<p>To review, working with forks on GitHub and CodePlex is easy if we do two things:</p>
<ul>
<li>Create an “upstream” remote to represent the repository our fork originated from
<li>Always work in named local and remote branches for any commits we do in our fork</li>
</ul>
<p>For more info, check out <a href="http://help.github.com/">GitHub’s help pages</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://lostechies.com/jimmybogard/2012/04/02/working-with-forks-on-github-or-codeplex/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to fork the ASP.NET Web Stack to GitHub</title>
		<link>http://lostechies.com/jimmybogard/2012/03/29/how-to-fork-the-asp-net-web-stack-to-github/</link>
		<comments>http://lostechies.com/jimmybogard/2012/03/29/how-to-fork-the-asp-net-web-stack-to-github/#comments</comments>
		<pubDate>Thu, 29 Mar 2012 13:26:18 +0000</pubDate>
		<dc:creator>Jimmy Bogard</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[git]]></category>

		<guid isPermaLink="false">http://lostechies.com/jimmybogard/2012/03/29/how-to-fork-the-asp-net-web-stack-to-github/</guid>
		<description><![CDATA[I find GitHub a lot easier to work with for visual diffs etc., so if you’re interested in forking the newly released ASP.NET Web Stack on CodePlex to GitHub, it’s quite simple. Prerequisites: First, get a GitHub account and make&#160;&#8230; <a href="http://lostechies.com/jimmybogard/2012/03/29/how-to-fork-the-asp-net-web-stack-to-github/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I find GitHub a lot easier to work with for visual diffs etc., so if you’re interested in forking the newly released ASP.NET Web Stack on CodePlex to GitHub, it’s quite simple.</p>
<h3>Prerequisites:</h3>
<p>First, get a GitHub account and make sure you’re set up locally. GitHub has a great tutorial here: <a href="http://help.github.com/win-set-up-git/">http://help.github.com/win-set-up-git/</a></p>
<p>Next, just create a dummy repository and make sure you can push/pull OK.</p>
<h3>Forking a CodePlex repository</h3>
<ol>
<li>Create an aspnetwebstack repository on GitHub:<br /><a href="http://lostechies.com/jimmybogard/files/2012/03/image4.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lostechies.com/jimmybogard/files/2012/03/image_thumb4.png" width="485" height="175"></a><br />You can just call it “aspnetwebstack” to make things easy.
<li>Clone the CodePlex repository locally, using the Git bash or Posh-Git
<pre>git clone https://git01.codeplex.com/aspnetwebstack
cd aspnetwebstack</pre>
<li>Create a Git remote for your GitHub repository:
<pre>git remote add github git@github.com:jbogard/aspnetwebstack.git</pre>
<li>Push the aspnetwebstack repository you just cloned up to GitHub:
<pre>git push github master</pre>
<li>Profit!</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://lostechies.com/jimmybogard/2012/03/29/how-to-fork-the-asp-net-web-stack-to-github/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>ASP.NET MVC, Web API, Razor and Open Source and what it means</title>
		<link>http://lostechies.com/jimmybogard/2012/03/28/asp-net-mvc-web-api-razor-and-open-source-and-what-it-means/</link>
		<comments>http://lostechies.com/jimmybogard/2012/03/28/asp-net-mvc-web-api-razor-and-open-source-and-what-it-means/#comments</comments>
		<pubDate>Wed, 28 Mar 2012 13:39:10 +0000</pubDate>
		<dc:creator>Jimmy Bogard</dc:creator>
				<category><![CDATA[Community]]></category>

		<guid isPermaLink="false">http://lostechies.com/jimmybogard/2012/03/28/asp-net-mvc-web-api-razor-and-open-source-and-what-it-means/</guid>
		<description><![CDATA[In case you missed it, a large part of ASP.NET is not only going open source, but will be developed in the open on CodePlex and will accept contributions. Now, ASP.NET MVC has always been open source, but has always&#160;&#8230; <a href="http://lostechies.com/jimmybogard/2012/03/28/asp-net-mvc-web-api-razor-and-open-source-and-what-it-means/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In case you missed it, a large part of ASP.NET is not only going open source, but <a href="http://weblogs.asp.net/scottgu/archive/2012/03/27/asp-net-mvc-web-api-razor-and-open-source.aspx">will be developed in the open on CodePlex and will accept contributions</a>. Now, ASP.NET MVC has always been open source, but has always been developed more or less behind closed doors.</p>
<p><em>Quick side note, to learn more about what “Open Source” means, check out the <a href="http://www.opensource.org/docs/osd">Open Source Definition</a> from the <a href="http://www.opensource.org/">OSI</a>. “Open Source” does not now nor has ever meant “accepts contributions”.</em></p>
<p>In ASP.NET MVC 3, the MVC framework was released under an OSI approved license, but Razor was not. But starting today, you can go to <a href="http://aspnetwebstack.codeplex.com/">CodePlex</a> and not only download the source, but watch as the ASP.NET team commits code on a daily basis.</p>
<p>So how does this affect the products now being developed in the open, and accepting contributions? As a maintainer of AutoMapper, which also went from closed source to open, I’ve felt the challenge of this transition. While I can’t speak for the ASP.NET team, I can offer my perspective on how going OSS affected the development of AutoMapper.</p>
<p><strong>Will the quality of the product be adversely affected because the team accepts contributions?</strong></p>
<p>No. Just because someone has the ability to accept contributions does not mean that anybody’s crappy code will make it in. Code submitted will go through the same review process that all other code commits go through from internal teams.</p>
<p><strong>Will some yahoo be able to sidestep my current feedback channel and take the product in some other (wrong) direction?</strong></p>
<p>Possible, but unlikely. In my experience, large features don’t get submitted as pull requests without intensive discussions with the team. I’ve only received one or two very large features in AutoMapper, and those were after long conversations on GitHub or the mailing list. The discussions are public, and anyone can participate.</p>
<p>Basically, if you submit a large pull request that isn’t on the team’s roadmap, it’s more than likely going to get rejected. I know, because I’ve rejected large pull requests for the same reasons. If a team develops in the open, you don’t go hide in a hole, develop a big feature, then expect it to get rolled in.</p>
<p><strong>Will my pull request get accepted?</strong></p>
<p>I don’t know. I don’t accept every pull request. It’s not even a size thing, sometimes I just don’t like a feature, don’t think it’s needed, or don’t like an implementation. I’ve submitted plenty of patches that got rejected for a wide variety of reasons, but again, OSS is all about the ability to make your own changes, not that your changes will make it back into the fold.</p>
<p><strong>Will someone else’s changes be supported?</strong></p>
<p>Yes. Because the team ultimately makes the call on whether or not code gets into the mainline and released, it’s actually not that important <em>who</em> wrote the code. It really just matters that the team has committed to support whatever code makes it in to the release.</p>
<p><strong>Are we in store for human sacrifice, dogs and cats living together, mass hysteria?</strong></p>
<p>No. But nice Ghostbusters reference.</p>
<p><strong>Can we trust these products now?</strong></p>
<p>I don’t see why not. You’ve never met the team that coded the original products, so it shouldn’t matter who wrote the code, rather that the code is approved.</p>
<p><strong>Is this a Good Thing?</strong></p>
<p>Yes. The biggest benefits I see are that:</p>
<ol>
<li>Small fixes that get lost in Connect issues etc. are much more easily addressed through contributions. The little stuff will get better and more complete.</li>
<li>Bigger changes will get feedback more early from the community. Often, by the time the community gets to see a working product, it’s way past the point of being able to provide substantial feedback.</li>
</ol>
<p><strong>What could go wrong?</strong></p>
<p>It’s not all rainbows and unicorns. The biggest thing is that once you start engaging directly with the community through your code, you’re going to start getting a lot more feedback. Now the feedback is better and more targeted, but it is a different animal.</p>
<p>However, I don’t think it’s going to be a big deal. Right now, the team receives feedback in big batches, after releases, making it quite hard to sift through all those comments at once. By developing in the open, those comments become spread out a lot more throughout the dev cycle. Also, I imagine that much of the feedback received after a release will just go away because things will just be addressed earlier when changes are much easier to make.</p>
<p><strong>Does this mean the Contrib projects are going away?</strong></p>
<p>No. Contrib projects have their place, as a repository of supplemental features that aren’t necessarily a good idea to fold into the main project. The part of the Contrib project that is filling out “missing” features will likely go away, but the “supplemental” parts are still definitely valuable. Also valuable are pieces that build on top of that product and another one, where the core product shouldn’t take that dependency.</p>
<p>As an example, Input Builders would never have made it into the MvcContrib project, as we would have just worked with the MVC team on a common design.</p>
<p><strong>Why CodePlex and not GitHub?</strong></p>
<p>Really? No no no wait. Really? You know, you can mirror the source on GitHub if that’s your style. It’s DISTIRBUTED VCS, ya know. But really? It’s called being a Team Player. And the CodePlex team is (re)committed to make CodePlex a viable place for OSS development. GitHub is still vastly superior, but the level of feedback that the MS teams will start giving the CodePlex team because of this new relationship is also a Good Thing.</p>
<p>In short, this is a Good Thing, and I’m excited that Microsoft is taking this step. It’s a step I never thought Microsoft would take, and just assumed that it would never happen. But it has, and now we can start pushing for other products/tools to be released the same way.</p>
]]></content:encoded>
			<wfw:commentRss>http://lostechies.com/jimmybogard/2012/03/28/asp-net-mvc-web-api-razor-and-open-source-and-what-it-means/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Dallas Day of Dot Net NServiceBus presentation posted</title>
		<link>http://lostechies.com/jimmybogard/2012/03/27/dallas-day-of-dot-net-nservicebus-presentation-posted/</link>
		<comments>http://lostechies.com/jimmybogard/2012/03/27/dallas-day-of-dot-net-nservicebus-presentation-posted/#comments</comments>
		<pubDate>Tue, 27 Mar 2012 14:18:08 +0000</pubDate>
		<dc:creator>Jimmy Bogard</dc:creator>
				<category><![CDATA[NServiceBus]]></category>

		<guid isPermaLink="false">http://lostechies.com/jimmybogard/2012/03/27/dallas-day-of-dot-net-nservicebus-presentation-posted/</guid>
		<description><![CDATA[About a week ago or so I presented at Dallas Day of .NET on Keeping Integration Sane with NServiceBus. I talked about the three main ways I’ve had to integrate with other systems I don’t control, including: Sending files Receiving&#160;&#8230; <a href="http://lostechies.com/jimmybogard/2012/03/27/dallas-day-of-dot-net-nservicebus-presentation-posted/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>About a week ago or so I presented at <a href="http://www.dallasdayofdotnet.com/">Dallas Day of .NET</a> on Keeping Integration Sane with <a href="http://www.nservicebus.com/">NServiceBus</a>. I talked about the three main ways I’ve had to integrate with other systems I don’t control, including:</p>
<ul>
<li>Sending files</li>
<li>Receiving files</li>
<li>Web services</li>
</ul>
<p>And how each of those ways of integrating can be rife with failures. I walked through a scenario of using an <a href="http://www.nservicebus.com/sagas.aspx">NServiceBus Saga</a> to help manage a process that communicates with multiple web services that each depend on each other. The code and slides can be found on my github here:</p>
<p><a href="https://github.com/jbogard/presentations">https://github.com/jbogard/presentations</a></p>
<p>Check it out!</p>
]]></content:encoded>
			<wfw:commentRss>http://lostechies.com/jimmybogard/2012/03/27/dallas-day-of-dot-net-nservicebus-presentation-posted/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Integrating and isolating the container in tests</title>
		<link>http://lostechies.com/jimmybogard/2012/03/19/integrating-and-isolating-the-container-in-tests/</link>
		<comments>http://lostechies.com/jimmybogard/2012/03/19/integrating-and-isolating-the-container-in-tests/#comments</comments>
		<pubDate>Mon, 19 Mar 2012 13:35:14 +0000</pubDate>
		<dc:creator>Jimmy Bogard</dc:creator>
				<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://lostechies.com/jimmybogard/2012/03/19/integrating-and-isolating-the-container-in-tests/</guid>
		<description><![CDATA[In unit tests, an IoC container rarely enters the mix. In integration tests, or more end-to-end tests, I like to use the exact same configuration for the container as I do in production. Recreating production scenarios and environments in tests&#160;&#8230; <a href="http://lostechies.com/jimmybogard/2012/03/19/integrating-and-isolating-the-container-in-tests/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In unit tests, an IoC container rarely enters the mix. In integration tests, or more end-to-end tests, I like to use the exact same configuration for the container as I do in production. Recreating production scenarios and environments in tests helps prevent those launch night bugs where “it works on my machine” but not when the entire app is up and running.</p>
<p>However, not everything needs to be up and running in integration tests. I often want to stub out services on the outermost periphery in my application. Places where I call into things I <em>can’t</em> control, like external web services, still need to be stubbed out in my tests.</p>
<p>This poses a challenge – I want to have a configured container in my tests, but provide test-specific behaviors. I want any custom configuration/stubs to go away after my test is done, and have each test start with a blank slate. So you could do something like this:</p>
<div id="gist-2112066" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">public</span> <span class="k">class</span> <span class="nc">TestClassBaseWithInitialize</span></div><div class='line' id='LC2'><span class="p">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">private</span> <span class="n">IContainer</span> <span class="n">_container</span><span class="p">;</span></div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'><span class="na">    [SetUp]</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="k">void</span> <span class="nf">SetUp</span><span class="p">()</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">_container</span> <span class="p">=</span> <span class="n">Root</span><span class="p">.</span><span class="n">BuildContainer</span><span class="p">();</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/2112066/9c1d1f2aadb9c92a608a70288961beaed00fe056/TestClassBaseWithInitialize.cs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/2112066#file_test_class_base_with_initialize.cs" style="float:right;margin-right:10px;color:#666">TestClassBaseWithInitialize.cs</a>
            <a href="https://gist.github.com/2112066">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>However, each time a test runs, the container is re-initialized. But in our production apps, we initialize the container just once, so why not just initialize once in our tests? The trick is that we need to reset the container to a known state before each test.</p>
<p>The solution here is to use the concept of nested/child containers. In a nested container, its configuration is cloned but isolated from the parent container. We want our tests to use this nested container instead:</p>
<div id="gist-2112149" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">public</span> <span class="k">abstract</span> <span class="k">class</span> <span class="nc">TestClassBase</span></div><div class='line' id='LC2'><span class="p">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">private</span> <span class="k">readonly</span> <span class="n">IContainer</span> <span class="n">_container</span> </div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">=</span> <span class="n">Root</span><span class="p">.</span><span class="n">Container</span><span class="p">.</span><span class="n">GetNestedContainer</span><span class="p">();</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">protected</span> <span class="n">T</span> <span class="n">GetInstance</span><span class="p">&lt;</span><span class="n">T</span><span class="p">&gt;()</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="n">_container</span><span class="p">.</span><span class="n">GetInstance</span><span class="p">&lt;</span><span class="n">T</span><span class="p">&gt;();</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC10'><br/></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">protected</span> <span class="k">void</span> <span class="n">Inject</span><span class="p">&lt;</span><span class="n">T</span><span class="p">&gt;(</span><span class="n">T</span> <span class="n">instance</span><span class="p">)</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">_container</span><span class="p">.</span><span class="n">Configure</span><span class="p">(</span><span class="n">cfg</span> <span class="p">=&gt;</span> <span class="n">cfg</span><span class="p">.</span><span class="n">For</span><span class="p">&lt;</span><span class="n">T</span><span class="p">&gt;().</span><span class="n">Use</span><span class="p">(</span><span class="n">instance</span><span class="p">));</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC15'><span class="p">}</span></div><div class='line' id='LC16'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/2112149/c2355fa5d06c53014b0239886dfb0e4b7b8e83a7/TestClassBase.cs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/2112149#file_test_class_base.cs" style="float:right;margin-right:10px;color:#666">TestClassBase.cs</a>
            <a href="https://gist.github.com/2112149">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Folks using the container need to go through this base GetInstance method, and not go straight to the container. For injecting dummy/stub instances, the Inject method configures just that nested container, and leaves the root container untouched.</p>
<p>If you’re doing a lot of integration/full-system-tests, you can get quite a drop in the time it takes your tests to run if you make sure those things that are expensive to initialize (container/ORM) are initialized only once per entire test suite. Once you have that in place, it’s just a matter of integrating and isolating changes from one test to the other. With nested containers, it’s a cinch.</p>
]]></content:encoded>
			<wfw:commentRss>http://lostechies.com/jimmybogard/2012/03/19/integrating-and-isolating-the-container-in-tests/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

