<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" version="2.0">
  <channel>
    <title>ploeh blog</title>
    <link>http://blog.ploeh.dk/</link>
    <description>Mark Seemann's .NET blog</description>
    <language>en-us</language>
    <copyright>Mark Seemann</copyright>
    <lastBuildDate>Tue, 24 Apr 2012 11:45:41 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>mark@seemann.ms</managingEditor>
    <webMaster>mark@seemann.ms</webMaster>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=29754191-c40d-474a-b063-d00b1154638b</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,29754191-c40d-474a-b063-d00b1154638b.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,29754191-c40d-474a-b063-d00b1154638b.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=29754191-c40d-474a-b063-d00b1154638b</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <title>Vendor Media Types With the ASP.NET Web API</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,29754191-c40d-474a-b063-d00b1154638b.aspx</guid>
      <link>http://blog.ploeh.dk/2012/04/24/VendorMediaTypesWithTheASPNETWebAPI.aspx</link>
      <pubDate>Tue, 24 Apr 2012 11:45:41 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;In RESTful services, media types (e.g. application/xml, application/json) are an important part of Content Negotiation (&lt;em&gt;conneg&lt;/em&gt; in the jargon). This enables an API to provide multiple &lt;em&gt;representations&lt;/em&gt; of the same resource.&lt;/p&gt; &lt;p&gt;Apart from the standard media types such as application/xml, application/json, etc. an API can (and often should, IMO) expose its resources using specialized media types. These often take the form of vendor-specific media types, such as application/vnd.247e.catalog+xml or application/vnd.247e.album+json.&lt;/p&gt; &lt;p&gt;In this article I'll present some initial findings I've made while investigating this in the &lt;a href="http://www.asp.net/web-api"&gt;ASP.NET Web API&lt;/a&gt; (beta).&lt;/p&gt; &lt;p&gt;For an introduction to conneg with the Web API, see &lt;a href="http://weblogs.asp.net/gunnarpeipman/default.aspx"&gt;Gunnar Peipman's ASP.NET blog&lt;/a&gt;, particularly these two posts:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="http://weblogs.asp.net/gunnarpeipman/archive/2012/04/19/asp-net-web-api-how-content-negotiation-works.aspx"&gt;ASP.NET Web API: How content negotiation works?&lt;/a&gt;  &lt;li&gt;&lt;a href="http://weblogs.asp.net/gunnarpeipman/archive/2012/04/20/asp-net-web-api-extending-content-negotiation-with-new-formats.aspx"&gt;ASP.NET Web API: Extending content negotiation with new formats&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;strong&gt;The Problem&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;In a particular RESTful API, I'd like to enable vendor-specific media types as well as the standard application/xml and application/json media types.&lt;/p&gt; &lt;p&gt;More specifically, I'd like to add these media types to the API:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;application/vnd.247e.album+xml  &lt;li&gt;application/vnd.247e.artist+xml  &lt;li&gt;application/vnd.247e.catalog+xml  &lt;li&gt;application/vnd.247e.search-result+xml  &lt;li&gt;application/vnd.247e.track+xml  &lt;li&gt;application/vnd.247e.album+json  &lt;li&gt;application/vnd.247e.artist+json  &lt;li&gt;application/vnd.247e.catalog+json  &lt;li&gt;application/vnd.247e.search-result+json  &lt;li&gt;application/vnd.247e.track+json&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;However, I can't just add all these media types to GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes or GlobalConfiguration.Configuration.Formatters.JsonFormatter.SupportedMediaTypes. If I do that, each and every resource in the API would accept and (claim to) return all of those media types. That's not what I want. Rather, I want specific resources to accept and return specific media types.&lt;/p&gt; &lt;p&gt;For example, if a resource (Controller) returns an instance of the SearchResult (Model) class, it should only accept the media types application/vnd.247e.search-result+xml, application/vnd.247e.search-result+json (as well as the standard application/xml and application/json media types).&lt;/p&gt; &lt;p&gt;Likewise, a resource handling the Album (Model) class should accept and return application/vnd.247e.album+xml and application/vnd.247e.album+json, and so on.&lt;/p&gt; &lt;p&gt;Figuring out how to enable such behavior took me a bit of fiddling (yes, &lt;a href="http://fiddler2.com/fiddler2/"&gt;Fiddler&lt;/a&gt; was involved).&lt;/p&gt; &lt;p&gt;&lt;strong&gt;The Solution&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;The Web API uses a polymorphic collection of &lt;a href="http://msdn.microsoft.com/en-us/library/system.net.http.formatting.mediatypeformatter%28v=vs.108%29.aspx"&gt;MediaTypeFormatter&lt;/a&gt; classes. These classes can be extended to be more specifically targeted at a specific Model class.&lt;/p&gt; &lt;p&gt;For XML formatting, this can be done by deriving from the built-in XmlMediaTypeFormatter class:&lt;/p&gt; &lt;div style="font-family: consolas, 'Courier New'; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;TypedXmlMediaTypeFormatter&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;XmlMediaTypeFormatter&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;readonly&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Type&lt;/span&gt; resourceType;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; TypedXmlMediaTypeFormatter(&lt;span style="color: #2b91af"&gt;Type&lt;/span&gt; resourceType,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MediaTypeHeaderValue&lt;/span&gt; mediaType)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.resourceType = resourceType;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.SupportedMediaTypes.Clear();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.SupportedMediaTypes.Add(mediaType);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;protected&lt;/span&gt; &lt;span style="color: blue"&gt;override&lt;/span&gt; &lt;span style="color: blue"&gt;bool&lt;/span&gt; CanReadType(&lt;span style="color: #2b91af"&gt;Type&lt;/span&gt; type)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.resourceType == type;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;protected&lt;/span&gt; &lt;span style="color: blue"&gt;override&lt;/span&gt; &lt;span style="color: blue"&gt;bool&lt;/span&gt; CanWriteType(&lt;span style="color: #2b91af"&gt;Type&lt;/span&gt; type)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.resourceType == type;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The implementation is quite simple. In the constructor, it makes sure to clear out any existing supported media types and to add only the media type passed in via the constructor.&lt;/p&gt;
&lt;p&gt;The CanReadType and CanWriteType overrides only return true of the type parameter matches the type targeted by the particular TypedXmlMediaTypeFormatter instance. You could say that the TypedXmlMediaTypeFormatter provides a specific match between a media type and a resource Model class.&lt;/p&gt;
&lt;p&gt;The JSON formatter is similar:&lt;/p&gt;
&lt;div style="font-family: consolas, 'Courier New'; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;TypedJsonMediaTypeFormatter&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;JsonMediaTypeFormatter&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;readonly&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Type&lt;/span&gt; resourceType;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; TypedJsonMediaTypeFormatter(&lt;span style="color: #2b91af"&gt;Type&lt;/span&gt; resourceType,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MediaTypeHeaderValue&lt;/span&gt; mediaType)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.resourceType = resourceType;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.SupportedMediaTypes.Clear();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.SupportedMediaTypes.Add(mediaType);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;protected&lt;/span&gt; &lt;span style="color: blue"&gt;override&lt;/span&gt; &lt;span style="color: blue"&gt;bool&lt;/span&gt; CanReadType(&lt;span style="color: #2b91af"&gt;Type&lt;/span&gt; type)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.resourceType == type;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;protected&lt;/span&gt; &lt;span style="color: blue"&gt;override&lt;/span&gt; &lt;span style="color: blue"&gt;bool&lt;/span&gt; CanWriteType(&lt;span style="color: #2b91af"&gt;Type&lt;/span&gt; type)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.resourceType == type;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The only difference from the TypedXmlMediaTypeFormatter class is that this one derives from JsonMediaTypeFormatter instead of XmlMediaTypeFormatter.&lt;/p&gt;
&lt;p&gt;With these two classes available, I can now register all the custom media types in Global.asax like this:&lt;/p&gt;
&lt;div style="font-family: consolas, 'Courier New'; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;GlobalConfiguration&lt;/span&gt;.Configuration.Formatters.Add(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;TypedXmlMediaTypeFormatter&lt;/span&gt;(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Album&lt;/span&gt;),&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MediaTypeHeaderValue&lt;/span&gt;(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"application/vnd.247e.album+xml"&lt;/span&gt;)));&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;GlobalConfiguration&lt;/span&gt;.Configuration.Formatters.Add(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;TypedXmlMediaTypeFormatter&lt;/span&gt;(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Artist&lt;/span&gt;),&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MediaTypeHeaderValue&lt;/span&gt;(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"application/vnd.247e.artist+xml"&lt;/span&gt;)));&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;GlobalConfiguration&lt;/span&gt;.Configuration.Formatters.Add(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;TypedXmlMediaTypeFormatter&lt;/span&gt;(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Catalog&lt;/span&gt;),&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MediaTypeHeaderValue&lt;/span&gt;(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"application/vnd.247e.catalog+xml"&lt;/span&gt;)));&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;GlobalConfiguration&lt;/span&gt;.Configuration.Formatters.Add(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;TypedXmlMediaTypeFormatter&lt;/span&gt;(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;SearchResult&lt;/span&gt;),&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MediaTypeHeaderValue&lt;/span&gt;(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"application/vnd.247e.search-result+xml"&lt;/span&gt;)));&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;GlobalConfiguration&lt;/span&gt;.Configuration.Formatters.Add(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;TypedXmlMediaTypeFormatter&lt;/span&gt;(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Track&lt;/span&gt;),&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MediaTypeHeaderValue&lt;/span&gt;(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"application/vnd.247e.track+xml"&lt;/span&gt;)));&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;GlobalConfiguration&lt;/span&gt;.Configuration.Formatters.Add(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;TypedJsonMediaTypeFormatter&lt;/span&gt;(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Album&lt;/span&gt;),&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MediaTypeHeaderValue&lt;/span&gt;(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"application/vnd.247e.album+json"&lt;/span&gt;)));&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;GlobalConfiguration&lt;/span&gt;.Configuration.Formatters.Add(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;TypedJsonMediaTypeFormatter&lt;/span&gt;(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Artist&lt;/span&gt;),&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MediaTypeHeaderValue&lt;/span&gt;(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"application/vnd.247e.artist+json"&lt;/span&gt;)));&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;GlobalConfiguration&lt;/span&gt;.Configuration.Formatters.Add(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;TypedJsonMediaTypeFormatter&lt;/span&gt;(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Catalog&lt;/span&gt;),&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MediaTypeHeaderValue&lt;/span&gt;(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"application/vnd.247e.catalog+json"&lt;/span&gt;)));&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;GlobalConfiguration&lt;/span&gt;.Configuration.Formatters.Add(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;TypedJsonMediaTypeFormatter&lt;/span&gt;(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;SearchResult&lt;/span&gt;),&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MediaTypeHeaderValue&lt;/span&gt;(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"application/vnd.247e.search-result+json"&lt;/span&gt;)));&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;GlobalConfiguration&lt;/span&gt;.Configuration.Formatters.Add(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;TypedJsonMediaTypeFormatter&lt;/span&gt;(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Track&lt;/span&gt;),&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MediaTypeHeaderValue&lt;/span&gt;(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"application/vnd.247e.track+json"&lt;/span&gt;)));&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is rather repetitive code, but I'll leave it as an exercise to the reader to write a set of conventions that appropriately register the correct media type for a Model class.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Caveats&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Please be aware that I've only tested this with a read-only API. You may need to tweak this solution in order to also handle incoming data.&lt;/p&gt;
&lt;p&gt;As far as I can tell from the &lt;a href="http://aspnetwebstack.codeplex.com/SourceControl/list/changesets"&gt;Web API source repository&lt;/a&gt;, it seems as though there are some breaking changes in the pipeline in this area, so don't bet the farm on this particular solution.&lt;/p&gt;
&lt;p&gt;Lastly, it seems as though this solution doesn't correctly respect opt-out quality parameters in incoming Accept headers. As an example, if I request a 'Catalog' resource, but supply the following Accept header, I'd expect the response to be 406 (Not Acceptable).&lt;/p&gt;
&lt;p&gt;Accept: application/vnd.247e.search-result+xml; q=1, */*; q=0.0&lt;/p&gt;
&lt;p&gt;However, the result is that the service falls back to its default representation, which is application/json. Whether this is a problem with my approach or a bug in the Web API, I haven't investigated.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=29754191-c40d-474a-b063-d00b1154638b"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,29754191-c40d-474a-b063-d00b1154638b.aspx</comments>
      <category>ASP.NET MVC</category>
      <category>REST</category>
      <category>Services</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=1c390f17-cd8a-40bc-8d9c-bad20c6f9880</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,1c390f17-cd8a-40bc-8d9c-bad20c6f9880.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,1c390f17-cd8a-40bc-8d9c-bad20c6f9880.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=1c390f17-cd8a-40bc-8d9c-bad20c6f9880</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <title>Wiring HttpControllerContext With Castle Windsor</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,1c390f17-cd8a-40bc-8d9c-bad20c6f9880.aspx</guid>
      <link>http://blog.ploeh.dk/2012/04/19/WiringHttpControllerContextWithCastleWindsor.aspx</link>
      <pubDate>Thu, 19 Apr 2012 15:14:30 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;In a previous post I demonstrated &lt;a href="http://blog.ploeh.dk/2012/04/17/InjectingHttpControllerContextWithTheASPNETWebAPI.aspx"&gt;how to wire up HttpControllerContext with Poor Man's DI&lt;/a&gt;. In this article I'll show how to wire up HttpControllerContext with Castle Windsor.&lt;/p&gt; &lt;p&gt;This turns out to be remarkably difficult, at least with the constraints I tend to set for myself:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Readers of this blog may have an inkling that I have an absolute abhorrence of static state, so anything relying on that is out of the question.  &lt;li&gt;In addition to that, I also prefer to leverage the container as much as possible, so I'm not keen on duplicating a responsibility that really belongs to the container.  &lt;li&gt;No injecting the container into itself. That's just unnatural and lewd (without being fun).  &lt;li&gt;If possible, the solution should be thread-safe.  &lt;li&gt;The overall solution should still adhere to the &lt;a href="http://blog.ploeh.dk/2010/09/29/TheRegisterResolveReleasePattern.aspx"&gt;Register Resolve Release pattern&lt;/a&gt;, so registering a captured HttpControllerContext is a no-go. It's also unlikely to work, since you'd need to somehow scope each registered instance to its source request.  &lt;li&gt;That also rules out nested containers.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;OK, so given these constraints, how can an object graph like this one be created, only with Castle Windsor instead of Poor Man's DI?&lt;/p&gt; &lt;div style="font-family: consolas, 'Courier New'; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;CatalogController&lt;/span&gt;(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;RouteLinker&lt;/span&gt;(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; baseUri,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; controllerContext));&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Somehow, the HttpControllerContext instance must be captured and made available for further resolution of a CatalogController instance.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Capturing the HttpControllerContext&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The first step is to capture the HttpControllerContext instance, as early as possible. From my previous post it should be reasonably clear that the best place to capture this instance is from within IHttpControllerActivator.Create.&lt;/p&gt;
&lt;p&gt;Here's the requirement: the HttpControllerContext instance must be captured and made available for further resolution of the object graph – preferably in a thread-safe manner. Ideally, it should be captured in a thread-safe object that can start out uninitialized, but then allow exactly one assignment of a value.&lt;/p&gt;
&lt;p&gt;At the time I first struggled with this, I was just finishing &lt;a href="http://www.amazon.com/gp/product/1935182641/ref=as_li_ss_tl?ie=UTF8&amp;amp;tag=ploeh-20&amp;amp;linkCode=as2&amp;amp;camp=1789&amp;amp;creative=390957&amp;amp;creativeASIN=1935182641"&gt;The Joy of Clojure&lt;/a&gt;, so this sounded to me an awful lot like the description of a &lt;a href="http://clojuredocs.org/clojure_core/clojure.core/promise"&gt;promise&lt;/a&gt;. Crowdsourcing on Twitter turned up that the .NET equivalent of a promise is &lt;a href="http://msdn.microsoft.com/en-us/library/dd449174.aspx"&gt;TaskCompletionSource&amp;lt;T&amp;gt;&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Creating a custom IHttpControllerActivator with an injected TaskCompletionSource&amp;lt;HttpControllerContext&amp;gt; sounds like a good approach. If the custom IHttpControllerActivator can be scoped to a specific request, that would be the solution then and there.&lt;/p&gt;
&lt;p&gt;However, as I've &lt;a href="http://blog.ploeh.dk/2012/03/20/RobustDIWithTheASPNETWebAPI.aspx"&gt;previously described&lt;/a&gt;, the current incarnation of the ASP.NET Web API has the unfortunate behavior that all framework Services (such as IHttpControllerActivator) are resolved once and cached forever (effectively turning them into having the Singleton lifestyle, despite what you may attempt to configure in your container).&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;With Dependency Injection, the common solution to bridge the gap between a long-lasting lifestyle and a shorter lifestyle is a factory.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Thus, instead of injecting TaskCompletionSource&amp;lt;HttpControllerContext&amp;gt; into a custom IHttpControllerActivator, a Func&amp;lt;TaskCompletionSource&amp;lt;HttpControllerContext&amp;gt;&amp;gt; can be injected to bridge the lifestyle gap.&lt;/p&gt;
&lt;p&gt;One other thing: the custom IHttpControllerActivator is only required to capture the HttpControllerContext for further reference, so I don't want to reimplement all the functionality of DefaultHttpControllerActivator. This is the reason why the custom IHttpControllerActivator ends up being a &lt;a href="http://en.wikipedia.org/wiki/Decorator_pattern"&gt;Decorator&lt;/a&gt;:&lt;/p&gt;
&lt;div style="font-family: consolas, 'Courier New'; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ContextCapturingControllerActivator&lt;/span&gt; :&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;IHttpControllerActivator&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;readonly&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IHttpControllerActivator&lt;/span&gt; activator;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;readonly&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;TaskCompletionSource&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;HttpControllerContext&lt;/span&gt;&amp;gt;&amp;gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; promiseFactory;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; ContextCapturingControllerActivator(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;TaskCompletionSource&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;HttpControllerContext&lt;/span&gt;&amp;gt;&amp;gt; promiseFactory,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;IHttpControllerActivator&lt;/span&gt; activator)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.activator = activator;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.promiseFactory = promiseFactory;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IHttpController&lt;/span&gt; Create(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;HttpControllerContext&lt;/span&gt; controllerContext,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Type&lt;/span&gt; controllerType)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.promiseFactory().SetResult(controllerContext);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.activator.Create(controllerContext, controllerType);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The ContextCapturingControllerActivator class simply Decorates another IHttpControllerActivator and does &lt;em&gt;one&lt;/em&gt; thing before delegating the call to the inner implementation: it uses the factory to create a new instance of TaskCompletionSource&amp;lt;HttpControllerContext&amp;gt; and assigns the HttpControllerContext instance to that &lt;em&gt;promise&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Scoping&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Because the Web API is basically being an ass (I can write this here, because I'm gambling that the solitary reader making it this far is so desperate that he or she is not going to care about the swearing) by treating framework Services as Singletons, it doesn't matter how it's being registered:&lt;/p&gt;
&lt;div style="font-family: consolas, 'Courier New'; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;container.Register(&lt;span style="color: #2b91af"&gt;Component&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .For&amp;lt;&lt;span style="color: #2b91af"&gt;IHttpControllerActivator&lt;/span&gt;&amp;gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .ImplementedBy&amp;lt;&lt;span style="color: #2b91af"&gt;ContextCapturingControllerActivator&lt;/span&gt;&amp;gt;());&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;container.Register(&lt;span style="color: #2b91af"&gt;Component&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .For&amp;lt;&lt;span style="color: #2b91af"&gt;IHttpControllerActivator&lt;/span&gt;&amp;gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .ImplementedBy&amp;lt;&lt;span style="color: #2b91af"&gt;DefaultHttpControllerActivator&lt;/span&gt;&amp;gt;());&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Notice that because Castle Windsor is being such an awesome library that it implicitly understands the Decorator pattern, I can simple register both Decorator and Decoratee in an ordered sequence.&lt;/p&gt;
&lt;p&gt;The factory itself must also be registered as a Singleton (the default in Castle Windsor):&lt;/p&gt;
&lt;div style="font-family: consolas, 'Courier New'; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;container.Register(&lt;span style="color: #2b91af"&gt;Component&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .For&amp;lt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;TaskCompletionSource&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;HttpControllerContext&lt;/span&gt;&amp;gt;&amp;gt;&amp;gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .AsFactory());&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, I'm taking advantage of Castle Windsor's &lt;a href="http://stw.castleproject.org/Windsor.Typed-Factory-Facility.ashx"&gt;Typed Factory Facility&lt;/a&gt;, so I'm simply asking it to treat a Func&amp;lt;TaskCompletionSource&amp;lt;HttpControllerContext&amp;gt;&amp;gt; as an &lt;a href="http://en.wikipedia.org/wiki/Abstract_factory_pattern"&gt;Abstract Factory&lt;/a&gt;. Doing that means that every time the delegate is being invoked, Castle Windsor will create an instance of TaskCompletionSource&amp;lt;HttpControllerContext&amp;gt; with the correct lifetime.&lt;/p&gt;
&lt;p&gt;This provides the bridge from Singleton lifestyle to PerWebRequest:&lt;/p&gt;
&lt;div style="font-family: consolas, 'Courier New'; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;container.Register(&lt;span style="color: #2b91af"&gt;Component&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .For&amp;lt;&lt;span style="color: #2b91af"&gt;TaskCompletionSource&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;HttpControllerContext&lt;/span&gt;&amp;gt;&amp;gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .LifestylePerWebRequest());&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Notice that TaskCompletionSource&amp;lt;HttpControllerContext&amp;gt; is registered with a PerWebRequest lifestyle, which means that every time the above delegate is invoked, it's going to create an instance which is scoped to the current request. This is exactly the desired behavior.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Registering HttpControllerContext&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The only thing left is registering the HttpControllerContext class itself:&lt;/p&gt;
&lt;div style="font-family: consolas, 'Courier New'; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;container.Register(&lt;span style="color: #2b91af"&gt;Component&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .For&amp;lt;&lt;span style="color: #2b91af"&gt;HttpControllerContext&lt;/span&gt;&amp;gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .UsingFactoryMethod(k =&amp;gt; &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; k.Resolve&amp;lt;&lt;span style="color: #2b91af"&gt;TaskCompletionSource&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;HttpControllerContext&lt;/span&gt;&amp;gt;&amp;gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Task.Result)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .LifestylePerWebRequest());&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This defines that HttpControllerContext instances are going to be resolved the following way: each time an HttpControllerContext instance is requested, the container is going to look up a TaskCompletionSource&amp;lt;HttpControllerContext&amp;gt; and return the result from that task.&lt;/p&gt;
&lt;p&gt;The TaskCompletionSource&amp;lt;HttpControllerContext&amp;gt; instance is scoped per web request and previously captured (as you may recall) by the ContextCapturingControllerActivator class.&lt;/p&gt;
&lt;p&gt;That's all (sic!) there's to it :)&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=1c390f17-cd8a-40bc-8d9c-bad20c6f9880"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,1c390f17-cd8a-40bc-8d9c-bad20c6f9880.aspx</comments>
      <category>ASP.NET MVC</category>
      <category>Castle Windsor</category>
      <category>Dependency Injection</category>
      <category>Services</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=3015e1f5-8e49-4b0e-84a5-4ccd570a868a</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,3015e1f5-8e49-4b0e-84a5-4ccd570a868a.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,3015e1f5-8e49-4b0e-84a5-4ccd570a868a.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=3015e1f5-8e49-4b0e-84a5-4ccd570a868a</wfw:commentRss>
      <slash:comments>8</slash:comments>
      <title>Injecting HttpControllerContext With the ASP.NET Web API</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,3015e1f5-8e49-4b0e-84a5-4ccd570a868a.aspx</guid>
      <link>http://blog.ploeh.dk/2012/04/17/InjectingHttpControllerContextWithTheASPNETWebAPI.aspx</link>
      <pubDate>Tue, 17 Apr 2012 15:17:05 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;The &lt;a href="http://www.asp.net/web-api"&gt;ASP.NET Web API&lt;/a&gt; (beta) defines a class called HttpControllerContext. As the name implies, it provides a context for a Controller. This article describes how to inject an instance of this class into a Service.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;The Problem&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;A Service may need an instance of the HttpControllerContext class. For an example, see the &lt;a href="http://blog.ploeh.dk/2012/04/17/HyperlinkingWithTheASPNETWebAPI.aspx"&gt;RouteLinker class in my previous post&lt;/a&gt;. A Controller, on the other hand, may depend on such a Service:&lt;/p&gt; &lt;div style="font-family: consolas, 'Courier New'; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; CatalogController(&lt;span style="color: #2b91af"&gt;IResourceLinker&lt;/span&gt; resourceLinker)&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;How can a CatalogController instance be wired up with an instance of RouteLinker, which again requires an instance of HttpControllerContext? In contrast to the existing ASP.NET MVC API, there's no easy way to read the current context. There's no HttpControllerContext.Current method or any other easy way (that I have found) to refer to an HttpControllerContext as part of the &lt;a href="http://blog.ploeh.dk/2011/07/28/CompositionRoot.aspx"&gt;Composition Root&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;True: it's easily available as a property on a Controller, but at the time of composition, there's no Controller instance (yet). A Controller instance is exactly what the Composition Root is attempting to create. This sounds like a circular reference problem. Fortunately, it's not.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The Solution&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;For Poor Man's DI, the solution is relatively simple. As &lt;a href="http://blog.ploeh.dk/2012/03/20/RobustDIWithTheASPNETWebAPI.aspx"&gt;I've previously described&lt;/a&gt;, by default the responsibility of creating Controller instances is handled by an instance of IHttpControllerActivator. This is, essentially, the Composition Root (at least for all Controllers).&lt;/p&gt;
&lt;p&gt;The Create method of that interface takes exactly the HttpControllerContext required by RouteLinker – or, put differently: the framework will supply an instance every time it invokes the Create method. Thus, a custom IHttpControllerActivator solves the problem:&lt;/p&gt;
&lt;div style="font-family: consolas, 'Courier New'; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;PoorMansCompositionRoot&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;IHttpControllerActivator&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IHttpController&lt;/span&gt; Create(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;HttpControllerContext&lt;/span&gt; controllerContext,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Type&lt;/span&gt; controllerType)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (controllerType == &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;CatalogController&lt;/span&gt;))&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; url = &lt;span style="color: #2b91af"&gt;HttpContext&lt;/span&gt;.Current.Request.Url;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; baseUri =&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;UriBuilder&lt;/span&gt;(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; url.Scheme,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; url.Host,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; url.Port).Uri;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;CatalogController&lt;/span&gt;(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;RouteLinker&lt;/span&gt;(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; baseUri,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; controllerContext));&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Handle other types here...&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;em&gt;controllerContext&lt;/em&gt; parameter is simply passed on to the RouteLinker constructor.&lt;/p&gt;
&lt;p&gt;The only thing left is to register the PoorMansCompositionRoot with the ASP.NET Web API. This can be done in Global.asax by using the GlobalConfiguration.Configuration.ServiceResolver.SetResolver method, as described in &lt;a href="http://blog.ploeh.dk/2012/03/20/RobustDIWithTheASPNETWebAPI.aspx"&gt;my previous post&lt;/a&gt;. Just resolve IHttpControllerActivator to an instance of PoorMansCompositionRoot.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=3015e1f5-8e49-4b0e-84a5-4ccd570a868a"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,3015e1f5-8e49-4b0e-84a5-4ccd570a868a.aspx</comments>
      <category>ASP.NET MVC</category>
      <category>Dependency Injection</category>
      <category>Services</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=c49a556c-9fe7-42b7-8b18-2bf7508239b6</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,c49a556c-9fe7-42b7-8b18-2bf7508239b6.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,c49a556c-9fe7-42b7-8b18-2bf7508239b6.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=c49a556c-9fe7-42b7-8b18-2bf7508239b6</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <title>Hyperlinking With the ASP.NET Web API</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,c49a556c-9fe7-42b7-8b18-2bf7508239b6.aspx</guid>
      <link>http://blog.ploeh.dk/2012/04/17/HyperlinkingWithTheASPNETWebAPI.aspx</link>
      <pubDate>Tue, 17 Apr 2012 14:46:42 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;When creating resources with the &lt;a href="http://www.asp.net/web-api"&gt;ASP.NET Web API&lt;/a&gt; (beta) it's important to be able to create correct hyperlinks (you know, &lt;a href="http://martinfowler.com/articles/richardsonMaturityModel.html"&gt;if it doesn't have hyperlinks, it's not REST&lt;/a&gt;). These hyperlinks may link to other resources in the same API, so it's important to keep the links consistent. A client following such a link should hit the desired resource.&lt;/p&gt; &lt;p&gt;This post describes an refactoring-safe approach to creating hyperlinks using the Web API RouteCollection and Expressions.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;The Problem&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Obviously hyperlinks can be hard-coded, but since incoming requests are matched based on the Web API's RouteCollection, there's a risk that hard-coded links become disconnected from the API's incoming routes. In other words, hard-coding links is probably not a good idea.&lt;/p&gt; &lt;p&gt;For reference, the default route in the Web API looks like this:&lt;/p&gt; &lt;div style="font-family: consolas, 'Courier New'; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;routes.MapHttpRoute(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; name: &lt;span style="color: #a31515"&gt;"DefaultApi"&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; routeTemplate: &lt;span style="color: #a31515"&gt;"{controller}/{id}"&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; defaults: &lt;span style="color: blue"&gt;new&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; controller = &lt;span style="color: #a31515"&gt;"Catalog"&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; id = &lt;span style="color: #2b91af"&gt;RouteParameter&lt;/span&gt;.Optional&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;);&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;A sample action fitting that route might look like this:&lt;/p&gt;
&lt;div style="font-family: consolas, 'Courier New'; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Artist&lt;/span&gt; Get(&lt;span style="color: blue"&gt;string&lt;/span&gt; id)&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;where the Get method is defined by the ArtistController class.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Desired Outcome&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In order to provide a refactoring-safe way to create links to e.g. the artist resource, the &lt;a href="http://joseoncode.com/2011/03/18/wcf-web-api-strongly-typed-resource-linker/"&gt;strongly typed Resource Linker&lt;/a&gt; approach outlined by &lt;a href="http://joseoncode.com/"&gt;José F. Romaniello&lt;/a&gt; can be adopted. The IResourceLinker interface looks like this:&lt;/p&gt;
&lt;div style="font-family: consolas, 'Courier New'; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;interface&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IResourceLinker&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt; GetUri&amp;lt;T&amp;gt;(&lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;T&amp;gt;&amp;gt; method);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This makes it possible to create links like this:&lt;/p&gt;
&lt;div style="font-family: consolas, 'Courier New'; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; artist = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Artist&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Name = artistName,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Links = &lt;span style="color: blue"&gt;new&lt;/span&gt;[]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Link&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Href = &lt;span style="color: blue"&gt;this&lt;/span&gt;.resourceLinker.GetUri&amp;lt;&lt;span style="color: #2b91af"&gt;ArtistController&lt;/span&gt;&amp;gt;(r =&amp;gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; r.Get(artistsId)).ToString(),&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Rel = &lt;span style="color: #a31515"&gt;"self"&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; },&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// More crap goes here...&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;};&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In this example, the &lt;em&gt;resourceLinker&lt;/em&gt; field is an injected instance of IResourceLinker.&lt;/p&gt;
&lt;p&gt;Since the input to the GetUri method is an Expression, it's being checked at compile time. It's refactoring-safe because a refactoring tool will be able to e.g. change the name of the method call in the Expression if the name of the method changes.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Example Implementation&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;It's possible to implement IResourceLinker over a Web API RouteCollection. Here's an example implementation:&lt;/p&gt;
&lt;div style="font-family: consolas, 'Courier New'; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;RouteLinker&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;IResourceLinker&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt; baseUri;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;readonly&lt;/span&gt; &lt;span style="color: #2b91af"&gt;HttpControllerContext&lt;/span&gt; ctx;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; RouteLinker(&lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt; baseUri, &lt;span style="color: #2b91af"&gt;HttpControllerContext&lt;/span&gt; ctx)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.baseUri = baseUri;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.ctx = ctx;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt; GetUri&amp;lt;T&amp;gt;(&lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;T&amp;gt;&amp;gt; method)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (method == &lt;span style="color: blue"&gt;null&lt;/span&gt;)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;throw&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"method"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; methodCallExp = method.Body &lt;span style="color: blue"&gt;as&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MethodCallExpression&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (methodCallExp == &lt;span style="color: blue"&gt;null&lt;/span&gt;)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;throw&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ArgumentException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"The expression's body must be a MethodCallExpression. The code block supplied should invoke a method.\nExample: x =&amp;gt; x.Foo()."&lt;/span&gt;, &lt;span style="color: #a31515"&gt;"method"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; routeValues = methodCallExp.Method.GetParameters()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .ToDictionary(p =&amp;gt; p.Name, p =&amp;gt; GetValue(methodCallExp, p));&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; controllerName = methodCallExp.Method.ReflectedType.Name&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .ToLowerInvariant().Replace(&lt;span style="color: #a31515"&gt;"controller"&lt;/span&gt;, &lt;span style="color: #a31515"&gt;""&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; routeValues.Add(&lt;span style="color: #a31515"&gt;"controller"&lt;/span&gt;, controllerName);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; relativeUri = &lt;span style="color: blue"&gt;this&lt;/span&gt;.ctx.Url.Route(&lt;span style="color: #a31515"&gt;"DefaultApi"&lt;/span&gt;, routeValues);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt;(&lt;span style="color: blue"&gt;this&lt;/span&gt;.baseUri, relativeUri);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;object&lt;/span&gt; GetValue(&lt;span style="color: #2b91af"&gt;MethodCallExpression&lt;/span&gt; methodCallExp,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;ParameterInfo&lt;/span&gt; p)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; arg = methodCallExp.Arguments[p.Position];&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; lambda = &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.Lambda(arg);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; lambda.Compile().DynamicInvoke().ToString();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This isn't much different from José F. Romaniello's example, apart from the fact that it creates a dictionary of route values and then uses the UrlHelper.Route method to create a relative URI.&lt;/p&gt;
&lt;p&gt;Please not that this is just an example implementation. For instance, the call to the Route method supplies the hard-coded string "DefaultApi" to indicate which route (from Global.asax) to use. I'll leave it as an exercise for the interested reader to provide a generalization of this implementation.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=c49a556c-9fe7-42b7-8b18-2bf7508239b6"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,c49a556c-9fe7-42b7-8b18-2bf7508239b6.aspx</comments>
      <category>ASP.NET MVC</category>
      <category>REST</category>
      <category>Services</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=19280084-d49f-4e7b-a938-f0203db31d9d</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,19280084-d49f-4e7b-a938-f0203db31d9d.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,19280084-d49f-4e7b-a938-f0203db31d9d.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=19280084-d49f-4e7b-a938-f0203db31d9d</wfw:commentRss>
      <slash:comments>18</slash:comments>
      <title>IQueryable&lt;T&gt; is Tight Coupling</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,19280084-d49f-4e7b-a938-f0203db31d9d.aspx</guid>
      <link>http://blog.ploeh.dk/2012/03/26/IQueryableIsTightCoupling.aspx</link>
      <pubDate>Mon, 26 Mar 2012 13:53:31 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;From time to time I encounter people who attempt to express an API in terms of &lt;a href="http://msdn.microsoft.com/en-us/library/bb351562.aspx"&gt;IQueryable&amp;lt;T&amp;gt;&lt;/a&gt;. That's almost always a bad idea. In this post, I'll explain why.&lt;/p&gt; &lt;p&gt;In short, the IQueryable&amp;lt;T&amp;gt; interface is one of the best examples of a &lt;a href="http://martinfowler.com/bliki/HeaderInterface.html"&gt;Header Interface&lt;/a&gt; that .NET has to offer. It's almost impossible to fully implement it.&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;Please note that this post is about the problematic aspects of designing an API around the IQueryable&amp;lt;T&amp;gt; interface. It's not an attack on the interface itself, which has its place in the BCL. It's also not an attack on all the wonderful LINQ methods available on &lt;a href="http://msdn.microsoft.com/en-us/library/9eekhta0.aspx"&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/a&gt;.&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;You can say that IQueryable&amp;lt;T&amp;gt; is one big &lt;a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle"&gt;Liskov Substitution Principle&lt;/a&gt; (LSP)violation just waiting to happen. In the next two section, I will apply &lt;a href="http://en.wikipedia.org/wiki/Postel%27s_law"&gt;Postel's law&lt;/a&gt; to explain why that is.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Consuming IQueryable&amp;lt;T&amp;gt;&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;The first part of Postel's law applied to API design states that an API should be &lt;em&gt;liberal in what it accepts&lt;/em&gt;. In other words, we are talking about input, so an API that consumes IQueryable&amp;lt;T&amp;gt; would take this generalized shape:&lt;/p&gt; &lt;div style="font-family: consolas, 'Courier New'; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;IFoo&lt;/span&gt; SomeMethod(&lt;span style="color: #2b91af"&gt;IQueryable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Bar&lt;/span&gt;&amp;gt; q);&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Is that a &lt;em&gt;liberal&lt;/em&gt; requirement? It must certainly is not. Such an interface &lt;em&gt;demands&lt;/em&gt; of any caller that they must be able to supply an implementation of IQueryable&amp;lt;Bar&amp;gt;. According to the LSP we must be able to supply &lt;em&gt;any&lt;/em&gt; implementation without changing the correctness of the program. That goes for both the implementer of IQueryable&amp;lt;Bar&amp;gt; as well as the implementation of SomeMethod.&lt;/p&gt;
&lt;p&gt;At this point it's important to keep in mind the &lt;em&gt;purpose&lt;/em&gt; of IQueryable&amp;lt;T&amp;gt;: it's &lt;a href="http://msdn.microsoft.com/en-us/library/bb351562.aspx"&gt;intended for implementation by query providers&lt;/a&gt;. In other words, this isn't just some sequence of Bar instances which can be filtered and projected; no, this is a query &lt;em&gt;expression&lt;/em&gt; which is intended to be &lt;em&gt;translated&lt;/em&gt; into a query somewhere else – most often some dialect of SQL.&lt;/p&gt;
&lt;p&gt;That's quite a demand to put on the caller.&lt;/p&gt;
&lt;p&gt;It's certainly a powerful interface (or so it would seem), but is it really necessary? Does SomeMethod really need to be able to perform &lt;em&gt;arbitrarily complex&lt;/em&gt; queries against a data source?&lt;/p&gt;
&lt;p&gt;In one recent discussion, it turns out that all the developer really wanted to do was to be able to &lt;a href="https://github.com/ploeh/cqrs-journey-code/commit/7b912136b470b75f418eadae605669c3489f53f9"&gt;select based on a handful of simple criteria&lt;/a&gt;. In another case, the developer only wanted to do simple paging.&lt;/p&gt;
&lt;p&gt;Such requirements could be modeled much simpler without making huge demands on the caller. In both cases, we could provide specialized &lt;a href="http://martinfowler.com/eaaCatalog/queryObject.html"&gt;Query Objects&lt;/a&gt; instead, or perhaps even simpler just a set of specialized queries:&lt;/p&gt;
&lt;div style="font-family: consolas, 'Courier New'; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;IFoo&lt;/span&gt; FindById(&lt;span style="color: blue"&gt;int&lt;/span&gt; fooId);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;IFoo&lt;/span&gt; FindByCorrelationId(&lt;span style="color: blue"&gt;int&lt;/span&gt; correlationId);&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Or, in the case of paging:&lt;/p&gt;
&lt;div style="font-family: consolas, 'Courier New'; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IFoo&lt;/span&gt;&amp;gt; GetFoos(&lt;span style="color: blue"&gt;int&lt;/span&gt; page);&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is certainly much more &lt;em&gt;liberal&lt;/em&gt; in that it requires the caller to supply only the required information in order to implement the methods. Designing APIs in terms of &lt;a href="http://martinfowler.com/bliki/RoleInterface.html"&gt;Role Interfaces&lt;/a&gt; instead of Header Interfaces makes the APIs much more flexible. This will enable you to respond to change.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Exposing IQueryable&amp;lt;T&amp;gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The other part of Postel's law states that an API should be &lt;em&gt;conservative in what it sends&lt;/em&gt;. In other words, a method must guarantee that the data it returns conforms rigorously to the contract between caller and implementer.&lt;/p&gt;
&lt;p&gt;A method returning IQueryable&amp;lt;T&amp;gt; would take this generalized shape:&lt;/p&gt;
&lt;div style="font-family: consolas, 'Courier New'; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;IQueryable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Bar&lt;/span&gt;&amp;gt; GetBars();&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;When designing APIs, a huge part of the contract is defined by the interface (or base class). Thus, the return type of a method specifies a conservative guarantee about the returned data. In the case of returning IQueryable&amp;lt;Bar&amp;gt; the method thus &lt;em&gt;guarantees&lt;/em&gt; that it will return a complete implementation of IQueryable&amp;lt;Bar&amp;gt;.&lt;/p&gt;
&lt;p&gt;Is that conservative?&lt;/p&gt;
&lt;p&gt;Once again invoking the LSP, a consumer must be able to do &lt;em&gt;anything&lt;/em&gt; allowed by IQueryable&amp;lt;Bar&amp;gt; without changing the correctness of the program.&lt;/p&gt;
&lt;p&gt;That's a big honking promise to make.&lt;/p&gt;
&lt;p&gt;Who can keep that promise?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Current Implementations&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Implementing IQueryable&amp;lt;T&amp;gt; is a huge undertaking. If you don't believe me, just take a look at the official &lt;a href="http://blogs.msdn.com/b/mattwar/archive/2008/11/18/linq-links.aspx"&gt;Building an IQueryable provider&lt;/a&gt; series of blog posts. Even so, the interface is so flexible and expressive that with a single exception, it's always possible to write a query that a given provider can't translate.&lt;/p&gt;
&lt;p&gt;Have you ever worked with LINQ to Entities or another ORM and received a NotSupportedException? &lt;a href="https://www.google.dk/search?q=linq+notsupportedexception"&gt;Lots of people have&lt;/a&gt;. In fact, with a single exception, it's my firm belief that all existing implementations violate the LSP (in fact, I challenge my readers to refer me to a real, publicly available implementation of IQueryable&amp;lt;T&amp;gt; that can accept &lt;em&gt;any&lt;/em&gt; expression I throw at it, and I'll ship a free copy of &lt;a href="http://affiliate.manning.com/idevaffiliate.php?id=1150_236"&gt;my book&lt;/a&gt; to the first reader to do so).&lt;/p&gt;
&lt;p&gt;Furthermore, the subset of features that each implementation supports varies from query provider to query provider. An expression that can be translated by the Entity framework may not work with Microsoft's OData query provider.&lt;/p&gt;
&lt;p&gt;The only implementation that fully implements IQueryable&amp;lt;T&amp;gt; is the &lt;a href="http://msdn.microsoft.com/en-us/library/cc190116.aspx"&gt;in-memory implementation&lt;/a&gt; (and referring to this one does not earn you a free book). Ironically, this implementation can be considered a &lt;a href="http://en.wikipedia.org/wiki/Null_Object_pattern"&gt;Null Object&lt;/a&gt; implementation and goes against the whole purpose of the IQueryable&amp;lt;T&amp;gt; interface exactly because it &lt;em&gt;doesn't&lt;/em&gt; translate the expression to another language.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Why This Matters&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;You may think this is all a theoretical exercise, but it actually does matter. When writing &lt;a href="http://www.amazon.com/gp/product/0132350882/ref=as_li_ss_tl?ie=UTF8&amp;amp;tag=ploeh-20&amp;amp;linkCode=as2&amp;amp;camp=1789&amp;amp;creative=390957&amp;amp;creativeASIN=0132350882"&gt;Clean Code&lt;/a&gt;, it's important to design an API in such a way that it's clear what it does.&lt;/p&gt;
&lt;p&gt;An interface like this makes false guarantees:&lt;/p&gt;
&lt;div style="font-family: consolas, 'Courier New'; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;interface&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IRepository&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;IQueryable&lt;/span&gt;&amp;lt;T&amp;gt; Query&amp;lt;T&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;According to the LSP and Postel's law, it would seem to guarantee that you can write &lt;em&gt;any&lt;/em&gt; query expression (no matter how complex) against the returned instance, and it would always work.&lt;/p&gt;
&lt;p&gt;In practice, this is never going to happen.&lt;/p&gt;
&lt;p&gt;Programmers who define such interfaces invariably have a specific ORM in mind, and they implicitly tend to stay within the bounds they know are safe for that specific ORM. This is a leaky abstraction.&lt;/p&gt;
&lt;p&gt;If you have a specific ORM in mind, then be explicit about it. Don't hide it behind an interface. It creates the illusion that you can replace one implementation with another. In practice, that's impossible. Imagine attempting to provide an implementation over an Event Store.&lt;/p&gt;
&lt;p&gt;The cake is a lie.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=19280084-d49f-4e7b-a938-f0203db31d9d"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,19280084-d49f-4e7b-a938-f0203db31d9d.aspx</comments>
      <category>Dependency Injection</category>
      <category>Software Design</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=073ad508-e007-4b4d-bdff-eb4999e0470d</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,073ad508-e007-4b4d-bdff-eb4999e0470d.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,073ad508-e007-4b4d-bdff-eb4999e0470d.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=073ad508-e007-4b4d-bdff-eb4999e0470d</wfw:commentRss>
      <slash:comments>12</slash:comments>
      <title>Robust DI With the ASP.NET Web API</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,073ad508-e007-4b4d-bdff-eb4999e0470d.aspx</guid>
      <link>http://blog.ploeh.dk/2012/03/20/RobustDIWithTheASPNETWebAPI.aspx</link>
      <pubDate>Tue, 20 Mar 2012 16:02:51 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;Like the WCF Web API, the new &lt;a href="http://www.asp.net/web-api"&gt;ASP.NET Web API&lt;/a&gt; supports Dependency Injection (DI), but the approach is different and the resulting code you'll have to write is likely to be more complex. This post describes how to enable robust DI with the new Web API. Since this is based on the beta release, I hope that it will become easier in the final release.&lt;/p&gt; &lt;p&gt;At first glance, enabling DI on an ASP.NET Web API &lt;a href="http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver"&gt;looks seductively simple&lt;/a&gt;. As always, though, the devil is in the details. &lt;a href="http://www.nikosbaxevanis.com/bonus-bits/"&gt;Nikos Baxevanis&lt;/a&gt; has already provided a &lt;a href="http://www.nikosbaxevanis.com/bonus-bits/2012/03/using-the-web-api-dependency-resolver-with-castle-windsor.html"&gt;more thorough description&lt;/a&gt;, but it's even more tricky than that.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Protocol&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;To enable DI, all you have to do is to call the SetResolver method, right? It even has an overload that enables you to supply two code blocks instead of implementing an interface (although you can certainly also implement IDependencyResolver). Could it be any easier than that?&lt;/p&gt; &lt;p&gt;Yes, it most certainly could.&lt;/p&gt; &lt;p&gt;Imagine that you'd like to hook up your DI Container of choice. As a first attempt, you try something like this:&lt;/p&gt; &lt;div style="font-family: consolas, 'Courier New'; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;GlobalConfiguration&lt;/span&gt;.Configuration.ServiceResolver.SetResolver(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; t =&amp;gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.container.Resolve(t),&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; t =&amp;gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.container.ResolveAll(t).Cast&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;());&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This compiles. Does it work? Yes, but in a rather scary manner. Although it satisfies the interface, it doesn't satisfy the &lt;em&gt;protocol&lt;/em&gt; ("an interface describes whether two components will &lt;em&gt;fit&lt;/em&gt; together, while a protocol describes whether they will &lt;em&gt;work&lt;/em&gt; together." (&lt;a href="http://www.amazon.com/gp/product/0321503627/ref=as_li_ss_tl?ie=UTF8&amp;amp;tag=ploeh-20&amp;amp;linkCode=as2&amp;amp;camp=1789&amp;amp;creative=390957&amp;amp;creativeASIN=0321503627"&gt;GOOS&lt;/a&gt;, p. 58)).&lt;/p&gt;
&lt;p&gt;The protocol, in this case, is that if you (or rather the container) can't resolve the type, you should return null. What's even worse is that &lt;em&gt;if&lt;/em&gt; your code throws an exception (&lt;em&gt;any&lt;/em&gt; exception, apparently), DependencyResolver will suppress it. In case you didn't know, this is strongly frowned upon in the &lt;a href="http://msdn.microsoft.com/en-us/library/ms229005.aspx"&gt;.NET Framework Design Guidelines&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Even so, the official introduction article instead chooses to play along with the protocol and explicitly handle any exceptions. Something along the lines of this ugly code:&lt;/p&gt;
&lt;div style="font-family: consolas, 'Courier New'; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;GlobalConfiguration&lt;/span&gt;.Configuration.ServiceResolver.SetResolver(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; t =&amp;gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;try&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.container.Resolve(t);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;catch&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;ComponentNotFoundException&lt;/span&gt;)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;null&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; },&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; t =&amp;gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;try&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.container.ResolveAll(t).Cast&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;catch&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;ComponentNotFoundException&lt;/span&gt;)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;);&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Notice how try/catch is used for control flow – another &lt;a href="http://msdn.microsoft.com/en-us/library/ms229030.aspx"&gt;major no no in the .NET Framework Design Guidelines&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;At least with a good DI Container, we can do something like this instead:&lt;/p&gt;
&lt;div style="font-family: consolas, 'Courier New'; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;GlobalConfiguration&lt;/span&gt;.Configuration.ServiceResolver.SetResolver(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; t =&amp;gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.container.Kernel.HasComponent(t) ?&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.container.Resolve(t) :&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;null&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; t =&amp;gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.container.ResolveAll(t).Cast&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;());&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Still, first impressions don't exactly inspire trust in the implementation...&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;API Design Issues&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Next, I would like to direct your attention to the DependencyResolver API. At its core, it looks like this:&lt;/p&gt;
&lt;div style="font-family: consolas, 'Courier New'; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;interface&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IDependencyResolver&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;object&lt;/span&gt; GetService(&lt;span style="color: #2b91af"&gt;Type&lt;/span&gt; serviceType);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt; GetServices(&lt;span style="color: #2b91af"&gt;Type&lt;/span&gt; serviceType);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It can create objects, but what about decommissioning? What if, deep in a dependency graph, a Controller contains an IDisposable object? This is not a particularly exotic scenario – it might be an instance of an Entity Framework &lt;a href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.aspx"&gt;ObjectContext&lt;/a&gt;. While an ApiController itself implements IDisposable, it may not know that it contains an injected object graph with one or more IDisposable leaf nodes.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;It's a fundamental rule of DI that you must &lt;a href="http://blog.ploeh.dk/2010/09/29/TheRegisterResolveReleasePattern.aspx"&gt;Release what you Resolve&lt;/a&gt;. That's not possible with the DependencyResolver API. The result may be memory leaks.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Fortunately, it turns out that there's a fix for this (at least for Controllers). Unfortunately, this workaround leverages another design problem with DependencyResolver.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Mixed Responsibilities&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;It turns out that when you wire a custom resolver up with the SetResolver method, the ASP.NET Web API will query your custom resolver (such as a DI Container) for not only your application classes, but also for its own infrastructure components. That surprised me a bit because of the mixed responsibility, but at least this is a useful hook.&lt;/p&gt;
&lt;p&gt;One of the first types the framework will ask for is an instance of IHttpControllerFactory, which looks like this:&lt;/p&gt;
&lt;div style="font-family: consolas, 'Courier New'; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;interface&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IHttpControllerFactory&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; IHttpController CreateController(HttpControllerContext controllerContext,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; controllerName);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;void&lt;/span&gt; ReleaseController(IHttpController controller);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Fortunately, this interface has a Release hook, so at least it's possible to release Controller instances, which is most important because there will be a lot of them (one per HTTP request).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Discoverability Issues&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The IHttpControllerFactory looks a lot like the well-known ASP.NET MVC &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.icontrollerfactory%28v=vs.98%29.aspx"&gt;IControllerFactory&lt;/a&gt; interface, but there are subtle differences. In ASP.NET MVC, there's a &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.defaultcontrollerfactory%28v=vs.98%29.aspx"&gt;DefaultControllerFactory&lt;/a&gt; with appropriate virtual methods one can overwrite (it follows the &lt;a href="http://en.wikipedia.org/wiki/Template_method_pattern"&gt;Template Method&lt;/a&gt; pattern).&lt;/p&gt;
&lt;p&gt;There's also a DefaultControllerFactory in the Web API, but unfortunately no Template Methods to override. While I &lt;em&gt;could&lt;/em&gt; write an algorithm that maps from the &lt;em&gt;controllerName&lt;/em&gt; parameter to a type which can be passed to a DI Container, I'd rather prefer to be able to reuse the implementation which the DefaultControllerFactory contains.&lt;/p&gt;
&lt;p&gt;In ASP.NET MVC, this is possible by overriding the &lt;a href="http://msdn.microsoft.com/en-us/library/ee264052%28v=vs.98%29.aspx"&gt;GetControllerInstance&lt;/a&gt; method, but it turns out that the Web API (beta) does this slightly differently. It favors composition over inheritance (which is actually a good thing, so kudos for that), so after mapping &lt;em&gt;controllerName&lt;/em&gt; to a Type instance, it invokes an instance of the IHttpControllerActivator interface (did I hear anyone say "FactoryFactory?"). Very loosely coupled (good), but not very discoverable (not so good). It would have been more discoverable if DefaultControllerFactory had used Constructor Injection to get its dependency, rather than relying on the &lt;a href="http://blog.ploeh.dk/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx"&gt;Service Locator&lt;/a&gt; which DependencyResolver really is.&lt;/p&gt;
&lt;p&gt;However, this is only an issue if you need to hook into the Controller creation process, e.g. in order to capture the HttpControllerContext for further use. In normal scenarios, despite what Nikos Baxevanis describes in his blog post, you don't &lt;em&gt;have&lt;/em&gt; to override or implement IHttpControllerFactory.CreateController. The DependencyResolver infrastructure will automatically invoke your GetService implementation (or the corresponding code block) whenever a Controller instance is required.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Releasing Controllers&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The easiest way to make sure that all Controller instances are being properly released is to derive a class from DefaultControllerFactory and override the ReleaseController method:&lt;/p&gt;
&lt;div style="font-family: consolas, 'Courier New'; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ReleasingControllerFactory&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;DefaultHttpControllerFactory&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;readonly&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt; release;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; ReleasingControllerFactory(&lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt; releaseCallback,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;HttpConfiguration&lt;/span&gt; configuration)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; : &lt;span style="color: blue"&gt;base&lt;/span&gt;(configuration)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.release = releaseCallback;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;override&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; ReleaseController(&lt;span style="color: #2b91af"&gt;IHttpController&lt;/span&gt; controller)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.release(controller);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;base&lt;/span&gt;.ReleaseController(controller);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Notice that it's not necessary to override the CreateController method, since the default implementation is good enough – it'll ask the DependencyResolver for an instance of IHttpControllerActivator, which will again ask the DependencyResolver for an instance of the Controller type, in the end invoking your custom GetObject implemention.&lt;/p&gt;
&lt;p&gt;To keep the above example generic, I just injected an Action&amp;lt;object&amp;gt; into ReleasingControllerFactory – I really don't wish to turn this into a discussion about the merits and demerits of various DI Containers. In any case, I'll leave it as an exercise to you to wire up your favorite DI Container so that the &lt;em&gt;releaseCallback&lt;/em&gt; is actually a call to the container's Release method.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Lifetime Cycles of Infrastructure Components&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Before I conclude, I'd like to point out another &lt;a href="http://en.wikipedia.org/wiki/Principle_of_least_astonishment"&gt;POLA&lt;/a&gt; violation that hit me during my investigation.&lt;/p&gt;
&lt;p&gt;The ASP.NET Web API utilizes DependencyResolver to resolve its own infrastructure types (such as IHttpControllerFactory, IHttpControllerActivator, etc.). Any custom DependencyResolver you supply will also be queried for these types. However:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;When resolving infrastructure components, the Web API doesn't respect any custom lifecycle you may have defined for these components.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;At a certain point while I investigated this, I wanted to configure a custom IHttpControllerActivator to have a &lt;em&gt;Web Request Context&lt;/em&gt; (&lt;a href="http://affiliate.manning.com/idevaffiliate.php?id=1150_236"&gt;my book&lt;/a&gt;, section 8.3.4) – in other words, I wanted to create a new instance of IHttpControllerActivator for each incoming HTTP request.&lt;/p&gt;
&lt;p&gt;This is not possible. The framework queries a custom DependencyResolver for an infrastructure type, but &lt;em&gt;even when it receives an instance&lt;/em&gt; (i.e. not null), it doesn't trust the DependencyResolver to efficiently manage the lifetime of that instance. Instead, it caches this instance for ever, and never asks for it again. This is, in my opinion, a mistaken responsibility, and I hope it will be corrected in the final release.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Concluding Thoughts&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Wiring up the ASP.NET Web API with robust DI is possible, but much harder than it ought to be. Suggestions for improvements are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A Release hook in DependencyResolver. 
&lt;li&gt;The framework itself should trust the DependencyResolver to efficiently manage lifetime of all objects it create.&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;As I've described, there are other places were minor adjustments would be helpful, but these two suggestions are the most important ones.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt; (2012.03.21): I've posted this feedback to the product group on &lt;a href="http://aspnet.uservoice.com/forums/147201-asp-net-web-api/suggestions/2701848-clean-up-dependency-injection-support"&gt;uservoice&lt;/a&gt; and &lt;a href="https://connect.microsoft.com/VisualStudio/feedback/details/732337/clean-up-dependency-injection-support-in-asp-net-web-api"&gt;Connect&lt;/a&gt; – if you agree, please visit those sites and vote up the issues.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=073ad508-e007-4b4d-bdff-eb4999e0470d"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,073ad508-e007-4b4d-bdff-eb4999e0470d.aspx</comments>
      <category>ASP.NET MVC</category>
      <category>Dependency Injection</category>
      <category>REST</category>
      <category>Services</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=3fa91879-8089-4c25-a0a8-25d23d17404e</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,3fa91879-8089-4c25-a0a8-25d23d17404e.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,3fa91879-8089-4c25-a0a8-25d23d17404e.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=3fa91879-8089-4c25-a0a8-25d23d17404e</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <title>Migrating from WCF Web API to ASP.NET Web API</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,3fa91879-8089-4c25-a0a8-25d23d17404e.aspx</guid>
      <link>http://blog.ploeh.dk/2012/03/19/MigratingFromWCFWebAPIToASPNETWebAPI.aspx</link>
      <pubDate>Mon, 19 Mar 2012 22:24:47 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;Now that &lt;a href="http://wcf.codeplex.com/wikipage?title=WCF%20Web%20API%20is%20now%20ASP.NET%20Web%20API"&gt;the WCF Web API has ‘become’ the ASP.NET Web API&lt;/a&gt;, I’ve had to migrate a semi-complex code base from the old to the new framework. These are my notes from that process.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Migrating Project References&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;As far as I can tell, the ASP.NET Web API isn’t just a port of the WCF Web API. At a cursory glance, it looks like a complete reimplementation. If it’s a port of the old code, it’s at least a rather radical one. The assemblies have completely different names, and so on.&lt;/p&gt; &lt;p&gt;Both old and new project, however, are based on NuGet packages, so it wasn’t particularly hard to change.&lt;/p&gt; &lt;p&gt;To remove the old project references, I ran this NuGet command:&lt;/p&gt; &lt;p&gt;Uninstall-Package webapi.all –RemoveDependencies&lt;/p&gt; &lt;p&gt;followed by&lt;/p&gt; &lt;p&gt;Install-Package aspnetwebapi&lt;/p&gt; &lt;p&gt;to install the project references for the ASP.NET Web API.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Rename Resources to Controllers&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;In the WCF Web API, there was no naming convention for the various resource classes. In the quickstarts, they were sometimes called &lt;em&gt;Apis&lt;/em&gt; (like ContactsApi), and I called mine &lt;em&gt;Resources&lt;/em&gt; (like CatalogResource). Whatever your naming convention was, the easiest things is to find them all and rename them to end with &lt;em&gt;Controller&lt;/em&gt; (e.g. CatalogController).&lt;/p&gt; &lt;p&gt;AFAICT you &lt;em&gt;can&lt;/em&gt; change the naming convention, but I didn’t care enough to do so.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Derive Controllers from ApiController&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Unless you care to manually implement IHttpController, each Controller should derive from ApiController:&lt;/p&gt; &lt;div style="font-family: consolas, 'Courier New'; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;CatalogController&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;ApiController&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Remove Attributes&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The WCF Web API uses the [WebGet] and [WebInvoke] attributes. The ASP.NET Web API, on the other hand, uses routes, so I removed all the attributes, including their UriTemplates:&lt;/p&gt;
&lt;div style="font-family: consolas, 'Courier New'; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: green"&gt;//[WebGet(UriTemplate = "/")]&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Catalog&lt;/span&gt; GetRoot()&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Add Routes&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;As a replacement for attributes and UriTemplates, I added HTTP routes:&lt;/p&gt;
&lt;div style="font-family: consolas, 'Courier New'; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;routes.MapHttpRoute(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; name: &lt;span style="color: #a31515"&gt;"DefaultApi"&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; routeTemplate: &lt;span style="color: #a31515"&gt;"{controller}/{id}"&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; defaults: &lt;span style="color: blue"&gt;new&lt;/span&gt; { controller = &lt;span style="color: #a31515"&gt;"Catalog"&lt;/span&gt;, id = &lt;span style="color: #2b91af"&gt;RouteParameter&lt;/span&gt;.Optional }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;);&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The MapHttpRoute method is an extension method defined in the System.Web.Http namespace, so I had to add a using directive for it.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Composition&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Wiring up Controllers with Constructor Injection turned out to be rather painful. For a starting point, I used &lt;a href="http://www.nikosbaxevanis.com/bonus-bits/"&gt;Nikos Baxevanis'&lt;/a&gt; &lt;a href="http://www.nikosbaxevanis.com/bonus-bits/2012/03/using-the-web-api-dependency-resolver-with-castle-windsor.html"&gt;guide&lt;/a&gt;, but it turns out there are further subtleties which should be addressed (more about this later, but to prevent a stream of comments about the DependencyResolver API: yes, I know about that, but it's inadequate for a number of reasons).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Media Types&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In the ASP.NET Web API application/json is now the default media type format if the client doesn't supply any Accept header. For the WCF Web API I had had to resort to &lt;a href="http://stackoverflow.com/questions/6779254/set-default-response-type-in-wcf-web-api/9002099#9002099"&gt;a hack to change the default&lt;/a&gt;, so this was a pleasant surprise.&lt;/p&gt;
&lt;p&gt;It's still pretty easy to add more supported media types:&lt;/p&gt;
&lt;div style="font-family: consolas, 'Courier New'; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;GlobalConfiguration&lt;/span&gt;.Configuration.Formatters.XmlFormatter&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .SupportedMediaTypes.Add(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MediaTypeHeaderValue&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"application/vnd.247e.artist+xml"&lt;/span&gt;));&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;GlobalConfiguration&lt;/span&gt;.Configuration.Formatters.JsonFormatter&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .SupportedMediaTypes.Add(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MediaTypeHeaderValue&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"application/vnd.247e.artist+json"&lt;/span&gt;));&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;(Talk about a &lt;a href="http://en.wikipedia.org/wiki/Law_of_Demeter"&gt;Law of Demeter&lt;/a&gt; violation, BTW...)&lt;/p&gt;
&lt;p&gt;However, due to an over-reliance on global state, it's not so easy to figure out how one would go about mapping certain media types to only a single Controller. This was much easier in the WCF Web API because it was possible to assign a separate configuration instance to each Controller/Api/Resource/Service/Whatever... This, I've still to figure out how to do...&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=3fa91879-8089-4c25-a0a8-25d23d17404e"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,3fa91879-8089-4c25-a0a8-25d23d17404e.aspx</comments>
      <category>ASP.NET MVC</category>
      <category>Dependency Injection</category>
      <category>REST</category>
      <category>Services</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=e0678efd-d233-470d-916a-7a44a1aa1a68</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,e0678efd-d233-470d-916a-7a44a1aa1a68.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,e0678efd-d233-470d-916a-7a44a1aa1a68.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=e0678efd-d233-470d-916a-7a44a1aa1a68</wfw:commentRss>
      <slash:comments>16</slash:comments>
      <title>Implementing an Abstract Factory</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,e0678efd-d233-470d-916a-7a44a1aa1a68.aspx</guid>
      <link>http://blog.ploeh.dk/2012/03/15/ImplementingAnAbstractFactory.aspx</link>
      <pubDate>Thu, 15 Mar 2012 21:01:13 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/Abstract_factory_pattern"&gt;Abstract Factory&lt;/a&gt; is a tremendously useful pattern when used with Dependency Injection (DI). While I’ve &lt;a href="http://stackoverflow.com/questions/2280170/why-do-we-need-abstract-factory-design-pattern/2280289#2280289"&gt;repeatedly described&lt;/a&gt; how it can be used to solve various problems in DI, apparently I’ve never described how to implement one. As a comment to &lt;a href="http://blog.ploeh.dk/2010/01/20/EnablingDIForLazyComponents.aspx"&gt;an older blog post of mine&lt;/a&gt;, &lt;a href="http://blogs.codes-sources.com/tja/"&gt;Thomas Jaskula&lt;/a&gt; asks how I’d implement the IOrderShipperFactory.&lt;/p&gt; &lt;p&gt;To stay consistent with &lt;a href="http://blog.ploeh.dk/2010/01/20/EnablingDIForLazyComponents.aspx"&gt;the old order shipper scenario&lt;/a&gt;, this blog post outlines three alternative ways to implement the IOrderShipperFactory interface.&lt;/p&gt; &lt;p&gt;To make it a bit more challenging, the implementation should create instances of the OrderShipper2 class, which itself has a dependency:&lt;/p&gt; &lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;OrderShipper2&lt;/font&gt;&lt;/span&gt; : &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;IOrderShipper&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;private&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;readonly&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IChannel&lt;/font&gt;&lt;/span&gt; channel;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; OrderShipper2(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IChannel&lt;/font&gt;&lt;/span&gt; channel)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (channel == &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;null&lt;/font&gt;&lt;/span&gt;)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;throw&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ArgumentNullException&lt;/font&gt;&lt;/span&gt;(&lt;span style="color: "&gt;&lt;font color="#a31515"&gt;"channel"&lt;/font&gt;&lt;/span&gt;);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.channel = channel;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt;&lt;/span&gt; Ship(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Order&lt;/font&gt;&lt;/span&gt; order)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#008000"&gt;// Ship the order and&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#008000"&gt;// raise a domain event over this.channel&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In order to be able to create an instance of OrderShipper2, any factory implementation must be able to supply an IChannel instance.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Manually Coded Factory&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The first option is to manually wire up the OrderShipper2 instance within the factory:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ManualOrderShipperFactory&lt;/font&gt;&lt;/span&gt; :&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;IOrderShipperFactory&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;private&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;readonly&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IChannel&lt;/font&gt;&lt;/span&gt; channel;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; ManualOrderShipperFactory(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IChannel&lt;/font&gt;&lt;/span&gt; channel)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (channel == &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;null&lt;/font&gt;&lt;/span&gt;)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;throw&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ArgumentNullException&lt;/font&gt;&lt;/span&gt;(&lt;span style="color: "&gt;&lt;font color="#a31515"&gt;"channel"&lt;/font&gt;&lt;/span&gt;);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.channel = channel;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IOrderShipper&lt;/font&gt;&lt;/span&gt; Create()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;OrderShipper2&lt;/font&gt;&lt;/span&gt;(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.channel);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This has the advantage that it’s easy to understand. It can be unit tested and implemented in the same library that also contains OrderShipper2 itself. This means that any client of that library is supplied with a read-to-use implementation.&lt;/p&gt;
&lt;p&gt;The disadvantage of this approach is that if/when the constructor of OrderShipper2 changes, the ManualOrderShipperFactory class must also be corrected. Pragmatically, this may not be a big deal, but one could successfully argue that this violates the &lt;a href="http://en.wikipedia.org/wiki/Open/closed_principle"&gt;Open/Closed Principle&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Container-based Factory&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Another option is to make the implementation a thin &lt;a href="http://en.wikipedia.org/wiki/Adapter_pattern"&gt;Adapter&lt;/a&gt; over a DI Container – in this example &lt;a href="http://castleproject.org/container/index.html"&gt;Castle Windsor&lt;/a&gt;:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ContainerFactory&lt;/font&gt;&lt;/span&gt; : &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;IOrderShipperFactory&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;private&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IWindsorContainer&lt;/font&gt;&lt;/span&gt; container;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; ContainerFactory(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IWindsorContainer&lt;/font&gt;&lt;/span&gt; container)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (container == &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;null&lt;/font&gt;&lt;/span&gt;)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;throw&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ArgumentNullException&lt;/font&gt;&lt;/span&gt;(&lt;span style="color: "&gt;&lt;font color="#a31515"&gt;"container"&lt;/font&gt;&lt;/span&gt;);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.container = container;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IOrderShipper&lt;/font&gt;&lt;/span&gt; Create()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.container.Resolve&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IOrderShipper&lt;/font&gt;&lt;/span&gt;&amp;gt;();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;But wait! Isn’t this an application of the &lt;a href="http://blog.ploeh.dk/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx"&gt;Service Locator anti-pattern&lt;/a&gt;? Not if this class is part of the &lt;a href="http://blog.ploeh.dk/2011/07/28/CompositionRoot.aspx"&gt;Composition Root&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;If this implementation was placed in the same library as OrderShipper2 itself, it would mean that the library would have a hard dependency on the container. In such a case, it would certainly be a Service Locator.&lt;/p&gt;
&lt;p&gt;However, when a Composition Root already references a container, it makes sense to place the ContainerFactory class there. This changes its &lt;a href="http://blog.ploeh.dk/2011/08/25/ServiceLocatorRolesVsMechanics.aspx"&gt;role&lt;/a&gt; to the pure infrastructure component it really ought to be. This seems more SOLID, but the disadvantage is that there’s no longer a ready-to-use implementation packaged together with the LazyOrderShipper2 class. All new clients must supply their own implementation.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Dynamic Proxy&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The third option is to basically reduce the principle behind the container-based factory to its core. Why bother writing even a thin Adapter if one can be automatically generated.&lt;/p&gt;
&lt;p&gt;With Castle Windsor, the &lt;a href="http://stw.castleproject.org/Windsor.Typed-Factory-Facility-interface-based-factories.ashx"&gt;Typed Factory Facility&lt;/a&gt; makes this possible:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;container.AddFacility&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;TypedFactoryFacility&lt;/font&gt;&lt;/span&gt;&amp;gt;();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;container.Register(&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;Component&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .For&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IOrderShipperFactory&lt;/font&gt;&lt;/span&gt;&amp;gt;()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .AsFactory());&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; factory =&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; container.Resolve&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IOrderShipperFactory&lt;/font&gt;&lt;/span&gt;&amp;gt;();&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;There &lt;em&gt;is no longer&lt;/em&gt; any code which implements IOrderShipperFactory. Instead, a class conceptually similar to the ContainerFactory class above is dynamically generated and emitted at runtime.&lt;/p&gt;
&lt;p&gt;While the code never materializes, conceptually, such a dynamically emitted implementation is still part of the Composition Root.&lt;/p&gt;
&lt;p&gt;This approach has the advantage that it’s very DRY, but the disadvantages are similar to the container-based implementation above: there’s no longer a ready-to-use implementation. There’s also the additional disadvantage that out of the three alternative here outlined, the proxy-based implementation is the most difficult to understand.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=e0678efd-d233-470d-916a-7a44a1aa1a68"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,e0678efd-d233-470d-916a-7a44a1aa1a68.aspx</comments>
      <category>Castle Windsor</category>
      <category>Dependency Injection</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=94a1c53c-51cd-412e-a520-2bf28d527da3</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,94a1c53c-51cd-412e-a520-2bf28d527da3.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,94a1c53c-51cd-412e-a520-2bf28d527da3.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=94a1c53c-51cd-412e-a520-2bf28d527da3</wfw:commentRss>
      <slash:comments>26</slash:comments>
      <title>Is Layering Worth the Mapping?</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,94a1c53c-51cd-412e-a520-2bf28d527da3.aspx</guid>
      <link>http://blog.ploeh.dk/2012/02/09/IsLayeringWorthTheMapping.aspx</link>
      <pubDate>Thu, 09 Feb 2012 22:55:44 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;For years, layered application architecture has been a de-facto standard for loosely coupled architectures, but the question is: does the layering really provide benefit?&lt;/p&gt; &lt;p&gt;In theory, layering is a way to decouple concerns so that UI concerns or data access technologies don’t pollute the domain model. However, this style of architecture seems to come at a pretty steep price: there’s a lot of mapping going on between the layers. Is it really worth the price, or is it OK to define data structures that cut across the various layers?&lt;/p&gt; &lt;p&gt;The short answer is that if you cut across the layers, it’s no longer a layered application. However, the price of layering may still be too high.&lt;/p&gt; &lt;p&gt;In this post I’ll examine the problem and the proposed solution and demonstrate why none of them are particularly good. In the end, I’ll provide a pointer going forward.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Proper Layering&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;To understand the problem with layering, I’ll describe a fairly simple example. Assume that you are building a music service and you’ve been asked to render a top 10 list for the day. It would have to look something like this in a web page:&lt;/p&gt; &lt;p&gt;&lt;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="Top10Tracks" border="0" alt="Top10Tracks" src="http://blog.ploeh.dk/content/binary/Windows-Live-Writer/04fbdab3357e_11E24/Top10Tracks_1.png" width="494" height="250"&gt;&lt;/p&gt; &lt;p&gt;As part of rending the list, you must color the &lt;em&gt;Movement&lt;/em&gt; values accordingly using CSS.&lt;/p&gt; &lt;p&gt;A properly layered architecture would look something like this:&lt;/p&gt; &lt;p&gt;&lt;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="ProperLayering" border="0" alt="ProperLayering" src="http://blog.ploeh.dk/content/binary/Windows-Live-Writer/04fbdab3357e_11E24/ProperLayering_3.png" width="385" height="480"&gt;&lt;/p&gt; &lt;p&gt;Each layer defines some services and some data-carrying classes (&lt;em&gt;Entities&lt;/em&gt;, if you want to stick with the &lt;a href="http://www.amazon.com/gp/product/0321125215/ref=as_li_ss_tl?ie=UTF8&amp;amp;tag=ploeh-20&amp;amp;linkCode=as2&amp;amp;camp=1789&amp;amp;creative=390957&amp;amp;creativeASIN=0321125215"&gt;Domain-Driven Design&lt;/a&gt; terminology). The Track class is defined by the Domain layer, while the TopTrackViewModel class is defined in the User Interface layer, and so on. If you are wondering about why Track is used to communicate both up and down, this is because the Domain layer should be the most decoupled layer, so the other layers exist to serve it. In fact, this is just a vertical representation of the Ports and Adapters architecture, with the Domain Model sitting in the center.&lt;/p&gt; &lt;p&gt;This architecture is very decoupled, but comes at the cost of much mapping and seemingly redundant repetition. To demonstrate why that is, I’m going to show you some of the code. This is an ASP.NET MVC application, so the Controller is an obvious place to start:&lt;/p&gt; &lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ViewResult&lt;/font&gt;&lt;/span&gt; Index()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; date = &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;DateTime&lt;/font&gt;&lt;/span&gt;.Now.Date;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; topTracks = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.trackService.GetTopFor(date);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.View(topTracks);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This doesn’t look so bad. It asks an ITrackService for the top tracks for the day and returns a list of TopTrackViewModel instances. This is the implementation of the track service:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IEnumerable&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;TopTrackViewModel&lt;/font&gt;&lt;/span&gt;&amp;gt; GetTopFor(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;DateTime&lt;/font&gt;&lt;/span&gt; date)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; today = &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;DateTime&lt;/font&gt;&lt;/span&gt;.Now.Date;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; yesterDay = today - &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;TimeSpan&lt;/font&gt;&lt;/span&gt;.FromDays(1);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; todaysTracks = &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.repository.GetTopFor(today).ToList();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; yesterdaysTracks = &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.repository.GetTopFor(yesterDay).ToList();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; length = todaysTracks.Count;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; positions = &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Enumerable&lt;/font&gt;&lt;/span&gt;.Range(1, length);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;from&lt;/font&gt;&lt;/span&gt; tt &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;in&lt;/font&gt;&lt;/span&gt; todaysTracks.Zip(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; positions, (t, p) =&amp;gt;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; { Position = p, Track = t })&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;let&lt;/font&gt;&lt;/span&gt; yp = (&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;from&lt;/font&gt;&lt;/span&gt; yt &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;in&lt;/font&gt;&lt;/span&gt; yesterdaysTracks.Zip(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; positions, (t, p) =&amp;gt; &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Position = p,&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Track = t&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; })&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;where&lt;/font&gt;&lt;/span&gt; yt.Track.Id == tt.Track.Id&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;select&lt;/font&gt;&lt;/span&gt; yt.Position)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .DefaultIfEmpty(-1)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Single()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;let&lt;/font&gt;&lt;/span&gt; cssClass = GetCssClass(tt.Position, yp)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;select&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;TopTrackViewModel&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Position = tt.Position,&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Name = tt.Track.Name,&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Artist = tt.Track.Artist,&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CssClass = cssClass&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;private&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;static&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; GetCssClass(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt; todaysPosition, &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt; yesterdaysPosition)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (yesterdaysPosition &amp;lt; 0)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#a31515"&gt;"new"&lt;/font&gt;&lt;/span&gt;;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (todaysPosition &amp;lt; yesterdaysPosition)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#a31515"&gt;"up"&lt;/font&gt;&lt;/span&gt;;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (todaysPosition == yesterdaysPosition)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#a31515"&gt;"same"&lt;/font&gt;&lt;/span&gt;;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#a31515"&gt;"down"&lt;/font&gt;&lt;/span&gt;;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;While that looks fairly complex, there’s really not a lot of mapping going on. Most of the work is spent getting the top 10 track for today and yesterday. For each position on today’s top 10, the query finds the position of the same track on yesterday’s top 10 and creates a TopTrackViewModel instance accordingly.&lt;/p&gt;
&lt;p&gt;Here’s the only mapping code involved:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;select&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;TopTrackViewModel&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Position = tt.Position,&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Name = tt.Track.Name,&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Artist = tt.Track.Artist,&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; CssClass = cssClass&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;};&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This maps from a Track (a Domain class) to a TopTrackViewModel (a UI class).&lt;/p&gt;
&lt;p&gt;This is the relevant implementation of the repository:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IEnumerable&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Track&lt;/font&gt;&lt;/span&gt;&amp;gt; GetTopFor(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;DateTime&lt;/font&gt;&lt;/span&gt; date)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; dbTracks = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.GetTopTracks(date);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;foreach&lt;/font&gt;&lt;/span&gt; (&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; dbTrack &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;in&lt;/font&gt;&lt;/span&gt; dbTracks)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;yield&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Track&lt;/font&gt;&lt;/span&gt;(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dbTrack.Id,&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dbTrack.Name,&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dbTrack.Artist);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You may be wondering about the translation from DbTrack to Track. In this case you can assume that the DbTrack class is a class representation of a database table, modeled along the lines of your favorite ORM. The Track class, on the other hand, is a proper object-oriented class which &lt;a href="http://blog.ploeh.dk/2011/05/24/PokayokeDesignFromSmellToFragrance.aspx"&gt;protects its invariants&lt;/a&gt;:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;Track&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;private&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;readonly&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt; id;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;private&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; name;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;private&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; artist;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; Track(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt; id, &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; name, &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; artist)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (name == &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;null&lt;/font&gt;&lt;/span&gt;)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;throw&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ArgumentNullException&lt;/font&gt;&lt;/span&gt;(&lt;span style="color: "&gt;&lt;font color="#a31515"&gt;"name"&lt;/font&gt;&lt;/span&gt;);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (artist == &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;null&lt;/font&gt;&lt;/span&gt;)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;throw&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ArgumentNullException&lt;/font&gt;&lt;/span&gt;(&lt;span style="color: "&gt;&lt;font color="#a31515"&gt;"artist"&lt;/font&gt;&lt;/span&gt;);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.id = id;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.name = name;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.artist = artist;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt; Id&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;get&lt;/font&gt;&lt;/span&gt; { &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.id; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; Name&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;get&lt;/font&gt;&lt;/span&gt; { &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.name; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#0000ff"&gt;set&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;value&lt;/font&gt;&lt;/span&gt; == &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;null&lt;/font&gt;&lt;/span&gt;)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;throw&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ArgumentNullException&lt;/font&gt;&lt;/span&gt;(&lt;span style="color: "&gt;&lt;font color="#a31515"&gt;"value"&lt;/font&gt;&lt;/span&gt;);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.name = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;value&lt;/font&gt;&lt;/span&gt;;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; Artist&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;get&lt;/font&gt;&lt;/span&gt; { &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.artist; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#0000ff"&gt;set&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;value&lt;/font&gt;&lt;/span&gt; == &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;null&lt;/font&gt;&lt;/span&gt;)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;throw&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ArgumentNullException&lt;/font&gt;&lt;/span&gt;(&lt;span style="color: "&gt;&lt;font color="#a31515"&gt;"value"&lt;/font&gt;&lt;/span&gt;);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.artist = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;value&lt;/font&gt;&lt;/span&gt;;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;No ORM I’ve encountered so far has been able to properly address such invariants – particularly the non-default constructor seems to be a showstopper. This is the reason a separate DbTrack class is required, even for ORMs with so-called POCO support.&lt;/p&gt;
&lt;p&gt;In summary, that’s a lot of mapping. What would be involved if a new field is required in the top 10 table? Imagine that you are being asked to provide the release label as an extra column.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;A Label column must be added to the database schema and the DbTrack class. 
&lt;li&gt;A Label property must be added to the Track class. 
&lt;li&gt;The mapping from DbTrack to Track must be updated. 
&lt;li&gt;A Label property must be added to the TopTrackViewModel class. 
&lt;li&gt;The mapping from Track to TopTrackViewModel must be updated. 
&lt;li&gt;The UI must be updated.&lt;/li&gt;&lt;/ol&gt;
&lt;p&gt;That’s a lot of work in order to add a single data element, and this is even a read-only scenario! Is it really worth it?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cross-Cutting Entities&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Is strict separation between layers really so important? What would happen if Entities were allowed to travel across all layers? Would that really be so bad?&lt;/p&gt;
&lt;p&gt;Such an architecture is often drawn like this:&lt;/p&gt;
&lt;p&gt;&lt;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="CrossCuttingEntities" border="0" alt="CrossCuttingEntities" src="http://blog.ploeh.dk/content/binary/Windows-Live-Writer/04fbdab3357e_11E24/CrossCuttingEntities_3.png" width="441" height="480"&gt;&lt;/p&gt;
&lt;p&gt;Now, a single Track class is allowed to travel from layer to layer in order to avoid mapping. The controller code hasn’t really changed, although the model returned to the View is no longer a sequence of TopTrackViewModel, but simply a sequence of Track instances:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ViewResult&lt;/font&gt;&lt;/span&gt; Index()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; date = &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;DateTime&lt;/font&gt;&lt;/span&gt;.Now.Date;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; topTracks = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.trackService.GetTopFor(date);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.View(topTracks);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The GetTopFor method also looks familiar:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IEnumerable&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Track&lt;/font&gt;&lt;/span&gt;&amp;gt; GetTopFor(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;DateTime&lt;/font&gt;&lt;/span&gt; date)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; today = &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;DateTime&lt;/font&gt;&lt;/span&gt;.Now.Date;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; yesterDay = today - &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;TimeSpan&lt;/font&gt;&lt;/span&gt;.FromDays(1);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; todaysTracks = &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.repository.GetTopFor(today).ToList();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; yesterdaysTracks = &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.repository.GetTopFor(yesterDay).ToList();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; length = todaysTracks.Count;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; positions = &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Enumerable&lt;/font&gt;&lt;/span&gt;.Range(1, length);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;from&lt;/font&gt;&lt;/span&gt; tt &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;in&lt;/font&gt;&lt;/span&gt; todaysTracks.Zip(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; positions, (t, p) =&amp;gt; &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; { Position = p, Track = t })&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;let&lt;/font&gt;&lt;/span&gt; yp = (&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;from&lt;/font&gt;&lt;/span&gt; yt &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;in&lt;/font&gt;&lt;/span&gt; yesterdaysTracks.Zip(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; positions, (t, p) =&amp;gt;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Position = p,&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Track = t&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; })&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;where&lt;/font&gt;&lt;/span&gt; yt.Track.Id == tt.Track.Id&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;select&lt;/font&gt;&lt;/span&gt; yt.Position)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .DefaultIfEmpty(-1)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Single()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;let&lt;/font&gt;&lt;/span&gt; cssClass = GetCssClass(tt.Position, yp)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;select&lt;/font&gt;&lt;/span&gt; Enrich(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tt.Track, tt.Position, cssClass);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;private&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;static&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; GetCssClass(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt; todaysPosition, &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt; yesterdaysPosition)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (yesterdaysPosition &amp;lt; 0)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#a31515"&gt;"new"&lt;/font&gt;&lt;/span&gt;;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (todaysPosition &amp;lt; yesterdaysPosition)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#a31515"&gt;"up"&lt;/font&gt;&lt;/span&gt;;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (todaysPosition == yesterdaysPosition)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#a31515"&gt;"same"&lt;/font&gt;&lt;/span&gt;;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#a31515"&gt;"down"&lt;/font&gt;&lt;/span&gt;;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;private&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;static&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Track&lt;/font&gt;&lt;/span&gt; Enrich(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Track&lt;/font&gt;&lt;/span&gt; track, &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt; position, &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; cssClass)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; track.Position = position;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; track.CssClass = cssClass;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; track;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Whether or not much has been gained is likely to be a subjective assessment. While mapping is no longer taking place, it’s still necessary to assign a CSS Class and Position to the track before handing it off to the View. This is the responsibility of the new Enrich method:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;private&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;static&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Track&lt;/font&gt;&lt;/span&gt; Enrich(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Track&lt;/font&gt;&lt;/span&gt; track, &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt; position, &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; cssClass)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; track.Position = position;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; track.CssClass = cssClass;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; track;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If not much is gained at the UI layer, perhaps the data access layer has become simpler? This is, indeed, the case:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IEnumerable&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Track&lt;/font&gt;&lt;/span&gt;&amp;gt; GetTopFor(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;DateTime&lt;/font&gt;&lt;/span&gt; date)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.GetTopTracks(date);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If, hypothetically, you were asked to add a label to the top 10 table it would be much simpler:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;A Label column must be added to the database schema and the Track class. 
&lt;li&gt;The UI must be updated.&lt;/li&gt;&lt;/ol&gt;
&lt;p&gt;This looks good. Are there any disadvantages? Yes, certainly. Consider the Track class:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;Track&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt; Id { &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;get&lt;/font&gt;&lt;/span&gt;; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;set&lt;/font&gt;&lt;/span&gt;; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; Name { &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;get&lt;/font&gt;&lt;/span&gt;; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;set&lt;/font&gt;&lt;/span&gt;; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; Artist { &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;get&lt;/font&gt;&lt;/span&gt;; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;set&lt;/font&gt;&lt;/span&gt;; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt; Position { &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;get&lt;/font&gt;&lt;/span&gt;; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;set&lt;/font&gt;&lt;/span&gt;; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; CssClass { &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;get&lt;/font&gt;&lt;/span&gt;; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;set&lt;/font&gt;&lt;/span&gt;; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It also looks simpler than before, but this is actually not particularly beneficial, as it doesn’t protect its invariants. In order to play nice with the ORM of your choice, it must have a &lt;a href="http://blog.ploeh.dk/2011/05/30/DesignSmellDefaultConstructor.aspx"&gt;default constructor&lt;/a&gt;. It also has &lt;a href="http://blog.ploeh.dk/2011/05/26/CodeSmellAutomaticProperty.aspx"&gt;automatic properties&lt;/a&gt;. However, most insidiously, it also somehow gained the Position and CssClass properties.&lt;/p&gt;
&lt;p&gt;What does the Position property imply outside of the context of a top 10 list? A position in relation to what?&lt;/p&gt;
&lt;p&gt;Even worse, why do we have a property called CssClass? CSS is a very web-specific technology so why is this property available to the Data Access and Domain layers? How does this fit if you are ever asked to build a native desktop app based on the Domain Model? Or a REST API? Or a batch job?&lt;/p&gt;
&lt;p&gt;When Entities are allowed to travel along layers, the layers basically collapse. UI concerns and data access concerns will inevitably be mixed up. You may think you have layers, but you don’t.&lt;/p&gt;
&lt;p&gt;Is that such a bad thing, though?&lt;/p&gt;
&lt;p&gt;Perhaps not, but I think it’s worth pointing out:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The choice is whether or not you want to build a layered application. If you want layering, the separation must be strict. If it isn’t, it’s not a layered application.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;There may be great benefits to be gained from allowing Entities to travel from database to user interface. Much mapping cost goes away, but you must realize that then you’re no longer building a layered application – now you’re building a web application (or whichever other type of app you’re initially building).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Further Thoughts&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;It’s a common question: &lt;em&gt;how hard is it to add a new field to the user interface?&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The underlying assumption is that the field must somehow originate from a corresponding database column. If this is the case, mapping seems to be in the way.&lt;/p&gt;
&lt;p&gt;However, if this is the major concern about the application you’re currently building, it’s a strong indication that you are building a CRUD application. If that’s the case, &lt;a href="http://msdn.microsoft.com/en-us/magazine/ee236415.aspx"&gt;you probably don’t need a Domain Model at all&lt;/a&gt;. Go ahead and let your ORM POCO classes travel up and down the stack, but don’t bother creating layers: you’ll be building a monolithic application no matter how hard you try not to.&lt;/p&gt;
&lt;p&gt;In the end it looks as though none of the options outlined in this article are particularly good. Strict layering leads to too much mapping, and no mapping leads to monolithic applications. Personally, I’ve certainly written quite a lot of strictly layered applications, and while the separation of concerns was good, I was never happy with the mapping overhead.&lt;/p&gt;
&lt;p&gt;At the heart of the problem is the CRUDy nature of many applications. In such applications, complex object graphs are traveling up and down the stack. The trick is to avoid complex object graphs.&lt;/p&gt;
&lt;p&gt;Move less data around and things are likely to become simpler. This is one of the many interesting promises of &lt;a href="http://abdullin.com/cqrs"&gt;CQRS&lt;/a&gt;, and particularly it’s asymmetric approach to data.&lt;/p&gt;
&lt;p&gt;To be honest, I wish I had fully realized this when I started writing &lt;a href="http://affiliate.manning.com/idevaffiliate.php?id=1150_236"&gt;my book&lt;/a&gt;, but when I finally realized what I’d implicitly felt for long, it was to late to change direction. Rest assured that nothing in the book is fundamentally flawed. The patterns etc. still hold, but the examples could have been cleaner if the sample applications had taken a CQRS-like direction instead of strict layering.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=94a1c53c-51cd-412e-a520-2bf28d527da3"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,94a1c53c-51cd-412e-a520-2bf28d527da3.aspx</comments>
      <category>Dependency Injection</category>
      <category>Software Design</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=45e8361f-bbed-4b7e-8632-ff28a7f11c05</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,45e8361f-bbed-4b7e-8632-ff28a7f11c05.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,45e8361f-bbed-4b7e-8632-ff28a7f11c05.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=45e8361f-bbed-4b7e-8632-ff28a7f11c05</wfw:commentRss>
      <slash:comments>9</slash:comments>
      <title>Loose Coupling and the Big Picture</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,45e8361f-bbed-4b7e-8632-ff28a7f11c05.aspx</guid>
      <link>http://blog.ploeh.dk/2012/02/02/LooseCouplingAndTheBigPicture.aspx</link>
      <pubDate>Thu, 02 Feb 2012 20:37:40 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;A common criticism of loosely coupled code is that it’s harder to understand. How do you see the big picture of an application when loose coupling is everywhere? When the entire code base has been programmed against interfaces instead of concrete classes, how do we understand how the objects are wired and how they interact?&lt;/p&gt; &lt;p&gt;In this post, I’ll provide answers on various levels, from high-level architecture over object-oriented principles to more nitty-gritty code. Before I do that, however, I’d like to pose a set of questions you should always be prepared to answer.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Mu&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;My first reaction to that sort of question is: &lt;em&gt;you say loosely coupled code is harder to understand. Harder than what?&lt;/em&gt;&lt;/p&gt; &lt;p&gt;If we are talking about a non-trivial application, odds are that it’s going to take some time to understand the code base – whether or not it’s loosely coupled. Agreed: understanding a loosely coupled code base takes some work, but so does understanding a tightly coupled code base. The question is whether it’s &lt;em&gt;harder&lt;/em&gt; to understand a loosely coupled code base?&lt;/p&gt; &lt;p&gt;Imagine that I’m having a discussion about this subject with Mary Rowan from &lt;a href="http://affiliate.manning.com/idevaffiliate.php?id=1150_236"&gt;my book&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Mary:&lt;/strong&gt; “Loosely coupled code is harder to understand.”&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Me:&lt;/strong&gt; “Why do you think that is?”&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Mary:&lt;/strong&gt; “It’s very hard to navigate the code base because I always end up at an interface.”&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Me:&lt;/strong&gt; “Why is that a problem?”&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Mary:&lt;/strong&gt; “Because I don’t know what the interface &lt;em&gt;does&lt;/em&gt;.”&lt;/p&gt; &lt;p&gt;At this point I’m very tempted to answer &lt;a href="http://en.wikipedia.org/wiki/Mu_%28negative%29"&gt;Mu&lt;/a&gt;. An interfaces doesn’t &lt;em&gt;do&lt;/em&gt; anything – that’s the whole point of it. According to the &lt;a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle"&gt;Liskov Substitution Principle&lt;/a&gt; (LSP), a consumer shouldn’t have to care about what happens on the other side of the interface.&lt;/p&gt; &lt;p&gt;However, developers used to tightly coupled code aren’t used to think about services in this way. They are used to navigate the code base from consumer to service to understand how the two of them interact, and I will gladly admit this: in principle, that’s &lt;em&gt;impossible&lt;/em&gt; to do in a loosely coupled code base. I’ll return to this subject in a little while, but first I want to discuss some strategies for understanding a loosely coupled code base.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Architecture and Documentation&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Yes: documentation. Don’t dismiss it. While I agree with Uncle Bob and like-minded spirits that the code &lt;em&gt;is&lt;/em&gt; the documentation, a two-page document that outlines the Big Picture might save you from many hours of code archeology.&lt;/p&gt; &lt;p&gt;The typical agile mindset is to minimize documentation because it tends to lose touch with the code base, but even so, it should be possible to maintain a two-page high-level document so that it stays up to date. Consider the alternative: if you have so much architectural churn that even a two-page overview regularly falls behind, then you’re probably having a greater problem than understanding your loosely coupled code base.&lt;/p&gt; &lt;p&gt;Maintaining such a document isn’t adverse to the agile spirit. You’ll find the same advice in &lt;a href="http://www.amazon.com/gp/product/0470684208/ref=as_li_ss_tl?ie=UTF8&amp;amp;tag=ploeh-20&amp;amp;linkCode=as2&amp;amp;camp=1789&amp;amp;creative=390957&amp;amp;creativeASIN=0470684208"&gt;Lean Architecture&lt;/a&gt; (p. 127). Don’t underestimate the value of such a document.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;See the Forest Instead of the Trees&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Understanding a loosely coupled code base typically tends to require a shift of mindset.&lt;/p&gt; &lt;p&gt;Recall my discussion with Mary Rowan. The criticism of loose coupling is that it’s difficult to understand which collaborators are being invoked. A developer like Mary Rowan is used to learn a code base by understanding all the myriad concrete details of it. In effect, while there may be ‘classes’ around, there are &lt;em&gt;no&lt;/em&gt; abstractions in place. In order to understand the behavior of a user interface element, it’s necessary to also understand what’s happening in the database – and everything in between.&lt;/p&gt; &lt;p&gt;A loosely coupled code base shouldn’t be like that.&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;The entire purpose of loose coupling is that we should be able to reason about a part (a ‘unit’, if you will) without understanding the whole.&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;In a tightly coupled code base, it’s often impossible to see the forest for the trees. Although we developers are good at relating to details, a tightly coupled code base requires us to be able to contain &lt;em&gt;the entire&lt;/em&gt; code base in our heads in order to understand it. As the size of the code base grows, this becomes increasingly difficult.&lt;/p&gt; &lt;p&gt;In a loosely coupled code base, on the other hand, it should be possible to understand smaller parts in isolation. However, I purposely wrote “should be”, because that’s not always the case. Often, a so-called “loosely coupled” code base violates basic principles of object-oriented design.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;RAP&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;The criticism that it’s hard to see “what’s on the other side of an interface” is, in my opinion, central. It betrays a mindset which is still tightly coupled.&lt;/p&gt; &lt;p&gt;In many code bases there’s often a single implementation of a given interface, so developers can be forgiven if they think about an interface as only a piece of friction that prevents them from reaching the concrete class on the other side. However, if that’s the case with most of the interfaces in a code base, it signifies a violation of the &lt;a href="http://codemanship.co.uk/parlezuml/blog/?postid=934"&gt;Reused Abstractions Principle&lt;/a&gt; (RAP) more than it signifies loose coupling.&lt;/p&gt; &lt;p&gt;Jim Cooper, a reader of my book, put it quite eloquently &lt;a href="http://www.manning-sandbox.com/thread.jspa?threadID=48257"&gt;on the book’s forum&lt;/a&gt;:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;“So many people think that using an interface magically decouples their code. It doesn't. It only changes the nature of the coupling. If you don't believe that, try changing a method signature in an interface - none of the code containing method references or any of the implementing classes will compile. I call that coupled”&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;Refactoring tools aside, I completely agree with this statement. The RAP is a test we can use to verify whether or not an interface is truly reusable – what better test is there than to actually reuse your interfaces?&lt;/p&gt; &lt;p&gt;The corollary of this discussion is that if a code base is massively violating the RAP then it’s going to be hard to understand. It has all the disadvantages of loose coupling with few of the benefits. If that’s the case, you would gain more benefit from making it either more tightly coupled or truly loosely coupled.&lt;/p&gt; &lt;p&gt;What does “truly loosely coupled” mean?&lt;/p&gt; &lt;p&gt;&lt;strong&gt;LSP&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;According to the LSP a consumer &lt;em&gt;must not&lt;/em&gt; concern itself with “what’s on the other side of the interface”. It should be possible to replace any implementation with any other implementation of the same interface without changing the correctness of the program.&lt;/p&gt; &lt;p&gt;This is why I previously said that in a truly loosely coupled code base, it isn’t ‘hard’ to understand “what’s on the other side of the interface” – it’s &lt;em&gt;impossible&lt;/em&gt;. At design-time, there’s &lt;em&gt;nothing&lt;/em&gt; ‘behind’ the interface. The interface is what you are programming against. It’s all there is.&lt;/p&gt; &lt;p&gt;Mary has been listening to all of this, and now she protests:&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Mary:&lt;/strong&gt; “At run-time, there’s going to be a concrete class behind the interface.”&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Me&lt;/strong&gt; (being annoyingly pedantic): “Not quite. There’s going to be an instance of a concrete class which the consumer invokes via the interface it implements.”&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Mary:&lt;/strong&gt; “Yes, but I still need to know which concrete class is going to be invoked.”&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Me:&lt;/strong&gt; “Why?”&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Mary:&lt;/strong&gt; “Because otherwise I don’t know what’s going to happen when I invoke the method.”&lt;/p&gt; &lt;p&gt;This type of answer often betrays a much more fundamental problem in a code base.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;CQS&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Now we are getting into the nitty-gritty details of class design. What would you expect that the following method does?&lt;/p&gt; &lt;div style="font-family: ; background: white; color: "&gt; &lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;List&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Order&lt;/font&gt;&lt;/span&gt;&amp;gt; GetOpenOrders(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Customer&lt;/font&gt;&lt;/span&gt; customer)&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;The method name indicates that it gets open orders, and the signature seems to back it up. A single database query might be involved, since this looks a lot like a read-operation. A quick glance at the implementation seems to verify that first impression:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;List&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Order&lt;/font&gt;&lt;/span&gt;&amp;gt; GetOpenOrders(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Customer&lt;/font&gt;&lt;/span&gt; customer)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; orders = GetOrders(customer);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; (&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;from&lt;/font&gt;&lt;/span&gt; o &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;in&lt;/font&gt;&lt;/span&gt; orders&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;where&lt;/font&gt;&lt;/span&gt; o.Status == &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;OrderStatus&lt;/font&gt;&lt;/span&gt;.Open&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;select&lt;/font&gt;&lt;/span&gt; o).ToList();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Is it safe to assume that this is a side-effect-free method call? As it turns out, this is far from the case in this particular code base:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;private&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;List&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Order&lt;/font&gt;&lt;/span&gt;&amp;gt; GetOrders(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Customer&lt;/font&gt;&lt;/span&gt; customer)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; gw = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;CustomerGateway&lt;/font&gt;&lt;/span&gt;(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.ConnectionString);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; orders = gw.GetOrders(customer);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; AuditOrders(orders);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; FixCustomer(gw, orders, customer);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; orders;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The devil is in the details. What does AuditOrders do? And what does FixCustomer do? One method at a time:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;private&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt;&lt;/span&gt; AuditOrders(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;List&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Order&lt;/font&gt;&lt;/span&gt;&amp;gt; orders)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; user = &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Thread&lt;/font&gt;&lt;/span&gt;.CurrentPrincipal.Identity.ToString();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; gw = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;OrderGateway&lt;/font&gt;&lt;/span&gt;(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.ConnectionString);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;foreach&lt;/font&gt;&lt;/span&gt; (&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; o &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;in&lt;/font&gt;&lt;/span&gt; orders)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; clone = o.Clone();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; ar = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;AuditRecord&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Time = &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;DateTime&lt;/font&gt;&lt;/span&gt;.Now,&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; User = user&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; clone.AuditTrail.Add(ar);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gw.Update(clone);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#008000"&gt;// We don't want the consumer to see the audit trail.&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; o.AuditTrail.Clear();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;OK, it turns out that this method actually makes a &lt;em&gt;copy&lt;/em&gt; of &lt;em&gt;each and every&lt;/em&gt; order and updates that copy, writing it back to the database in order to leave behind an audit trail. It also &lt;em&gt;mutates&lt;/em&gt; each order before returning to the caller. Not only does this method result in an unexpected N+1 problem, it also &lt;em&gt;mutates its input&lt;/em&gt;, and perhaps even more surprising, it’s leaving the system in a state where the in-memory object is different from the database. This could lead to some pretty interesting bugs.&lt;/p&gt;
&lt;p&gt;Then what happens in the FixCustomer method? This:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#008000"&gt;// Fixes the customer status field if there were orders&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#008000"&gt;// added directly through the database.&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;private&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;static&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt;&lt;/span&gt; FixCustomer(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;CustomerGateway&lt;/font&gt;&lt;/span&gt; gw,&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;List&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Order&lt;/font&gt;&lt;/span&gt;&amp;gt; orders, &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Customer&lt;/font&gt;&lt;/span&gt; customer)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; total = orders.Sum(o =&amp;gt; o.Total);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (customer.Status != &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;CustomerStatus&lt;/font&gt;&lt;/span&gt;.Preferred&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;amp;&amp;amp; total &amp;gt; PreferredThreshold)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; customer.Status = &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;CustomerStatus&lt;/font&gt;&lt;/span&gt;.Preferred;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gw.Update(customer);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Another potential database write operation, as it turns out – complete with &lt;a href="http://butunclebob.com/ArticleS.TimOttinger.ApologizeIncode"&gt;an apology&lt;/a&gt;. Now that we’ve learned all about the details of the code, even the GetOpenOrders method is beginning to look suspect. The GetOrders method returns all orders, with the side effect that &lt;em&gt;all&lt;/em&gt; orders were audited as having been read by the user, but the GetOpenOrders filters the output. In the end, it turns out that we can’t even trust the audit trail.&lt;/p&gt;
&lt;p&gt;While I must apologize for this contrived example of a &lt;a href="http://martinfowler.com/eaaCatalog/transactionScript.html"&gt;Transaction Script&lt;/a&gt;, it’s clear that when code looks like that, it’s no wonder why developers think that it’s necessary to contain the entire code base in their heads. When this is the case, interfaces are only in the way.&lt;/p&gt;
&lt;p&gt;However, this is not the fault of loose coupling, but rather a failure to adhere to the very fundamental principle of &lt;a href="http://en.wikipedia.org/wiki/Command-query_separation"&gt;Command-Query Separation&lt;/a&gt; (CQS). You should be able to tell from the method signature alone whether invoking the method will or will not have side-effects. This is one of the key messages from &lt;a href="http://www.amazon.com/gp/product/0132350882/ref=as_li_ss_tl?ie=UTF8&amp;amp;tag=ploeh-20&amp;amp;linkCode=as2&amp;amp;camp=1789&amp;amp;creative=390957&amp;amp;creativeASIN=0132350882"&gt;Clean Code&lt;/a&gt;: the method name and signature is an abstraction. You should be able to reason about the behavior of the method from its declaration. You shouldn’t have to read the code to get an idea about what it does.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Abstractions always hide details. Method declarations do too. The point is that you should be able to read just the method declaration in order to gain a basic understanding of what’s going on. You can always return to the method’s code later in order to understand detail, but reading the method declaration alone should provide the Big Picture.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Strictly adhering to CQS goes a long way in enabling you to understand a loosely coupled code base. If you can reason about methods at a high level, you don’t need to see “the other side of the interface” in order to understand the Big Picture.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Stack Traces&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Still, even in a loosely coupled code base with great test coverage, integration issues arise. While each class works fine in isolation, when you integrate them, sometimes the interactions between them cause errors. This is often because of incorrect assumptions about the collaborators, which often indicates that the LSP was somehow violated.&lt;/p&gt;
&lt;p&gt;To understand why such errors occur, we need to understand which concrete classes are interacting. How do we do that in a loosely coupled code base?&lt;/p&gt;
&lt;p&gt;That’s actually easy: look at the stack trace from your error report. If your error report doesn’t include a stack trace, make sure that it’s going to do that in the future.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The stack trace is one of the most important troubleshooting tools in a loosely coupled code base, because it’s going to tell you exactly which classes were interacting when an exception was thrown.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Furthermore, if the code base also adheres to the &lt;a href="http://en.wikipedia.org/wiki/Single_responsibility_principle"&gt;Single Responsibility Principle&lt;/a&gt; and the ideals from Clean Code, each method should be very small (under 15 lines of code). If that’s the case, you can often understand the exact nature of the error from the stack trace and the error message alone. It shouldn’t even be necessary to attach a debugger to understand the bug, but in a pinch, you can still do that.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Tooling&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Returning to the original question, I often hear people advocating tools such as IDE add-ins which support navigation across interfaces. Such tools might provide a menu option which enables you to “go to implementation”. At this point it should be clear that such a tool is mainly going to be helpful in code bases that violate the RAP.&lt;/p&gt;
&lt;p&gt;(I’m not naming any particular tools here because I’m not interested in turning this post into a discussion about the merits of various specific tools.)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;It’s the responsibility of the loosely coupled code base to make sure that it’s easy to understand the Big Picture and that it’s easy to work with. In the end, that responsibility falls on the developers who &lt;em&gt;write&lt;/em&gt; the code – not the developer who’s trying to understand it.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=45e8361f-bbed-4b7e-8632-ff28a7f11c05"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,45e8361f-bbed-4b7e-8632-ff28a7f11c05.aspx</comments>
      <category>Dependency Injection</category>
      <category>Software Design</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=301fac13-cb4c-4081-9985-559ca8fbe358</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,301fac13-cb4c-4081-9985-559ca8fbe358.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,301fac13-cb4c-4081-9985-559ca8fbe358.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=301fac13-cb4c-4081-9985-559ca8fbe358</wfw:commentRss>
      <slash:comments>13</slash:comments>
      <title>SOLID is Append-only</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,301fac13-cb4c-4081-9985-559ca8fbe358.aspx</guid>
      <link>http://blog.ploeh.dk/2012/01/03/SOLIDIsAppendonly.aspx</link>
      <pubDate>Tue, 03 Jan 2012 14:43:47 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/Solid_%28object-oriented_design%29"&gt;SOLID&lt;/a&gt; is a set of principles that, if applied consistently, has some surprising effect on code. In a &lt;a href="http://blog.ploeh.dk/2011/06/07/SOLIDCodeIsnt.aspx"&gt;previous post&lt;/a&gt; I provided a sketch of what it means to meticulously apply the &lt;a href="http://en.wikipedia.org/wiki/Single_responsibility_principle"&gt;Single Responsibility Principle&lt;/a&gt;. In this article I will describe what happens when you follow the &lt;a href="http://en.wikipedia.org/wiki/Open/closed_principle"&gt;Open/Closed Principle&lt;/a&gt; (OCP) to its logical conclusion.&lt;/p&gt; &lt;p&gt;In case a refresher is required, the OCP states that a class should be open for extension, but &lt;em&gt;closed for modification&lt;/em&gt;. It seems to me that people often forget the second part. What does it mean?&lt;/p&gt; &lt;p&gt;It means that once implemented, you &lt;em&gt;shouldn’t touch that piece of code ever again&lt;/em&gt; (unless you need to correct a bug).&lt;/p&gt; &lt;p&gt;Then how can new functionality be added to a code base? This is still possible through either inheritance or polymorphic recomposition. Since the L in SOLID signifies the &lt;a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle"&gt;Liskov Substitution Principle&lt;/a&gt;, SOLID code tends to be based on loosely coupled code &lt;em&gt;composed&lt;/em&gt; into an application through copious use of interfaces – basically, &lt;a href="http://en.wikipedia.org/wiki/Strategy_pattern"&gt;Strategies&lt;/a&gt; injected into other Strategies and so on (also due to &lt;a href="http://en.wikipedia.org/wiki/Dependency_inversion_principle"&gt;Dependency Inversion Principle&lt;/a&gt;). In order to add functionality, you can create new implementations of these interfaces and redefine the application’s &lt;a href="http://blog.ploeh.dk/2011/07/28/CompositionRoot.aspx"&gt;Composition Root&lt;/a&gt;. Perhaps you’d be wrapping existing functionality in a &lt;a href="http://en.wikipedia.org/wiki/Decorator_pattern"&gt;Decorator&lt;/a&gt; or adding it to a &lt;a href="http://en.wikipedia.org/wiki/Composite_pattern"&gt;Composite&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;Once in a while, you’ll stop using an old implementation of an interface. Should you then delete this implementation? What would be the point? At a certain point in time, this implementation was valuable. Maybe it will become valuable again. Leaving it as an potential building block seems a better choice.&lt;/p&gt; &lt;p&gt;Thus, if we think about working with code as a CRUD endeavor, SOLID code can be Created and Read, but never Updated or Deleted. In other words, true SOLID code is append-only code.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Example: Changing AutoFixture’s Number Generation Algorithm&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;In early 2011 &lt;a href="http://autofixture.codeplex.com/workitem/4223"&gt;an issue was reported&lt;/a&gt; for &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt;: Anonymous numbers were created in monotonically increasing sequences, but with separate sequences for each number type:&lt;/p&gt; &lt;p&gt;integers: 1, 2, 3, 4, 5, …&lt;/p&gt; &lt;p&gt;decimals: 1.0, 2.0, 3.0, 4.0, 5.0, …&lt;/p&gt; &lt;p&gt;and so on. However, the person reporting the issue thought it made more sense if all numbers shared a single sequence. After thinking about it a little while, I agreed.&lt;/p&gt; &lt;p&gt;Because the AutoFixture code base is fairly SOLID we decided to leave the old implementations in place and implement the new behavior in new classes.&lt;/p&gt; &lt;p&gt;The old behavior was composed from a set of ISpecimenBuilders. As an example, integers were generated by this class:&lt;/p&gt; &lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Int32SequenceGenerator&lt;/font&gt;&lt;/span&gt; : &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;ISpecimenBuilder&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;private&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt; i;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt; CreateAnonymous()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Interlocked&lt;/font&gt;&lt;/span&gt;.Increment(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;ref&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.i);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;object&lt;/font&gt;&lt;/span&gt; Create(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;object&lt;/font&gt;&lt;/span&gt; request,&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ISpecimenContext&lt;/font&gt;&lt;/span&gt; context)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (request != &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;typeof&lt;/font&gt;&lt;/span&gt;(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt;))&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;NoSpecimen&lt;/font&gt;&lt;/span&gt;(request);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.CreateAnonymous();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Similar implementations generated decimals, floats, doubles, etc. Instead of modifying any of these classes, we left them in the code base and created a new ISpecimenBuilder that generates all numbers from a single sequence:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;NumericSequenceGenerator&lt;/font&gt;&lt;/span&gt; : &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;ISpecimenBuilder&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;private&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt; value;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;object&lt;/font&gt;&lt;/span&gt; Create(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;object&lt;/font&gt;&lt;/span&gt; request,&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ISpecimenContext&lt;/font&gt;&lt;/span&gt; context)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; type = request &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;as&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Type&lt;/font&gt;&lt;/span&gt;;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (type == &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;null&lt;/font&gt;&lt;/span&gt;)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;NoSpecimen&lt;/font&gt;&lt;/span&gt;(request);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.CreateNumericSpecimen(type);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;private&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;object&lt;/font&gt;&lt;/span&gt; CreateNumericSpecimen(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Type&lt;/font&gt;&lt;/span&gt; request)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; typeCode = &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Type&lt;/font&gt;&lt;/span&gt;.GetTypeCode(request);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;switch&lt;/font&gt;&lt;/span&gt; (typeCode)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;case&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;TypeCode&lt;/font&gt;&lt;/span&gt;.Byte:&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; (&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;byte&lt;/font&gt;&lt;/span&gt;)&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.GetNextNumber();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;case&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;TypeCode&lt;/font&gt;&lt;/span&gt;.Decimal:&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; (&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;decimal&lt;/font&gt;&lt;/span&gt;)&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.GetNextNumber();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;case&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;TypeCode&lt;/font&gt;&lt;/span&gt;.Double:&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; (&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;double&lt;/font&gt;&lt;/span&gt;)&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.GetNextNumber();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;case&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;TypeCode&lt;/font&gt;&lt;/span&gt;.Int16:&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; (&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;short&lt;/font&gt;&lt;/span&gt;)&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.GetNextNumber();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;case&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;TypeCode&lt;/font&gt;&lt;/span&gt;.Int32:&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.GetNextNumber();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;case&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;TypeCode&lt;/font&gt;&lt;/span&gt;.Int64:&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; (&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;long&lt;/font&gt;&lt;/span&gt;)&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.GetNextNumber();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;case&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;TypeCode&lt;/font&gt;&lt;/span&gt;.SByte:&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; (&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;sbyte&lt;/font&gt;&lt;/span&gt;)&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.GetNextNumber();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;case&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;TypeCode&lt;/font&gt;&lt;/span&gt;.Single:&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; (&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;float&lt;/font&gt;&lt;/span&gt;)&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.GetNextNumber();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;case&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;TypeCode&lt;/font&gt;&lt;/span&gt;.UInt16:&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; (&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;ushort&lt;/font&gt;&lt;/span&gt;)&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.GetNextNumber();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;case&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;TypeCode&lt;/font&gt;&lt;/span&gt;.UInt32:&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; (&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;uint&lt;/font&gt;&lt;/span&gt;)&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.GetNextNumber();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;case&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;TypeCode&lt;/font&gt;&lt;/span&gt;.UInt64:&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; (&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;ulong&lt;/font&gt;&lt;/span&gt;)&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.GetNextNumber();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;default&lt;/font&gt;&lt;/span&gt;:&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;NoSpecimen&lt;/font&gt;&lt;/span&gt;(request);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;private&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt; GetNextNumber()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Interlocked&lt;/font&gt;&lt;/span&gt;.Increment(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;ref&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.value);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Adding a new class in itself has no effect, so in order to recompose the default behavior of AutoFixture, we changed a class called DefaultPrimitiveBuilders by removing the old ISpecimenBuilders like Int32SequenceGenerator and instead adding NumericSequenceGenerator:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;yield&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;StringGenerator&lt;/font&gt;&lt;/span&gt;(() =&amp;gt; &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Guid&lt;/font&gt;&lt;/span&gt;.NewGuid());&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;yield&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ConstrainedStringGenerator&lt;/font&gt;&lt;/span&gt;();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;yield&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;StringSeedRelay&lt;/font&gt;&lt;/span&gt;();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;yield&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;NumericSequenceGenerator&lt;/font&gt;&lt;/span&gt;();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;yield&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;CharSequenceGenerator&lt;/font&gt;&lt;/span&gt;();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;yield&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;RangedNumberGenerator&lt;/font&gt;&lt;/span&gt;();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#008000"&gt;// even more builders...&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;NumericSequenceGenerator is the fourth class being yielded here. Before we added NumericSequenceGenerator, this class instead yielded Int32SequenceGenerator and similar classes. These were removed.&lt;/p&gt;
&lt;p&gt;The DefaultPrimitiveBuilders class is part of AutoFixture’s default &lt;a href="http://en.wikipedia.org/wiki/Facade_pattern"&gt;Facade&lt;/a&gt; and is the closest we get to a Composition Root for the library. Recomposing this Facade enabled us to change the behavior of AutoFixture without modifying (other) existing classes.&lt;/p&gt;
&lt;p&gt;As &lt;a href="http://megakemp.wordpress.com/"&gt;Enrico&lt;/a&gt; (who implemented this change) points out, the beauty is that the &lt;a href="http://megakemp.wordpress.com/2011/09/06/behavior-changes-in-autofixture-2-2-anonymous-numbers/"&gt;previous behavior is still in the box&lt;/a&gt;, and all it takes is a single method call to bring it back:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; fixture = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Fixture&lt;/font&gt;&lt;/span&gt;().Customize(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;NumericSequencePerTypeCustomization&lt;/font&gt;&lt;/span&gt;());&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The only class we had to modify was the DefaultPrimitiveBuilders, which is where the object graph is composed. In applications this corresponds to the Composition Root, so even in the face of SOLID code, you still need to modify the Composition Root in order to recompose the application. However, use of a good DI Container and a strong set of conventions can do much to minimize the required editing of such a class.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;SOLID versus Refactoring&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;SOLID is a goal I strive towards in the way I write code and design APIs, but I don’t think I’ve ever written a significant code base which is perfectly SOLID. While I consider AutoFixture a ‘fairly’ SOLID code base, it’s not perfect, and I’m currently performing some design work in order to change some abstractions for version 3.0. This will require changing some of the existing types and thereby violating the OCP.&lt;/p&gt;
&lt;p&gt;It’s worth noting that as long as you can stick with the OCP you can avoid introducing breaking changes. A breaking change is also an OCP violation, so adhering to the OCP is more than just an academic exercise – particularly if you write reusable libraries.&lt;/p&gt;
&lt;p&gt;Still, while none of my code is perfect and I occasionally have to refactor, I don’t refactor much. By definition, refactoring means violating the OCP, and while I have nothing against refactoring code when it’s required, I much prefer putting myself in a situation where it’s rarely necessary in the first place.&lt;/p&gt;
&lt;p&gt;I’ve often been derided for my lack of use of &lt;a href="http://www.jetbrains.com/resharper/"&gt;Resharper&lt;/a&gt;. When replying that I have little use for Resharper because I write SOLID code and thus don’t do much refactoring, I’ve been ridiculed for being totally clueless. People don’t realize the intimate relationship between SOLID and refactoring. I hope this post has helped highlight that connection.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=301fac13-cb4c-4081-9985-559ca8fbe358"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,301fac13-cb4c-4081-9985-559ca8fbe358.aspx</comments>
      <category>AutoFixture</category>
      <category>Dependency Injection</category>
      <category>Software Design</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=a4a1141f-94ef-411c-8992-3fa8248ec4e4</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,a4a1141f-94ef-411c-8992-3fa8248ec4e4.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,a4a1141f-94ef-411c-8992-3fa8248ec4e4.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=a4a1141f-94ef-411c-8992-3fa8248ec4e4</wfw:commentRss>
      <slash:comments>10</slash:comments>
      <title>Testing Container Configurations</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,a4a1141f-94ef-411c-8992-3fa8248ec4e4.aspx</guid>
      <link>http://blog.ploeh.dk/2011/12/21/TestingContainerConfigurations.aspx</link>
      <pubDate>Wed, 21 Dec 2011 13:25:32 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;Here’s a question I often get:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;“Should I test my DI Container configuration?”&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;The motivation for asking mostly seems to be that people want to know whether or not their applications are correctly wired. That makes sense.&lt;/p&gt; &lt;p&gt;A related question I also often get is whether or not a particular container has a self-test feature? In this post I’ll attempt to answer both questions.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Container Self-testing&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Some DI Containers have a method you can invoke to make it perform a consistency check on itself. As an example, &lt;a href="http://structuremap.net/structuremap/"&gt;StructureMap&lt;/a&gt; has the AssertConfigurationIsValid method that, according to documentation does “a full environment test of the configuration of [the] container.” It will “try to create every configured instance [...]”&lt;/p&gt; &lt;p&gt;Calling the method is &lt;em&gt;really&lt;/em&gt; easy:&lt;/p&gt; &lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;container.AssertConfigurationIsValid();&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Such a self-test can often be an expensive operation (not only for StructureMap, but in general) because it’s basically attempting to create an instance of each and every Service registered in the container. If the configuration is large, it’s going to take some time, but it’s still going to be faster than performing a manual test of the application.&lt;/p&gt;
&lt;p&gt;Two questions remain: Is it worth invoking this method? Why don’t all containers have such a method?&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The quick answer is that such a method is close to worthless, which also explains why many containers don’t include one.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;To understand the answer, consider the set of all components contained in the container in this figure:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blog.ploeh.dk/content/binary/Windows-Live-Writer/Testing-Container-Configurations_84D9/containersets_2.png"&gt;&lt;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="containersets" border="0" alt="containersets" src="http://blog.ploeh.dk/content/binary/Windows-Live-Writer/Testing-Container-Configurations_84D9/containersets_thumb.png" width="206" height="480"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The container contains the set of components IFoo, IBar, IBaz, Foo, Bar, Baz, and Qux so a self-test will try to create a single instance of each of these seven types. If all seven instances can be created, the test succeeds.&lt;/p&gt;
&lt;p&gt;All this accomplishes is to verify that the configuration is internally &lt;em&gt;consistent.&lt;/em&gt; Even so, an application could require instances of the ICorge, Corge or Grault types which are completely external to the configuration, in which case resolution would fail.&lt;/p&gt;
&lt;p&gt;Even more subtly, resolution would also fail whenever the container is queried for an instance of IQux, since this interface isn’t part of the configuration, even though it’s related to the concrete Qux type which &lt;em&gt;is&lt;/em&gt; registered in the container. A self-test only verifies that the concrete Qux class can be resolved, but it never attempts to create an instance of the IQux interface.&lt;/p&gt;
&lt;p&gt;In short, the fact that a container’s configuration is internally consistent doesn’t guarantee that all services required by an application can be served.&lt;/p&gt;
&lt;p&gt;Still, you may think that at least a self-test can constitute an early warning system: if the self-test fails, surely it must mean that the configuration is invalid? Unfortunately, that’s not true either.&lt;/p&gt;
&lt;p&gt;If a container is being configured using Auto-registration/Convention over Configuration to scan one or more assemblies and register certain types contained within, chances are that ‘too many’ types will end up being registered – particularly if one or more of these assemblies are reusable libraries (as opposed to application-specific assemblies). Often, the number of redundant types added is negligible, but they &lt;em&gt;may&lt;/em&gt; make the configuration internally &lt;em&gt;inconsistent&lt;/em&gt;. However, if the inconsistency only affects the redundant types, it doesn’t matter. The container will still be able to resolve &lt;em&gt;everything&lt;/em&gt; the current application requires.&lt;/p&gt;
&lt;p&gt;Thus, a container self-test method is worthless.&lt;/p&gt;
&lt;p&gt;Then how can the container configuration be tested?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Explicit Testing of Container Configuration&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Since a container self-test doesn’t achieve the desired goal, how can we ensure that an application can be composed correctly?&lt;/p&gt;
&lt;p&gt;One option is to write an automated integration test (not a unit test) for each service that the application requires. Still, if done manually, you run the risk of forgetting to write a test for a specific service. A better option is to come up with a convention so that you can identify &lt;em&gt;all&lt;/em&gt; the services required by a specific application, and then write a convention-based test to verify that the container can resolve them all.&lt;/p&gt;
&lt;p&gt;Will this guarantee that the application can be correctly composed?&lt;/p&gt;
&lt;p&gt;No, it only guarantees that it can be composed – not that this composition is &lt;em&gt;correct.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Even when a composed instance can be created for each and every service, many things may still be wrong:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Composition is just plain wrong: 
&lt;ul&gt;
&lt;li&gt;Decorators may be missing 
&lt;li&gt;Decorators may have been added in the wrong order 
&lt;li&gt;The wrong services are injected into consumers (this is more likely to happen when you follow the &lt;a href="http://codemanship.co.uk/parlezuml/blog/?postid=934"&gt;Reused Abstractions Principle&lt;/a&gt;, since there will be multiple concrete implementations of each interface)&lt;/li&gt;&lt;/ul&gt;
&lt;li&gt;Configuration values like connection strings and such are incorrect – e.g. while a connection string is supplied to a constructor, it may not contain the correct values. 
&lt;li&gt;Even if &lt;em&gt;everything&lt;/em&gt; is correctly composed, the run-time environment may prevent the application from working. As an example, even if an injected connection string is correct, there may not be any connection to the database due to network or security misconfiguration.&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;In short, a &lt;a href="http://xunitpatterns.com/Layer%20Test.html"&gt;Subcutaneous Test&lt;/a&gt; or full System Test is the only way to verify that &lt;em&gt;everything&lt;/em&gt; is correctly wired. However, if you have good test coverage at the unit test level, a series of &lt;a href="http://xunitpatterns.com/Smoke%20Test.html"&gt;Smoke Tests&lt;/a&gt; is all that you need at the System Test level because in general you have good reason to believe that all behavior is correct. The remaining question is whether all this correct behavior can be correctly connected, and that tends to be an all-or-nothing proposition.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;While it would be possible to write a set of convention-based integration tests to verify the configuration of a DI Container, the return of investment is too low since it doesn’t remove the need for a set of Smoke Tests at the System Test level.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=a4a1141f-94ef-411c-8992-3fa8248ec4e4"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,a4a1141f-94ef-411c-8992-3fa8248ec4e4.aspx</comments>
      <category>Dependency Injection</category>
      <category>StructureMap</category>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=e942dc33-aa21-4818-85d3-2e5f64aa3977</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,e942dc33-aa21-4818-85d3-2e5f64aa3977.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,e942dc33-aa21-4818-85d3-2e5f64aa3977.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=e942dc33-aa21-4818-85d3-2e5f64aa3977</wfw:commentRss>
      <slash:comments>5</slash:comments>
      <title>Factory Overload</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,e942dc33-aa21-4818-85d3-2e5f64aa3977.aspx</guid>
      <link>http://blog.ploeh.dk/2011/12/19/FactoryOverload.aspx</link>
      <pubDate>Mon, 19 Dec 2011 13:04:55 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;Recently I received a question from &lt;a href="http://kellabyte.com/"&gt;Kelly Sommers&lt;/a&gt; about good ways to refactor away from Factory Overload. Basically, she’s working in a code base where there’s an explosion of &lt;a href="http://en.wikipedia.org/wiki/Abstract_factory_pattern"&gt;Abstract Factories&lt;/a&gt; which seems to be counter-productive. In this post I’ll take a look at the example problem and propose a set of alternatives.&lt;/p&gt; &lt;p align="left"&gt;An &lt;a href="http://en.wikipedia.org/wiki/Abstract_factory_pattern"&gt;Abstract Factory&lt;/a&gt; (and its close relative Product Trader) can serve as a &lt;a href="http://stackoverflow.com/questions/2280170/why-do-we-need-abstract-factory-design-pattern/2280289#2280289"&gt;solution to various challenges&lt;/a&gt; that come up when writing loosely coupled code (chapter 6 of &lt;a href="http://affiliate.manning.com/idevaffiliate.php?id=1150_236"&gt;my book&lt;/a&gt; describes the most common scenarios). However, introducing an Abstract Factory may be a &lt;a href="http://en.wikipedia.org/wiki/Leaky_abstraction"&gt;leaky abstraction&lt;/a&gt;, so don’t do it blindly. For example, an Abstract Factory is &lt;a href="http://stackoverflow.com/questions/4648318/dependency-injection-new-instance-required-in-several-of-a-classes-methods/4650050#4650050"&gt;rarely the best approach to address lifetime management concerns&lt;/a&gt;. In other words, the Abstract Factory has to make sense as a pure model element.&lt;/p&gt; &lt;p align="left"&gt;That’s not the case in the following example.&lt;/p&gt; &lt;p align="left"&gt;&lt;strong&gt;Problem Statement&lt;/strong&gt;&lt;/p&gt; &lt;p align="left"&gt;The question centers around a code base that integrates with a database closely guarded by DBA police. Apparently, every single database access must happen through a set of &lt;em&gt;very&lt;/em&gt; fine-grained stored procedures.&lt;/p&gt; &lt;p align="left"&gt;For example, to update the first name of a user, a set of stored procedures exist to support this scenario, &lt;em&gt;depending on the context of the current application user:&lt;/em&gt;&lt;/p&gt; &lt;table border="1" cellspacing="0" cellpadding="2" width="400"&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td valign="top" width="135"&gt;&lt;strong&gt;User type&lt;/strong&gt;&lt;/td&gt; &lt;td valign="top" width="146"&gt;&lt;strong&gt;Stored procedure&lt;/strong&gt;&lt;/td&gt; &lt;td valign="top" width="117"&gt;&lt;strong&gt;Parameter name&lt;/strong&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td valign="top" width="135"&gt;Admin&lt;/td&gt; &lt;td valign="top" width="157"&gt;update_admin_firstname&lt;/td&gt; &lt;td valign="top" width="148"&gt;adminFirstName&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td valign="top" width="135"&gt;Guest&lt;/td&gt; &lt;td valign="top" width="157"&gt;update_guest_firstname&lt;/td&gt; &lt;td valign="top" width="148"&gt;guestFirstName&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td valign="top" width="135"&gt;Regular&lt;/td&gt; &lt;td valign="top" width="157"&gt;update_regular_firstname&lt;/td&gt; &lt;td valign="top" width="148"&gt;regularFirstName&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td valign="top" width="135"&gt;Restricted&lt;/td&gt; &lt;td valign="top" width="157"&gt;update_restricted_firstname&lt;/td&gt; &lt;td valign="top" width="148"&gt;restrictedFirstName&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt; &lt;p&gt;As this table demonstrates, not only is there a stored procedure for each user context, but the parameter name differs as well. However, in this particular case it &lt;em&gt;seems&lt;/em&gt; as though there’s a pattern to the names.&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;If this pattern is consistent, I think the easiest way to address these variations would be to algorithmically build the strings from a couple of templates.&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;However, this is not the route taken by Kelly’s team, so I assume that things are more complicated than that; apparently, a templated approach is not viable, so for the rest of&amp;nbsp; this article I’m going to assume that it’s necessary to write at least &lt;em&gt;some&lt;/em&gt; code to address each case individually.&lt;/p&gt; &lt;p&gt;The current solution that Kelly’s team has implemented is to use an Abstract Factory (Product Trader) to translate the user type into an appropriate IUserFirstNameModifier instance. From the consuming code, it looks like this:&lt;/p&gt; &lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; modifier = factory.Create(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;UserTypes&lt;/font&gt;&lt;/span&gt;.Admin);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;modifier.Commit(&lt;span style="color: "&gt;&lt;font color="#a31515"&gt;"first"&lt;/font&gt;&lt;/span&gt;);&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;where the factory variable is an instance of the IUserFirstNameModifierFactory interface. This is certainly loosely coupled, but looks like a leaky abstraction. Why is a factory needed? It seems that its single responsibility is to translate a UserTypes instance (an enum) into an IUserFirstNameModifier. There’s a code smell buried here – try to spot it before you read on :)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proposed Solution&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Kelly herself suggests an alternative involving a concrete Builder which can create instances of a single concrete UserFirstNameModifier with or without an implicit conversion:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#008000"&gt;// Implicit conversion.&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;&lt;font style="font-size: 10pt"&gt;UserFirstNameModifier&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; modifier1 = &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; builder.WithUserType(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;UserTypes&lt;/font&gt;&lt;/span&gt;.Guest);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#008000"&gt;// Without implicit conversion.&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; modifier2 = builder&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .WithUserType(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;UserTypes&lt;/font&gt;&lt;/span&gt;.Restricted)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Create();&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;While this may seem to reduce the number of classes involved, it has several drawbacks:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;First of all, the Fluent Builder pattern implies that you can forgo invoking any of the WithXyz methods (WithUserType) and just accept all the default values encapsulated in the builder. This again implies that there’s a default user type, which may or may not make sense in that particular domain. Looking at Kelly’s code, UserTypes is an enum (and thus has a default value), so if WithUserType isn’t invoked, the Create method defaults to UserTypes.Admin. That’s a bit too implicit for my taste. 
&lt;li&gt;Since all involved classes are now concrete, the proposed solution isn’t extensibile (and by corollary hard to unit test). 
&lt;li&gt;The builder is essentially a big switch statement.&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;Both the current implementation and the proposed solution involves passing an enum as a method parameter to a different class. If you’ve read and memorized &lt;a href="http://www.amazon.com/gp/product/0201485672/ref=as_li_ss_tl?ie=UTF8&amp;amp;tag=ploeh-20&amp;amp;linkCode=as2&amp;amp;camp=1789&amp;amp;creative=390957&amp;amp;creativeASIN=0201485672"&gt;Refactoring&lt;/a&gt; you should by now have recognized both a code smell and the remedy.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Alternative 1a: Make UserType Polymorphic&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The code smell is &lt;a href="http://c2.com/cgi/wiki?FeatureEnvySmell"&gt;Feature Envy&lt;/a&gt; and a possible refactoring is to &lt;a href="http://martinfowler.com/refactoring/catalog/replaceTypeCodeWithStateStrategy.html"&gt;replace the enum with a Strategy&lt;/a&gt;. In order to do that, an IUserType interface is introduced:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;interface&lt;/font&gt;&lt;/span&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;IUserType&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IUserFirstNameModifer&lt;/font&gt;&lt;/span&gt; CreateUserFirstNameModifier();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Usage becomes as simple as this:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; modifier = userType.CreateUserFirstNameModifier();&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Obviously, more methods can be added to IUserType to support other update operations, but care should be taken to avoid creating a &lt;a href="http://martinfowler.com/bliki/HeaderInterface.html"&gt;Header Interface&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;While this solution is much more object-oriented, I’m still not quite happy with it, because apparently, the context is a &lt;a href="http://abdullin.com/cqrs"&gt;CQRS&lt;/a&gt; style architecture. Since an update operation is essentially a Command, then why model the implementation along the lines of a Query? Both Abstract Factory and Factory Method patterns represent Queries, so it seems redundant in this case. It should be possible to apply the &lt;a href="http://en.wikipedia.org/wiki/Hollywood_principle_%28computer_programming%29"&gt;Hollywood Principle&lt;/a&gt; here.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Alternative 1b: Tell, Don’t Ask&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Why have the user type &lt;em&gt;return&lt;/em&gt; an modifier? Why can’t it perform the update itself? The IUserType interface should be changed to something like this:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;interface&lt;/font&gt;&lt;/span&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;IUserType&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt;&lt;/span&gt; CommitUserFirtName(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; firstName);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This makes it easier for the consumer to commit the user’s first name because it can be done directly on the IUserType instance instead of first creating the modifier.&lt;/p&gt;
&lt;p&gt;It also makes it much easier to unit test the consumer because there’s no longer a mix of Command and Queries within the same method. From &lt;a href="http://www.amazon.com/gp/product/0321503627/ref=as_li_ss_tl?ie=UTF8&amp;amp;tag=ploeh-20&amp;amp;linkCode=as2&amp;amp;camp=1789&amp;amp;creative=390957&amp;amp;creativeASIN=0321503627"&gt;Growing Object-Oriented Software&lt;/a&gt; we know that Queries should be modeled with &lt;a href="http://xunitpatterns.com/Test%20Stub.html"&gt;Stubs&lt;/a&gt; and Commands with &lt;a href="http://xunitpatterns.com/Mock%20Object.html"&gt;Mocks&lt;/a&gt;, and if you’ve ever tried mixing the two you know that it’s a sort of interaction that should be minimized.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Alternative 2a: Distinguish by Type&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;While I personally like alternative 1b best, it may not be practical in all situations, so it’s always valuable to examine other alternatives.&lt;/p&gt;
&lt;p&gt;The root cause of the problem is that there’s a lot of stored procedures. I want to reiterate that I still think that the absolutely easiest solution would be to generate a SqlCommand from a string template, but given that this article assumes that this isn’t possible or desirable, it follows that code must be written for each stored procedure.&lt;/p&gt;
&lt;p&gt;Why not simply define an interface for each one? As an example, to update the user’s first name in the context of being an ‘Admin’ user, this &lt;a href="http://martinfowler.com/bliki/RoleInterface.html"&gt;Role Interface&lt;/a&gt; can be used:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;interface&lt;/font&gt;&lt;/span&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;IUserFirstNameAdminModifier&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt;&lt;/span&gt; Commit(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; firstName);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Similar interfaces can be defined for the other user types, such as IUserFirstNameRestrictedModifier, IUserFirstNameGuestModifier and so on.&lt;/p&gt;
&lt;p&gt;This is a very simple solution; it’s easy to implement, but risks violating the &lt;a href="http://codemanship.co.uk/parlezuml/blog/?postid=934"&gt;Reused Abstractions Principle&lt;/a&gt; (RAP).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Alternative 2b: Distinguish by Generic Type&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The problem with introducing interfaces like IUserFirstNameAdminModifier, IUserFirstNameRestrictedModifier, IUserFirstNameGuestModifier etc. is that they differ only by name. The Commit method is the same for all these interfaces, so this seems to violate the RAP. It’d be better to merge all these interfaces into a single interface, which is what Kelly’s team is currently doing. However, the problem with this is that the type carries no information about the &lt;em&gt;role&lt;/em&gt; that the modifier is playing.&lt;/p&gt;
&lt;p&gt;Another alternative is to turn the modifier interface into a generic interface like this:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;interface&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IUserFirstNameModifier&lt;/font&gt;&lt;/span&gt;&amp;lt;T&amp;gt; &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;where&lt;/font&gt;&lt;/span&gt; T : &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;IUserType&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt;&lt;/span&gt; Commit(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; firstName);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The IUserType is a &lt;a href="http://en.wikipedia.org/wiki/Marker_interface_pattern"&gt;Marker Interface&lt;/a&gt;, so .NET purists are not going to like this solution, since &lt;a href="http://msdn.microsoft.com/en-us/library/ms229022.aspx"&gt;the .NET Type Design Guidelines recommend against using Marker Interfaces&lt;/a&gt;. However, it’s impossible to constrain a generic type argument against an attribute, so the party line solution is out of the question.&lt;/p&gt;
&lt;p&gt;This solution ensures that consumers can now have dependencies on IUserFirstNameModifier&amp;lt;AdminUserType&amp;gt;, IUserFirstNameModifier&amp;lt;RestrictedUserType&amp;gt;, etc.&lt;/p&gt;
&lt;p&gt;However, the need for a marker interface gives off another odeur.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Alternative 3: Distinguish by Role&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The problem at the heart of alternative 2 is that it attempts to use the &lt;em&gt;type&lt;/em&gt; of the interfaces as an indicator of the roles that Services play. It’s seems that making the type distinct works against the RAP, but when the RAP is applied, the type becomes ambiguous.&lt;/p&gt;
&lt;p&gt;However, as &lt;a href="http://www.tedneward.com/"&gt;Ted Neward&lt;/a&gt; &lt;a href="http://msdn.microsoft.com/en-us/magazine/hh205754.aspx"&gt;points out&lt;/a&gt; in his excellent series on &lt;a href="http://msdn.microsoft.com/en-us/magazine/ff955611.aspx"&gt;Multiparadigmatic .NET&lt;/a&gt;, the type is only one axis of variability among many. Perhaps, in this case, it may be much easier to use the &lt;em&gt;name&lt;/em&gt; of the dependency to communicate its role instead of the type.&lt;/p&gt;
&lt;p&gt;Given a single, ambiguous IUserFirstNameModifier interface (just as in the original problem statement), a consumer can distinguish between the various roles of modifiers by their names:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;partial&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;SomeConsumer&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;private&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;readonly&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IUserFirstNameModifier&lt;/font&gt;&lt;/span&gt; adminModifier;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;private&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;readonly&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IUserFirstNameModifier&lt;/font&gt;&lt;/span&gt; guestModifier;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; SomeConsumer(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IUserFirstNameModifier&lt;/font&gt;&lt;/span&gt; adminModifier,&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IUserFirstNameModifier&lt;/font&gt;&lt;/span&gt; guestModifier)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.adminModifier = adminModifier;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.guestModifier = guestModifier;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt;&lt;/span&gt; DoSomething()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.UseAdmin)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.adminModifier.Commit(&lt;span style="color: "&gt;&lt;font color="#a31515"&gt;"first"&lt;/font&gt;&lt;/span&gt;);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#0000ff"&gt;else&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.guestModifier.Commit(&lt;span style="color: "&gt;&lt;font color="#a31515"&gt;"first"&lt;/font&gt;&lt;/span&gt;);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now it’s entirely up to the &lt;a href="http://blog.ploeh.dk/2011/07/28/CompositionRoot.aspx"&gt;Composition Root&lt;/a&gt; to compose SomeConsumer with the correct modifiers, and while this can be done manually, it’s an excellent case for a DI Container and a bit of Convention over Configuration.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I’m sure that if I’d spent more time analyzing the problem I could have come up with more alternatives, but this post is becoming long enough already.&lt;/p&gt;
&lt;p&gt;Of the alternatives I’ve suggested here, I prefer 1b or 3, depending on the exact requirements.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=e942dc33-aa21-4818-85d3-2e5f64aa3977"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,e942dc33-aa21-4818-85d3-2e5f64aa3977.aspx</comments>
      <category>Dependency Injection</category>
      <category>Software Design</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=1c3204f4-a6d3-48f1-aa97-42f4dc1aaec0</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,1c3204f4-a6d3-48f1-aa97-42f4dc1aaec0.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,1c3204f4-a6d3-48f1-aa97-42f4dc1aaec0.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=1c3204f4-a6d3-48f1-aa97-42f4dc1aaec0</wfw:commentRss>
      <slash:comments>5</slash:comments>
      <title>Polymorphic Consistency</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,1c3204f4-a6d3-48f1-aa97-42f4dc1aaec0.aspx</guid>
      <link>http://blog.ploeh.dk/2011/12/07/PolymorphicConsistency.aspx</link>
      <pubDate>Wed, 07 Dec 2011 08:40:21 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;Asynchronous message passing combined with &lt;a href="http://en.wikipedia.org/wiki/Eventual_consistency"&gt;eventual consistency&lt;/a&gt; makes it possible to build very scalable systems. However, sometimes eventual consistency isn’t appropriate in parts of the system, while it’s acceptable in other parts. How can a consistent architecture be defined to fit both &lt;a href="http://en.wikipedia.org/wiki/ACID"&gt;ACID&lt;/a&gt; and eventual consistency? This article provides an answer.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;The case of an online game&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Last week I visited &lt;a href="http://pixelpandemic.net/"&gt;Pixel Pandemic&lt;/a&gt;, a company that produces browser-based &lt;a href="http://en.wikipedia.org/wiki/Massively_multiplayer_online_role-playing_game"&gt;MMORPGs&lt;/a&gt;. Since each game world has lots of players who can all potentially interact with each other, scalability is very important.&lt;/p&gt; &lt;p&gt;In traditional line of business applications, eventual consistency is often an excellent fit because the application is a projection of the real world. My favorite example is an inventory system: it models what’s going on in one or more physical warehouses, but the real world is the ultimate source of truth. A warehouse worker might accidentally drop and damage some of the goods, in which case the application must adjust &lt;em&gt;after the fact&lt;/em&gt;.&lt;/p&gt; &lt;p&gt;In other words, the information contained within line of business applications tend to lag after the real world. It’s impossible to guarantee that the application is always consistent with the real world, so eventual consistency is a natural fit.&lt;/p&gt; &lt;p&gt;That’s not the case with an online game world. The game world itself is the source of truth, and it must be internally consistent at all times. As an example, in &lt;a href="http://www.zombiepandemic.com/"&gt;Zombie Pandemic&lt;/a&gt;, players fight against zombies and may take damage along the way. Players can heal themselves, but they would prefer (I gather) that the healing action takes place immediately, and not some time in the future where the character might be dead. Similarly, when a player hits a zombie, they’d prefer to apply the damage immediately. (However, I think that even here, eventual consistency might provide some interesting game mechanics, but that’s another discussion.)&lt;/p&gt; &lt;p&gt;While discussing these matters with the nice people in Pixel Pandemic, it turned out that while some parts of the game world have to be internally consistent, it’s perfectly acceptable to use eventual consistency in other cases. One example is the game’s high score table. While a single player should have a consistent view of his or her own score, it’s acceptable if the high score table lags a bit.&lt;/p&gt; &lt;p&gt;At this point it seemed clear that this particular online game could use an appropriate combination of ACID and eventual consistency, and I think this conclusion can be generalized. The question now becomes: how can a consistent architecture encompass both types of consistency?&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Problem statement&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;With the above example scenario in mind the problem statement can be generalized:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;Given that an application should apply a mix of ACID and eventual consistency, how can a consistent architecture be defined?&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;Keep in mind that ACID consistency implies that all writes to a transactional resource must take place as a blocking method call. This seems to be at odds with the concept of asynchronous message passing that works so well with eventual consistency.&lt;/p&gt; &lt;p&gt;However, an application architecture where blocking ACID calls are fundamentally different than asynchronous message passing isn’t really an architecture at all. Developers will have to decide up-front whether or not a given operation is or isn’t synchronous, so the ‘architecture’ offers little implementation guidance. The end result is likely to be a heterogeneous mix of Services, Repositories, Units of Work, Message Channels, etc. A uniform principle will be difficult to distill, and the whole thing threatens to devolve into &lt;a href="http://en.wikipedia.org/wiki/Spaghetti_code"&gt;Spaghetti Code&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;The solution turns out to be not at all difficult, but it requires that we invert our thinking a bit. Most of us tend to think about synchronous code first. When we think about code performing synchronous work it seems difficult (perhaps even impossible) to retrofit asynchrony to that model. On the other hand, the converse isn’t true.&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;Given an asynchronous API, it’s trivial to provide a synchronous, blocking implementation.&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;Adopting an architecture based on asynchronous message passing (the &lt;a href="http://eaipatterns.com/PipesAndFilters.html"&gt;Pipes and Filters&lt;/a&gt; architecture) enables both scenarios. Eventual consistency can be achieved by passing messages around on persistent queues, while ACID consistency can be achieved by handling a message in a blocking call that enlists a (potentially distributed) transaction.&lt;/p&gt; &lt;p&gt;An example seems to be in order here.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Example: keeping score&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;In the online game world, each player accumulates a score based on his or her actions. From the perspective of the player, the score should always be consistent. When you defeat the zombie boss, you want to see the result in your score right away. That sounds an awful lot like the Player is an Aggregate Root and the score is part of that Entity. ACID consistency is warranted whenever the Player is updated.&lt;/p&gt; &lt;p&gt;On the other hand, each time a score changes it may influence the high score table, but this doesn’t need to be ACID consistent; eventual consistency is fine in this case.&lt;/p&gt; &lt;p&gt;Once again, polymorphism comes to the rescue.&lt;/p&gt; &lt;p&gt;Imagine that the application has a GameEngine class that handles updates in the game. Using an injected IChannel&amp;lt;PointsChangedEvent&amp;gt; it can update the score for a player as simple as this:&lt;/p&gt; &lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#008000"&gt;/* Lots of other interesting things happen&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#008000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; * here, like calculating the new score... */&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; cmd =&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ScoreChangedEvent&lt;/font&gt;&lt;/span&gt;(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.playerId, score);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;this&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;.pointsChannel.Send(cmd);&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The Send method returns void, so it’s a good example of a naturally asynchronous API. However, the implementation must do two things:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Update the Player Aggregate Root in a transaction 
&lt;li&gt;Update the high score table (eventually)&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;That’s two different types of consistency within the same method call.&lt;/p&gt;
&lt;p&gt;The first step to enable this is to employ the trusty old &lt;a href="http://en.wikipedia.org/wiki/Composite_pattern"&gt;Composite&lt;/a&gt; design pattern:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;CompositeChannel&lt;/font&gt;&lt;/span&gt;&amp;lt;T&amp;gt; : &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IChannel&lt;/font&gt;&lt;/span&gt;&amp;lt;T&amp;gt;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;private&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;readonly&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IEnumerable&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IChannel&lt;/font&gt;&lt;/span&gt;&amp;lt;T&amp;gt;&amp;gt; channels;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; CompositeChannel(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;params&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IChannel&lt;/font&gt;&lt;/span&gt;&amp;lt;T&amp;gt;[] channels)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.channels = channels;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt;&lt;/span&gt; Send(T message)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;foreach&lt;/font&gt;&lt;/span&gt; (&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; c &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;in&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.channels)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; c.Send(message);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;With a Composite channel it’s possible to compose a polymorphic mix of IChannel&amp;lt;T&amp;gt; implementations, some blocking and some asynchronous.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;ACID write&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;To update the Player Aggregate Root a simple &lt;a href="http://en.wikipedia.org/wiki/Adapter_pattern"&gt;Adapter&lt;/a&gt; writes the event to a persistent data store. This could be a relational database, a document database, a REST resource or something else – it doesn’t really matter exactly which technology is used.&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;PlayerStoreChannel&lt;/font&gt;&lt;/span&gt; : &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IChannel&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ScoreChangedEvent&lt;/font&gt;&lt;/span&gt;&amp;gt;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;private&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;readonly&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IPlayerStore&lt;/font&gt;&lt;/span&gt; store;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; PlayerStoreChannel(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IPlayerStore&lt;/font&gt;&lt;/span&gt; store)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.store = store;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt;&lt;/span&gt; Send(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ScoreChangedEvent&lt;/font&gt;&lt;/span&gt; message)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.store.Save(message.PlayerId, message);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The important thing to realize is that the IPlayerStore.Save method will be a blocking method call – perhaps wrapped in a distributed transaction. This ensures that updates to the Player Aggregate Root always leave the data store in a consistent state. Either the operation succeeds or it fails during the method call itself.&lt;/p&gt;
&lt;p&gt;This takes care of the ACID consistent write, but the application must also update the high score table.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Asynchronous write&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Since eventual consistency is acceptable for the high score table, the message can be transmitted over a persistent queue to be picked up by a background process.&lt;/p&gt;
&lt;p&gt;A generic class can server as an Adapter over an IQueue abstraction:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;QueueChannel&lt;/font&gt;&lt;/span&gt;&amp;lt;T&amp;gt; : &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IChannel&lt;/font&gt;&lt;/span&gt;&amp;lt;T&amp;gt;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;private&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;readonly&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IQueue&lt;/font&gt;&lt;/span&gt; queue;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;private&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;readonly&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IMessageSerializer&lt;/font&gt;&lt;/span&gt; serializer;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; QueueChannel(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IQueue&lt;/font&gt;&lt;/span&gt; queue,&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IMessageSerializer&lt;/font&gt;&lt;/span&gt; serializer)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.queue = queue;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.serializer = serializer;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt;&lt;/span&gt; Send(T message)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.queue.Enqueue(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.serializer.Serialize(message));&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Obvously, the Enqueue method is another void method. In the case of a persistent queue, it’ll block while the message is being written to the queue, but that will tend to be a fast operation.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Composing polymorphic consistency&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Now all the building blocks are available to compose both channel implementations into the GameEngine via the CompositeChannel. That might look like this:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; playerConnString = &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;ConfigurationManager&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .ConnectionStrings[&lt;span style="color: "&gt;&lt;font color="#a31515"&gt;"player"&lt;/font&gt;&lt;/span&gt;].ConnectionString;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; gameEngine = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;GameEngine&lt;/font&gt;&lt;/span&gt;(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;CompositeChannel&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ScoreChangedEvent&lt;/font&gt;&lt;/span&gt;&amp;gt;(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;PlayerStoreChannel&lt;/font&gt;&lt;/span&gt;(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;DbPlayerStore&lt;/font&gt;&lt;/span&gt;(playerConnString)),&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;QueueChannel&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ScoreChangedEvent&lt;/font&gt;&lt;/span&gt;&amp;gt;(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;PersistentQueue&lt;/font&gt;&lt;/span&gt;(&lt;span style="color: "&gt;&lt;font color="#a31515"&gt;"messageQueue"&lt;/font&gt;&lt;/span&gt;),&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;JsonSerializer&lt;/font&gt;&lt;/span&gt;())));&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;When the Send method is invoked on the channel, it’ll first invoke a blocking call that ensures ACID consistency for the Player, followed by asynchronous message passing for eventual consistency in other parts of the application.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Even when parts of an application must be implemented in a synchronous fashion to ensure ACID consistency, an architecture based on asynchronous message passing provides a flexible foundation that enables you to polymorphically mix both kinds of consistency in a single method call. From the perspective of the application layer, this provides a consistent and uniform architecture because all mutating actions are modeled as commands end events encapsulated in messages.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=1c3204f4-a6d3-48f1-aa97-42f4dc1aaec0"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,1c3204f4-a6d3-48f1-aa97-42f4dc1aaec0.aspx</comments>
      <category>Dependency Injection</category>
      <category>Software Design</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=f3355b05-3604-460f-9520-edc1d0384e64</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,f3355b05-3604-460f-9520-edc1d0384e64.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,f3355b05-3604-460f-9520-edc1d0384e64.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=f3355b05-3604-460f-9520-edc1d0384e64</wfw:commentRss>
      <slash:comments>6</slash:comments>
      <title>TDD improves reusability</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,f3355b05-3604-460f-9520-edc1d0384e64.aspx</guid>
      <link>http://blog.ploeh.dk/2011/11/10/TDDImprovesReusability.aspx</link>
      <pubDate>Thu, 10 Nov 2011 16:55:10 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;There’s this meme going around that software &lt;a href="http://www.udidahan.com/2009/06/07/the-fallacy-of-reuse/"&gt;reuse is a fallacy&lt;/a&gt;. Bollocks! The &lt;em&gt;reuse is a fallacy meme&lt;/em&gt; is a fallacy :) To be fair, I’m not claiming that everything can and should be reused, but my claim is that all code produced by Test-Driven Development (TDD) is being reused. Here’s why:&lt;/p&gt; &lt;p&gt;When tests are written first, they act as a kind of &lt;a href="http://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop"&gt;REPL&lt;/a&gt;. Tests tease out the API of the &lt;a href="http://xunitpatterns.com/SUT.html"&gt;SUT&lt;/a&gt;, as well as its behavior. In this point in the development process, the tests serve as a feedback mechanism. Only later, when the tests and the SUT stabilize, will the tests be repurposed (dare I say ‘reused’?) as regression tests. In other words: over time, tests written during TDD have more than one role:&lt;/p&gt; &lt;ol&gt; &lt;li&gt;Feedback mechanism  &lt;li&gt;Regression test&lt;/li&gt;&lt;/ol&gt; &lt;p&gt;Each test plays one of these roles at a time, but not both.&lt;/p&gt; &lt;p&gt;While the purpose of TDD is to evolve the SUT, the process produces two types of artifacts:&lt;/p&gt; &lt;ol&gt; &lt;li&gt;Tests  &lt;li&gt;Production code&lt;/li&gt;&lt;/ol&gt; &lt;p&gt;Notice how the tests appear &lt;em&gt;before&lt;/em&gt; the production code, which is an artifact of the test code.&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;The unit tests are the &lt;em&gt;first&lt;/em&gt; client of the production API.&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;When the production code is composed into an application, that application becomes the &lt;em&gt;second&lt;/em&gt; client, so it &lt;em&gt;reuses&lt;/em&gt; the API. This is a very beneficial effect of TDD, and probably one of the main reasons why TDD, if done correctly, produces code of high quality.&lt;/p&gt; &lt;p&gt;A colleague once told me (when referring to scale-out) that the hardest step is to go from one server to two servers, and I’ve later found that principle to apply much more broadly. Generalizing from a single case to two distinct cases is often the hardest step, and it becomes much easier to generalize further from two to an arbitrary number of cases.&lt;/p&gt; &lt;p&gt;This explains why TDD is such an efficient process. Apart from the beneficial side effect of producing a regression test suite, it also ensures that at the time the API goes into production, it’s already being shared (or reused) between at least two distinct clients. If, at a later time, it becomes necessary to add a third client, the hard part is already done.&lt;/p&gt; &lt;p&gt;TDD produces reusable code because the production application reuses the API which were realized by the tests.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=f3355b05-3604-460f-9520-edc1d0384e64"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,f3355b05-3604-460f-9520-edc1d0384e64.aspx</comments>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=b8651f8b-647f-4769-bdb1-22965b5bb719</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,b8651f8b-647f-4769-bdb1-22965b5bb719.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,b8651f8b-647f-4769-bdb1-22965b5bb719.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=b8651f8b-647f-4769-bdb1-22965b5bb719</wfw:commentRss>
      <slash:comments>6</slash:comments>
      <title>Independency</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,b8651f8b-647f-4769-bdb1-22965b5bb719.aspx</guid>
      <link>http://blog.ploeh.dk/2011/11/08/Independency.aspx</link>
      <pubDate>Tue, 08 Nov 2011 15:29:05 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;Now that &lt;a href="http://affiliate.manning.com/idevaffiliate.php?id=1150_236"&gt;my book&lt;/a&gt; about Dependency Injection is out, it’s only fitting that I also invert my own dependencies by striking out as an independent consultant/advisor. In the future I’m hoping to combine my writing and speaking efforts, as well as my open source interests, with helping out clients write better code.&lt;/p&gt; &lt;p&gt;If you’d like to get help with Dependency Injection or Test-Driven Development, SOLID, API design, application architecture or one of the other topics I regularly cover here on my blog, I’m available as a consultant worldwide.&lt;/p&gt; &lt;p&gt;When it comes to Windows Azure, I’ll be renewing my alliance with my penultimate employer &lt;a href="http://www.commentor.dk/"&gt;Commentor&lt;/a&gt;, so you can also hire me as part of larger deal with Commentor.&lt;/p&gt; &lt;p&gt;In case you are wondering what happened to &lt;a href="http://blog.ploeh.dk/2011/08/01/JoiningAppHarbor.aspx"&gt;my employment with AppHarbor&lt;/a&gt;, I resigned from my position there because I couldn’t make it work with all the other things I also would like to do. I still think &lt;a href="http://appharbor.com"&gt;AppHarbor&lt;/a&gt; is a very interesting project, and I wish my former employers the best of luck with their endeavor.&lt;/p&gt; &lt;p&gt;This has been a message from the blog’s sponsor (myself). Soon, regular content will resume.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=b8651f8b-647f-4769-bdb1-22965b5bb719"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,b8651f8b-647f-4769-bdb1-22965b5bb719.aspx</comments>
      <category>Miscellaneous</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=d3e2aaa7-be82-4755-a646-dbd18965f2cb</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,d3e2aaa7-be82-4755-a646-dbd18965f2cb.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,d3e2aaa7-be82-4755-a646-dbd18965f2cb.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=d3e2aaa7-be82-4755-a646-dbd18965f2cb</wfw:commentRss>
      <slash:comments>5</slash:comments>
      <title>SOLID concrete</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,d3e2aaa7-be82-4755-a646-dbd18965f2cb.aspx</guid>
      <link>http://blog.ploeh.dk/2011/10/25/SOLIDConcrete.aspx</link>
      <pubDate>Tue, 25 Oct 2011 15:01:15 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;&lt;a href="https://twitter.com/#!/gregyoung"&gt;Greg Young&lt;/a&gt; gave a talk at &lt;a href="http://gotocon.com//aarhus-2011/"&gt;GOTO Aarhus 2011&lt;/a&gt; titled &lt;a href="http://gotocon.com/aarhus-2011/presentation/Developers%20have%20a%20mental%20disorder"&gt;Developers have a mental disorder&lt;/a&gt;, which was (semi-)humorously meant, but still addressed some very real concerns about the cognitive biases of software developers as a group. While I have no intention to provide a complete resume of the talk, Greg said one thing that made me think a bit (more) about SOLID code. To paraphrase, it went something like this:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;Developers have a tendency to attempt to solve specific problems with general solutions. This leads to coupling and complexity. Instead of being general, code should be specific.&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;This sounds correct at first glance, but once again I think that SOLID code offers a solution. Due to the &lt;a href="http://en.wikipedia.org/wiki/Single_responsibility_principle"&gt;Single Responsibility Principle&lt;/a&gt; each SOLID concrete (pardon the pun) class will tend to very specifically address a very narrow problem.&lt;/p&gt; &lt;p&gt;Such a class may &lt;em&gt;implement&lt;/em&gt; one (or more) general-purpose interface(s), but the concrete type is specific.&lt;/p&gt; &lt;p&gt;The difference between the generality of an interface and the specificity of a concrete type becomes more and more apparent the better a code base applies the &lt;a href="http://codemanship.co.uk/parlezuml/blog/?postid=934"&gt;Reused Abstractions Principle&lt;/a&gt;. This is best done by defining an API in terms of &lt;a href="http://martinfowler.com/bliki/RoleInterface.html"&gt;Role Interfaces&lt;/a&gt;, which makes it possible to define a few core abstractions that apply very broadly, while implementations are very specific.&lt;/p&gt; &lt;p&gt;As an example, consider &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt;’s ISpecimenBuilder interface. This is a very central interface in AutoFixture (in fact, I don’t even know just how many implementations it has, and I’m currently too lazy to count them). As an API, it has proven to be very generally useful, but each concrete implementation is still very specific, like the CurrentDateTimeGenerator shown here:&lt;/p&gt; &lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;CurrentDateTimeGenerator&lt;/font&gt;&lt;/span&gt; : &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;ISpecimenBuilder&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;object&lt;/font&gt;&lt;/span&gt; Create(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;object&lt;/font&gt;&lt;/span&gt; request, &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ISpecimenContext&lt;/font&gt;&lt;/span&gt; context)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (request != &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;typeof&lt;/font&gt;&lt;/span&gt;(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;DateTime&lt;/font&gt;&lt;/span&gt;))&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;NoSpecimen&lt;/font&gt;&lt;/span&gt;(request);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;DateTime&lt;/font&gt;&lt;/span&gt;.Now;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is, literally, the entire implementation of the class. I hope we can agree that it’s very specific.&lt;/p&gt;
&lt;p&gt;In my opinion, SOLID is a set of principles that can help us keep an API general while each implementation is very specific.&lt;/p&gt;
&lt;p&gt;In SOLID code all concrete types are specific.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=d3e2aaa7-be82-4755-a646-dbd18965f2cb"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,d3e2aaa7-be82-4755-a646-dbd18965f2cb.aspx</comments>
      <category>AutoFixture</category>
      <category>Software Design</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=976bd91f-008d-46c7-90b7-080f76d07be2</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,976bd91f-008d-46c7-90b7-080f76d07be2.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,976bd91f-008d-46c7-90b7-080f76d07be2.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=976bd91f-008d-46c7-90b7-080f76d07be2</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <title>Checking for exactly one item in a sequence using C# and F#</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,976bd91f-008d-46c7-90b7-080f76d07be2.aspx</guid>
      <link>http://blog.ploeh.dk/2011/10/11/CheckingForExactlyOneItemInASequenceUsingCAndF.aspx</link>
      <pubDate>Tue, 11 Oct 2011 14:36:03 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;Here’s a programming issue that comes up from time to time. A method takes a sequence of items as input, like this:&lt;/p&gt; &lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt;&lt;/span&gt; Route(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IEnumerable&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt;&amp;gt; args)&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;While the signature of the method may be given, the &lt;em&gt;implementation&lt;/em&gt; may be concerned with finding out whether there is exactly &lt;em&gt;one&lt;/em&gt; element in the sequence. (I’d argue that this would be a violation of the &lt;a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle"&gt;Liskov Substitution Principle&lt;/a&gt;, but that’s another discussion.) By corollary, we might also be interested in the result sets on each side of that single element: no elements and multiple elements.&lt;/p&gt;
&lt;p&gt;Let’s assume that we’re required to raise the appropriate event for each of these three cases.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Naïve approach in C#&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A naïve implementation would be something like this:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt;&lt;/span&gt; Route(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IEnumerable&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt;&amp;gt; args)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; countCategory = args.Count();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;switch&lt;/font&gt;&lt;/span&gt; (countCategory)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;case&lt;/font&gt;&lt;/span&gt; 0:&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.RaiseNoArgument();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt;;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;case&lt;/font&gt;&lt;/span&gt; 1:&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.RaiseSingleArgument(args.Single());&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt;;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;default&lt;/font&gt;&lt;/span&gt;:&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.RaiseMultipleArguments(args);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt;;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;However, the problem with that is that IEnumerable&amp;lt;string&amp;gt; carries no guarantee that the sequence will ever end. In fact, there’s a whole category of implementations that keep iterating forever – these are called &lt;a href="http://en.wikipedia.org/wiki/Generator_%28computer_science%29"&gt;Generators&lt;/a&gt;. If you pass a Generator to the above implementation, it will never return because the Count method will block forever.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Robust implementation in C#&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A better solution comes from the realization that we’re only interested in knowing about which of the three categories the input matches: No elements, a single element, or multiple elements. The last case is covered if we find at least two elements. In other words, we don’t have to read more than &lt;em&gt;at most two&lt;/em&gt; elements to figure out the category. Here’s a more robust solution:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt;&lt;/span&gt; Route(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IEnumerable&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt;&amp;gt; args)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; countCategory = args.Take(2).Count();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;switch&lt;/font&gt;&lt;/span&gt; (countCategory)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;case&lt;/font&gt;&lt;/span&gt; 0:&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.RaiseNoArgument();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt;;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;case&lt;/font&gt;&lt;/span&gt; 1:&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.RaiseSingleArgument(args.Single());&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt;;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;default&lt;/font&gt;&lt;/span&gt;:&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.RaiseMultipleArguments(args);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt;;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Notice the inclusion of the Take(2) method call, which is the only difference from the first attempt. This will give us &lt;em&gt;at most two&lt;/em&gt; elements that we can then count with the Count method.&lt;/p&gt;
&lt;p&gt;While this is better, it still annoys me that it’s necessary with a secondary LINQ call (to the Single method) to extract that single element. Not that it’s particularly inefficient, but it still &lt;em&gt;feels&lt;/em&gt; like I’m repeating myself here.&lt;/p&gt;
&lt;p&gt;(We could also have converted the Take(2) iterator into an array, which would have enabled us to query its Length property, as well as index into it to get the single value, but it basically amounts to the same work.)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Implementation in F#&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In F# we can implement the same functionality in a much more compact manner, taking advantage of pattern matching against native F# lists:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;member&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; this.Route args =&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;let&lt;/font&gt;&lt;/span&gt; atMostTwo = args |&amp;gt; Seq.truncate 2 |&amp;gt; Seq.toList&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;match&lt;/font&gt;&lt;/span&gt; atMostTwo &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#0000ff"&gt;with&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; | [] &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;-&amp;gt;&lt;/font&gt;&lt;/span&gt; onNoArgument.Trigger(Unit.Default)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; | [arg] &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;-&amp;gt;&lt;/font&gt;&lt;/span&gt; onSingleArgument.Trigger(arg)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; | _ &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;-&amp;gt;&lt;/font&gt;&lt;/span&gt; onMultipleArguments.Trigger(args)&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The first thing happening here is that the input is being piped through a couple of functions. The truncate method does the same thing as the Take LINQ method does, and the toList method subsequently converts that sequence of at most two elements into a native F# list.&lt;/p&gt;
&lt;p&gt;The beautiful thing about native F# lists is that they support pattern matching, so instead of first figuring out in which category the input belongs, and then subsequently extract the data in the single element case, we can match and forward the element in a single statement.&lt;/p&gt;
&lt;p&gt;Why is this important? I don’t know… it’s just satisfying on an aesthetic level :)&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=976bd91f-008d-46c7-90b7-080f76d07be2"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,976bd91f-008d-46c7-90b7-080f76d07be2.aspx</comments>
      <category>Languages</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=c256a22e-65a5-430a-9166-0d5619811236</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,c256a22e-65a5-430a-9166-0d5619811236.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,c256a22e-65a5-430a-9166-0d5619811236.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=c256a22e-65a5-430a-9166-0d5619811236</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <title>Weakly-typed versus Strongly-typed Message Channels</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,c256a22e-65a5-430a-9166-0d5619811236.aspx</guid>
      <link>http://blog.ploeh.dk/2011/09/23/WeaklytypedVersusStronglytypedMessageChannels.aspx</link>
      <pubDate>Fri, 23 Sep 2011 09:08:53 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;Soon after I posted my previous blog post on &lt;a href="http://blog.ploeh.dk/2011/09/19/MessageDispatchingWithoutServiceLocation.aspx"&gt;message dispatching without Service Location&lt;/a&gt; I received an email from Jeff Saunders with some great observations. Jeff has been so kind to allow me to quote his email here on the blog, so here it is:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;“I enjoyed your latest blog post about message dispatching. I have to ask, though: why do we want weakly-typed messages? Why can't we just inject an appropriate IConsumer&amp;lt;T&amp;gt; into our services - they know which messages they're going to send or receive.&lt;/p&gt; &lt;p&gt;“A really good example of this is ISubject&amp;lt;T&amp;gt; from Rx. It implements both IObserver&amp;lt;T&amp;gt; (a message consumer) and IObservable&amp;lt;T&amp;gt; (a message producer) and the default implementation Subject&amp;lt;T&amp;gt; routes messages directly from its IObserver side to its IObservable side. &lt;/p&gt; &lt;p&gt;“We can use this with DI quite nicely - I have written an example in .NET Pad: &lt;a href="http://dotnetpad.net/ViewPaste/woTkGk6_GEq3P9xTVEJYZg#c9,c26,"&gt;http://dotnetpad.net/ViewPaste/woTkGk6_GEq3P9xTVEJYZg#c9,c26,&lt;/a&gt;&lt;/p&gt; &lt;p&gt;“The good thing about this is that we now have access to all of the standard LINQ query operators and the new ones added in Rx, so we can use a select query to map messages between layers, for instance.&lt;/p&gt; &lt;p&gt;“This way we get all the benefits of a weakly-typed IChannel interface, with the added advantages of strong typing for our messages and composability using Rx.&lt;/p&gt; &lt;p&gt;“One potential benefit of weak typing that could be raised is that we can have just a single implementation for IChannel, instead of an ISubject&amp;lt;T&amp;gt; for each message type. I don't think this is really a benefit, though, as we may want different propagation behaviour for each message type - there are other implementations of ISubject&amp;lt;T&amp;gt; that call consumers asynchronously, and we could pass any IObservable&amp;lt;T&amp;gt; or IObserver&amp;lt;T&amp;gt; into a service for testing purposes.”&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;These are great observations and I think that Rx holds much promise in this space. Basically you can say that in &lt;a href="http://abdullin.com/cqrs"&gt;CQRS&lt;/a&gt;-style architectures we’re already pushing events (and commands) around, so why not build upon what the framework offers?&lt;/p&gt; &lt;p&gt;Even if you find the &lt;a href="http://msdn.microsoft.com/en-us/library/dd783449.aspx"&gt;IObserver&amp;lt;T&amp;gt;&lt;/a&gt; interface a bit too clunky with its &lt;a href="http://msdn.microsoft.com/en-us/library/dd782792.aspx"&gt;OnNext&lt;/a&gt;, &lt;a href="http://msdn.microsoft.com/en-us/library/dd781657.aspx"&gt;OnError&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/dd782982.aspx"&gt;OnCompleted&lt;/a&gt; methods compared to the strongly typed IConsumer&amp;lt;T&amp;gt; interface, the question still remains: why do we want weakly-typed messages?&lt;/p&gt; &lt;p&gt;We don’t, necessarily. My previous post wasn’t meant as a particular endorsement of a weakly typed messaging channel. It was more an observation that I’ve seen many variations of this IChannel interface:&lt;/p&gt; &lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;interface&lt;/font&gt;&lt;/span&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;IChannel&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt;&lt;/span&gt; Send&amp;lt;T&amp;gt;(T message);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The most important thing I wanted to point out was that while the generic type argument may create the illusion that this is a strongly typed method, this is all it is: an illusion. IChannel isn’t strongly typed because you can invoke the Send method with &lt;em&gt;any&lt;/em&gt; type of message – and the code will still compile. This is no different than the &lt;a href="http://blog.ploeh.dk/2010/11/01/PatternRecognitionAbstractFactoryOrServiceLocator.aspx"&gt;mechanical distinction between a Service Locator and an Abstract Factory&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Thus, when defining a channel interface I normally prefer to make this explicit and instead model it like this:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;interface&lt;/font&gt;&lt;/span&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;IChannel&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt;&lt;/span&gt; Send(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;object&lt;/font&gt;&lt;/span&gt; message);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This achieves exactly the same and is more honest.&lt;/p&gt;
&lt;p&gt;Still, this doesn’t really answer Jeff’s question: is this preferable to one or more strongly typed IConsumer&amp;lt;T&amp;gt; dependencies?&lt;/p&gt;
&lt;p&gt;Any high-level application entry point that relies on a weakly typed IChannel can get by with a single IChannel dependency. This is flexible, but (just like with &lt;a href="http://blog.ploeh.dk/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx"&gt;Service Locator&lt;/a&gt;), it might hide that the client may have (or (d)evolve) too many responsibilities.&lt;/p&gt;
&lt;p&gt;If, instead, the client would rely on &lt;a href="http://stackoverflow.com/questions/2420193/dependency-injection-constructor-madness/2420245#2420245"&gt;strongly typed dependencies it becomes much easier to see&lt;/a&gt; if/when it violates the &lt;a href="http://en.wikipedia.org/wiki/Single_responsibility_principle"&gt;Single Responsibility Principle&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In conclusion, I’d tend to prefer strongly typed &lt;a href="http://www.eaipatterns.com/DatatypeChannel.html"&gt;Datatype Channels&lt;/a&gt; instead of a single weakly typed channel, but one shouldn’t underestimate the flexibility of a general-purpose channel either.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=c256a22e-65a5-430a-9166-0d5619811236"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,c256a22e-65a5-430a-9166-0d5619811236.aspx</comments>
      <category>Dependency Injection</category>
      <category>Software Design</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=aa13e712-8f85-46d8-8a7f-b0e2da71f439</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,aa13e712-8f85-46d8-8a7f-b0e2da71f439.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,aa13e712-8f85-46d8-8a7f-b0e2da71f439.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=aa13e712-8f85-46d8-8a7f-b0e2da71f439</wfw:commentRss>
      <slash:comments>17</slash:comments>
      <title>Message Dispatching without Service Location</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,aa13e712-8f85-46d8-8a7f-b0e2da71f439.aspx</guid>
      <link>http://blog.ploeh.dk/2011/09/19/MessageDispatchingWithoutServiceLocation.aspx</link>
      <pubDate>Mon, 19 Sep 2011 14:44:47 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;Once upon a time I wrote a blog post about why &lt;a href="http://blog.ploeh.dk/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx"&gt;Service Locator is an anti-pattern&lt;/a&gt;, and ever since then, I occasionally receive rebuffs from people who agree with me in principle, but think that, still: in various special cases (the argument goes), Service Locator does have its uses.&lt;/p&gt; &lt;p&gt;Most of these arguments actually stem from mistaking the &lt;a href="http://blog.ploeh.dk/2011/08/25/ServiceLocatorRolesVsMechanics.aspx"&gt;mechanics for the role of a Service Locator&lt;/a&gt;. Still, once in a while a compelling argument seems to come my way. One of the &lt;a href="http://smart421.wordpress.com/2011/08/12/to-iservicelocator-or-not/"&gt;most insistent arguments concerns message dispatching&lt;/a&gt; – a pattern which is currently gaining in prominence due to the increasing popularity of &lt;a href="http://abdullin.com/cqrs/"&gt;CQRS&lt;/a&gt;, &lt;a href="http://www.udidahan.com/2009/06/14/domain-events-salvation/"&gt;Domain Events&lt;/a&gt; and kindred architectural styles.&lt;/p&gt; &lt;p&gt;In this article I’ll first provide a quick sketch of the scenario, followed by a typical implementation based on a ‘Service Locator’, and then conclude by demonstrating why a Service Locator isn’t necessary.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Scenario: Message Dispatching&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Appropriate use of message dispatching internally in an application can significantly help decouple the code and make roles explicit. A common implementation utilizes a messaging interface like this one:&lt;/p&gt; &lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;interface&lt;/font&gt;&lt;/span&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;IChannel&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt;&lt;/span&gt; Send&amp;lt;T&amp;gt;(T message);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Personally, I find that the generic typing of the Send method is entirely redundant (not to mention heavily reminiscent of the &lt;a href="http://blog.ploeh.dk/2010/11/01/PatternRecognitionAbstractFactoryOrServiceLocator.aspx"&gt;shape of a Service Locator&lt;/a&gt;), but it’s very common and not particularly important right now (but more about that later).&lt;/p&gt;
&lt;p&gt;An application might use the IChannel interface like this:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; registerUser = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;RegisterUserCommand&lt;/font&gt;&lt;/span&gt;(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Guid&lt;/font&gt;&lt;/span&gt;.NewGuid(),&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#a31515"&gt;"Jane Doe"&lt;/font&gt;&lt;/span&gt;,&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#a31515"&gt;"password"&lt;/font&gt;&lt;/span&gt;,&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#a31515"&gt;"jane@ploeh.dk"&lt;/font&gt;&lt;/span&gt;);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;this&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;.channel.Send(registerUser);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#008000"&gt;// ...&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; changeUserName = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ChangeUserNameCommand&lt;/font&gt;&lt;/span&gt;(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; registerUser.UserId,&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#a31515"&gt;"Jane Ploeh"&lt;/font&gt;&lt;/span&gt;);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;this&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;.channel.Send(changeUserName);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#008000"&gt;// ...&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; resetPassword = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ResetPasswordCommand&lt;/font&gt;&lt;/span&gt;(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; registerUser.UserId);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;this&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;.channel.Send(resetPassword);&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Obviously, in this example, the channel variable is an injected instance of the IChannel interface.&lt;/p&gt;
&lt;p&gt;On the receiving end, these messages must be dispatched to appropriate consumers, which must all implement this interface:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;interface&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IConsumer&lt;/font&gt;&lt;/span&gt;&amp;lt;T&amp;gt;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt;&lt;/span&gt; Consume(T message);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Thus, each of the command messages in the example have a corresponding consumer:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;RegisterUserConsumer&lt;/font&gt;&lt;/span&gt; : &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IConsumer&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;RegisterUserCommand&lt;/font&gt;&lt;/span&gt;&amp;gt;&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ChangeUserNameConsumer&lt;/font&gt;&lt;/span&gt; : &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IConsumer&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ChangeUserNameCommand&lt;/font&gt;&lt;/span&gt;&amp;gt;&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ResetPasswordConsumer&lt;/font&gt;&lt;/span&gt; : &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IConsumer&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ResetPasswordCommand&lt;/font&gt;&lt;/span&gt;&amp;gt;&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This certainly &lt;em&gt;is&lt;/em&gt; a very powerful pattern, so it’s often used as an argument to prove that Service Locator is, after all, not an anti-pattern.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Message Dispatching using a DI Container&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In order to implement IChannel it’s necessary to match messages to their appropriate consumers. One easy way to do this is by employing a DI Container. Here’s an example that uses &lt;a href="http://autofac.org"&gt;Autofac&lt;/a&gt; to implement IChannel, but any other container would do as well:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;private&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;AutofacChannel&lt;/font&gt;&lt;/span&gt; : &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;IChannel&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;private&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;readonly&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IComponentContext&lt;/font&gt;&lt;/span&gt; container;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; AutofacChannel(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IComponentContext&lt;/font&gt;&lt;/span&gt; container)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (container == &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;null&lt;/font&gt;&lt;/span&gt;)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;throw&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ArgumentNullException&lt;/font&gt;&lt;/span&gt;(&lt;span style="color: "&gt;&lt;font color="#a31515"&gt;"container"&lt;/font&gt;&lt;/span&gt;);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.container = container;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt;&lt;/span&gt; Send&amp;lt;T&amp;gt;(T message)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; consumer = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.container.Resolve&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IConsumer&lt;/font&gt;&lt;/span&gt;&amp;lt;T&amp;gt;&amp;gt;();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; consumer.Consume(message);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This class is an &lt;a href="http://en.wikipedia.org/wiki/Adapter_pattern"&gt;Adapter&lt;/a&gt; from Autofac’s IComponentContext interface to the IChannel interface. At this point I can always see the “Q.E.D.” around the corner: “look! Service Locator isn’t an anti-pattern after all! I’d like to see you implement IChannel without a Service Locator.”&lt;/p&gt;
&lt;p&gt;While I’ll do the latter in just a moment, I’d like to dwell on the DI Container-based implementation for a moment.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Is it simple? Yes. 
&lt;li&gt;Is it flexible? Yes, although it has shortcomings. 
&lt;li&gt;Would I use it like this? Perhaps. It depends :) 
&lt;li&gt;Is it the only way to implement IChannel? No – see the next section. 
&lt;li&gt;Does it use a Service Locator? No.&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;While AutofacChannel uses Autofac (a DI Container) to implement the functionality, it’s not (necessarily) a Service Locator in action. This was the point I already tried to get across in my &lt;a href="http://blog.ploeh.dk/2011/08/25/ServiceLocatorRolesVsMechanics.aspx"&gt;previous post about the subject&lt;/a&gt;: just because its mechanics look like Service Locator it doesn’t mean that it &lt;em&gt;is&lt;/em&gt; one. In my implementation, the AutofacChannel class is a piece of pure infrastructure code. I even made it a private nested class in my &lt;a href="http://blog.ploeh.dk/2011/07/28/CompositionRoot.aspx"&gt;Composition Root&lt;/a&gt; to underscore the point. The container is still not available to the application code, so is never used in the Service Locator &lt;em&gt;role&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;One of the shortcomings about the above implementations is that it provides no fallback mechanism. What happens if the container can’t resolve the matching consumer? Perhaps there isn’t a consumer for the message. That’s entirely possible because there are no safeguards in place to ensure that there’s a consumer for every possibly message.&lt;/p&gt;
&lt;p&gt;The shape of the Send method enables the client to send any conceivable message type, and the code still compiles even if no consumer exists. That may look like a problem, but is actually an important insight into implementing an alternative IChannel class.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Message Dispatching using weakly typed matching&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Consider the IChannel.Send method once again:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;void&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; Send&amp;lt;T&amp;gt;(T message);&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Despite its generic signature it’s important to realize that this is, in fact, a weakly typed method (at least when used with type inferencing, as in the above example). Equivalently to a bona fide Service Locator, it’s possible for a developer to define a new class (Foo) and send it – and the code still compiles:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;this&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;.channel.Send(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Foo&lt;/font&gt;&lt;/span&gt;());&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;However, at run-time, this will fail because there’s no matching consumer. Despite the generic signature of the Send method, it contains no type safety. This insight can be used to implement IChannel without a DI Container.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Before I go on I should point out that I don’t consider the following solution intrinsically superior to using a DI Container. However, readers of &lt;a href="http://affiliate.manning.com/idevaffiliate.php?id=1150_236"&gt;my book&lt;/a&gt; will know that I consider it a very illuminating exercise to try to implement everything with Poor Man’s DI once in a while.&lt;/p&gt;
&lt;p&gt;Using Poor Man’s DI often helps unearth some important design elements of DI because it helps to think about solutions in terms of patterns and principles instead of in terms of technology.&lt;/p&gt;
&lt;p&gt;However, once I have arrived at an appropriate conclusion while considering Poor Man’s DI, I still tend to prefer mapping it back to an implementation that involves a DI Container.&lt;/p&gt;
&lt;p&gt;Thus, the purpose of this section is first and foremost to outline how message dispatching can be implemented without relying on a Service Locator.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;While this alternative implementation isn’t allowed to change any of the existing API, it’s a pure implementation detail to encapsulate the insight about the weakly typed nature of IChannel into a similarly weakly typed consumer interface:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;private&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;interface&lt;/font&gt;&lt;/span&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;IConsumer&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt;&lt;/span&gt; Consume(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;object&lt;/font&gt;&lt;/span&gt; message);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Notice that this is a private nested interface of my Poor Man’s DI Composition Root – it’s a pure implementation detail. However, given this private interface, it’s now possible to implement IChannel like this:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;private&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;PoorMansChannel&lt;/font&gt;&lt;/span&gt; : &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;IChannel&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;private&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;readonly&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IEnumerable&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IConsumer&lt;/font&gt;&lt;/span&gt;&amp;gt; consumers;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; PoorMansChannel(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;params&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IConsumer&lt;/font&gt;&lt;/span&gt;[] consumers)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.consumers = consumers;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt;&lt;/span&gt; Send&amp;lt;T&amp;gt;(T message)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;foreach&lt;/font&gt;&lt;/span&gt; (&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; c &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;in&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.consumers)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; c.Consume(message);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Notice that this is another private nested type that belongs to the Composition Root. It loops though all injected consumers, so it’s up to each consumer to decide whether or not to do anything about the message.&lt;/p&gt;
&lt;p&gt;A final private nested class bridges the generically typed world with the weakly typed world:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;private&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Consumer&lt;/font&gt;&lt;/span&gt;&amp;lt;T&amp;gt; : &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;IConsumer&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;private&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;readonly&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IConsumer&lt;/font&gt;&lt;/span&gt;&amp;lt;T&amp;gt; consumer;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; Consumer(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IConsumer&lt;/font&gt;&lt;/span&gt;&amp;lt;T&amp;gt; consumer)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.consumer = consumer;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt;&lt;/span&gt; Consume(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;object&lt;/font&gt;&lt;/span&gt; message)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (message &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;is&lt;/font&gt;&lt;/span&gt; T)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.consumer.Consume((T)message);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This generic class is another Adapter – this time adapting the generic IConsumer&amp;lt;T&amp;gt; interface to the weakly typed (private) IConsumer interface. Notice that it only delegates the message to the adapted consumer if the type of the message matches the consumer.&lt;/p&gt;
&lt;p&gt;Each implementer of IConsumer&amp;lt;T&amp;gt; can be wrapped in the (private) Consumer&amp;lt;T&amp;gt; class and injected into the PoorMansChannel class:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; channel = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;PoorMansChannel&lt;/font&gt;&lt;/span&gt;(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Consumer&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ChangeUserNameCommand&lt;/font&gt;&lt;/span&gt;&amp;gt;(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ChangeUserNameConsumer&lt;/font&gt;&lt;/span&gt;(store)),&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Consumer&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;RegisterUserCommand&lt;/font&gt;&lt;/span&gt;&amp;gt;(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;RegisterUserConsumer&lt;/font&gt;&lt;/span&gt;(store)),&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Consumer&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ResetPasswordCommand&lt;/font&gt;&lt;/span&gt;&amp;gt;(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ResetPasswordConsumer&lt;/font&gt;&lt;/span&gt;(store)));&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So there you have it: type-based message dispatching without a DI Container in sight. However, it would be easy to use convention-based configuration to scan an assembly and register all IConsumer&amp;lt;T&amp;gt; implementations and wrap them in Consumer&amp;lt;T&amp;gt; instances and use this list to compose a PoorMansChannel instance. However, I will leave this as an exercise to the reader (or a later blog post).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;My claim still stands&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In conclusion, I find that I can still defend my original claim: &lt;a href="http://blog.ploeh.dk/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx"&gt;Service Locator is an anti-pattern&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;That claim, by the way, is &lt;a href="http://en.wikipedia.org/wiki/Falsifiability"&gt;falsifiable&lt;/a&gt;, so I do appreciate that people take it seriously enough by attempting to disprove it. However, until now, I’ve yet to be presented with a scenario where I couldn’t come up with a better solution that didn’t involve a Service Locator.&lt;/p&gt;
&lt;p&gt;Keep in mind that a Service Locator is defined by the role it plays – not the shape of the API.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=aa13e712-8f85-46d8-8a7f-b0e2da71f439"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,aa13e712-8f85-46d8-8a7f-b0e2da71f439.aspx</comments>
      <category>Dependency Injection</category>
      <category>Software Design</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=5342fbcc-2443-4af2-b94f-1bc269d1c19e</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,5342fbcc-2443-4af2-b94f-1bc269d1c19e.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,5342fbcc-2443-4af2-b94f-1bc269d1c19e.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=5342fbcc-2443-4af2-b94f-1bc269d1c19e</wfw:commentRss>
      <title>AutoFixture goes Continuous Delivery with Semantic Versioning</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,5342fbcc-2443-4af2-b94f-1bc269d1c19e.aspx</guid>
      <link>http://blog.ploeh.dk/2011/09/06/AutoFixtureGoesContinuousDeliveryWithSemanticVersioning.aspx</link>
      <pubDate>Tue, 06 Sep 2011 20:34:42 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;For the last couple of months I’ve been working on setting up &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; for Continuous Delivery (thanks to the nice people at &lt;a title="http://teamcity.codebetter.com/" href="http://teamcity.codebetter.com/"&gt;http://teamcity.codebetter.com/&lt;/a&gt; for supplying the CI service) and I think I’ve finally succeeded. I’ve just pushed some code from my local Mercurial repository, and 5 minutes later the release is &lt;a href="http://autofixture.codeplex.com/releases/view/72947"&gt;live on both the CodePlex site&lt;/a&gt; as well as &lt;a href="http://nuget.org/List/Search?searchTerm=autofixture"&gt;on the NuGet Gallery&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;The plan for AutoFixture going forward is to maintain Continuous Delivery and switch the versioning scheme from ad hoc to &lt;a href="http://semver.org/"&gt;Semantic Versioning&lt;/a&gt;. This means that obviously you’ll see releases much more often, and versions are going to be incremented much more often. Since the previous release the current release incidentally ended at version 2.2.44, but since the versioning scheme has now changed, you can expect to see 2.3, 2.4 etc. in rapid succession.&lt;/p&gt; &lt;p&gt;While I’ve been mostly focused on setting up Continuous Delivery, &lt;a href="http://www.nikosbaxevanis.com/bonus-bits/"&gt;Nikos Baxevanis&lt;/a&gt; and &lt;a href="http://megakemp.wordpress.com/"&gt;Enrico Campidoglio&lt;/a&gt; have been busy writing new features:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="http://megakemp.wordpress.com/2011/08/01/anonymous-delegates-in-autofixture/"&gt;Support for anonymous delegates&lt;/a&gt;  &lt;li&gt;&lt;a href="http://www.nikosbaxevanis.com/bonus-bits/2011/08/heuristics-for-static-factory-methods-in-autofixture.html"&gt;Heuristics for static factory methods&lt;/a&gt;  &lt;li&gt;&lt;a href="http://www.nikosbaxevanis.com/bonus-bits/2011/08/combining-xunit-data-theories.html"&gt;Inline AutoData Theories&lt;/a&gt;  &lt;li&gt;&lt;a href="http://megakemp.wordpress.com/2011/09/06/behavior-changes-in-autofixture-2-2-anonymous-numbers/"&gt;More anonymous numbers&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Apart from these excellent contributions, other new features are&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="http://blog.ploeh.dk/2011/04/18/EnumerablesAreDynamicalsoInAutoFixture.aspx"&gt;Added StableFiniteSequenceCustomization&lt;/a&gt;  &lt;li&gt;Added [FavorArrays], [FavorEnumerables] and [FavorLists] attributes to xUnit.net extension  &lt;li&gt;Added a Generator&amp;lt;T&amp;gt; class  &lt;li&gt;Added a completely new project/package called &lt;em&gt;Idioms&lt;/em&gt;, which contains convention-based tests (more about this later)  &lt;li&gt;Probably some other things I’ve forgotten about…&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;While you can expect to see version numbers to increase more rapidly and releases to occur more frequently, I’m also beginning to think about AutoFixture 3.0. This release will streamline some of the API in the root namespace, which, I’ll admit, was always a bit haphazard. For those people who care, I have no plans to touch the API in the Ploeh.AutoFixture.Kernel namespace. AutoFixture 3.0 will mainly target the API contained in the Ploeh.AutoFixture namespace itself.&lt;/p&gt; &lt;p&gt;Some of the changes I have in mind will hopefully make the default experience with AutoFixture more pleasant – I’m unofficially thinking about AutoFixture 3.0 as the ‘pit of success’ release. It will also enable some of the various &lt;a href="http://autofixture.codeplex.com/workitem/list/basic"&gt;outstanding feature requests&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;Feedback is, as usual, appreciated.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=5342fbcc-2443-4af2-b94f-1bc269d1c19e"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,5342fbcc-2443-4af2-b94f-1bc269d1c19e.aspx</comments>
      <category>AutoFixture</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=40663e09-df4d-464d-8b34-b1690056db48</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,40663e09-df4d-464d-8b34-b1690056db48.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,40663e09-df4d-464d-8b34-b1690056db48.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=40663e09-df4d-464d-8b34-b1690056db48</wfw:commentRss>
      <slash:comments>10</slash:comments>
      <title>Service Locator: roles vs. mechanics</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,40663e09-df4d-464d-8b34-b1690056db48.aspx</guid>
      <link>http://blog.ploeh.dk/2011/08/25/ServiceLocatorRolesVsMechanics.aspx</link>
      <pubDate>Thu, 25 Aug 2011 18:55:12 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;It’s time to take a step back from the whole debate about whether or not &lt;a href="http://blog.ploeh.dk/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx"&gt;Service Locator&lt;/a&gt; is, or isn’t, an anti-pattern. It remains my strong belief that it’s an anti-pattern, while others disagree. Although everyone is welcome to think differently than me, I’ve noticed that &lt;a href="http://smart421.wordpress.com/2011/08/12/to-iservicelocator-or-not/"&gt;some of the arguments being put forth in defense of Service Locator seem very convincing&lt;/a&gt;. However, I believe that in those cases we no longer talk about Service Locator, but something that looks an awful lot like it.&lt;/p&gt; &lt;p&gt;Some APIs are easy to confuse with a ‘real’ Service Locator. It probably doesn’t help that last year I published an article on &lt;a href="http://blog.ploeh.dk/2010/11/01/PatternRecognitionAbstractFactoryOrServiceLocator.aspx"&gt;how to tell the difference between a Service Locator and an Abstract Factory&lt;/a&gt;. In this article I may have focused too much on the &lt;em&gt;mechanics&lt;/em&gt; of Service Locator, but as &lt;a href="http://lostechies.com/derickbailey/"&gt;Derick Bailey&lt;/a&gt; was so kind to point out, this hides the &lt;em&gt;role&lt;/em&gt; the API might play.&lt;/p&gt; &lt;p&gt;To repeat that earlier post, a Service Locator looks like this:&lt;/p&gt; &lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;interface&lt;/font&gt;&lt;/span&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;IServiceLocator&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; T Create&amp;lt;T&amp;gt;(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;object&lt;/font&gt;&lt;/span&gt; context);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;All Service Locators I’ve seen so far look like that, or some variation thereof, but that doesn’t mean that the relationship is transitive. Just because an API &lt;em&gt;looks&lt;/em&gt; like that it doesn’t &lt;em&gt;automatically&lt;/em&gt; means that it’s a Service Locator.&lt;/p&gt;
&lt;p&gt;If it was, all DI containers would be Service Locators. As an example, here’s Castle Windsor’s Resolve method:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; T Resolve&amp;lt;T&amp;gt;()&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Even &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; has an API like that:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;&lt;font style="font-size: 10pt"&gt;MyClass&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; sut = fixture.CreateAnonymous&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;MyClass&lt;/font&gt;&lt;/span&gt;&amp;gt;();&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It has never been my intention to denounce every single DI container available, as well as my own open source framework. Service Locator is ultimately not identified by the &lt;em&gt;mechanics&lt;/em&gt; of its API, but by the &lt;em&gt;role&lt;/em&gt; it plays.&lt;/p&gt;
&lt;p&gt;A DI container encapsulated in a &lt;a href="http://blog.ploeh.dk/2011/07/28/CompositionRoot.aspx"&gt;Composition Root&lt;/a&gt; is not a Service Locator – it’s an &lt;em&gt;infrastructure&lt;/em&gt; component.&lt;/p&gt;
&lt;p&gt;It &lt;em&gt;becomes&lt;/em&gt; a Service Locator if used incorrectly: when &lt;em&gt;application&lt;/em&gt; code (as opposed to infrastructure code) actively &lt;em&gt;queries&lt;/em&gt; a service in order to be provided with required dependencies, then it has become a Service Locator.&lt;/p&gt;
&lt;p&gt;Service Locators are spread thinly and pervasively throughout a code base – &lt;em&gt;that&lt;/em&gt; is just as much a defining characteristic.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=40663e09-df4d-464d-8b34-b1690056db48"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,40663e09-df4d-464d-8b34-b1690056db48.aspx</comments>
      <category>Dependency Injection</category>
      <category>Software Design</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=4de7115c-79c2-4c9e-91b9-9b33bc69cc6c</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,4de7115c-79c2-4c9e-91b9-9b33bc69cc6c.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,4de7115c-79c2-4c9e-91b9-9b33bc69cc6c.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=4de7115c-79c2-4c9e-91b9-9b33bc69cc6c</wfw:commentRss>
      <slash:comments>7</slash:comments>
      <title>Joining AppHarbor</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,4de7115c-79c2-4c9e-91b9-9b33bc69cc6c.aspx</guid>
      <link>http://blog.ploeh.dk/2011/08/01/JoiningAppHarbor.aspx</link>
      <pubDate>Mon, 01 Aug 2011 14:03:10 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;I’m pleased to announce that I’ll be joining &lt;a href="http://appharbor.com"&gt;AppHarbor&lt;/a&gt; as a developer. With my long-standing interest in TDD and OOD as well as my more recent interests in open-source .NET software, distributed source control systems, Continuous Delivery etc. AppHarbor seems like a perfect match for me.&lt;/p&gt; &lt;p&gt;Although AppHarbor is very attractive to me, this has been a difficult decision as &lt;a href="http://www.commentor.dk/"&gt;Commentor&lt;/a&gt; has been a great employer. However, despite great customers I just don’t feel like consulting at the moment. Since Safewhere went out of business I’ve been writing much less code than I’d liked, so when presented with an opportunity to join such a congenial outfit as AppHarbor I had few doubts.&lt;/p&gt; &lt;p&gt;I’ll still be working out of Copenhagen, Denmark, and I also expect to keep up my usual community engagement at home as well as abroad.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=4de7115c-79c2-4c9e-91b9-9b33bc69cc6c"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,4de7115c-79c2-4c9e-91b9-9b33bc69cc6c.aspx</comments>
      <category>Miscellaneous</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=eeb3103a-da41-40b1-b91d-dab5d7c91b1e</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,eeb3103a-da41-40b1-b91d-dab5d7c91b1e.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,eeb3103a-da41-40b1-b91d-dab5d7c91b1e.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=eeb3103a-da41-40b1-b91d-dab5d7c91b1e</wfw:commentRss>
      <slash:comments>8</slash:comments>
      <title>Composition Root</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,eeb3103a-da41-40b1-b91d-dab5d7c91b1e.aspx</guid>
      <link>http://blog.ploeh.dk/2011/07/28/CompositionRoot.aspx</link>
      <pubDate>Thu, 28 Jul 2011 15:22:04 GMT</pubDate>
      <description>&lt;div&gt;&lt;p align="left"&gt;In &lt;a href="http://affiliate.manning.com/idevaffiliate.php?id=1150_236"&gt;my book&lt;/a&gt; I describe the Composition Root pattern in chapter 3. This post serves as a summary description of the pattern.&lt;/p&gt; &lt;p align="left"&gt;The Constructor Injection pattern is easy to understand until a follow-up question comes up:&lt;/p&gt; &lt;blockquote&gt; &lt;p align="left"&gt;Where should we compose object graphs?&lt;/p&gt;&lt;/blockquote&gt; &lt;p align="left"&gt;It’s easy to understand that each class should require its dependencies through its constructor, but this pushes the responsibility of composing the classes with their dependencies &lt;a href="http://www.natpryce.com/articles/000783.html"&gt;to a third party&lt;/a&gt;. Where should that be?&lt;/p&gt; &lt;p align="left"&gt;It seems to me that most people are eager to compose as early as possible, but the correct answer is:&lt;/p&gt; &lt;blockquote&gt; &lt;p align="left"&gt;As close as possible to the application’s entry point.&lt;/p&gt;&lt;/blockquote&gt; &lt;p align="left"&gt;This place is called the &lt;em&gt;Composition Root&lt;/em&gt; of the application and defined like this:&lt;/p&gt; &lt;blockquote&gt; &lt;p align="left"&gt;A Composition Root is a (preferably) unique location in an application where modules are composed together.&lt;/p&gt;&lt;/blockquote&gt; &lt;p align="left"&gt;This means that all the application code relies solely on Constructor Injection (or other injection patterns), but is &lt;em&gt;never composed&lt;/em&gt;. Only at the entry point of the application is the &lt;a href="http://blog.ploeh.dk/2011/03/04/ComposeObjectGraphsWithConfidence.aspx"&gt;entire object graph&lt;/a&gt; finally composed.&lt;/p&gt; &lt;p align="left"&gt;The appropriate entry point depends on the framework:&lt;/p&gt; &lt;ul&gt; &lt;li&gt; &lt;div align="left"&gt;In console applications it’s the Main method&lt;/div&gt; &lt;li&gt; &lt;div align="left"&gt;In ASP.NET MVC applications it’s global.asax and a custom IControllerFactory&lt;/div&gt; &lt;li&gt; &lt;div align="left"&gt;In WPF applications it’s the Application.OnStartup method&lt;/div&gt; &lt;li&gt; &lt;div align="left"&gt;In WCF it’s a custom ServiceHostFactory&lt;/div&gt; &lt;li&gt; &lt;div align="left"&gt;etc.&lt;/div&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p align="left"&gt;(you can read more about framework-specific Composition Roots in chapter 7 of my book.)&lt;/p&gt; &lt;p align="left"&gt;The Composition Root is an &lt;em&gt;application infrastructure component&lt;/em&gt;.&lt;/p&gt; &lt;blockquote&gt; &lt;p align="left"&gt;Only applications should have Composition Roots. Libraries and frameworks shouldn’t.&lt;/p&gt;&lt;/blockquote&gt; &lt;p align="left"&gt;The Composition Root can be implemented with Poor Man’s DI, but is also the (only) appropriate place to use a DI Container.&lt;/p&gt; &lt;blockquote&gt; &lt;p align="left"&gt;A DI Container should only be referenced from the Composition Root. All other modules should have no reference to the container.&lt;/p&gt;&lt;/blockquote&gt; &lt;p align="left"&gt;Using a DI Container is often a good choice. In that case it should be applied using the &lt;a href="http://blog.ploeh.dk/2010/09/29/TheRegisterResolveReleasePattern.aspx"&gt;Register Resolve Release&lt;/a&gt; pattern entirely from within the Composition Root.&lt;/p&gt; &lt;p align="left"&gt;Read more in &lt;a href="http://affiliate.manning.com/idevaffiliate.php?id=1150_236"&gt;Dependency Injection in .NET&lt;/a&gt;.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=eeb3103a-da41-40b1-b91d-dab5d7c91b1e"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,eeb3103a-da41-40b1-b91d-dab5d7c91b1e.aspx</comments>
      <category>Dependency Injection</category>
      <category>Software Design</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=d770cccd-8543-4de5-aef7-df63137ecca5</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,d770cccd-8543-4de5-aef7-df63137ecca5.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,d770cccd-8543-4de5-aef7-df63137ecca5.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=d770cccd-8543-4de5-aef7-df63137ecca5</wfw:commentRss>
      <slash:comments>13</slash:comments>
      <title>SOLID Code isn’t</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,d770cccd-8543-4de5-aef7-df63137ecca5.aspx</guid>
      <link>http://blog.ploeh.dk/2011/06/07/SOLIDCodeIsnt.aspx</link>
      <pubDate>Tue, 07 Jun 2011 13:46:07 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;Recently I had an interesting conversation with a developer at my current client, about how the &lt;a href="http://en.wikipedia.org/wiki/Solid_%28object-oriented_design%29"&gt;SOLID&lt;/a&gt; principles would impact their code base. The client wants to write SOLID code – who doesn’t? It’s a beautiful acronym that fully demonstrates the power of catchy terminology.&lt;/p&gt; &lt;p align="left"&gt;However, when you start to outline what it actually &lt;em&gt;means&lt;/em&gt; people become uneasy. At the point where the discussion became interesting, I had already sketched &lt;a href="http://blog.ploeh.dk/2011/05/24/PokayokeDesignFromSmellToFragrance.aspx"&gt;my view on encapsulation&lt;/a&gt;. However, the client’s current code base is designed around validation at the perimeter. Most of the classes in the Domain Model are actually &lt;a href="http://msdn.microsoft.com/en-us/library/7c5ka91b.aspx"&gt;internal&lt;/a&gt; and implicitly trust input.&lt;/p&gt; &lt;p align="left"&gt;We were actually discussing Test-Driven Development, and I had already told them that they should only test against the public API of their code base. The discussion went something like this (I’m hoping I’m not making my ‘opponent’ sound dumb, because the real developer I talked to was anything but):&lt;/p&gt; &lt;p align="left"&gt;&lt;strong&gt;Client:&lt;/strong&gt; “That would mean that each and every class we expose must validate input!”&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Me:&lt;/strong&gt; “Yes…?”&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Client:&lt;/strong&gt; “That would be a lot of extra work.”&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Me:&lt;/strong&gt; “Would it? Why is that?”&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Client:&lt;/strong&gt; “The input that we deal with consist of complex data structures, and we must validate that all values are present and correct.”&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Me:&lt;/strong&gt; “Assume that input is SOLID as well. This would mean that each input instance can be assumed to be in a valid state because that would be its own responsibility. Given that, what would validation really mean?”&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Client:&lt;/strong&gt; “I’m not sure I understand what you mean…”&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Me:&lt;/strong&gt; “Assuming that the input instance is a self-validating reference type, what could possibly go wrong?”&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Client:&lt;/strong&gt; “The instance might be null…”&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Me:&lt;/strong&gt; “Yes. Anything else?”&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Client:&lt;/strong&gt; “Not that I can think of…”&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Me:&lt;/strong&gt; “Me neither. This means that while you must add more code to implement proper encapsulation, it’s really trivial code. It’s just some Guard Clauses.”&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Client:&lt;/strong&gt; “But isn’t it still gold plating?”&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Me:&lt;/strong&gt; “Not really, because we are designing for &lt;em&gt;change&lt;/em&gt; in the general sense. We know that we can’t predict &lt;em&gt;specific&lt;/em&gt; change, but I can guarantee you that change requests &lt;em&gt;will&lt;/em&gt; occur. Instead of trying to predict specific changes and design variability in those specific places, we simply put interfaces around everything because the cost of doing so is really low. This means that when change does happen, we already have Seams in the right places.”&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Client:&lt;/strong&gt; “How does SOLID help with that?”&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Me:&lt;/strong&gt; “A result of the &lt;a href="http://en.wikipedia.org/wiki/Single_responsibility_principle"&gt;Single Responsibility Principle&lt;/a&gt; is that each self-encapsulated class becomes really small, and there will be a lot of them.”&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Client:&lt;/strong&gt; “Lots of classes… I’m not sure I’m comfortable with that. Doesn’t it make it much harder to find what you need?”&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Me:&lt;/strong&gt; “I don’t think so. Each class is very small, so although you have many of them, understanding what each one does is easy. In my experience this is a lot easier than trying to figure out what a big class with thousands of lines of code does. When you have few big classes, your object model might look something like this:”&lt;/p&gt; &lt;p&gt;&lt;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="Large Grained Objects" border="0" alt="Large Grained Objects" src="http://blog.ploeh.dk/content/binary/Windows-Live-Writer/SOLID-Code-isnt_B434/largegrainedobjects_3.png" width="400" height="257"&gt;&lt;/p&gt; &lt;p&gt;“There’s a few objects and they kind of fit together to form the overall picture. However, if you need to change something, you’ll need to substantially change the shape of each of those objects. That’s a lot of work, and this is why such an object design isn’t particularly adaptable to change.&lt;/p&gt; &lt;p&gt;“With SOLID, on the other hand, you have lots of small-grained objects which you can easily re-arrange to match new requirements:”&lt;/p&gt; &lt;p&gt;&lt;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="Fine Grained Objects" border="0" alt="Fine Grained Objects" src="http://blog.ploeh.dk/content/binary/Windows-Live-Writer/SOLID-Code-isnt_B434/finegrainedobjects_3.png" width="246" height="233"&gt;&lt;/p&gt; &lt;p align="left"&gt;And that’s when it hit me: SOLID code isn’t really solid at all. I’m not a material scientist, but to me a &lt;a href="http://en.wikipedia.org/wiki/Solid"&gt;solid&lt;/a&gt; indicates a rigid structure. In essence a structure where the particles are tightly locked to each other and can’t easily move about.&lt;/p&gt; &lt;p align="left"&gt;However, when thinking about SOLID code, it actually helps to think about it more like a &lt;a href="http://en.wikipedia.org/wiki/Liquid"&gt;liquid&lt;/a&gt; (although perhaps a rather viscous one). Each class has much more room to maneuver because it is small and fits together with other classes in many different ways. It’s clear that when you &lt;a href="http://xkcd.com/895/"&gt;push an analogy too far, it breaks apart&lt;/a&gt;.&lt;/p&gt; &lt;p align="left"&gt;Still, a closing anecdote is appropriate…&lt;/p&gt; &lt;p align="left"&gt;My (then) three-year old son one day handed me a handful of &lt;a href="http://en.wikipedia.org/wiki/Lego_Duplo"&gt;Duplo&lt;/a&gt; bricks and asked me to build him a dragon. If you’ve ever tried to build anything out of Duplo you’ll know that the ‘resolution’ of the bricks is rather coarse-grained. Given that ‘a handful’ for a three-year old isn’t a lot of bricks, this was quite a challenge. Fortunately, I had an appreciative audience with quite a bit of imagination, so I was able to put the few bricks together in a way that satisfied my son.&lt;/p&gt; &lt;p align="left"&gt;Still, building a dragon of comparable size out of &lt;a href="http://en.wikipedia.org/wiki/Lego"&gt;Lego&lt;/a&gt; bricks is much easier because the bricks have a much finer ‘resolution’. SOLID code is more comparable to Lego than Duplo.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=d770cccd-8543-4de5-aef7-df63137ecca5"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,d770cccd-8543-4de5-aef7-df63137ecca5.aspx</comments>
      <category>Software Design</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=2a251d22-ad3e-443a-bc98-6dd165d639e2</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,2a251d22-ad3e-443a-bc98-6dd165d639e2.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,2a251d22-ad3e-443a-bc98-6dd165d639e2.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=2a251d22-ad3e-443a-bc98-6dd165d639e2</wfw:commentRss>
      <slash:comments>23</slash:comments>
      <title>At the Boundaries, Applications are Not Object-Oriented</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,2a251d22-ad3e-443a-bc98-6dd165d639e2.aspx</guid>
      <link>http://blog.ploeh.dk/2011/05/31/AtTheBoundariesApplicationsAreNotObjectOriented.aspx</link>
      <pubDate>Tue, 31 May 2011 13:27:11 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;My recent series of blog posts about &lt;a href="http://blog.ploeh.dk/2011/05/24/PokayokeDesignFromSmellToFragrance.aspx"&gt;Poka-yoke Design&lt;/a&gt; generated a few responses (I would have been disappointed had this not been the case). Quite a few of these reactions relate to various serialization or translation technologies usually employed at application boundaries: Serialization, XML (de)hydration, UI validation, etc. Note that such translation happens not only at the perimeter of the application, but also at the persistence layer. ORMs are also a translation mechanism.&lt;/p&gt; &lt;p&gt;Common to most of the comments is that lots of serialization technologies require the presence of a default constructor. As an example, the &lt;a href="http://msdn.microsoft.com/en-us/library/182eeyhh.aspx"&gt;XmlSerializer&lt;/a&gt; requires a default constructor and public writable properties. Most ORMs I’ve investigated seem to have the same kind of requirements. Windows Forms and WPF Controls (UI is also an application boundary) also must have default constructors. Doesn’t that break encapsulation? Yes and no.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Objects at the Boundary&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;It certainly &lt;em&gt;would&lt;/em&gt; break encapsulation if you were to expose your (domain) &lt;em&gt;objects&lt;/em&gt; directly at the boundary. Consider a simple XML document like this one:&lt;/p&gt; &lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;&amp;lt;&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;&lt;span style="color: "&gt;&lt;font color="#a31515"&gt;name&lt;/font&gt;&lt;/span&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#0000ff"&gt;&amp;gt;&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp; &amp;lt;&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;&lt;span style="color: "&gt;&lt;font color="#a31515"&gt;firstName&lt;/font&gt;&lt;/span&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&amp;gt;&lt;/font&gt;&lt;/span&gt;Mark&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&amp;lt;/&lt;/font&gt;&lt;/span&gt;&lt;span style="color: "&gt;&lt;font color="#a31515"&gt;firstName&lt;/font&gt;&lt;/span&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#0000ff"&gt;&amp;gt;&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp; &amp;lt;&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;&lt;span style="color: "&gt;&lt;font color="#a31515"&gt;lastName&lt;/font&gt;&lt;/span&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&amp;gt;&lt;/font&gt;&lt;/span&gt;Seemann&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&amp;lt;/&lt;/font&gt;&lt;/span&gt;&lt;span style="color: "&gt;&lt;font color="#a31515"&gt;lastName&lt;/font&gt;&lt;/span&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#0000ff"&gt;&amp;gt;&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;&amp;lt;/&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;&lt;span style="color: "&gt;&lt;font color="#a31515"&gt;name&lt;/font&gt;&lt;/span&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#0000ff"&gt;&amp;gt;&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Whether or not we have formal contract (XSD) or not, we might stipulate that both the firstName and lastName elements are &lt;em&gt;required&lt;/em&gt;. However, despite such a contract, I can easily create a document that breaks it:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;&amp;lt;&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;&lt;span style="color: "&gt;&lt;font color="#a31515"&gt;name&lt;/font&gt;&lt;/span&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#0000ff"&gt;&amp;gt;&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp; &amp;lt;&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;&lt;span style="color: "&gt;&lt;font color="#a31515"&gt;firstName&lt;/font&gt;&lt;/span&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&amp;gt;&lt;/font&gt;&lt;/span&gt;Mark&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&amp;lt;/&lt;/font&gt;&lt;/span&gt;&lt;span style="color: "&gt;&lt;font color="#a31515"&gt;firstName&lt;/font&gt;&lt;/span&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#0000ff"&gt;&amp;gt;&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;&amp;lt;/&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;&lt;span style="color: "&gt;&lt;font color="#a31515"&gt;name&lt;/font&gt;&lt;/span&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#0000ff"&gt;&amp;gt;&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We can’t &lt;em&gt;enforce&lt;/em&gt; the contract as there’s no compilation step involved. We can &lt;em&gt;validate&lt;/em&gt; input (and output), but that’s a different matter. Exactly because there’s no enforcement it’s very easy to create malformed input. The same argument can be made for UI input forms and any sort of serialized byte sequence. This is why we must treat all input as suspect.&lt;/p&gt;
&lt;p&gt;This isn’t a new observation at all. In &lt;a href="http://www.martinfowler.com/books.html#eaa"&gt;Patterns of Enterprise Application Architecture&lt;/a&gt;, &lt;a href="http://www.martinfowler.com"&gt;Martin Fowler&lt;/a&gt; described this as a &lt;a href="http://martinfowler.com/eaaCatalog/dataTransferObject.html"&gt;Data Transfer Object&lt;/a&gt; (DTO). However, despite the name we should realize that DTOs are not really objects at all. This is nothing new either. Back in 2004 Don Box formulated the &lt;a href="http://msdn.microsoft.com/en-us/magazine/cc164026.aspx"&gt;Four Tenets of Service Orientation&lt;/a&gt;. (Yes, I know that they are not in vogue any more and that people &lt;a href="http://www.pluralsight-training.net/community/blogs/dbox/archive/2007/08/15/48232.aspx"&gt;wanted to retire them&lt;/a&gt;, but some of them still make tons of sense.) Particularly the third tenet is germane to this particular discussion:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Services share schema and contract, not class.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Yes, and that means they are &lt;em&gt;not objects&lt;/em&gt;. A DTO is a &lt;em&gt;representation&lt;/em&gt; of such a piece of data &lt;em&gt;mapped into&lt;/em&gt; an object-oriented language. That still doesn’t make them objects in the sense of encapsulation. It would be impossible. Since all input is suspect, we can hardly enforce any invariants at all.&lt;/p&gt;
&lt;p&gt;Often, as &lt;a href="http://blogs.teamb.com/craigstuntz/"&gt;Craig Stuntz&lt;/a&gt; points out in &lt;a href="http://blog.ploeh.dk/2011/05/27/DesignSmellRedundantRequiredAttribute.aspx"&gt;a comment to one of my previous posts&lt;/a&gt;, even if the input is invalid, we want to capture what we &lt;em&gt;did&lt;/em&gt; receive in order to present a proper error message (this argument also applies on machine-to-machine boundaries). This means that any DTO must have &lt;em&gt;very&lt;/em&gt; weak invariants (if any at all).&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;DTOs don’t break encapsulation because they aren’t objects at all.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Don’t be fooled by your tooling. The .NET framework very, very much wants you to treat DTOs as objects. Code generation ensues.&lt;/p&gt;
&lt;p&gt;However, the strong typing provided by such auto-generated classes gives a false sense of security. You may think that you get &lt;a href="http://blog.ploeh.dk/2011/04/29/FeedbackMechanismsAndTradeoffs.aspx"&gt;rapid feedback from the compiler&lt;/a&gt;, but there are many possible ways you can get run-time errors (most notably when you forget to update the auto-generated code based on new schema versions).&lt;/p&gt;
&lt;p&gt;An even more problematic result of representing input and output as objects is that it tricks lots of developers into dealing with them as though they represent the real object model. The result is invariably an &lt;a href="http://www.martinfowler.com/bliki/AnemicDomainModel.html"&gt;anemic domain model&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;More and more, this line of reasoning is leading me towards the conclusion that the DTO mental model that we have gotten used to over the last ten years is a dead end.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What Should Happen at the Boundary&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Given that we write write object-oriented code and that data at the boundary is anything but object-oriented, how do we deal with it?&lt;/p&gt;
&lt;p&gt;One option is to stick with what we already have. To bridge the gap we must then develop &lt;em&gt;translation layers&lt;/em&gt; that can translate the DTOs to properly encapsulated domain objects. This is the route I take with the samples in &lt;a href="http://affiliate.manning.com/idevaffiliate.php?id=1150_236"&gt;my book&lt;/a&gt;. However, this is a solution that more and more I’m beginning to think may not be the best. It has issues with maintainability. (Incidentally, that’s the problem with writing a book: at the time you’re done, you know so much more than you did when you started out… Not that I’m denouncing the book – it’s just not perfect…)&lt;/p&gt;
&lt;p&gt;Another option is to stop treating data as objects and start treating it as the &lt;em&gt;structured data&lt;/em&gt; that it really is. It would be really nice if our programming language had a separate concept of &lt;em&gt;structured data&lt;/em&gt;… Interestingly, while C# has nothing of the kind, F# has tons of ways to model data structures without behavior. Perhaps that’s a more honest approach to dealing with data… I will need to experiment more with this…&lt;/p&gt;
&lt;p&gt;A third option is to look towards dynamic types. In his article &lt;a href="http://msdn.microsoft.com/en-us/magazine/ff796227.aspx"&gt;Cutting Edge: Expando Objects in C# 4.0&lt;/a&gt;, Dino Esposito outlines a dynamic approach towards consuming structured data that shortcuts auto-generated code and provides a lightweight API to structured data. This also looks like a promising approach… It doesn’t provide compile-time feedback, but that’s only a false sense of security anyway. We must resort to &lt;a href="http://blog.ploeh.dk/2011/04/29/FeedbackMechanismsAndTradeoffs.aspx"&gt;unit tests to get rapid feedback&lt;/a&gt;, but we’re all using TDD already, right?&lt;/p&gt;
&lt;p&gt;In summary, my entire series about encapsulation relates to &lt;em&gt;object-oriented programming&lt;/em&gt;. Although there are lots of technologies available to represent boundary data as ‘objects’, they are false objects. Even if we use an object-oriented language at the boundary, the code has nothing to do with object orientation. Thus, the Poka-yoke Design rules don’t apply there.&lt;/p&gt;
&lt;p&gt;Now go back and reread this post, but replace ‘DTO’ with ‘Entity’ (or whatever your ORM calls its representation of a relational table row) and you should begin to see the contours of why ORMs are problematic.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=2a251d22-ad3e-443a-bc98-6dd165d639e2"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,2a251d22-ad3e-443a-bc98-6dd165d639e2.aspx</comments>
      <category>Services</category>
      <category>Software Design</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=13f91979-d50a-49da-b27c-fdebeb7dde1b</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,13f91979-d50a-49da-b27c-fdebeb7dde1b.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,13f91979-d50a-49da-b27c-fdebeb7dde1b.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=13f91979-d50a-49da-b27c-fdebeb7dde1b</wfw:commentRss>
      <slash:comments>9</slash:comments>
      <title>Design Smell: Default Constructor</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,13f91979-d50a-49da-b27c-fdebeb7dde1b.aspx</guid>
      <link>http://blog.ploeh.dk/2011/05/30/DesignSmellDefaultConstructor.aspx</link>
      <pubDate>Mon, 30 May 2011 13:02:02 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;This post is the fifth in a series about &lt;a href="http://blog.ploeh.dk/2011/05/24/PokayokeDesignFromSmellToFragrance.aspx"&gt;Poka-yoke Design&lt;/a&gt; – also known as &lt;em&gt;encapsulation&lt;/em&gt;.&lt;/p&gt; &lt;p&gt;Default constructors are code smells. There you have it. That probably sounds outrageous, but consider this: object-orientation is about &lt;em&gt;encapsulating&lt;/em&gt; behavior &lt;em&gt;and data&lt;/em&gt; into cohesive pieces of code (classes). Encapsulation means that the class should protect the integrity of the data it encapsulates. When data is required, it must often be supplied through a constructor. Conversely, a default constructor implies that no external data is &lt;em&gt;required&lt;/em&gt;. That’s a rather weak statement about the invariants of the class.&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;Please be aware that this post represents a &lt;em&gt;smell&lt;/em&gt;. This indicates that whenever a certain idiom or pattern (in this case a default constructor) is encountered in code it should trigger further investigation.&lt;/p&gt; &lt;p&gt;As I will outline below, there are several scenarios where default constructors are perfectly fine, so the purpose of this blog post is not to thunder against default constructors. It’s to provide food for thought.&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;If you have read &lt;a href="http://affiliate.manning.com/idevaffiliate.php?id=1150_236"&gt;my book&lt;/a&gt; you will know that Constructor Injection is the dominating DI pattern exactly because it statically advertises dependencies and protects the integrity of those dependencies by guaranteeing that an initialized consumer is always in a consistent state. This is fail-safe design because &lt;a href="http://blog.ploeh.dk/2011/04/29/FeedbackMechanismsAndTradeoffs.aspx"&gt;the compiler can enforce the relationship, thus providing rapid feedback&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;This principle extends far beyond DI. In a &lt;a href="http://blog.ploeh.dk/2011/05/24/DesignSmellTemporalCoupling.aspx"&gt;previous post&lt;/a&gt; I described how a constructor with arguments statically advertises that the argument is required:&lt;/p&gt; &lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Fragrance&lt;/font&gt;&lt;/span&gt; : &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;IFragrance&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;private&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;readonly&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; name;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; Fragrance(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; name)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (name == &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;null&lt;/font&gt;&lt;/span&gt;)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;throw&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ArgumentNullException&lt;/font&gt;&lt;/span&gt;(&lt;span style="color: "&gt;&lt;font color="#a31515"&gt;"name"&lt;/font&gt;&lt;/span&gt;);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.name = name;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; Spread()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.name;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The Fragrance class protects the integrity of the name by requiring it through the constructor. Since this class requires the name to implement its behavior, requesting it through the constructor is the correct thing to do. A default constructor would not have been fail-safe, since it would introduce a &lt;a href="http://blog.ploeh.dk/2011/05/24/DesignSmellTemporalCoupling.aspx"&gt;temporal coupling&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Consider that objects are supposed to be containers of behavior and data. Whenever an object contains data, the data must be encapsulated. In the (very common) case where no meaningful default value can be defined, the data must be provided via the constructor. Thus, default constructors might indicate that encapsulation is broken.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;When are Default Constructors OK?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;There are still scenarios where default constructors are in order (I’m sure there are more than those listed here).&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If a default constructor can assign meaningful default values to all contained fields a default constructor still protects the invariants of the class. As an example, the default constructor of &lt;a href="http://msdn.microsoft.com/en-us/library/system.uribuilder.aspx"&gt;UriBuilder&lt;/a&gt; initializes its internal values to a consistent set that will build the Uri http://localhost unless one or more of its properties are subsequently manipulated. You may agree or disagree with this default behavior, but it’s consistent and so encapsulation is preserved. 
&lt;li&gt;If a class contains no data obviously there is no data to protect. This may be a symptom of the &lt;a href="http://c2.com/cgi/wiki?FeatureEnvySmell"&gt;Feature Envy&lt;/a&gt; code smell, which is often evidenced by the class in question being a concrete class. 
&lt;ul&gt;
&lt;li&gt;If such a class can be turned into a static class it’s a certain sign of Feature Envy. 
&lt;li&gt;If, on the other hand, the class implements an interface, it might be a sign that it actually represents pure behavior.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;A class that represents pure behavior by implementing an interface is not necessarily a bad thing. This can be a very powerful construct.&lt;/p&gt;
&lt;p&gt;In summary, a default constructor should be a signal to stop and think about the invariants of the class in question. Does the default constructor sufficiently guarantee the integrity of the encapsulated data? If so, the default constructor is appropriate, but otherwise it’s not. In my experience, default constructors tend to be the exception rather than the rule.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=13f91979-d50a-49da-b27c-fdebeb7dde1b"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,13f91979-d50a-49da-b27c-fdebeb7dde1b.aspx</comments>
      <category>Software Design</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=04140ccb-6793-4428-972b-cbf26ce5dae6</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,04140ccb-6793-4428-972b-cbf26ce5dae6.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,04140ccb-6793-4428-972b-cbf26ce5dae6.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=04140ccb-6793-4428-972b-cbf26ce5dae6</wfw:commentRss>
      <slash:comments>9</slash:comments>
      <title>Design Smell: Redundant Required Attribute</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,04140ccb-6793-4428-972b-cbf26ce5dae6.aspx</guid>
      <link>http://blog.ploeh.dk/2011/05/27/DesignSmellRedundantRequiredAttribute.aspx</link>
      <pubDate>Fri, 27 May 2011 13:21:06 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;This post is the fourth in a series about &lt;a href="http://blog.ploeh.dk/2011/05/24/PokayokeDesignFromSmellToFragrance.aspx"&gt;Poka-yoke Design&lt;/a&gt; – also known as &lt;em&gt;encapsulation&lt;/em&gt;.&lt;/p&gt; &lt;p&gt;Recently I saw this apparently enthusiastic tweet reporting from some Microsoft technology event:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;&lt;a href="https://twitter.com/#!/jennifermarsman/status/57850114424848385"&gt;[Required] attribute in code automatically creates a non-nullable entry in DB and validation in the webpage – nice […]&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;I imagine that it must look something like this:&lt;/p&gt; &lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;Smell&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Required&lt;/font&gt;&lt;/span&gt;]&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt; Id { &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;get&lt;/font&gt;&lt;/span&gt;; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;set&lt;/font&gt;&lt;/span&gt;; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Every time I see something like this I die a little inside. If you already read my previous posts it should by now be painfully clear why this breaks encapsulation. Despite the [Required] attribute there’s &lt;a href="http://blog.ploeh.dk/2011/05/24/DesignSmellTemporalCoupling.aspx"&gt;no guarantee that the Id property will ever be assigned a value&lt;/a&gt;. The attribute is just a piece of garbage making a claim it can’t back up.&lt;/p&gt;
&lt;p&gt;Code like that is not fail-safe.&lt;/p&gt;
&lt;p&gt;I understand that the attribute mentioned in the above tweet is intended to signal to some tool (probably EF) that the property must be mapped to a database schema as non-nullable, but it’s still redundant. Attributes are not the correct way to make a statement about invariants.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Improved Design&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The [Required] attribute is redundant because there’s a much better way to state that a piece of data is required. This has been possible since .NET 1.0. Here’s the Poka-yoke version of that same statement:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;Fragrance&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;private&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;readonly&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt; id;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; Fragrance(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt; id)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.id = id;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt; Id&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;get&lt;/font&gt;&lt;/span&gt; { &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.id; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This simple structural design ensures that the ID truly is required (and if the ID can only be positive a Guard Clause can be added). An instance of Fragrance can only be created with an ID. Since this is a structural construction, the &lt;a href="http://blog.ploeh.dk/2011/04/29/FeedbackMechanismsAndTradeoffs.aspx"&gt;compiler can enforce the requirement, giving us rapid feedback&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I do realize that the [Required] attribute mentioned above is intended to address the challenge of mapping objects to relational data and rendering, but instead of closing the impedance mismatch gap, it widens it. Instead of introducing yet another redundant attribute the team should have made their tool understand simple idioms for encapsulation like the one above.&lt;/p&gt;
&lt;p&gt;This isn’t at all hard to do. As an example, DI Containers thrive on structural information encoded into constructors (this is called &lt;em&gt;Auto-wiring&lt;/em&gt;). The team behind the [Required] attribute could have done that as well. The [Required] attribute is a primitive and toxic hack.&lt;/p&gt;
&lt;p&gt;This is the major reason I never expect to use EF. It forces developers to break encapsulation, which is a principle upon which I refuse to compromise.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=04140ccb-6793-4428-972b-cbf26ce5dae6"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,04140ccb-6793-4428-972b-cbf26ce5dae6.aspx</comments>
      <category>Software Design</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=70e00361-46ae-4406-a148-da382b1c8d76</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,70e00361-46ae-4406-a148-da382b1c8d76.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,70e00361-46ae-4406-a148-da382b1c8d76.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=70e00361-46ae-4406-a148-da382b1c8d76</wfw:commentRss>
      <slash:comments>16</slash:comments>
      <title>Code Smell: Automatic Property</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,70e00361-46ae-4406-a148-da382b1c8d76.aspx</guid>
      <link>http://blog.ploeh.dk/2011/05/26/CodeSmellAutomaticProperty.aspx</link>
      <pubDate>Thu, 26 May 2011 13:33:13 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;This post is the third in a series about &lt;a href="http://blog.ploeh.dk/2011/05/24/PokayokeDesignFromSmellToFragrance.aspx"&gt;Poka-yoke Design&lt;/a&gt; – also known as &lt;em&gt;encapsulation&lt;/em&gt;.&lt;/p&gt; &lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/bb384054.aspx"&gt;Automatic properties&lt;/a&gt; are one of the most redundant features of C#. I know that some people really love them, but they address a problem you shouldn’t have in the first place.&lt;/p&gt; &lt;p&gt;I totally agree that code like this looks redundant:&lt;/p&gt; &lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;private&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; name;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; Name&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;get&lt;/font&gt;&lt;/span&gt; { &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.name; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;set&lt;/font&gt;&lt;/span&gt; { &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.name = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;value&lt;/font&gt;&lt;/span&gt;; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;However, the solution is not to write this instead:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; Name { &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;get&lt;/font&gt;&lt;/span&gt;; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;set&lt;/font&gt;&lt;/span&gt;; }&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The problem with the first code snippet isn’t that it contains too much ceremony. The problem is that it breaks encapsulation. In fact&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;“[…] getters and setters do not achieve encapsulation or information hiding: they are a language-legitimized way to violate them.”&lt;/p&gt;
&lt;p&gt;James O. Coplien &amp;amp; Gertrud Bjørnvig. &lt;em&gt;Lean Architecture&lt;/em&gt;. Wiley. 2010. p. 134.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;While I personally think that properties do have their uses, I very rarely find use for &lt;em&gt;automatic&lt;/em&gt; properties. They are never appropriate for reference types, and only rarely for value types.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Code Smell: Automatic Reference Type Property&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;First of all, let’s consider the very large set of properties that expose a reference type.&lt;/p&gt;
&lt;p&gt;In the case of reference types, null is a possible value. However, when we think about Poka-yoke design, null is never an appropriate value because it leads to NullReferenceExceptions. The &lt;a href="http://en.wikipedia.org/wiki/Null_Object_pattern"&gt;Null Object&lt;/a&gt; pattern provides a better alternative to deal with situations where a value might be undefined.&lt;/p&gt;
&lt;p&gt;In other words, an automatic property like the Name property above is never appropriate. The setter must have some kind of Guard Clause to protect it against null (and possibly other invalid values). Here’s the most fundamental example:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;private&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; name;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; Name&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;get&lt;/font&gt;&lt;/span&gt; { &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.name; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;set&lt;/font&gt;&lt;/span&gt; &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;value&lt;/font&gt;&lt;/span&gt; == &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;null&lt;/font&gt;&lt;/span&gt;)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;throw&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ArgumentNullException&lt;/font&gt;&lt;/span&gt;(&lt;span style="color: "&gt;&lt;font color="#a31515"&gt;"value"&lt;/font&gt;&lt;/span&gt;);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.name = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;value&lt;/font&gt;&lt;/span&gt;; &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As an alternative, a Guard Clause could also check for null and provide a default Null Object in the cases where the assigned value is null:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;private&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; name;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; Name&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;get&lt;/font&gt;&lt;/span&gt; { &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.name; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;set&lt;/font&gt;&lt;/span&gt; &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;value&lt;/font&gt;&lt;/span&gt; == &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;null&lt;/font&gt;&lt;/span&gt;)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.name = &lt;span style="color: "&gt;&lt;font color="#a31515"&gt;""&lt;/font&gt;&lt;/span&gt;;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt;;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.name = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;value&lt;/font&gt;&lt;/span&gt;; &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p align="left"&gt;However, this implementation contains a &lt;a href="http://en.wikipedia.org/wiki/Principle_of_least_astonishment"&gt;POLA&lt;/a&gt; violation because the getter sometimes returns a different value than what was assigned. It’s possible to fix this problem by adding an associated boolean field indicating whether the name was assigned null so that null can be returned from the setter in this special case, but that leads to another code smell.&lt;/p&gt;
&lt;p align="left"&gt;&lt;strong&gt;Code Smell: Automatic Value Type Property&lt;/strong&gt;&lt;/p&gt;
&lt;p align="left"&gt;If the type of the property is a value type, the case is less clear-cut because value types can’t be null. This means that a Null Guard is never appropriate. However, directly consuming a value type may still be inappropriate. In fact, &lt;a href="http://blog.ploeh.dk/2011/05/25/DesignSmellPrimitiveObsession.aspx"&gt;it’s only appropriate if the class can meaningfully accept and handle any value of that type&lt;/a&gt;.&lt;/p&gt;
&lt;p align="left"&gt;If, for example, the class can really only handle a certain subset of all possible values, a Guard Clause must be introduced. Consider this example:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt; RetryCount { &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;get&lt;/font&gt;&lt;/span&gt;; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;set&lt;/font&gt;&lt;/span&gt;; }&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p align="left"&gt;This property might be used to set the appropriate number or retries for a given operation. The problem with using an automatic property is that it’s possible to assign a negative value to it, and that wouldn’t make any sense. One possible remedy is to add a Guard Clause:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;private&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt; retryCount;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt; RetryCount&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;get&lt;/font&gt;&lt;/span&gt; { &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.retryCount; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#0000ff"&gt;set&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;value&lt;/font&gt;&lt;/span&gt; &amp;lt; 0)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;throw&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ArgumentOutOfRangeException&lt;/font&gt;&lt;/span&gt;();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.retryCount = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;value&lt;/font&gt;&lt;/span&gt;;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;However, in many cases, exposing a primitive property is more likely to be a case of Primitive Obsession.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Improved Design: Guard Clause&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;As I described above, the most immediate fix for automatic properties is to properly implement the property with a Guard Clause. This ensures that the class’ invariants are properly encapsulated.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Improved Design: Value Object Property&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When the automatic property is a value type, a Guard Clause may still be in order. However, when the property is really a symptom of Primitive Obsession, a better alternative is to introduce a proper Value Object.&lt;/p&gt;
&lt;p&gt;Consider, as an example, this property:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt; Temperature { &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;get&lt;/font&gt;&lt;/span&gt;; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;set&lt;/font&gt;&lt;/span&gt;; }&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is bad design for a number of reasons. It doesn’t communicate the unit of measure and allows unbounded values to be assigned. What happens if –100 is assigned? If the unit of measure is Celcius it should succeed, although in the case when it’s Kelvin, it should fail. No matter the unit of measure, attempting to assign int.MinValue should fail.&lt;/p&gt;
&lt;p&gt;A more robust design can be had if we introduce a new Temperature type and change the property to have that type. Apart from protection of invariants it would also encapsulate conversion between different temperature scales.&lt;/p&gt;
&lt;p&gt;However, if that Value Object is implemented as a reference type the situation is equivalent to the situation described above, and a Null Guard is necessary. Only in the case where the Value Object is implemented as a value type is an anonymous property appropriate.&lt;/p&gt;
&lt;p&gt;The bottom line is that automatic properties are rarely appropriate. In fact, they are only appropriate when the type of the property is a value type and all conceivable values are allowed. Since there &lt;em&gt;are&lt;/em&gt; a few cases where automatic properties are appropriate their use can’t be entirely dismissed, but it should be treated as warranting further investigation. It’s a code smell, not an anti-pattern.&lt;/p&gt;
&lt;p&gt;On a different note properties also violate the &lt;a href="http://en.wikipedia.org/wiki/Law_of_Demeter"&gt;Law of Demeter&lt;/a&gt;, but that’s the topic of a future blog post…&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=70e00361-46ae-4406-a148-da382b1c8d76"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,70e00361-46ae-4406-a148-da382b1c8d76.aspx</comments>
      <category>Software Design</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=0207ab74-33c5-4fef-8b30-948a7e741262</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,0207ab74-33c5-4fef-8b30-948a7e741262.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,0207ab74-33c5-4fef-8b30-948a7e741262.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=0207ab74-33c5-4fef-8b30-948a7e741262</wfw:commentRss>
      <slash:comments>5</slash:comments>
      <title>Design Smell: Primitive Obsession</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,0207ab74-33c5-4fef-8b30-948a7e741262.aspx</guid>
      <link>http://blog.ploeh.dk/2011/05/25/DesignSmellPrimitiveObsession.aspx</link>
      <pubDate>Wed, 25 May 2011 15:03:31 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;This post is the second in a series about &lt;a href="http://blog.ploeh.dk/2011/05/24/PokayokeDesignFromSmellToFragrance.aspx"&gt;Poka-yoke Design&lt;/a&gt; – also known as &lt;em&gt;encapsulation&lt;/em&gt;.&lt;/p&gt; &lt;p&gt;Many classes have a tendency to consume or expose primitive values like integers and strings. While such primitive types exist on any platform, they tend to lead to procedural code. Furthermore they often break encapsulation by allowing invalid values to be assigned.&lt;/p&gt; &lt;p&gt;This problem has been addressed many times before. Years ago &lt;a href="http://lostechies.com/jimmybogard/"&gt;Jimmy Bogard&lt;/a&gt; provided an &lt;a href="http://grabbagoft.blogspot.com/2007/12/dealing-with-primitive-obsession.html"&gt;excellent treatment of the issue, as well as guidance on how to resolve it&lt;/a&gt;. In relation to &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; I also &lt;a href="http://blog.ploeh.dk/2009/05/01/DealingWithConstrainedInput.aspx"&gt;touched upon the subject some time ago&lt;/a&gt;. As such, the current post is mostly a placeholder.&lt;/p&gt; &lt;p&gt;However, it’s worth noting that both Jimmy’s and my own post address the concern that strings and integers do not sufficiently encapsulate the concepts of Zip codes and phone numbers.&lt;/p&gt; &lt;ul&gt; &lt;li&gt;When a Zip code is represented as a string it’s possible to assign values such as null, string.Emtpy, “foo”, very long strings, etc. Jimmy’s ZipCode class encapsulates the concept by guaranteeing that an instance can only be successfully created with a correct value.  &lt;li&gt;When a Danish phone number is represented as an integer it’s possible to assign values such as –98, 0, int.MaxValue, etc. Once again the DanishPhoneNumber class from the above example encapsulates the concept by guaranteeing that an instance can only be successfully created with a correct value.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Encapsulation is broken unless the concept represented by a primitive value can truly take &lt;em&gt;any&lt;/em&gt; of the possible values of the primitive type. This is rarely the case.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Design Smell:&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;A class consumes a primitive type. However, further analysis shows that not all possible values of the type are legal values.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Improved Design:&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Encapsulate the primitive value in a Value Object that contains appropriate Guard Clauses etc. to guarantee that only valid instances are possible.&lt;/p&gt; &lt;p&gt;Primitives tend to not be fail-safe, but encapsulated Value Objects are.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=0207ab74-33c5-4fef-8b30-948a7e741262"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,0207ab74-33c5-4fef-8b30-948a7e741262.aspx</comments>
      <category>Software Design</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=1cd9a774-cf57-469b-80b5-ddaa24e704c8</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,1cd9a774-cf57-469b-80b5-ddaa24e704c8.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,1cd9a774-cf57-469b-80b5-ddaa24e704c8.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=1cd9a774-cf57-469b-80b5-ddaa24e704c8</wfw:commentRss>
      <slash:comments>19</slash:comments>
      <title>Design Smell: Temporal Coupling</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,1cd9a774-cf57-469b-80b5-ddaa24e704c8.aspx</guid>
      <link>http://blog.ploeh.dk/2011/05/24/DesignSmellTemporalCoupling.aspx</link>
      <pubDate>Tue, 24 May 2011 14:00:42 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;This post is the first in a series about &lt;a href="http://blog.ploeh.dk/2011/05/24/PokayokeDesignFromSmellToFragrance.aspx"&gt;Poka-yoke Design&lt;/a&gt; – also known as &lt;em&gt;encapsulation&lt;/em&gt;.&lt;/p&gt; &lt;p&gt;A common problem in API design is &lt;em&gt;temporal coupling&lt;/em&gt;, which occurs when there’s an implicit relationship between two, or more, members of a class requiring clients to invoke one member before the other. This tightly couples the members in the temporal dimension.&lt;/p&gt; &lt;p&gt;The archetypical example is the use of an Initialize method, although copious other examples can be found – even in the BCL. As an example, this usage of &lt;a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.endpointaddressbuilder.aspx"&gt;EndpointAddressBuilder&lt;/a&gt; compiles, but fails at run-time:&lt;/p&gt; &lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; b = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;EndpointAddressBuilder&lt;/font&gt;&lt;/span&gt;();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; e = b.ToEndpointAddress();&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It turns out that at least an URI is required before an EndpointAddress can be created. The following code compiles &lt;em&gt;and&lt;/em&gt; succeeds at run-time:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; b = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;EndpointAddressBuilder&lt;/font&gt;&lt;/span&gt;();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;b.Uri = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;UriBuilder&lt;/font&gt;&lt;/span&gt;().Uri;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; e = b.ToEndpointAddress();&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The API provides no hint that this is necessary, but there’s a temporal coupling between the Uri property and the ToEndpointAddress method.&lt;/p&gt;
&lt;p&gt;In the rest of the post I will provide a more complete example, as well as a guideline to improve the API towards Poka-yoke Design.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Smell Example&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This example describes a more abstract code smell, exhibited by the Smell class. The public API looks like this:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;Smell&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt;&lt;/span&gt; Initialize(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; name)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; Spread()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Semantically the name of the Initialize method is obviously a clue, but on a structural level this API gives us no indication of temporal coupling. Thus, code like this compiles, but throws an exception at run-time:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; s = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Smell&lt;/font&gt;&lt;/span&gt;();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; n = s.Spread();&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It turns out that the Spread method throws an InvalidOperationException because the Smell has not been initialized with a name. The problem with the Smell class is that it doesn’t properly protect its invariants. In other words, encapsulation is broken.&lt;/p&gt;
&lt;p&gt;To fix the issue the Initialize method must be invoked before the Spread method:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; sut = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Smell&lt;/font&gt;&lt;/span&gt;();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;sut.Initialize(&lt;span style="color: "&gt;&lt;font color="#a31515"&gt;"Sulphur"&lt;/font&gt;&lt;/span&gt;);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; n = sut.Spread();&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;While it’s possible to write unit tests that explore the behavior of the Smell class, it would be better if the design was improved to enable the &lt;a href="http://blog.ploeh.dk/2011/04/29/FeedbackMechanismsAndTradeoffs.aspx"&gt;compiler to provide feedback&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Improvement: Constructor Injection&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Encapsulation (Poka-yoke style) requires that the class can never be in an inconsistent state. Since the name of the smell is required, a guarantee that it is always available must be built into the class. If no good default value is available, the name must be requested via the constructor:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Fragrance&lt;/font&gt;&lt;/span&gt; : &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;IFragrance&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;private&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;readonly&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; name;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; Fragrance(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; name)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (name == &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;null&lt;/font&gt;&lt;/span&gt;)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;throw&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ArgumentNullException&lt;/font&gt;&lt;/span&gt;(&lt;span style="color: "&gt;&lt;font color="#a31515"&gt;"name"&lt;/font&gt;&lt;/span&gt;);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.name = name;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; Spread()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.name;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This effectively guarantees that the name is always available in all instances of the class. There&amp;nbsp; are also positive side effects:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The cyclomatic complexity of the class has been reduced 
&lt;li&gt;The class is now immutable, and thereby thread-safe&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;However, there are times when the original version of the class implements an interface that causes the temporal coupling. It might have looked like this:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;interface&lt;/font&gt;&lt;/span&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;ISmell&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt;&lt;/span&gt; Initialize(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; name);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; Spread();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In many cases the injected value (name) is unknown until run-time, in which case straight use of the constructor seems prohibitive – after all, &lt;a href="http://blog.ploeh.dk/2011/02/28/InterfacesAreAccessModifiers.aspx"&gt;the constructor is an implementation detail and not part of the loosely coupled API&lt;/a&gt;. When programming against an interface it’s not possible to invoke the constructor.&lt;/p&gt;
&lt;p&gt;There’s a solution for that as well.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Improvement: Abstract Factory&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;To decouple the methods in the ISmell (ha ha) interface the Initialize method can be moved to a new interface. Instead of mutating the (inconsistent) state of a class, the Create method (formerly known as Initialize) returns a new instance of the IFragrance interface:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;interface&lt;/font&gt;&lt;/span&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;IFragranceFactory&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IFragrance&lt;/font&gt;&lt;/span&gt; Create(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; name);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The implementation is straightforward:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;FragranceFactory&lt;/font&gt;&lt;/span&gt; : &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;IFragranceFactory&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IFragrance&lt;/font&gt;&lt;/span&gt; Create(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; name)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (name == &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;null&lt;/font&gt;&lt;/span&gt;)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;throw&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ArgumentNullException&lt;/font&gt;&lt;/span&gt;(&lt;span style="color: "&gt;&lt;font color="#a31515"&gt;"name"&lt;/font&gt;&lt;/span&gt;);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Fragrance&lt;/font&gt;&lt;/span&gt;(name);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This enables encapsulation because both the FragranceFactory and Fragrance classes protect their invariants. They can never be in an inconsistent state. A client previously interacting with the ISmell interface can use the IFragranceFactory/IFragrance combination to achieve the same funcionality:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; f = factory.Create(name);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; n = f.Spread();&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is better because improper use of the API can now be detected by the compiler instead of at run-time. An interesting side-effect by moving towards a more statically declared interaction structure is that classes tend towards immutability. Immutable classes are automatically thread-safe, which is an increasingly important trait in the (relatively) new multi-core era.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=1cd9a774-cf57-469b-80b5-ddaa24e704c8"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,1cd9a774-cf57-469b-80b5-ddaa24e704c8.aspx</comments>
      <category>Software Design</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=fc0d0cde-e12f-4589-b34e-469c05f7c946</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,fc0d0cde-e12f-4589-b34e-469c05f7c946.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,fc0d0cde-e12f-4589-b34e-469c05f7c946.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=fc0d0cde-e12f-4589-b34e-469c05f7c946</wfw:commentRss>
      <slash:comments>8</slash:comments>
      <title>Poka-yoke Design: From Smell to Fragrance</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,fc0d0cde-e12f-4589-b34e-469c05f7c946.aspx</guid>
      <link>http://blog.ploeh.dk/2011/05/24/PokayokeDesignFromSmellToFragrance.aspx</link>
      <pubDate>Tue, 24 May 2011 13:57:39 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;Encapsulation is one of the most misunderstood aspects of object-oriented programming. Most people seem to think that the related concept of &lt;em&gt;information hiding&lt;/em&gt; simply means that private fields should be exposed by public properties (or getter/setter methods in languages that don’t have native properties).&lt;/p&gt; &lt;p&gt;Have you ever wondered what’s the &lt;em&gt;real&lt;/em&gt; benefit to be derived from code like the following?&lt;/p&gt; &lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;private&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; name;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt; Name&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;get&lt;/font&gt;&lt;/span&gt; { &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.name; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;set&lt;/font&gt;&lt;/span&gt; { &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.name = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;value&lt;/font&gt;&lt;/span&gt;; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This feels awfully much like redundant code to me (and &lt;a href="http://msdn.microsoft.com/en-us/library/bb384054.aspx"&gt;automatic properties&lt;/a&gt; are not the answer – it’s just a compiler trick that still creates private backing fields). No information is actually hidden. &lt;a href="http://lostechies.com/derickbailey/"&gt;Derick Bailey&lt;/a&gt; has a &lt;a href="http://lostechies.com/derickbailey/2011/03/28/encapsulation-youre-doing-it-wrong/"&gt;good piece on why this view of encapsulation is too narrow&lt;/a&gt;, so I’m not going to reiterate all his points here.&lt;/p&gt;
&lt;p&gt;So then what &lt;em&gt;is&lt;/em&gt; encapsulation?&lt;/p&gt;
&lt;p&gt;The whole point of object-orientation is to produce cohesive pieces of code (classes) that solve given problems once and for all, so that programmers can use those classes without having to learn about the intricate details of the implementations.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;This is what encapsulation is all about: exposing a solution to a problem without requiring the consumer to fully understand the problem domain.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;This is what all well-designed classes do.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You don’t have to know the intricate details of &lt;a href="http://en.wikipedia.org/wiki/Tabular_Data_Stream"&gt;TDS&lt;/a&gt; to use ADO.NET against SQL Server. 
&lt;li&gt;You don’t have to know the intricate details of painting on the screen to use WPF or Windows Forms. 
&lt;li&gt;You don’t have to know the intricate details of Reflection to use a DI Container. 
&lt;li&gt;You don’t have to know how to efficiently sort a list in order to efficiently sort a list in .NET. 
&lt;li&gt;Etc.&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;What makes encapsulation so important is exactly this trait. The class must hide the information it encapsulates in order to protect it against ‘naïve’ users. &lt;a href="http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29"&gt;Wikipedia has this to say&lt;/a&gt;:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Hiding the internals of the object protects its integrity by preventing users from setting the internal data of the component into an invalid or inconsistent state.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Keep in mind that users are &lt;em&gt;expected&lt;/em&gt; to not fully understand the internal implementation of a class. This makes it obvious what encapsulation is really about:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Encapsulation is a fail-safe mechanism.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;By corollary, encapsulation does &lt;em&gt;not&lt;/em&gt; mean hiding complexity. Whenever complexity is hidden (&lt;a href="http://blog.ploeh.dk/2011/04/27/ProviderIsNotAPattern.aspx"&gt;as is the case for Providers&lt;/a&gt;) feedback time increases. Rapid feedback is much preferred, so delaying feedback is not desirable if it can be avoided.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Encapsulation is not about hiding complexity, but conversely exposing complexity in a fail-safe manner.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;In Lean this is known as &lt;a href="http://en.wikipedia.org/wiki/Poka-yoke"&gt;Poka-yoke&lt;/a&gt;, so I find it only fitting to think about encapsulation as &lt;em&gt;Poka-yoke Design&lt;/em&gt;: APIs that make it as hard as possible to do the wrong thing. Considering that &lt;a href="http://blog.ploeh.dk/2011/04/29/FeedbackMechanismsAndTradeoffs.aspx"&gt;compilation is the cheapest feedback mechanism&lt;/a&gt;, it’s preferable to design APIs so that the code can only compile when classes are used correctly.&lt;/p&gt;
&lt;p&gt;In a series of blog posts I will look at various design smells that break encapsulation, as well as provide guidance on how to improve the design to make it safer, thus going from smell to fragrance.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href="http://blog.ploeh.dk/2011/05/24/DesignSmellTemporalCoupling.aspx"&gt;Design Smell: Temporal Coupling&lt;/a&gt; 
&lt;li&gt;&lt;a href="http://blog.ploeh.dk/2011/05/25/DesignSmellPrimitiveObsession.aspx"&gt;Design Smell: Primitive Obsession&lt;/a&gt; 
&lt;li&gt;&lt;a href="http://blog.ploeh.dk/2011/05/26/CodeSmellAutomaticProperty.aspx"&gt;Code Smell: Automatic Property&lt;/a&gt; 
&lt;li&gt;&lt;a href="http://blog.ploeh.dk/2011/05/27/DesignSmellRedundantRequiredAttribute.aspx"&gt;Design Smell: Redundant Required Attribute&lt;/a&gt; 
&lt;li&gt;&lt;a href="http://blog.ploeh.dk/2011/05/30/DesignSmellDefaultConstructor.aspx"&gt;Design Smell: Default Constructor&lt;/a&gt;&lt;/li&gt;&lt;/ol&gt;
&lt;p&gt;Postscript: &lt;a href="http://blog.ploeh.dk/2011/05/31/AtTheBoundariesApplicationsAreNotObjectOriented.aspx"&gt;At the Boundaries, Applications are Not Object-Oriented&lt;/a&gt;&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=fc0d0cde-e12f-4589-b34e-469c05f7c946"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,fc0d0cde-e12f-4589-b34e-469c05f7c946.aspx</comments>
      <category>Software Design</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=300de4ea-0211-4921-8aed-9295b77e90e6</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,300de4ea-0211-4921-8aed-9295b77e90e6.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,300de4ea-0211-4921-8aed-9295b77e90e6.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=300de4ea-0211-4921-8aed-9295b77e90e6</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <title>Tennis Kata with immutable types and a cyclomatic complexity of 1</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,300de4ea-0211-4921-8aed-9295b77e90e6.aspx</guid>
      <link>http://blog.ploeh.dk/2011/05/16/TennisKataWithImmutableTypesAndACyclomaticComplexityOf1.aspx</link>
      <pubDate>Mon, 16 May 2011 11:01:00 GMT</pubDate>
      <description>&lt;div&gt;&lt;p align="left"&gt;Recently I had the inclination to do the &lt;a href="http://codingdojo.org/cgi-bin/wiki.pl?KataTennis"&gt;Tennis Kata&lt;/a&gt; a couple of times. The first time I saw it I thought it wasn’t terribly interesting as an exercise in C# development. It would basically just be an application of the &lt;a href="http://en.wikipedia.org/wiki/State_pattern"&gt;State&lt;/a&gt; pattern, so I decided to make it a bit more interesting. More or less by intuition I decided to give myself the following constraints:&lt;/p&gt; &lt;ul&gt; &lt;li&gt; &lt;div align="left"&gt;All types must be immutable&lt;/div&gt; &lt;/li&gt;&lt;li&gt; &lt;div align="left"&gt;All members must have a &lt;a href="http://en.wikipedia.org/wiki/Cyclomatic_complexity"&gt;cyclomatic complexity&lt;/a&gt; of 1&lt;/div&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p align="left"&gt;Now that’s more interesting :)&lt;/p&gt; &lt;p align="left"&gt;Given these constraints, what would be the correct approach? Given that this is a finite state machine with a fixed number of states, the &lt;a href="http://en.wikipedia.org/wiki/Visitor_pattern"&gt;Visitor&lt;/a&gt; pattern will be a good match.&lt;/p&gt; &lt;p align="left"&gt;Each player’s score can be modeled as a Value Object that can be one of these types:&lt;/p&gt; &lt;ul&gt; &lt;li&gt; &lt;div align="left"&gt;ZeroPoints&lt;/div&gt; &lt;/li&gt;&lt;li&gt; &lt;div align="left"&gt;FifteenPoints&lt;/div&gt; &lt;/li&gt;&lt;li&gt; &lt;div align="left"&gt;ThirtyPoints&lt;/div&gt; &lt;/li&gt;&lt;li&gt; &lt;div align="left"&gt;FortyPoints&lt;/div&gt; &lt;/li&gt;&lt;li&gt; &lt;div align="left"&gt;AdvantagePoint&lt;/div&gt; &lt;/li&gt;&lt;li&gt; &lt;div align="left"&gt;GamePoint&lt;/div&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p align="left"&gt;All of these classes implement the IPoints interface:&lt;/p&gt; &lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;interface&lt;/font&gt;&lt;/span&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;IPoints&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IPoints&lt;/font&gt;&lt;/span&gt; Accept(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IPoints&lt;/font&gt;&lt;/span&gt; visitor);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IPoints&lt;/font&gt;&lt;/span&gt; LoseBall();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IPoints&lt;/font&gt;&lt;/span&gt; WinBall(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IPoints&lt;/font&gt;&lt;/span&gt; opponentPoints);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IPoints&lt;/font&gt;&lt;/span&gt; WinBall(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;AdvantagePoint&lt;/font&gt;&lt;/span&gt; opponentPoints);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IPoints&lt;/font&gt;&lt;/span&gt; WinBall(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;FortyPoints&lt;/font&gt;&lt;/span&gt; opponentPoints);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p align="left"&gt;The interesting insight here is that until the opponent's score reaches FortyPoints nothing special happens. Those states can be effectively collapsed into the WinBall(IPoints) method. However, when the opponent either has FortyPoints or AdvantagePoint, special things happen, so IPoints has specialized methods for those cases. All implementations should use &lt;a href="http://en.wikipedia.org/wiki/Double_dispatch"&gt;double dispatch&lt;/a&gt; to invoke the correct overload of WinBall, so the Accept method must be implemented like this:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IPoints&lt;/font&gt;&lt;/span&gt; Accept(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IPoints&lt;/font&gt;&lt;/span&gt; visitor)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; visitor.WinBall(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p align="left"&gt;That’s the core of the Visitor pattern in action. When the implementer of the Accept method is either FortyPoints or AdvantagePoint, the specialized overload will be invoked.&lt;/p&gt;
&lt;p align="left"&gt;It’s now possible to create a context around a pair of IPoints (called a Game) to implement a method to register that Player 1 won a ball:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Game&lt;/font&gt;&lt;/span&gt; PlayerOneWinsBall()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; newPlayerOnePoints = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.PlayerTwoScore&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Accept(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.PlayerOneScore);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; newPlayerTwoPoints = &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.PlayerTwoScore.LoseBall();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Game&lt;/font&gt;&lt;/span&gt;(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; newPlayerOnePoints, newPlayerTwoPoints);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p align="left"&gt;A similar method for player two simply reverses the roles. (I’m currently reading &lt;a href="http://www.amazon.com/Lean-Architecture-Agile-Software-Development/dp/0470684208"&gt;Lean Architecture&lt;/a&gt;, but have yet to reach the chapter on &lt;a href="http://en.wikipedia.org/wiki/Data,_Context_and_Interaction"&gt;DCI&lt;/a&gt;. However, considering what I’ve already read about DCI, this seems to fit the bill pretty well… although I might be wrong on that account.)&lt;/p&gt;
&lt;p align="left"&gt;The context calculates new scores for both players and returns the result as a new instance of the Game class. This keeps the Game and IPoints implementations immutable.&lt;/p&gt;
&lt;p&gt;The new score for the winner depends on the opponent’s score, so the appropriate overload of WinBall should be invoked. The Visitor implementation makes it possible to pick the right overload without resorting to casts and &lt;em&gt;if&lt;/em&gt; statements. As an example, the FortyPoints class implements the three WinBall overloads like this:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IPoints&lt;/font&gt;&lt;/span&gt; WinBall(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IPoints&lt;/font&gt;&lt;/span&gt; opponentPoints)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;GamePoint&lt;/font&gt;&lt;/span&gt;();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IPoints&lt;/font&gt;&lt;/span&gt; WinBall(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;FortyPoints&lt;/font&gt;&lt;/span&gt; opponentPoints)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;AdvantagePoint&lt;/font&gt;&lt;/span&gt;();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IPoints&lt;/font&gt;&lt;/span&gt; WinBall(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;AdvantagePoint&lt;/font&gt;&lt;/span&gt; opponentPoints)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It’s also important to correctly implement the LoseBall method. In most cases, losing a ball doesn’t change the current state of the loser, in which case the implementation looks like this:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IPoints&lt;/font&gt;&lt;/span&gt; LoseBall()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;However, when the player has advantage and loses the ball, he or she loses the advantage, so for the AdvantagePoint class the implementation looks like this:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IPoints&lt;/font&gt;&lt;/span&gt; LoseBall()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;FortyPoints&lt;/font&gt;&lt;/span&gt;();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;To keep things simple I decided to implicitly model &lt;em&gt;deuce&lt;/em&gt; as both players having FortyPoints, so there’s not explicit Deuce class. Thus, AdvantagePoint returns FortyPoints when losing the ball.&lt;/p&gt;
&lt;p&gt;Using the Visitor pattern it’s possible to keep the cyclomatic complexity at 1. The code has no branches or loops. It’s immutable to boot, so a game might look like this:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;[&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Fact&lt;/font&gt;&lt;/span&gt;]&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt;&lt;/span&gt; PlayerOneWinsAfterHardFight()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; game = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Game&lt;/font&gt;&lt;/span&gt;()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .PlayerOneWinsBall()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .PlayerOneWinsBall()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .PlayerOneWinsBall()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .PlayerTwoWinsBall()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .PlayerTwoWinsBall()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .PlayerTwoWinsBall()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .PlayerTwoWinsBall()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .PlayerOneWinsBall()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .PlayerOneWinsBall()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .PlayerOneWinsBall();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Assert&lt;/font&gt;&lt;/span&gt;.Equal(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;GamePoint&lt;/font&gt;&lt;/span&gt;(), game.PlayerOneScore);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Assert&lt;/font&gt;&lt;/span&gt;.Equal(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;FortyPoints&lt;/font&gt;&lt;/span&gt;(), game.PlayerTwoScore);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In case you’d like to take a closer look at the code I’m attaching it to this post. It was driven completely by using the &lt;a href="http://blog.ploeh.dk/2010/10/08/AutoDataTheoriesWithAutoFixture.aspx"&gt;AutoFixture.Xunit extension&lt;/a&gt;, so if you are interested in idiomatic &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; code it’s also a good example of that.&lt;/p&gt;&lt;a href="http://blog.ploeh.dk/content/binary/TennisKata.zip"&gt;TennisKata.zip (3.09 MB)&lt;/a&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=300de4ea-0211-4921-8aed-9295b77e90e6"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,300de4ea-0211-4921-8aed-9295b77e90e6.aspx</comments>
      <category>AutoFixture</category>
      <category>Software Design</category>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=991d4f09-1bc8-4550-81cc-a11309d07555</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,991d4f09-1bc8-4550-81cc-a11309d07555.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,991d4f09-1bc8-4550-81cc-a11309d07555.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=991d4f09-1bc8-4550-81cc-a11309d07555</wfw:commentRss>
      <title>Generic unit testing with xUnit.net</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,991d4f09-1bc8-4550-81cc-a11309d07555.aspx</guid>
      <link>http://blog.ploeh.dk/2011/05/09/GenericUnitTestingWithXUnitnet.aspx</link>
      <pubDate>Mon, 09 May 2011 12:37:17 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;Generics in .NET are wonderful, but sometimes when doing Test-Driven Development against a generic class I’ve felt frustrated because I’ve been feeling that dropping down to the lowest common denominator and testing against, say, Foo&amp;lt;object&amp;gt; doesn’t properly capture the variability inherent in generics. On the other hand, writing the same test for five different types of T have seemed too wasteful (not to mention boring) to bother with.&lt;/p&gt; &lt;p&gt;That’s until it occurred to me that in xUnit.net (and possibly other unit testing frameworks) I can define a generic test class. As an example, I wanted to test-drive a class with this class definition:&lt;/p&gt; &lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Interval&lt;/font&gt;&lt;/span&gt;&amp;lt;T&amp;gt;&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Instead of writing a set of tests against Interval&amp;lt;object&amp;gt; I rather wanted to write a set of tests against a representative set of T. This is so easy to do that I don’t know why I haven’t thought of it before. I simply declared the test class itself as a generic class:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;abstract&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IntervalFacts&lt;/font&gt;&lt;/span&gt;&amp;lt;T&amp;gt;&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The reason I declared the class as abstract is because that effectively prevents the test runner from trying to run the test methods directly on the class. That would fail because T is still open. However, it enabled me to write tests like this:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;[&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Theory&lt;/font&gt;&lt;/span&gt;, &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;AutoCatalogData&lt;/font&gt;&lt;/span&gt;]&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt;&lt;/span&gt; MinimumIsCorrect(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IComparable&lt;/font&gt;&lt;/span&gt;&amp;lt;T&amp;gt; first, &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IComparable&lt;/font&gt;&lt;/span&gt;&amp;lt;T&amp;gt; second)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; sut = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Interval&lt;/font&gt;&lt;/span&gt;&amp;lt;T&amp;gt;(first, second);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IComparable&lt;/font&gt;&lt;/span&gt;&amp;lt;T&amp;gt; result = sut.Minimum;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Assert&lt;/font&gt;&lt;/span&gt;.Equal(result, first);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In this test, I also use &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt;’s &lt;a href="http://blog.ploeh.dk/2010/10/08/AutoDataTheoriesWithAutoFixture.aspx"&gt;xUnit.net extension&lt;/a&gt;, but that’s completely optional. You might as well just write an old-fashioned unit test, but then you’ll need a &lt;a href="http://blog.ploeh.dk/2009/02/13/SUTFactory.aspx"&gt;SUT Factory&lt;/a&gt; that can resolve generics. If you don’t use AutoFixture any other (auto-mocking) container will do, and if you don’t use one of those, you can define a &lt;a href="http://en.wikipedia.org/wiki/Factory_method_pattern"&gt;Factory Method&lt;/a&gt; that creates appropriate instances of T (and, in this particular case, instances of IComparable&amp;lt;T&amp;gt;).&lt;/p&gt;
&lt;p&gt;In the above example I used a [Theory], but I might as well have been using a [Fact] as long as I had a container or a Factory Method to create the appropriate instances.&lt;/p&gt;
&lt;p&gt;The above test doesn’t execute in itself because the owning class is abstract. I needed to declare an appropriate set of constructed types for which I wanted the test to run. To do that, I defined the following test classes:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;DecimalIntervalFacts&lt;/font&gt;&lt;/span&gt; : &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IntervalFacts&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;decimal&lt;/font&gt;&lt;/span&gt;&amp;gt; { }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;StringIntervalFacts&lt;/font&gt;&lt;/span&gt; : &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IntervalFacts&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt;&amp;gt; { }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;DateTimeIntervalFacts&lt;/font&gt;&lt;/span&gt; : &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IntervalFacts&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;DateTime&lt;/font&gt;&lt;/span&gt;&amp;gt; { }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;TimSpanIntervalFacts&lt;/font&gt;&lt;/span&gt; : &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IntervalFacts&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;TimeSpan&lt;/font&gt;&lt;/span&gt;&amp;gt; { }&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The only thing these classes do is to each pick a particular type for T. However, since they are concrete classes with test methods, the test runner will pick up the test cases and execute them. This means that the single unit test I wrote above is now being executed four times – one for each constructed type.&lt;/p&gt;
&lt;p&gt;It’s even possible to specialize the generic class and add more methods to a single specialized class. As a sanity check I wanted to write a set of tests specifically against Interval&amp;lt;int&amp;gt;, so I added this class as well:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Int32IntervalFacts&lt;/font&gt;&lt;/span&gt; : &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IntervalFacts&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt;&amp;gt;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Theory&lt;/font&gt;&lt;/span&gt;]&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;InlineData&lt;/font&gt;&lt;/span&gt;(0, 0, 0, &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;true&lt;/font&gt;&lt;/span&gt;)]&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;InlineData&lt;/font&gt;&lt;/span&gt;(-1, 1, -1, &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;true&lt;/font&gt;&lt;/span&gt;)]&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;InlineData&lt;/font&gt;&lt;/span&gt;(-1, 1, 0, &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;true&lt;/font&gt;&lt;/span&gt;)]&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;InlineData&lt;/font&gt;&lt;/span&gt;(-1, 1, 1, &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;true&lt;/font&gt;&lt;/span&gt;)]&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;InlineData&lt;/font&gt;&lt;/span&gt;(-1, 1, -2, &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;false&lt;/font&gt;&lt;/span&gt;)]&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;InlineData&lt;/font&gt;&lt;/span&gt;(-1, 1, 2, &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;false&lt;/font&gt;&lt;/span&gt;)]&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt;&lt;/span&gt; Int32ContainsReturnsCorrectResult(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt; minimum, &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt; maximum, &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt; value,&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;bool&lt;/font&gt;&lt;/span&gt; expectedResult)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; sut = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Interval&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt;&amp;gt;(minimum, maximum);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; result = sut.Contains(value);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Assert&lt;/font&gt;&lt;/span&gt;.Equal(expectedResult, result);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#008000"&gt;// More tests...&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;When added as an extra class in addition to the four ‘empty’ concrete classes above, it now causes each generic method to be executed five times, whereas the above unit test is only executed for the Int32IntervalFacts class (on the other hand it’s a parameterized test, so the method is actually executed six times).&lt;/p&gt;
&lt;p&gt;It’s also possible to write parameterized tests in the generic test class itself:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;[&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Theory&lt;/font&gt;&lt;/span&gt;]&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;[&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;InlineData&lt;/font&gt;&lt;/span&gt;(-1, -1, &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;false&lt;/font&gt;&lt;/span&gt;)]&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;[&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;InlineData&lt;/font&gt;&lt;/span&gt;(-1, 0, &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;true&lt;/font&gt;&lt;/span&gt;)]&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;[&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;InlineData&lt;/font&gt;&lt;/span&gt;(-1, 1, &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;true&lt;/font&gt;&lt;/span&gt;)]&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;[&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;InlineData&lt;/font&gt;&lt;/span&gt;(0, -1, &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;false&lt;/font&gt;&lt;/span&gt;)]&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;[&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;InlineData&lt;/font&gt;&lt;/span&gt;(0, 0, &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;true&lt;/font&gt;&lt;/span&gt;)]&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;[&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;InlineData&lt;/font&gt;&lt;/span&gt;(0, 1, &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;true&lt;/font&gt;&lt;/span&gt;)]&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;[&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;InlineData&lt;/font&gt;&lt;/span&gt;(1, -1, &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;false&lt;/font&gt;&lt;/span&gt;)]&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;[&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;InlineData&lt;/font&gt;&lt;/span&gt;(1, 0, &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;false&lt;/font&gt;&lt;/span&gt;)]&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;[&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;InlineData&lt;/font&gt;&lt;/span&gt;(1, 1, &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;false&lt;/font&gt;&lt;/span&gt;)]&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt;&lt;/span&gt; ContainsReturnsCorrectResult(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt; minumResult, &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt; maximumResult,&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;bool&lt;/font&gt;&lt;/span&gt; expectedResult)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#008000"&gt;// Test method body&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Since this parameterized test in itself has 9 variations and is declared by IntervalFacts&amp;lt;T&amp;gt; which now has 5 constructed implementers, this single test method will be executed 9 x 5 = 45 times!&lt;/p&gt;
&lt;p&gt;Not that the the number of executed tests in itself is any measure of the quality of the test, but I do appreciate the ability to write generic unit tests against generic types.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=991d4f09-1bc8-4550-81cc-a11309d07555"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,991d4f09-1bc8-4550-81cc-a11309d07555.aspx</comments>
      <category>Unit Testing</category>
      <category>xUnit.net</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=420377cb-f645-4bde-a696-102faac51e86</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,420377cb-f645-4bde-a696-102faac51e86.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,420377cb-f645-4bde-a696-102faac51e86.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=420377cb-f645-4bde-a696-102faac51e86</wfw:commentRss>
      <title>AutoFixture 2.1</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,420377cb-f645-4bde-a696-102faac51e86.aspx</guid>
      <link>http://blog.ploeh.dk/2011/05/02/AutoFixture21.aspx</link>
      <pubDate>Mon, 02 May 2011 18:34:52 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;Since I announced &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; 2.1 beta 1 no issues have been reported, so I’ve now promoted the release to the official 2.1 release. This means that if you already downloaded AutoFixture 2.1 beta 1, you already have the 2.1 binaries. If not, you can head over to the &lt;a href="http://autofixture.codeplex.com/releases/view/64686"&gt;release page&lt;/a&gt; to get it.&lt;/p&gt; &lt;p&gt;The NuGet packages will soon be available.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt; (2011.5.4): The NuGet packages are now available.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=420377cb-f645-4bde-a696-102faac51e86"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,420377cb-f645-4bde-a696-102faac51e86.aspx</comments>
      <category>AutoFixture</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=6ee7b4b9-cb98-4360-a252-cfc6ab99d7b4</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,6ee7b4b9-cb98-4360-a252-cfc6ab99d7b4.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,6ee7b4b9-cb98-4360-a252-cfc6ab99d7b4.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=6ee7b4b9-cb98-4360-a252-cfc6ab99d7b4</wfw:commentRss>
      <title>Windows Azure migration smell: SQL Server over-utilization</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,6ee7b4b9-cb98-4360-a252-cfc6ab99d7b4.aspx</guid>
      <link>http://blog.ploeh.dk/2011/05/02/WindowsAzureMigrationSmellSQLServerOverutilization.aspx</link>
      <pubDate>Mon, 02 May 2011 12:23:49 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;Recently I partook in a Windows Azure migration workshop, helping developers from existing development organizations port their applications to Windows Azure. Once more an old design smell popped up: &lt;em&gt;SQL Server over-utilization&lt;/em&gt;. This ought to be old news to anyone with experience designing software on the Wintel stack, but apparently it bears repetition:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;Don’t put logic in your database. SQL Server should be used only for persistent storage of data.&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;(Yes: this post &lt;em&gt;is&lt;/em&gt; written in 2011…)&lt;/p&gt; &lt;p&gt;Many years ago I heard that role described as a ‘bit bucket’ – you put in data and pull it out again, and that’s all you do. No fancy stored procedures or functions or triggers.&lt;/p&gt; &lt;p&gt;Why wouldn’t we want to use the database if we have one? Scalability is the answer. SQL Server doesn’t scale horizontally. You can’t add more servers to take the load off a database server (well, some of my old colleagues will argue that this is possible with Oracle, and that may be true, but with SQL Server it’s impossible).&lt;/p&gt; &lt;p&gt;Yes, we can jump through hoops like partitioning and splitting the database up into several smaller databases, but it still doesn’t give us horizontal scalability. SQL Server is a bottleneck in any system in which it takes part.&lt;/p&gt; &lt;p&gt;How is this relevant to Windows Azure? It’s relevant for two important reasons:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;There’s an upper size limit on SQL Azure. Currently that size limit is 50 GB, and while it’s likely to grow in the future, there’s going to be a ceiling for a long time.  &lt;li&gt;You can’t fine tune the hardware for performance. The server runs on virtual hardware.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Development organizations that rely heavily on the database for execution of logic often need expensive hardware and experienced DBAs to squeeze extra performance out of the database servers. Such people know that write-intensive/append-only tables work best with one type of RAID, while read-intensive tables are better hosted on other file groups on different disks with different RAID configurations.&lt;/p&gt; &lt;p&gt;With SQL Azure you can just forget about all that.&lt;/p&gt; &lt;p&gt;The bottom line is that there are fundamental rules for software development that you must follow if you want to be able to successfully migrate to Windows Azure. I previously described an &lt;a href="http://blog.ploeh.dk/2010/10/14/WindowsAzureMigrationSanityCheck.aspx"&gt;even simpler sanity check you should perform&lt;/a&gt;, but after that you should take a good look at your database.&lt;/p&gt; &lt;p&gt;The best solution is if you can completely replace SQL Server with Azure’s very scalable storage services, but those &lt;a href="http://blog.ploeh.dk/2011/01/24/ScalableDoesntMeanFast.aspx"&gt;come with their own set of challenges&lt;/a&gt;.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=6ee7b4b9-cb98-4360-a252-cfc6ab99d7b4"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,6ee7b4b9-cb98-4360-a252-cfc6ab99d7b4.aspx</comments>
      <category>Azure</category>
      <category>Software Design</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=aec9ab37-ac49-4e6f-b875-a236a47a6ca1</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,aec9ab37-ac49-4e6f-b875-a236a47a6ca1.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,aec9ab37-ac49-4e6f-b875-a236a47a6ca1.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=aec9ab37-ac49-4e6f-b875-a236a47a6ca1</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <title>Feedback mechanisms and tradeoffs</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,aec9ab37-ac49-4e6f-b875-a236a47a6ca1.aspx</guid>
      <link>http://blog.ploeh.dk/2011/04/29/FeedbackMechanismsAndTradeoffs.aspx</link>
      <pubDate>Fri, 29 Apr 2011 13:02:14 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;&lt;em&gt;Rapid feedback&lt;/em&gt; is one of the cornerstones of agile development. The faster we can get feedback, the less it costs to correct errors. Unit testing is one of the ways to get feedback, but not the only one. Each way we can get feedback comes with its own advantages and disadvantages.&lt;/p&gt; &lt;p&gt;In my experience there’s a tradeoff between the cost of setting up a feedback mechanism and the level of confidence we can get from it. This is quite intuitive to most people, but I’ve rarely seen it explicitly described. The purpose of this post is to explicitly describe and relate those different mechanisms.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Compilation&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;In compiled languages, compilation provides the first level of feedback. I think a lot of people don’t think about this as feedback as (for compiled languages) it’s a necessary step before the code can be executed.&lt;/p&gt; &lt;p&gt;The level of confidence may not be very high – we tend to laugh at statements like ‘if it compiles, it works’. However, the compiler still catches a lot of mistakes. Think about it: does your code always compile, or do you sometimes get compilation errors? Do you sometimes try to compile your code just to see if it’s possible? I certainly do, and that’s because the compiler is always available, and it’s the first and fastest level of feedback we get.&lt;/p&gt; &lt;p&gt;Code can be designed to take advantage of this verification step. Constructor Injection, for example, ensures that we can’t create a new instance of a class without supplying it with its &lt;em&gt;required&lt;/em&gt; dependencies.&lt;/p&gt; &lt;p&gt;It’s also interesting to note that anecdotal evidence seems to suggest that unit testing is more prevalent for interpreted languages. That makes a lot of sense, because as developers we need feedback as soon as possible, and if there’s no compiler we must reach for the next level of feedback mechanism.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Static code analysis&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;The idea of static code analysis isn’t specific to .NET, but in certain versions of Visual Studio we have Code Analysis (which is also available as FxCop). Static code analysis is a step up from compilation – not only is code checked for syntactical correctness, but it’s also checked against a set of known anti-patterns and idioms.&lt;/p&gt; &lt;p&gt;Static code analysis is encapsulated expert knowledge, yet surprisingly few people use it. I think part of the reason for this is because the ratio of time against confidence isn’t as compelling as with other feedback mechanism. It takes some time to run the analysis, but like compilation the level of confidence gained from getting a clean result is not very high.&lt;/p&gt; &lt;p&gt;Personally, I use static code analysis once in a while (e.g. before checking in code to the default branch), but not as often as other feedback mechanisms. Still, I think this type of feedback mechanism is under-utilized.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Unit testing&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Running a unit test suite is one of the most efficient ways to gain rapid feedback. The execution time is often comparable to running static code analysis while the level of confidence is much higher.&lt;/p&gt; &lt;p&gt;Obviously a successful unit test execution is no guarantee that the final application works as intended, but it’s a much better indicator than the two previous mechanisms. When presented with the choice of using either static code analysis or unit tests, I prefer unit tests every time because the confidence level is much higher.&lt;/p&gt; &lt;p&gt;If you have sufficiently high code coverage I’d say that a successful unit test suite is enough confidence to check in code and let continuous integration take care of the rest.&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;Keep in mind that continuous integration and other types of automated builds are feedback mechanisms. If you institute rules where people are ‘punished’ for checking in code that breaks the build, you effectively disable the automated build as a source of feedback.&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;Thus, the rest of these feedback mechanisms should be used rarely (if at all) on developer work stations.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Integration testing&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Integration testing comes in several sub-categories. You can integrate multiple classes from within the same library, or integrate multiple libraries while still using &lt;a href="http://xunitpatterns.com/Test%20Double.html"&gt;Test Doubles&lt;/a&gt; instead of out-of-process resources. At this level, the cost/confidence ratio is comparable to unit testing.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Subcutaneous testing&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;A more full level of integration testing comes when all resources are integrated, but the tests are still driven by code instead of through the UI. While this gives a degree of confidence that is close to what you can get from a full systems test, the cost of setting up the entire testing environment is &lt;em&gt;much&lt;/em&gt; higher. In many cases I consider this the realm of a dedicated testing department, as it’s often a full-time job to maintain the suite of automation tools that enable such tests – particularly if we are talking about distributed systems.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;System testing&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;This is a test of the full system, often driven through the UI and supplemented with manual testing (such as exploratory testing). This provides the highest degree of confidence, but comes with a very high cost. It’s often at this level we find formal acceptance tests.&lt;/p&gt; &lt;p&gt;The time before we can get feedback at this level is often considerable. It may take days or even weeks or months.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Understand the cost/confidence ratio&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;The purpose of this post has been to talk about different ways we can get feedback when developing software. If we could get &lt;em&gt;instantaneous&lt;/em&gt; feedback with &lt;em&gt;guaranteed&lt;/em&gt; confidence it would be easy to choose, but as this is not the case we must understand the benefits and drawbacks of each mechanism.&lt;/p&gt; &lt;p&gt;It’s important to realize that there are many mechanisms, and that we can combine them in a staggered approach where we start with fast feedback (like the compiler) with a low degree of confidence and then move through successive stages that each provide a higher level of confidence.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=aec9ab37-ac49-4e6f-b875-a236a47a6ca1"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,aec9ab37-ac49-4e6f-b875-a236a47a6ca1.aspx</comments>
      <category>Productivity</category>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=ada0595f-f5c5-488e-b0ae-e179a6774188</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,ada0595f-f5c5-488e-b0ae-e179a6774188.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,ada0595f-f5c5-488e-b0ae-e179a6774188.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=ada0595f-f5c5-488e-b0ae-e179a6774188</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <title>Provider is not a pattern</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,ada0595f-f5c5-488e-b0ae-e179a6774188.aspx</guid>
      <link>http://blog.ploeh.dk/2011/04/27/ProviderIsNotAPattern.aspx</link>
      <pubDate>Wed, 27 Apr 2011 12:14:52 GMT</pubDate>
      <description>&lt;div&gt;&lt;p align="left"&gt;Developers exposed to ASP.NET are likely to be familiar with the so-called &lt;a href="http://msdn.microsoft.com/en-us/library/ms972319.aspx"&gt;Provider pattern&lt;/a&gt;. You see it a lot in that part of the BCL: &lt;a href="http://msdn.microsoft.com/en-us/library/6b241xwt.aspx"&gt;Role Provider&lt;/a&gt;, &lt;a href="http://msdn.microsoft.com/en-us/library/sx3h274z.aspx"&gt;Membership Provider&lt;/a&gt;, &lt;a href="http://msdn.microsoft.com/en-us/library/014bec1k.aspx"&gt;Profile Provider&lt;/a&gt;, etc. Lots of text has already been written about Providers, but the reason I want to add yet another blog post on the topic is because once in a while I get the question on how it relates to Dependency Injection (DI).&lt;/p&gt; &lt;p align="left"&gt;Is Provider a proper way to do DI?&lt;/p&gt; &lt;p align="left"&gt;No, it has nothing to do with DI, but as it tries to mimic loose coupling I can understand the confusion.&lt;/p&gt; &lt;p align="left"&gt;First things first. Let’s start with the name. Is it a pattern at all? Regular readers of this blog may get the impression that I’m fond of calling everything and the kitchen sink an anti-pattern. That’s not true because I &lt;a href="http://blog.ploeh.dk/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx"&gt;only make that claim when I’m certain I can hold that position&lt;/a&gt;, so I’m not going to denounce Provider as an anti-pattern. On the contrary I will make the claim that Provider is not a pattern at all.&lt;/p&gt; &lt;p align="left"&gt;A design pattern is not &lt;em&gt;invented&lt;/em&gt; – it’s &lt;em&gt;discovered&lt;/em&gt; as a repeated solution to a commonly recurring problem. Providers, on the other hand, were invented by Microsoft, and I’ve rarely seen them used outside their original scope. Secondly I’d also dispute that they solve anything.&lt;/p&gt; &lt;p align="left"&gt;That aside, however, I want to explain why Provider is bad design:&lt;/p&gt; &lt;ul&gt; &lt;li&gt; &lt;div align="left"&gt;It uses the Constrained Construction anti-pattern&lt;/div&gt; &lt;li&gt; &lt;div align="left"&gt;It hides complexity&lt;/div&gt; &lt;li&gt; &lt;div align="left"&gt;It prevents proper lifetime management&lt;/div&gt; &lt;li&gt; &lt;div align="left"&gt;It’s not testable&lt;/div&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p align="left"&gt;In the rest of this post I will explain each point in detail, but before I do that we need an example to look at. The &lt;a href="http://blog.ploeh.dk/2010/02/02/RefactoringToAggregateServices.aspx"&gt;old OrderProcessor example&lt;/a&gt; suffices, but instead of injecting IOrderValidator, IOrderCollector, and IOrderShipper this variation uses Providers to provide instances of the Services:&lt;/p&gt; &lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;SuccessResult&lt;/font&gt;&lt;/span&gt; Process(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Order&lt;/font&gt;&lt;/span&gt; order)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IOrderValidator&lt;/font&gt;&lt;/span&gt; validator = &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ValidatorProvider&lt;/font&gt;&lt;/span&gt;.Validator;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;bool&lt;/font&gt;&lt;/span&gt; isValid = validator.Validate(order);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (isValid)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;CollectorProvider&lt;/font&gt;&lt;/span&gt;.Collector.Collect(order);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ShipperProvider&lt;/font&gt;&lt;/span&gt;.Shipper.Ship(order);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.CreateStatus(isValid);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p align="left"&gt;The ValidatorProvider uses the configuration system to create and return an instance of IOrderValidator:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;static&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IOrderValidator&lt;/font&gt;&lt;/span&gt; Validator&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;get&lt;/font&gt;&lt;/span&gt; &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; section = &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;OrderValidationConfigurationSection&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .GetSection();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; typeName = section.ValidatorTypeName;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; type = &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Type&lt;/font&gt;&lt;/span&gt;.GetType(typeName, &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;true&lt;/font&gt;&lt;/span&gt;);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; obj = &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Activator&lt;/font&gt;&lt;/span&gt;.CreateInstance(type);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; (&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IOrderValidator&lt;/font&gt;&lt;/span&gt;)obj;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p align="left"&gt;There are lots of details I omitted here. I could have saved the reference for later use instead of creating a new instance each time the property is accessed. In that case I would also have had to make the code thread-safe, so I decided to skip that complexity. The code could also be more defensive, but I’m sure you get the picture.&lt;/p&gt;
&lt;p align="left"&gt;The type name is defined in the app.config file like this:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;&amp;lt;&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;&lt;span style="color: "&gt;&lt;font color="#a31515"&gt;orderValidation&lt;/font&gt;&lt;/span&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#0000ff"&gt; &lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp; &lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;&lt;span style="color: "&gt;&lt;font color="#ff0000"&gt;type&lt;/font&gt;&lt;/span&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;=&lt;/font&gt;&lt;/span&gt;"&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#0000ff"&gt;Ploeh.Samples.OrderModel.UnitTest.TrueOrderValidator,&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Ploeh.Samples.OrderModel.UnitTest&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;"&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#0000ff"&gt; /&amp;gt;&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p align="left"&gt;Obviously, CollectorProvider and ShipperProvider follow the same… blueprint.&lt;/p&gt;
&lt;p align="left"&gt;This should be well-known to most .NET developers, so what’s wrong with this model?&lt;/p&gt;
&lt;p align="left"&gt;&lt;strong&gt;Constrained Construction&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In &lt;a href="http://affiliate.manning.com/idevaffiliate.php?id=1150_236"&gt;my book&lt;/a&gt;’s chapter on DI anti-patterns I describe the &lt;em&gt;Constrained Construction&lt;/em&gt; anti-pattern. Basically it occurs every time there’s an implicit constraint on the constructor of an implementer. In the case of Providers the constraint is that each implementer must have a default constructor. In the example the culprit is this line of code:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; obj = &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Activator&lt;/font&gt;&lt;/span&gt;.CreateInstance(type);&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This constrains any implementation of IOrderValidator to have a default constructor, which obviously means that the most fundamental DI pattern Constructor Injection is out of the question.&lt;/p&gt;
&lt;p&gt;Variations of the Provider idiom is to supply an Initialize method with a context, but this creates a temporal coupling while still not enabling us to inject arbitrary Services into our implementations. I’m not going to repeat six pages of detailed description of Constrained Construction here, but the bottom line is that you can’t fix it – you have to refactor towards true DI – preferably Constructor Injection.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Hidden complexity&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Providers hide the complexity of their implementations. This is not the same as &lt;em&gt;encapsulation&lt;/em&gt;. Rather it’s a dishonest API and the problem is that it just postpones the moment when you discover how complex the implementation really is.&lt;/p&gt;
&lt;p&gt;When you implement a client and use code like the following everything looks &lt;em&gt;deceptively&lt;/em&gt; simple:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;&lt;font style="font-size: 10pt"&gt;IOrderValidator&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; validator = &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ValidatorProvider&lt;/font&gt;&lt;/span&gt;.Validator;&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;However, if this is the only line of code you write it will fail, but you will not notice until run-time. Check back to the implementation of the Validator property if you need to refresh the implementation: there’s a lot of things that can go wrong here:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The appropriate configuration section is not available in the app.config file. 
&lt;li&gt;The ValidatorTypeName is not provided, or is null, or is malformed. 
&lt;li&gt;The ValidatorTypeName is correctly formed, but the type in question cannot be located by Fusion. 
&lt;li&gt;The Type doesn’t have a default constructor. This is one of the other problems of Constrained Construction: it can’t be statically enforced because a constructor is not part of an &lt;a href="http://blog.ploeh.dk/2011/02/28/InterfacesAreAccessModifiers.aspx"&gt;abstraction’s API&lt;/a&gt;. 
&lt;li&gt;The created type doesn’t implement IOrderValidator.&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;I’m sure I even forgot a thing or two, but the above list is sufficient for me. None of these problems are caught by the compiler, so you don’t discover these issues until you run an integration test. So much for rapid feedback.&lt;/p&gt;
&lt;p&gt;I don’t like APIs that lie about their complexity.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Hiding complexity does not make an API easier to use; it makes it harder.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;An API that hides necessary complexity makes it impossible to discover problems at compile time. It simply creates more friction.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Lifetime management issues&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A Provider exerts too much control over the instances it creates. This is a variation of the &lt;em&gt;Control Freak&lt;/em&gt; anti-pattern (also from my book). In the current implementation the Validator property totally violates the &lt;a href="http://en.wikipedia.org/wiki/Principle_of_least_astonishment"&gt;Principle of least surprise&lt;/a&gt; since it returns a new instance every time you invoke the getter. I did this to keep the implementation simple (this is, after all, example code), but a more normal implementation would reuse the same instance every time.&lt;/p&gt;
&lt;p&gt;However, reusing the same instance every time may be problematic in a multi-threaded context (such as a web application) because you’ll need to make sure that the implementation is thread-safe. Often, we’d much prefer to scope the lifetime of the Service to each HTTP request.&lt;/p&gt;
&lt;p&gt;HTTP request scoping &lt;em&gt;can&lt;/em&gt; be built into the Provider, but then it would &lt;em&gt;only&lt;/em&gt; work in web applications. That’s not very flexible.&lt;/p&gt;
&lt;p&gt;What’s even more problematic is that once we move away from the Singleton lifestyle (not to be confused with the Singleton design pattern) we may have a memory leak at hand, since the implementation may implement IDisposable. This can be solved by adding a Release method to each Provider, but now we are moving so far into DI Container territory that I find it far more reasonable to just use proper DI instead of trying to reinvent the wheel.&lt;/p&gt;
&lt;p&gt;Furthermore, the fact that each Provider owns the lifetime of the Service it controls makes it impossible to share resources. What if the implementation we want to use implements several &lt;a href="http://martinfowler.com/bliki/RoleInterface.html"&gt;Role Interfaces&lt;/a&gt; each served up by a different Provider? We might want to use that common implementation to share or coordinate state across different Services, but that’s not possible because we can’t share an instance across multiple providers.&lt;/p&gt;
&lt;p&gt;Even if we configure all Providers with the same concrete class, each will instantiate and serve its own separate instance.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Testability&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The Control Freak also impacts testability. Since a Provider creates instances of interfaces based on XML configuration and Activator.CreateInstance, there’s no way to inject a dynamic mock.&lt;/p&gt;
&lt;p&gt;It &lt;em&gt;is&lt;/em&gt; possible to use hard-coded &lt;a href="http://xunitpatterns.com/Test%20Double.html"&gt;Test Doubles&lt;/a&gt; such as &lt;a href="http://xunitpatterns.com/Test%20Stub.html"&gt;Stubs&lt;/a&gt; or &lt;a href="http://xunitpatterns.com/Fake%20Object.html"&gt;Fakes&lt;/a&gt; because we can configure the XML with their type names, but even a &lt;a href="http://xunitpatterns.com/Test%20Spy.html"&gt;Spy&lt;/a&gt; is problematic because we’ll rarely have an object reference to the Test Double.&lt;/p&gt;
&lt;p&gt;In short, the Provider idiom is not a good approach to loose coupling. Although Microsoft uses it in some of their products, it only leads to problems, so there’s no reason to mimic it. Instead, use Constructor Injection to create loosely coupled components and wire them in the application’s &lt;em&gt;Composition Root&lt;/em&gt; using the &lt;a href="http://blog.ploeh.dk/2010/09/29/TheRegisterResolveReleasePattern.aspx"&gt;Register Resolve Release&lt;/a&gt; pattern.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=ada0595f-f5c5-488e-b0ae-e179a6774188"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,ada0595f-f5c5-488e-b0ae-e179a6774188.aspx</comments>
      <category>Dependency Injection</category>
      <category>Software Design</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=41a52013-c79e-45da-ac5f-29a1d3ba6ed5</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,41a52013-c79e-45da-ac5f-29a1d3ba6ed5.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,41a52013-c79e-45da-ac5f-29a1d3ba6ed5.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=41a52013-c79e-45da-ac5f-29a1d3ba6ed5</wfw:commentRss>
      <title>AutoFixture 2.1 beta 1</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,41a52013-c79e-45da-ac5f-29a1d3ba6ed5.aspx</guid>
      <link>http://blog.ploeh.dk/2011/04/19/AutoFixture21Beta1.aspx</link>
      <pubDate>Tue, 19 Apr 2011 14:05:11 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;It gives me great pleasure to announce that &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; 2.1 beta 1 is now &lt;a href="http://autofixture.codeplex.com/releases/view/64686"&gt;available for download&lt;/a&gt;. Compared to version 2.0 this release mostly contains added features as well as a few bug fixes. The release page contains a list of the new features.&lt;/p&gt; &lt;p&gt;The general availability of beta 1 of AutoFixture 2.1 marks the beginning of a trial period. If no new issues are reported within the next few weeks, a final version 2.1 will be released. If too many issues are reported, a new beta version may be necessary.&lt;/p&gt; &lt;p&gt;Please &lt;a href="http://autofixture.codeplex.com/workitem/list/basic"&gt;report any issues&lt;/a&gt; you find.&lt;/p&gt; &lt;p&gt;NuGet packages will follow the RTW version.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=41a52013-c79e-45da-ac5f-29a1d3ba6ed5"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,41a52013-c79e-45da-ac5f-29a1d3ba6ed5.aspx</comments>
      <category>AutoFixture</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=7c036292-a005-4205-8d5c-cd6de6c2378e</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,7c036292-a005-4205-8d5c-cd6de6c2378e.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,7c036292-a005-4205-8d5c-cd6de6c2378e.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=7c036292-a005-4205-8d5c-cd6de6c2378e</wfw:commentRss>
      <title>Constructor strategies for AutoFixture</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,7c036292-a005-4205-8d5c-cd6de6c2378e.aspx</guid>
      <link>http://blog.ploeh.dk/2011/04/19/ConstructorStrategiesForAutoFixture.aspx</link>
      <pubDate>Tue, 19 Apr 2011 06:59:19 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;When &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; creates complex objects it invokes the constructors of the types in question. When a class exposes more than one constructor, the &lt;a href="http://blog.ploeh.dk/2009/03/24/HowAutoFixtureCreatesObjects.aspx"&gt;default behavior is to pick the constructor with the fewest number&amp;nbsp; of arguments&lt;/a&gt;. This is the exact opposite of most DI Containers that select the greediest constructor.&lt;/p&gt; &lt;p&gt;For a DI Container it makes more sense to select the greediest constructor because that maximizes the chance that all dependencies are properly being auto-wired.&lt;/p&gt; &lt;p&gt;The reason that AutoFixture’s default behavior is different is that it maximizes the chance that an object graph can be created at all. If a convenience overload is available, this gives AutoFixture a better chance to compose the object graph.&lt;/p&gt; &lt;p align="left"&gt;This works well until a class comes along and introduces the wrong kind of ambiguity. Consider this case of &lt;em&gt;Bastard Injection&lt;/em&gt; (&lt;a href="http://affiliate.manning.com/idevaffiliate.php?id=1150_236"&gt;Dependency Injection in .NET&lt;/a&gt;, section 5.2):&lt;/p&gt; &lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;Bastard&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;private&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;readonly&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IFoo&lt;/font&gt;&lt;/span&gt; foo;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; Bastard()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; : &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;DefaultFoo&lt;/font&gt;&lt;/span&gt;())&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; Bastard(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IFoo&lt;/font&gt;&lt;/span&gt; foo)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (foo == &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;null&lt;/font&gt;&lt;/span&gt;)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;throw&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ArgumentNullException&lt;/font&gt;&lt;/span&gt;(&lt;span style="color: "&gt;&lt;font color="#a31515"&gt;"foo"&lt;/font&gt;&lt;/span&gt;);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.foo = foo;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IFoo&lt;/font&gt;&lt;/span&gt; Foo&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;get&lt;/font&gt;&lt;/span&gt; { &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.foo; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p align="left"&gt;By default AutoFixture will use the default constructor which means that the Foo will always be the instance of DefaultFoo owned by the Bastard class itself. This is problematic if we wish to &lt;a href="http://blog.ploeh.dk/2010/03/27/FreezingMocks.aspx"&gt;inject and freeze a Test Double&lt;/a&gt; because even if we freeze an IFoo instance, it will never be injected because the default constructor is being invoked.&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; fixture = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Fixture&lt;/font&gt;&lt;/span&gt;();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;fixture.Register&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IFoo&lt;/font&gt;&lt;/span&gt;&amp;gt;(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.CreateAnonymous&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;DummyFoo&lt;/font&gt;&lt;/span&gt;&amp;gt;);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; b = fixture.CreateAnonymous&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Bastard&lt;/font&gt;&lt;/span&gt;&amp;gt;();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;&lt;font style="font-size: 10pt"&gt;Assert&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;.IsAssignableFrom&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;DefaultFoo&lt;/font&gt;&lt;/span&gt;&amp;gt;(b.Foo);&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p align="left"&gt;As the above unit test demonstrates, even though the Register method call defines a mapping from IFoo to DummyFoo, the Foo property is an instance of DefaultFoo. The DummyFoo instance is never injected into the Bastard instance since the default constructor is used.&lt;/p&gt;
&lt;p align="left"&gt;Your first reaction in such a case should be to get rid of all convenience constructors to make the the class’ constructor unambiguous. However, it’s also possible to change AutoFixture’s behavior.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p align="left"&gt;The following is a description of a feature that will be a available in AutoFixture 2.1. It’s not available in AutoFixture 2.0, but is already available in the code repository. Thus, if you can’t wait for AutoFixture 2.1 you can download the source and build it.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p align="left"&gt;As always, AutoFixture’s very extensible interface makes it possible to change this behavior. Constructor selection is guided by an interface called IConstructorQuery, and while ModestConstructorQuery is the default implementation, there’s also an implementation called GreedyConstructorQuery.&lt;/p&gt;
&lt;p align="left"&gt;To change the behavior specifically for the Bastard class the Fixture instance must be customized. The following unit test demonstrates how to do that.&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; fixture = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Fixture&lt;/font&gt;&lt;/span&gt;();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;fixture.Customize&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Bastard&lt;/font&gt;&lt;/span&gt;&amp;gt;(c =&amp;gt; c.FromFactory(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ConstructorInvoker&lt;/font&gt;&lt;/span&gt;(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;GreedyConstructorQuery&lt;/font&gt;&lt;/span&gt;())));&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;fixture.Register&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IFoo&lt;/font&gt;&lt;/span&gt;&amp;gt;(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.CreateAnonymous&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;DummyFoo&lt;/font&gt;&lt;/span&gt;&amp;gt;);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; b = fixture.CreateAnonymous&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Bastard&lt;/font&gt;&lt;/span&gt;&amp;gt;();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;&lt;font style="font-size: 10pt"&gt;Assert&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;.IsAssignableFrom&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;DummyFoo&lt;/font&gt;&lt;/span&gt;&amp;gt;(b.Foo);&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p align="left"&gt;Notice that the only difference from before is an additional call to the Customize method where Bastard is modified to use greedy constructor selection. This changes the factory for the Bastard type, but not for any other types.&lt;/p&gt;
&lt;p align="left"&gt;If you’d rather prefer to completely change the overall constructor selection behavior for AutoFixture you can add the GreedyConstructorQuery wrapped in a ConstructorInvoker to the Fixture’s Customizations:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; fixture = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Fixture&lt;/font&gt;&lt;/span&gt;();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;fixture.Customizations.Add(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ConstructorInvoker&lt;/font&gt;&lt;/span&gt;(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;GreedyConstructorQuery&lt;/font&gt;&lt;/span&gt;()));&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;fixture.Register&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IFoo&lt;/font&gt;&lt;/span&gt;&amp;gt;(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.CreateAnonymous&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;DummyFoo&lt;/font&gt;&lt;/span&gt;&amp;gt;);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; b = fixture.CreateAnonymous&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Bastard&lt;/font&gt;&lt;/span&gt;&amp;gt;();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;&lt;font style="font-size: 10pt"&gt;Assert&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;.IsAssignableFrom&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;DummyFoo&lt;/font&gt;&lt;/span&gt;&amp;gt;(b.Foo);&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p align="left"&gt;In this unit test, the only change from the previous is that instead of assigning the GreedyConstructorQuery specifically to Bastard, it’s now assigned as the new default strategy. All specimens created by AutoFixture will be created by invoking their most greedy constructor.&lt;/p&gt;
&lt;p align="left"&gt;As always I find the default behavior more attractive, but the option to change behavior is there if you need it.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=7c036292-a005-4205-8d5c-cd6de6c2378e"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,7c036292-a005-4205-8d5c-cd6de6c2378e.aspx</comments>
      <category>AutoFixture</category>
      <category>Dependency Injection</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=0ba5b516-1130-4851-8a9d-ff72f49ec95f</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,0ba5b516-1130-4851-8a9d-ff72f49ec95f.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,0ba5b516-1130-4851-8a9d-ff72f49ec95f.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=0ba5b516-1130-4851-8a9d-ff72f49ec95f</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <title>Enumerables are dynamic–also in AutoFixture</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,0ba5b516-1130-4851-8a9d-ff72f49ec95f.aspx</guid>
      <link>http://blog.ploeh.dk/2011/04/18/EnumerablesAreDynamicalsoInAutoFixture.aspx</link>
      <pubDate>Mon, 18 Apr 2011 13:22:29 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;I just got a question about &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt;’s way of dealing with enumerables, and since I suspect this is something that might surprise a few people I found it reasonable to answer in a blog post.&lt;/p&gt; &lt;p&gt;In short, this unit test &lt;em&gt;succeeds:&lt;/em&gt;&lt;/p&gt; &lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; fixture = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Fixture&lt;/font&gt;&lt;/span&gt;();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; expected = fixture.CreateMany&amp;lt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt;&amp;gt;();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;&lt;font style="font-size: 10pt"&gt;Assert&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;.False(expected.SequenceEqual(expected));&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If this doesn’t surprise you, then read the assertion again. The sequence is expected to &lt;em&gt;not&lt;/em&gt; equal itself!&lt;/p&gt;
&lt;p&gt;&lt;em&gt;This behavior is by design.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;(I’ve always wanted to write that.) The reason is that the return type of the CreateMany method is IEnumerable&amp;lt;T&amp;gt;, and that interface makes no guarantee that it will return the same sequence every time you iterate over it. The implementation may be a &lt;a href="http://en.wikipedia.org/wiki/Generator_%28computer_science%29"&gt;Generator&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;According to its principle of &lt;a href="http://blog.ploeh.dk/2009/03/05/ConstrainedNonDeterminism.aspx"&gt;Constrained Non-determinism&lt;/a&gt;, AutoFixture goes to great lengths to ensure that since IEnumerable&amp;lt;T&amp;gt; &lt;em&gt;might be&lt;/em&gt; non-deterministic, it will be. This can be very useful in flushing out incorrect assumptions made by the &lt;a href="http://xunitpatterns.com/SUT.html"&gt;SUT&lt;/a&gt;.&lt;/p&gt;
&lt;p align="left"&gt;This behavior is carried forward by the &lt;a href="http://blog.ploeh.dk/2011/02/08/CreatingGeneralPopulatedListsWithAutoFixture.aspx"&gt;MultipleCustomization&lt;/a&gt;. This unit test also succeeds:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; fixture = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Fixture&lt;/font&gt;&lt;/span&gt;()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Customize(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;MultipleCustomization&lt;/font&gt;&lt;/span&gt;());&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; expected =&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.CreateAnonymous&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IEnumerable&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt;&amp;gt;&amp;gt;();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;&lt;font style="font-size: 10pt"&gt;Assert&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;.False(expected.SequenceEqual(expected));&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The behavior is the same because with the MultipleCustomization IEnumerable&amp;lt;T&amp;gt; acts as a &lt;a href="http://nblumhardt.com/2010/01/the-relationship-zoo/"&gt;relationship type&lt;/a&gt;. The class responsible for this behavior is called FiniteSequenceRelay, but AutoFixture 2.1 will also ship with an alternative StableFiniteSequenceRelay which wraps the sequence in a List&amp;lt;T&amp;gt;.&lt;/p&gt;
&lt;p&gt;To change the default behavior you can add the StableFiniteSequenceRelay to a Fixture’s customizations:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; fixture = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Fixture&lt;/font&gt;&lt;/span&gt;();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; stableRelay = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;StableFiniteSequenceRelay&lt;/font&gt;&lt;/span&gt;();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;fixture.Customizations.Add(stableRelay);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; expected =&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.CreateMany&amp;lt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt;&amp;gt;();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;&lt;font style="font-size: 10pt"&gt;Assert&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;.True(expected.SequenceEqual(expected));&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The above unit test succeeds. Notice that the assertion now verifies that the sequence of strings is equal to itself.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Just like MultipleCustomization the StableFiniteSequenceRelay class will be available in AutoFixture 2.1, but is not availale in 2.0.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;As always, it’s best to &lt;a href="http://blog.ploeh.dk/2011/03/18/EncapsulatingAutoFixtureCustomizations.aspx"&gt;encapsulate this behavior change into a Customization&lt;/a&gt;:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;StableFiniteSequenceCustomization&lt;/font&gt;&lt;/span&gt; :&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;ICustomization&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt;&lt;/span&gt; Customize(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IFixture&lt;/font&gt;&lt;/span&gt; fixture)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; stableRelay = &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;StableFiniteSequenceRelay&lt;/font&gt;&lt;/span&gt;();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.Customizations.Add(stableRelay);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This makes it easier to bundle it with the MultipleCustomization if we want all the other benefits of conventions for multiples:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;StableMultipeCustomization&lt;/font&gt;&lt;/span&gt; : &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;CompositeCustomization&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; StableMultipeCustomization()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; : &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;base&lt;/font&gt;&lt;/span&gt;(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;StableFiniteSequenceCustomization&lt;/font&gt;&lt;/span&gt;(),&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;MultipleCustomization&lt;/font&gt;&lt;/span&gt;())&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;With the StableMultipleCustomization the following unit test now passes:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; fixture = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Fixture&lt;/font&gt;&lt;/span&gt;()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Customize(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;StableMultipeCustomization&lt;/font&gt;&lt;/span&gt;());&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; expected =&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.CreateAnonymous&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IEnumerable&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;/span&gt;&amp;gt;&amp;gt;();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;&lt;font style="font-size: 10pt"&gt;Assert&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;.True(expected.SequenceEqual(expected));&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I still prefer the default behavior for its potential to act as an early warning system, but as I realize that this behavior may be problematic in some scenarios, the StableFiniteSequenceRelay provides an easy way to change that behavior.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Update (2011.8.10):&lt;/strong&gt; StableFiniteSequenceCustomization is now part of AutoFixture and will be available in AutoFixture 2.2.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=0ba5b516-1130-4851-8a9d-ff72f49ec95f"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,0ba5b516-1130-4851-8a9d-ff72f49ec95f.aspx</comments>
      <category>AutoFixture</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=e05fda1e-56a3-4aa6-8259-b943978f2843</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,e05fda1e-56a3-4aa6-8259-b943978f2843.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,e05fda1e-56a3-4aa6-8259-b943978f2843.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=e05fda1e-56a3-4aa6-8259-b943978f2843</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <title>MSDN Magazine article about CQRS on Windows Azure</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,e05fda1e-56a3-4aa6-8259-b943978f2843.aspx</guid>
      <link>http://blog.ploeh.dk/2011/04/05/MSDNMagazineArticleAboutCQRSOnWindowsAzure.aspx</link>
      <pubDate>Tue, 05 Apr 2011 19:52:57 GMT</pubDate>
      <description>&lt;div&gt;&lt;p align="left"&gt;My latest MSDN Magazine article, this time about CQRS on Windows Azure, is now &lt;a href="http://msdn.microsoft.com/en-us/magazine/gg983487.aspx"&gt;available at the April MSDN Magazine web site&lt;/a&gt;.&lt;/p&gt; &lt;p align="left"&gt;It’s mostly meant as an introduction to CQRS as well as containing some tips and tricks that are specific to applying CQRS on Windows Azure.&lt;/p&gt; &lt;p&gt;As an added bonus the code sample download contains lots of idiomatic unit tests written with &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt;’s &lt;a href="http://blog.ploeh.dk/2010/10/08/AutoDataTheoriesWithAutoFixture.aspx"&gt;xUnit.net extensions&lt;/a&gt;, so if you’d like to see the result of my TDD work with AutoFixture, there’s a complete code base to look at there.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=e05fda1e-56a3-4aa6-8259-b943978f2843"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,e05fda1e-56a3-4aa6-8259-b943978f2843.aspx</comments>
      <category>AutoFixture</category>
      <category>Azure</category>
      <category>Software Design</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=7cae4639-8453-4a39-9ad8-49269ff28896</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,7cae4639-8453-4a39-9ad8-49269ff28896.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,7cae4639-8453-4a39-9ad8-49269ff28896.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=7cae4639-8453-4a39-9ad8-49269ff28896</wfw:commentRss>
      <title>Commands are Composable</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,7cae4639-8453-4a39-9ad8-49269ff28896.aspx</guid>
      <link>http://blog.ploeh.dk/2011/03/22/CommandsAreComposable.aspx</link>
      <pubDate>Tue, 22 Mar 2011 13:09:25 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;A few months back I wrote a (somewhat theoretical) &lt;a href="http://blog.ploeh.dk/2010/12/03/TowardsBetterAbstractions.aspx"&gt;post on composable interfaces&lt;/a&gt;. A major point of that post was that &lt;a href="http://martinfowler.com/bliki/RoleInterface.html"&gt;Role Interfaces&lt;/a&gt;&amp;nbsp; with a single &lt;a href="http://en.wikipedia.org/wiki/Command_pattern"&gt;Command&lt;/a&gt; method (i.e. a method that returns no value) is a very versatile category of abstraction.&lt;/p&gt; &lt;p&gt;Some of my readers asked for examples, so in this post I will provide a few. Consider this interface that fits the above description:&lt;/p&gt; &lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;interface&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IMessageConsumer&lt;/font&gt;&lt;/span&gt;&amp;lt;T&amp;gt;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt;&lt;/span&gt; Consume(T message);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is a very common type of interface you will tend to encounter a lot in distributed, message-based architectures such as &lt;a href="http://abdullin.com/cqrs/"&gt;CQRS&lt;/a&gt; or &lt;a href="http://msdn.microsoft.com/en-us/architecture/aa699424"&gt;Udi Dahan’s view of SOA&lt;/a&gt;. Some people would call it a &lt;em&gt;message subscriber&lt;/em&gt; instead…&lt;/p&gt;
&lt;p&gt;In the rest of this post I will examine how we can create compositions out of the IMessageConsumer&amp;lt;T&amp;gt; interface using (in order of significance) &lt;a href="http://en.wikipedia.org/wiki/Decorator_pattern"&gt;Decorator&lt;/a&gt;, &lt;a href="http://en.wikipedia.org/wiki/Null_Object_pattern"&gt;Null Object&lt;/a&gt;, &lt;a href="http://en.wikipedia.org/wiki/Composite_pattern"&gt;Composite&lt;/a&gt;, and other well-known programming constructs.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Decorator&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Can we create a meaningful Decorator around the IMessageConsumer&amp;lt;T&amp;gt; interface? Yes, that’s easy – &lt;a href="http://blog.ploeh.dk/2010/04/07/DependencyInjectionIsLooseCoupling.aspx"&gt;I’ve earlier provided various detailed examples of Decorators&lt;/a&gt;, so I’m not going to repeat them here.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;I’ve yet to come up with an example of an interface that prevents us from applying a Decorator, so it’s a valid &lt;a href="http://en.wikipedia.org/wiki/Falsifiability"&gt;falsifiable&lt;/a&gt; claim that we can always Decorate an interface. However, I have yet to prove that this is true, so until now we’ll have to call it a &lt;a href="http://en.wikipedia.org/wiki/Conjecture"&gt;conjecture&lt;/a&gt;.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;However, since it’s so easy to apply a Decorator to an interface, it’s not a particularly valuable trait when evaluating the composability of an interface.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Null Object&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;It can be difficult to implement the Null Object pattern when the method(s) in question return a value, but for Commands it’s easy:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;NullMessageConsumer&lt;/font&gt;&lt;/span&gt;&amp;lt;T&amp;gt; : &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IMessageConsumer&lt;/font&gt;&lt;/span&gt;&amp;lt;T&amp;gt;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt;&lt;/span&gt; Consume(T message)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The implementation simply ignores the input and does nothing.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Once again my lack of formal CS education prevents me from putting forth a formal proof, but I strongly suspect that it’s always possibly to apply the Null Object pattern to a Command (keep in mind that &lt;em&gt;out&lt;/em&gt; parameters count as output, so any interface with one or more of these are not Commands).&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;It’s often valuable to be able to use a Null Object, but the real benefit comes when we can compose various implementations together.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Composite&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;To be truly composable, an interface should make it possible to create various concrete implementations that each adhere to the &lt;a href="http://en.wikipedia.org/wiki/Single_responsibility_principle"&gt;Single Responsibility Principle&lt;/a&gt; and then compose those together in a complex implementation. A Composite is a general-purpose implementation of this concept, and it’s easy to create a Composite out of a Command:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;CompositeMessageConsumer&lt;/font&gt;&lt;/span&gt;&amp;lt;T&amp;gt; : &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IMessageConsumer&lt;/font&gt;&lt;/span&gt;&amp;lt;T&amp;gt;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;private&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;readonly&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IEnumerable&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IMessageConsumer&lt;/font&gt;&lt;/span&gt;&amp;lt;T&amp;gt;&amp;gt; &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; consumers;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; CompositeMessageConsumer(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;params&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IMessageConsumer&lt;/font&gt;&lt;/span&gt;&amp;lt;T&amp;gt;[] consumers)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (consumers == &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;null&lt;/font&gt;&lt;/span&gt;)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;throw&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ArgumentNullException&lt;/font&gt;&lt;/span&gt;(&lt;span style="color: "&gt;&lt;font color="#a31515"&gt;"consumers"&lt;/font&gt;&lt;/span&gt;);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.consumers = consumers;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IEnumerable&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IMessageConsumer&lt;/font&gt;&lt;/span&gt;&amp;lt;T&amp;gt;&amp;gt; Consumers&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;get&lt;/font&gt;&lt;/span&gt; { &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.consumers; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #region&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; IMessageConsumer&amp;lt;T&amp;gt; Members&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt;&lt;/span&gt; Consume(T message)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;foreach&lt;/font&gt;&lt;/span&gt; (&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; consumer &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;in&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.Consumers)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; consumer.Consume(message);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#0000ff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #endregion&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The implementation of the Consume method simply loops over each composed IMessageConsumer&amp;lt;T&amp;gt; and invokes its Consume method.&lt;/p&gt;
&lt;p&gt;I can, for example, implement a sequence of actions that will take place by composing the individual concrete implementations. First we have a guard that protects against invalid messages, followed by a consumer that writes the message to a persistent store, completed by a consumer that raises a Domain Event that the reservation request was accepted.&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; c = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;CompositeMessageConsumer&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;MakeReservationCommand&lt;/font&gt;&lt;/span&gt;&amp;gt;(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; guard, &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; writer, &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; acceptRaiser);&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Consumers following the &lt;a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle"&gt;Liskov Substitution Principle&lt;/a&gt; will not notice the difference, as all they will see is an implementation of IMessageConsumer&amp;lt;MakeReservationCommand&amp;gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;More advanced programming constructs&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The Composite pattern only describes a single, general way to compose implementations, but with a Command interface we can do more. As &lt;a href="http://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215"&gt;Domain-Driven Design&lt;/a&gt; explains, a successful interface is often characterized by making it possible to apply well-known arithmetic or logical operators. As an example, in the case of the IMessageConsumer&amp;lt;T&amp;gt; interface, we can easily mimic the well-known ?! ternary operator from C#:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ConditionalMessageConsumer&lt;/font&gt;&lt;/span&gt;&amp;lt;T&amp;gt; : &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IMessageConsumer&lt;/font&gt;&lt;/span&gt;&amp;lt;T&amp;gt;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;private&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Func&lt;/font&gt;&lt;/span&gt;&amp;lt;T, &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;bool&lt;/font&gt;&lt;/span&gt;&amp;gt; condition;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;private&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IMessageConsumer&lt;/font&gt;&lt;/span&gt;&amp;lt;T&amp;gt; first;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;private&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IMessageConsumer&lt;/font&gt;&lt;/span&gt;&amp;lt;T&amp;gt; second;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; ConditionalMessageConsumer(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Func&lt;/font&gt;&lt;/span&gt;&amp;lt;T, &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;bool&lt;/font&gt;&lt;/span&gt;&amp;gt; condition, &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IMessageConsumer&lt;/font&gt;&lt;/span&gt;&amp;lt;T&amp;gt; first, &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IMessageConsumer&lt;/font&gt;&lt;/span&gt;&amp;lt;T&amp;gt; second)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (condition == &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;null&lt;/font&gt;&lt;/span&gt;)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;throw&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ArgumentNullException&lt;/font&gt;&lt;/span&gt;(&lt;span style="color: "&gt;&lt;font color="#a31515"&gt;"condition"&lt;/font&gt;&lt;/span&gt;);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (first == &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;null&lt;/font&gt;&lt;/span&gt;)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;throw&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ArgumentNullException&lt;/font&gt;&lt;/span&gt;(&lt;span style="color: "&gt;&lt;font color="#a31515"&gt;"first"&lt;/font&gt;&lt;/span&gt;);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (second == &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;null&lt;/font&gt;&lt;/span&gt;)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;throw&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ArgumentNullException&lt;/font&gt;&lt;/span&gt;(&lt;span style="color: "&gt;&lt;font color="#a31515"&gt;"second"&lt;/font&gt;&lt;/span&gt;);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.condition = condition;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.first = first;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.second = second;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt;&lt;/span&gt; Consume(T message)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.condition(message) ? &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.first : &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.second)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Consume(message);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is more verbose than the ?! operator because C# doesn’t allow us to define operator overloads for interfaces, but apart from that, it does exactly the same thing. Notice particularly that the Consume method uses the ?! operator to select among the two alternatives, and then subsequently invokes the Consume method on the selected consumer.&lt;/p&gt;
&lt;p&gt;We can use the ConditionalMessageConsumer to define branches in the consumption of messages. As an example, we can encapsulate the previous CompositeMessageConsumer&amp;lt;MakeReservationCommand&amp;gt; into a conditional branch like this:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; consumer = &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;ConditionalMessageConsumer&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;MakeReservationCommand&lt;/font&gt;&lt;/span&gt;&amp;gt;(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; guard.HasCapacity, c, rejectRaiser);&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Notice that I use the method group syntax to supply the condition delegate. If the HasCapacity method returns true, the previous composite (c) is being invoked, but if the result is false we instead use a consumer that raises the Domain Event that the reservation request was rejected.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Concluding thoughts&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Apart from the direct purpose of providing examples of the immensely powerful composition options a Command interface provides I want to point out a couple of things:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Each of the design pattern implementations (Null Object, Composite – even Conditional) are generic. This is a strong testament to the power of this particular abstraction. 
&lt;li&gt;The dynamic mocks I’m familiar with (&lt;a href="http://code.google.com/p/moq/"&gt;Moq&lt;/a&gt;, &lt;a href="http://ayende.com/projects/rhino-mocks.aspx"&gt;Rhino Mocks&lt;/a&gt;) will by default try to create Null Object implementations for interfaces without explicit setups. Since it’s trivial to implement a Null Command, they just emit them by default. If you use an AutoMocking Container with your unit tests, you can refactor to your heart’s content, adding and removing dependencies as long as they are Command interfaces, and your testing infrastructure will just take care of things for you. It’ll just work.
&lt;li&gt;Did you notice that even with these few building blocks we have implemented a large part of a sequential workflow engine? We can execute consumers in sequence as well as branch between different sequences. Obviously, more building blocks are needed to make a full-blown workflow engine, but not that many. I’ll leave the rest as an exercise to the reader :)&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;As I &lt;a href="http://blog.ploeh.dk/2010/12/03/TowardsBetterAbstractions.aspx"&gt;originally sketched&lt;/a&gt;, a Command interface is the ultimate in composability. To illustrate, the application from where I took the above examples is a small application with 48 types (classes and interfaces) in the production code base (that is, excluding unit tests). Of these, 9 are implementations of the IMessageConsumer&amp;lt;T&amp;gt; interface. If we also count the interface itself, it accounts for more than 20 percent of the code base. According to the &lt;a href="http://parlezuml.com/blog/?postid=934"&gt;Reused Abstractions Principle (RAP)&lt;/a&gt; I consider this a very successful abstraction.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=7cae4639-8453-4a39-9ad8-49269ff28896"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,7cae4639-8453-4a39-9ad8-49269ff28896.aspx</comments>
      <category>Software Design</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=12de0069-5d1f-4500-8aca-c12b8c2eedec</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,12de0069-5d1f-4500-8aca-c12b8c2eedec.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,12de0069-5d1f-4500-8aca-c12b8c2eedec.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=12de0069-5d1f-4500-8aca-c12b8c2eedec</wfw:commentRss>
      <title>Encapsulating AutoFixture Customizations</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,12de0069-5d1f-4500-8aca-c12b8c2eedec.aspx</guid>
      <link>http://blog.ploeh.dk/2011/03/18/EncapsulatingAutoFixtureCustomizations.aspx</link>
      <pubDate>Fri, 18 Mar 2011 12:51:08 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;&lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; is designed around the 80-20 principle. If your code is well-designed I’d expect a default instance of Fixture to be able to create specimens of your classes without too much trouble. There are cases where AutoFixture needs extra help:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;If a class consumes interfaces a default Fixture will not be able to create it. However, this is easily fixed through one of the &lt;a href="http://blog.ploeh.dk/2010/08/19/AutoFixtureAsAnAutomockingContainer.aspx"&gt;AutoMocking Customizations&lt;/a&gt;.  &lt;li&gt;If an API has circular references, Fixture might enter an infinite recursion, but you can easily customize it to cut off one the references.  &lt;li&gt;Some constructors may only accept arguments that don’t fit with the default specimens created by Fixture. &lt;a href="http://blog.ploeh.dk/2009/05/01/DealingWithConstrainedInput.aspx"&gt;There are ways to deal with that&lt;/a&gt; as well.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;I could keep on listing examples, but let’s keep it at that. The key assumption underlying AutoFixture is that these special cases are a relatively small part of the overall API you want to test – preferably much less than 20 percent.&lt;/p&gt; &lt;p&gt;Still, to address those special cases you’ll need to customize a Fixture instance in some way, but you also want to keep your test code DRY.&lt;/p&gt; &lt;p&gt;As the popularity of AutoFixture grows, I’m getting more and more glimpses of how people address this challenge: Some derive from Fixture, others create extension methods or other static methods, while others again wrap creation of Fixture instances in Factories or Builders.&lt;/p&gt; &lt;p&gt;There really is no need to do that. AutoFixture has an idiomatic way to encapsulate Customizations. It’s called… well… a &lt;em&gt;Customization&lt;/em&gt;, which is just another way to say that there’s an ICustomization interface that you can implement. This concept corresponds closely to the modularization APIs for several well-known DI Containers. Castle Windsor has &lt;em&gt;Installers&lt;/em&gt;, StructureMap has &lt;em&gt;Registries&lt;/em&gt; and Autofac has&lt;em&gt; Modules&lt;/em&gt;.&lt;/p&gt; &lt;p&gt;The ICustomization interface is simple and has a very gentle learning curve:&lt;/p&gt; &lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;interface&lt;/font&gt;&lt;/span&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;ICustomization&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt;&lt;/span&gt; Customize(&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;IFixture&lt;/font&gt;&lt;/span&gt; fixture);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Anything you can do with a Fixture instance you can also do with the IFixture instance passed to the Customize method, so this is the perfect place to encapsulate common Customizations to AutoFixture. Note that the AutoMocking extensions as well as several other optional behaviors for AutoFixture are already defined as Customizations.&lt;/p&gt;
&lt;p&gt;Using a Customization is also easy:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; fixture = &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Fixture&lt;/font&gt;&lt;/span&gt;()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Customize(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;DomainCustomization&lt;/font&gt;&lt;/span&gt;());&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You just need to invoke the Customize method on the Fixture. That’s no more difficult than calling a custom Factory or extension method – particularly if you also use a Visual Studio Code Snippet.&lt;/p&gt;
&lt;p&gt;When I start a new unit testing project, one of the first things I always do is to create a new ‘default’ customization for that project. It usually doesn’t take long before I need to tweak it a bit – if nothing else, then for adding the AutoMoqCustomization. To apply separation of concerns I encapsulate each Customization in its own class and compose them with a CompositeCustomization:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;DomainCustomization&lt;/font&gt;&lt;/span&gt; : &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;CompositeCustomization&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; DomainCustomization()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; : &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;base&lt;/font&gt;&lt;/span&gt;(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;AutoMoqCustomization&lt;/font&gt;&lt;/span&gt;(),&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;FuncCustomization&lt;/font&gt;&lt;/span&gt;())&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Whenever I need to make sweeping changes to my Fixtures I can simply modify DomainCustomization or one of the Customizations it composes.&lt;/p&gt;
&lt;p&gt;In fact, these days I rarely explicitly create new Fixture instances, but rather encapsulate them in a custom AutoDataAttribute like this:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;AutoDomainDataAttribute&lt;/font&gt;&lt;/span&gt; : &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;AutoDataAttribute&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; AutoDomainDataAttribute()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; : &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;base&lt;/font&gt;&lt;/span&gt;(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Fixture&lt;/font&gt;&lt;/span&gt;()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Customize(&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;DomainCustomization&lt;/font&gt;&lt;/span&gt;()))&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This means that I can reuse the DomainCustomization across normal, imperative unit tests as well as the &lt;a href="http://blog.ploeh.dk/2010/10/08/AutoDataTheoriesWithAutoFixture.aspx"&gt;declarative, xUnit.net-powered data theories I normally prefer&lt;/a&gt;:&lt;/p&gt;
&lt;div style="font-family: ; background: white; color: "&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;[&lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Theory&lt;/font&gt;&lt;/span&gt;, &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;AutoDomainData&lt;/font&gt;&lt;/span&gt;]&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt;&lt;/span&gt; CanReserveReturnsTrueWhenQuantityIsEqualToRemaining(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Capacity&lt;/font&gt;&lt;/span&gt; sut, &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Guid&lt;/font&gt;&lt;/span&gt; id)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; result = sut.CanReserve(sut.Remaining, id);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: "&gt;&lt;font color="#2b91af"&gt;Assert&lt;/font&gt;&lt;/span&gt;.True(result);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Using Customizations to encapsulate your own specializations makes it easy to compose and manage them in an object-oriented fashion.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=12de0069-5d1f-4500-8aca-c12b8c2eedec"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,12de0069-5d1f-4500-8aca-c12b8c2eedec.aspx</comments>
      <category>AutoFixture</category>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=3421c846-492c-4970-b6f4-1b43c625a4cc</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,3421c846-492c-4970-b6f4-1b43c625a4cc.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,3421c846-492c-4970-b6f4-1b43c625a4cc.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=3421c846-492c-4970-b6f4-1b43c625a4cc</wfw:commentRss>
      <title>Resolving closed types with MEF</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,3421c846-492c-4970-b6f4-1b43c625a4cc.aspx</guid>
      <link>http://blog.ploeh.dk/2011/03/14/ResolvingClosedTypesWithMEF.aspx</link>
      <pubDate>Mon, 14 Mar 2011 20:49:11 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;A while back I posed the challenge of &lt;a href="http://blog.ploeh.dk/2010/12/24/ChallengeResolveClosedTypesWithMEF.aspx"&gt;resolving closed types with MEF&lt;/a&gt;. I received some responses, but I also wanted to provide an alternative outline for a solution. In case you don’t remember the problem statement, it revolved around using the &lt;a href="http://mef.codeplex.com/"&gt;Managed Extensibility Framework&lt;/a&gt; (MEF) to compose classes in those cases where it’s impossible to annotate those classes with the MEF attributes. In the given example I want to compose the Mayonnaise class from EggYolk and OliveOil, but all three classes are sealed and cannot be recompiled.&lt;/p&gt; &lt;p&gt;As I describe &lt;a href="http://affiliate.manning.com/idevaffiliate.php?id=1150_236"&gt;in my book&lt;/a&gt;, a general solution to this type of problem is to create a sort of adapter the exports the closed type via a read-only attribute, like these EggYolkAdapter and MayonnaiseAdapter classes (the OliveOilAdapter looks just like the EggYolkAdapter):&lt;/p&gt; &lt;div style="font-family: ; background: white"&gt;&lt;pre style="margin: 0px"&gt;&lt;span&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;/font&gt;&lt;span&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;EggYolkAdapter&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;private&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#0000ff"&gt;readonly&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;EggYolk&lt;/font&gt;&lt;/span&gt; eggYolk;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; EggYolkAdapter()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.eggYolk = &lt;span&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;EggYolk&lt;/font&gt;&lt;/span&gt;();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;span&gt;&lt;font color="#2b91af"&gt;Export&lt;/font&gt;&lt;/span&gt;]&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#0000ff"&gt;virtual&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;EggYolk&lt;/font&gt;&lt;/span&gt; EggYolk&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;get&lt;/font&gt;&lt;/span&gt; { &lt;span&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.eggYolk; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font size="2"&gt;&lt;/font&gt;&amp;nbsp;&lt;/pre&gt;&lt;/div&gt;
&lt;div style="font-family: ; background: white"&gt;&lt;pre style="margin: 0px"&gt;&lt;span&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;/font&gt;&lt;span&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;MayonnaiseAdapter&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;private&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#0000ff"&gt;readonly&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;Mayonnaise&lt;/font&gt;&lt;/span&gt; mayo;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;span&gt;&lt;font color="#2b91af"&gt;ImportingConstructor&lt;/font&gt;&lt;/span&gt;]&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; MayonnaiseAdapter(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#2b91af"&gt;EggYolk&lt;/font&gt;&lt;/span&gt; yolk, &lt;span&gt;&lt;font color="#2b91af"&gt;OliveOil&lt;/font&gt;&lt;/span&gt; oil)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (yolk == &lt;span&gt;&lt;font color="#0000ff"&gt;null&lt;/font&gt;&lt;/span&gt;)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;throw&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;ArgumentNullException&lt;/font&gt;&lt;/span&gt;(&lt;span&gt;&lt;font color="#a31515"&gt;"yolk"&lt;/font&gt;&lt;/span&gt;);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (oil == &lt;span&gt;&lt;font color="#0000ff"&gt;null&lt;/font&gt;&lt;/span&gt;)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;throw&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;ArgumentNullException&lt;/font&gt;&lt;/span&gt;(&lt;span&gt;&lt;font color="#a31515"&gt;"oil"&lt;/font&gt;&lt;/span&gt;);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.mayo = &lt;span&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;Mayonnaise&lt;/font&gt;&lt;/span&gt;(yolk, oil);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;span&gt;&lt;font color="#2b91af"&gt;Export&lt;/font&gt;&lt;/span&gt;]&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#0000ff"&gt;virtual&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;Mayonnaise&lt;/font&gt;&lt;/span&gt; Mayonnaise&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;get&lt;/font&gt;&lt;/span&gt; { &lt;span&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.mayo; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Doing it like this is always possible, but if you have a lot of types that you need to compose, it becomes tedious having to define a lot of similar adapters. Fortunately, we can take it a step further and generalize the idea of a MEF adapter to a small set of generic classes.&lt;/p&gt;
&lt;p&gt;The EggYolkAdapter can be generalized as follows:&lt;/p&gt;
&lt;div style="font-family: ; background: white"&gt;&lt;pre style="margin: 0px"&gt;&lt;span&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;MefAdapter&lt;/font&gt;&lt;/span&gt;&amp;lt;T&amp;gt; &lt;span&gt;&lt;font color="#0000ff"&gt;where&lt;/font&gt;&lt;/span&gt; T : &lt;span&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt;()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;private&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#0000ff"&gt;readonly&lt;/font&gt;&lt;/span&gt; T export;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; MefAdapter()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.export = &lt;span&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; T();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;span&gt;&lt;font color="#2b91af"&gt;Export&lt;/font&gt;&lt;/span&gt;]&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#0000ff"&gt;virtual&lt;/font&gt;&lt;/span&gt; T Export&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;get&lt;/font&gt;&lt;/span&gt; { &lt;span&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.export; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Notice that I’ve more or less just replaced the EggYolk class with a type argument (T). However, I also had to add the generic new() constraint, which is often quite restrictive. However, to support a type like Mayonnaise, I can create another, similar generic MEF adapter like this:&lt;/p&gt;
&lt;div style="font-family: ; background: white"&gt;&lt;pre style="margin: 0px"&gt;&lt;span&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;MefAdapter&lt;/font&gt;&lt;/span&gt;&amp;lt;T1, T2, TResult&amp;gt;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;private&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#0000ff"&gt;readonly&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#0000ff"&gt;static&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;Func&lt;/font&gt;&lt;/span&gt;&amp;lt;T1, T2, TResult&amp;gt; createExport =&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#2b91af"&gt;FuncFactory&lt;/font&gt;&lt;/span&gt;.Create&amp;lt;T1, T2, TResult&amp;gt;();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;private&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#0000ff"&gt;readonly&lt;/font&gt;&lt;/span&gt; TResult export;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;span&gt;&lt;font color="#2b91af"&gt;ImportingConstructor&lt;/font&gt;&lt;/span&gt;]&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; MefAdapter(T1 arg1, T2 arg2)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.export = createExport(arg1, arg2);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;span&gt;&lt;font color="#2b91af"&gt;Export&lt;/font&gt;&lt;/span&gt;]&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#0000ff"&gt;virtual&lt;/font&gt;&lt;/span&gt; TResult Export&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;get&lt;/font&gt;&lt;/span&gt; { &lt;span&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.export; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The major difference from the simple MefAdapter&amp;lt;T&amp;gt; is that we need slightly more complicated code to invoke the constructor of TResult, which is expected to take two constructor arguments of types T1 and T2. This work is delegated to a FuncFactory that builds and compiles the appropriate delegate using an expression tree:&lt;/p&gt;
&lt;div style="font-family: ; background: white"&gt;&lt;pre style="margin: 0px"&gt;&lt;span&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;internal&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span&gt;&lt;font color="#0000ff"&gt;static&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;Func&lt;/font&gt;&lt;/span&gt;&amp;lt;T1, T2, TResult&amp;gt;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Create&amp;lt;T1, T2, TResult&amp;gt;()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; arg1Exp =&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#2b91af"&gt;Expression&lt;/font&gt;&lt;/span&gt;.Parameter(&lt;span&gt;&lt;font color="#0000ff"&gt;typeof&lt;/font&gt;&lt;/span&gt;(T1), &lt;span&gt;&lt;font color="#a31515"&gt;"arg1"&lt;/font&gt;&lt;/span&gt;);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; arg2Exp = &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#2b91af"&gt;Expression&lt;/font&gt;&lt;/span&gt;.Parameter(&lt;span&gt;&lt;font color="#0000ff"&gt;typeof&lt;/font&gt;&lt;/span&gt;(T2), &lt;span&gt;&lt;font color="#a31515"&gt;"arg2"&lt;/font&gt;&lt;/span&gt;);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; ctorInfo = &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;typeof&lt;/font&gt;&lt;/span&gt;(TResult).GetConstructor(&lt;span&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt;[]&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;typeof&lt;/font&gt;&lt;/span&gt;(T1),&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;typeof&lt;/font&gt;&lt;/span&gt;(T2)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; });&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; ctorExp =&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#2b91af"&gt;Expression&lt;/font&gt;&lt;/span&gt;.New(ctorInfo, arg1Exp, arg2Exp);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;Expression&lt;/font&gt;&lt;/span&gt;.Lambda&amp;lt;&lt;span&gt;&lt;font color="#2b91af"&gt;Func&lt;/font&gt;&lt;/span&gt;&amp;lt;T1, T2, TResult&amp;gt;&amp;gt;(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ctorExp, arg1Exp, arg2Exp).Compile();&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;With a couple of MEF adapters, I can now compose a MEF Catalog &lt;em&gt;almost&lt;/em&gt; like I can with a real DI Container, and resolve the Mayonnaise class:&lt;/p&gt;
&lt;div style="font-family: ; background: white"&gt;&lt;pre style="margin: 0px"&gt;&lt;span&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; catalog = &lt;span&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;TypeCatalog&lt;/font&gt;&lt;/span&gt;(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;typeof&lt;/font&gt;&lt;/span&gt;(&lt;span&gt;&lt;font color="#2b91af"&gt;MefAdapter&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span&gt;&lt;font color="#2b91af"&gt;OliveOil&lt;/font&gt;&lt;/span&gt;&amp;gt;),&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;typeof&lt;/font&gt;&lt;/span&gt;(&lt;span&gt;&lt;font color="#2b91af"&gt;MefAdapter&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span&gt;&lt;font color="#2b91af"&gt;EggYolk&lt;/font&gt;&lt;/span&gt;&amp;gt;),&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;typeof&lt;/font&gt;&lt;/span&gt;(&lt;span&gt;&lt;font color="#2b91af"&gt;MefAdapter&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span&gt;&lt;font color="#2b91af"&gt;EggYolk&lt;/font&gt;&lt;/span&gt;, &lt;span&gt;&lt;font color="#2b91af"&gt;OliveOil&lt;/font&gt;&lt;/span&gt;, &lt;span&gt;&lt;font color="#2b91af"&gt;Mayonnaise&lt;/font&gt;&lt;/span&gt;&amp;gt;)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; );&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; container = &lt;span&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;CompositionContainer&lt;/font&gt;&lt;/span&gt;(catalog);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; mayo = container.GetExportedValue&amp;lt;&lt;span&gt;&lt;font color="#2b91af"&gt;Mayonnaise&lt;/font&gt;&lt;/span&gt;&amp;gt;();&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If you want to change the Creation Policy to NonShared, you can derive from the MefAdapter classes and annotate them with [PartCreationPolicy] attributes.&lt;/p&gt;
&lt;p&gt;I’d never voluntarily choose to use MEF like this, but if I was stuck with MEF in a project and had to use it like a DI Container, I’d do something like this.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=3421c846-492c-4970-b6f4-1b43c625a4cc"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,3421c846-492c-4970-b6f4-1b43c625a4cc.aspx</comments>
      <category>Dependency Injection</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=df05c5b1-86bb-456c-a7b8-93cb78239424</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,df05c5b1-86bb-456c-a7b8-93cb78239424.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,df05c5b1-86bb-456c-a7b8-93cb78239424.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=df05c5b1-86bb-456c-a7b8-93cb78239424</wfw:commentRss>
      <title>Compose object graphs with confidence</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,df05c5b1-86bb-456c-a7b8-93cb78239424.aspx</guid>
      <link>http://blog.ploeh.dk/2011/03/04/ComposeObjectGraphsWithConfidence.aspx</link>
      <pubDate>Fri, 04 Mar 2011 11:15:10 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;The main principle behind the &lt;a href="http://blog.ploeh.dk/2010/09/29/TheRegisterResolveReleasePattern.aspx"&gt;Register Resolve Release&lt;/a&gt; pattern is that loosely coupled object graphs should be composed as &lt;em&gt;a single action&lt;/em&gt; in the entry point of the application (the Composition Root). For request-based applications (web sites and services), we use a variation where we compose &lt;em&gt;once&lt;/em&gt; per request.&lt;/p&gt; &lt;p&gt;It seems to me that a lot of people are apprehensive when they first hear about this concept. It may sound reasonable from an architectural point of view, but isn’t it horribly inefficient? A well-known example of such a concern is &lt;a href="http://jeffreypalermo.com/"&gt;Jeffrey Palermo&lt;/a&gt;’s blog post &lt;a href="http://jeffreypalermo.com/blog/constructor-over-injection-anti-pattern/"&gt;Constructor over-injection anti-pattern&lt;/a&gt;. Is it really a good idea to compose a complete object graph in one go? What if we don’t need part of the graph, or only need it later? Doesn’t it adversely affect response times?&lt;/p&gt; &lt;p&gt;Normally it doesn’t, and if it does, there are elegant ways to address the issue.&lt;/p&gt; &lt;p&gt;In the rest of this blog post I will expand on this topic. To keep the discussion as simple as possible, I’ll restrict my analysis to object trees instead of full graphs. This is quite a reasonable simplification as we should strive to avoid circular dependencies, but even in the case of full graphs the arguments and techniques put forward below hold.&lt;/p&gt; &lt;p&gt;Consider a simple tree composed of classes from three different assemblies:&lt;/p&gt; &lt;p&gt;&lt;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="Tree" border="0" alt="Tree" src="http://blog.ploeh.dk/content/binary/Windows-Live-Writer/Compose-object-graphs-with-confidence_921A/Tree_1.png" width="373" height="131"&gt;&lt;/p&gt; &lt;p&gt;All the A classes (blue) are defined in the A assembly, B classes (green) in the B assembly, and the C1 class (red) in the C assembly. In code we create the tree with Constructor Injection like this:&lt;/p&gt; &lt;div style="font-family: ; background: white"&gt;&lt;pre style="margin: 0px"&gt;&lt;span&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; t =&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;A1&lt;/font&gt;&lt;/span&gt;(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;A2&lt;/font&gt;&lt;/span&gt;(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;B1&lt;/font&gt;&lt;/span&gt;(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;B2&lt;/font&gt;&lt;/span&gt;()),&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;A3&lt;/font&gt;&lt;/span&gt;()),&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;C1&lt;/font&gt;&lt;/span&gt;(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;B3&lt;/font&gt;&lt;/span&gt;()));&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Given the tree above, we can now address the most common concerns about composing object trees in one go.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Will it be slow?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Most likely not. Keep in mind that &lt;a href="http://blog.ploeh.dk/2011/03/03/InjectionConstructorsShouldBeSimple.aspx"&gt;Injection Constructors should be very simple&lt;/a&gt;, so not a lot of work is going on during composition. Obviously just creating new object instances takes a bit of time in itself, but we create objects instances all the time in .NET code, and it’s often a very fast operation.&lt;/p&gt;
&lt;p&gt;Even when using DI Containers, which perform a lot of (necessary) extra work when creating objects, &lt;a href="http://www.codinginstinct.com/2008/05/ioc-container-benchmark-rerevisted.html"&gt;we can create tens of thousand trees per second&lt;/a&gt;. Creation of objects simply isn’t that big a deal.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;But still: what about assembly loading?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I glossed over an important point in the above argument. While object creation is fast, it sometimes takes a bit of time to load an assembly. The tree above uses classes from three different assemblies, so to create the tree all three assemblies must be loaded.&lt;/p&gt;
&lt;p&gt;In many cases that’s a performance hit you’ll have to take because &lt;em&gt;you need those classes anyway&lt;/em&gt;, but sometimes you might be concerned with taking this performance hit too early. However, I make the claim that in the vast majority of cases, this concern is irrelevant.&lt;/p&gt;
&lt;p&gt;In this particular context there are two different types of applications: Request-based applications (web) and all the rest (desktop apps, daemons, batch-jobs, etc.).&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Request-based applications&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;For request-based applications such as web sites and REST services, an object tree must be composed for each request. However, all requests are served by the same AppDomain, so once an assembly is loaded, it sticks around to be available for all subsequent requests. Thus, the first few requests will suffer a performance penalty from having to load all assemblies, but after that there will be no performance impact.&lt;/p&gt;
&lt;p&gt;In short, in request-based applications, you can compose object trees with confidence. In only extremely rare cases should you have performance issues from composing the entire tree in one go.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Long-running applications&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;For long-running applications the entire object tree must be composed at start-up. For background services such as daemons and batch processes the start-up time probably doesn’t matter much, but for desktop applications it can be of great importance.&lt;/p&gt;
&lt;p&gt;In some cases the application &lt;em&gt;requires&lt;/em&gt; the entire tree to be immediately available, in which case there’s not a lot you can do. Still, once all assemblies have been loaded, actually creating the tree will be very fast.&lt;/p&gt;
&lt;p&gt;In other cases an entire branch of the tree may not be immediately required. As an example, if the C1 node in the above graph isn’t needed right away, we could improve start-up time if we could somehow defer creating that branch, because this would also defer loading of the entire C assembly.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Deferred branches&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Since object creation is fast, the only case where it makes sense to defer loading of a branch is when creation of that branch causes an assembly to be loaded. If we can defer creation of such a branch, we can also defer loading of the assembly, thus improving the time it takes to compose the initial tree.&lt;/p&gt;
&lt;p&gt;Imagine that we wish to defer creation of the C1 branch of the above tree. It will prevent the C assembly from being loaded because that assembly is not used in any other place in the tree. However, it will not prevent the B assembly from being loaded, since that assembly is also being used by the A2 node.&lt;/p&gt;
&lt;p&gt;Still, in those rare situations where it makes sense to defer creation of a branch, we can make that cut into a part of the infrastructure of the tree. &lt;a href="http://blog.ploeh.dk/2010/01/20/RebuttalConstructorOverinjectionAntipattern.aspx"&gt;I originally described this technique&lt;/a&gt; as a reaction to the above mentioned post by Jeffrey Palermo, but here’s a restatement in the current context.&lt;/p&gt;
&lt;p&gt;We can defer creating the C1 node by wrapping it in a lazy implementation of the same interface. The C1 node implements an interface called ISolo&amp;lt;IMarker&amp;gt;, so we can wrap it in a Virtual Proxy that defers creation of C1 until it’s needed:&lt;/p&gt;
&lt;div style="font-family: ; background: white"&gt;&lt;pre style="margin: 0px"&gt;&lt;span&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;LazySoloMarker&lt;/font&gt;&lt;/span&gt; : &lt;span&gt;&lt;font color="#2b91af"&gt;ISolo&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span&gt;&lt;font color="#2b91af"&gt;IMarker&lt;/font&gt;&lt;/span&gt;&amp;gt;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;private&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#0000ff"&gt;readonly&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;Lazy&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span&gt;&lt;font color="#2b91af"&gt;ISolo&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span&gt;&lt;font color="#2b91af"&gt;IMarker&lt;/font&gt;&lt;/span&gt;&amp;gt;&amp;gt; lazy;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; LazySoloMarker(&lt;span&gt;&lt;font color="#2b91af"&gt;Lazy&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span&gt;&lt;font color="#2b91af"&gt;ISolo&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span&gt;&lt;font color="#2b91af"&gt;IMarker&lt;/font&gt;&lt;/span&gt;&amp;gt;&amp;gt; lazy)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (lazy == &lt;span&gt;&lt;font color="#0000ff"&gt;null&lt;/font&gt;&lt;/span&gt;)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;throw&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;ArgumentNullException&lt;/font&gt;&lt;/span&gt;(&lt;span&gt;&lt;font color="#a31515"&gt;"lazy"&lt;/font&gt;&lt;/span&gt;);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.lazy = lazy;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #region&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; ISolo&amp;lt;IMarker&amp;gt; Members&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;IMarker&lt;/font&gt;&lt;/span&gt; Item&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;get&lt;/font&gt;&lt;/span&gt; { &lt;span&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.lazy.Value.Item; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span&gt;&lt;font style="font-size: 10pt" color="#0000ff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #endregion&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This Virtual Proxy takes a Lazy&amp;lt;ISolo&amp;lt;IMarker&amp;gt;&amp;gt; as input and defers to it to implement the members of the interface. This only causes the Value property to be created when it’s first accessed – which may be long after the LazySoloMarker instance was created.&lt;/p&gt;
&lt;p&gt;The tree can now be composed like this:&lt;/p&gt;
&lt;div style="font-family: ; background: white"&gt;&lt;pre style="margin: 0px"&gt;&lt;span&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; t =&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;A1&lt;/font&gt;&lt;/span&gt;(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;A2&lt;/font&gt;&lt;/span&gt;(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;B1&lt;/font&gt;&lt;/span&gt;(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;B2&lt;/font&gt;&lt;/span&gt;()),&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;A3&lt;/font&gt;&lt;/span&gt;()),&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;LazySoloMarker&lt;/font&gt;&lt;/span&gt;(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;Lazy&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span&gt;&lt;font color="#2b91af"&gt;ISolo&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span&gt;&lt;font color="#2b91af"&gt;IMarker&lt;/font&gt;&lt;/span&gt;&amp;gt;&amp;gt;(() =&amp;gt; &lt;span&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;C1&lt;/font&gt;&lt;/span&gt;(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;B3&lt;/font&gt;&lt;/span&gt;()))));&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This retains all the original behavior of the original tree, but defers creation of the C1 node until it’s needed for the first time.&lt;/p&gt;
&lt;p&gt;The bottom line is this: you can compose the entire object graph with confidence. It’s not going to be a performance bottleneck.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=df05c5b1-86bb-456c-a7b8-93cb78239424"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,df05c5b1-86bb-456c-a7b8-93cb78239424.aspx</comments>
      <category>Dependency Injection</category>
      <category>Software Design</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=f7498461-6e53-4990-aade-d893333c6a14</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,f7498461-6e53-4990-aade-d893333c6a14.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,f7498461-6e53-4990-aade-d893333c6a14.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=f7498461-6e53-4990-aade-d893333c6a14</wfw:commentRss>
      <slash:comments>4</slash:comments>
      <title>Injection Constructors should be simple</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,f7498461-6e53-4990-aade-d893333c6a14.aspx</guid>
      <link>http://blog.ploeh.dk/2011/03/03/InjectionConstructorsShouldBeSimple.aspx</link>
      <pubDate>Thu, 03 Mar 2011 14:18:54 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;The Constructor Injection design pattern is a extremely useful way to implement loose coupling. It’s easy to understand and implement, but sometime perhaps a bit misunderstood.&lt;/p&gt; &lt;p&gt;The pattern itself is easily described through an example:&lt;/p&gt; &lt;div style="font-family: ; background: white"&gt;&lt;pre style="margin: 0px"&gt;&lt;span&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;private&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span&gt;&lt;font color="#0000ff"&gt;readonly&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;ISpecimenBuilder&lt;/font&gt;&lt;/span&gt; builder;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; SpecimenContext(&lt;span&gt;&lt;font color="#2b91af"&gt;ISpecimenBuilder&lt;/font&gt;&lt;/span&gt; builder)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (builder == &lt;span&gt;&lt;font color="#0000ff"&gt;null&lt;/font&gt;&lt;/span&gt;)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;throw&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;ArgumentNullException&lt;/font&gt;&lt;/span&gt;(&lt;span&gt;&lt;font color="#a31515"&gt;"builder"&lt;/font&gt;&lt;/span&gt;);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.builder = builder;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The SpecimenContext constructor &lt;em&gt;statically declares&lt;/em&gt; that it &lt;em&gt;requires&lt;/em&gt; an ISpecimenBuilder instance as an argument. To guarantee that the the builder field is an invariant of the class, the constructor contains a Guard Clause before it assigns the builder parameter to the builder field. This pattern can be repeated for each constructor argument.&lt;/p&gt;
&lt;p&gt;It’s important to understand that when using Constructor Injection the constructor should contain no additional logic.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;An Injection Constructor should do no more than receiving the dependencies.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;This is simply a rephrasing of &lt;a href="http://blog.vuscode.com/"&gt;Nikola Malovic&lt;/a&gt;’s &lt;a href="http://blog.vuscode.com/malovicn/archive/2009/10/16/inversion-of-control-single-responsibility-principle-and-nikola-s-laws-of-dependency-injection.aspx"&gt;4th law of IoC&lt;/a&gt;. There are several reasons for this rule of thumb:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;When we compose applications with Constructor Injection we often create substantial object graphs, and we want to be able to create these graphs as efficiently as possible. This is Nikola’s original argument. 
&lt;li&gt;In the odd (and not recommended) cases where you have circular dependencies, the injected dependencies may not yet be fully initialized, so an attempt to invoke their members at that time may result in an exception. This issue is similar to the &lt;a href="http://blogs.msdn.com/b/brada/archive/2004/08/12/213951.aspx"&gt;issue of invoking virtual members from the constructor&lt;/a&gt;. Conceptually, an injected dependency is equivalent to a virtual member. 
&lt;li&gt;With Constructor Injection, the constructor’s responsibility is to demand and receive the dependencies. Thus, according to the &lt;a href="http://en.wikipedia.org/wiki/Single_responsibility_principle"&gt;Single Responsibility Principle&lt;/a&gt; (SRP), it should not try to do something else as well. Some readers might argue that I’m misusing the SRP here, but I think I’m simply applying the underlying principle in a more granular context.&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;There’s no reason to feel constrained by this rule, as in any case the &lt;a href="http://blog.ploeh.dk/2011/02/28/InterfacesAreAccessModifiers.aspx"&gt;constructor is an implementation detail&lt;/a&gt;. In loosely coupled code, the constructor is not part of the overall application API. When we consider the API at that level, we are still free to design the API as we’d like.&lt;/p&gt;
&lt;p&gt;Please notice that this rule is contextual: it applies to Services that use Constructor Injection. Entities and Value Objects tend not to use DI, so their constructors are covered by other rules.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=f7498461-6e53-4990-aade-d893333c6a14"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,f7498461-6e53-4990-aade-d893333c6a14.aspx</comments>
      <category>Dependency Injection</category>
      <category>Software Design</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=7218be6f-54a5-460a-8da4-8c098ce7d4fc</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,7218be6f-54a5-460a-8da4-8c098ce7d4fc.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,7218be6f-54a5-460a-8da4-8c098ce7d4fc.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=7218be6f-54a5-460a-8da4-8c098ce7d4fc</wfw:commentRss>
      <slash:comments>9</slash:comments>
      <title>Interfaces are access modifiers</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,7218be6f-54a5-460a-8da4-8c098ce7d4fc.aspx</guid>
      <link>http://blog.ploeh.dk/2011/02/28/InterfacesAreAccessModifiers.aspx</link>
      <pubDate>Mon, 28 Feb 2011 13:19:04 GMT</pubDate>
      <description>&lt;div&gt;&lt;p align="left"&gt;.NET developers should by familiar with the &lt;a href="http://msdn.microsoft.com/en-us/library/ms173121.aspx"&gt;standard access modifiers&lt;/a&gt; (public, protected, internal, private). However, in loosely coupled code we can regard interface implementations as a fifth access modifier. This concept was originally introduced to me by &lt;a href="http://www.udidahan.com/?blog=true"&gt;Udi Dahan&lt;/a&gt; the only time I’ve had the pleasure of meeting him. That was many years ago and while I didn’t grok it back then, I’ve subsequently come to appreciate it quite a lot.&lt;/p&gt; &lt;p align="left"&gt;Although I can’t take credit for the idea, I’ve never seen it described, and it really deserves to be.&lt;/p&gt; &lt;p align="left"&gt;The basic idea is simple:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;If a consumer respects the &lt;a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle"&gt;Liskov Substitution Principle&lt;/a&gt; (LSP), the only visible members are those belonging to the interface. Thus, the interface represents a dimension of visibility.&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;As an example, consider this simple interface from &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt;:&lt;/p&gt; &lt;div style="font-family: ; background: white"&gt;&lt;pre style="margin: 0px"&gt;&lt;span&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span&gt;&lt;font color="#0000ff"&gt;interface&lt;/font&gt;&lt;/span&gt; &lt;/font&gt;&lt;span&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;ISpecimenContext&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;object&lt;/font&gt;&lt;/span&gt; Resolve(&lt;span&gt;&lt;font color="#0000ff"&gt;object&lt;/font&gt;&lt;/span&gt; request);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;A well-behaved consumer can only invoke the Resolve method even though an implementation may have additional public members:&lt;/p&gt;
&lt;div style="font-family: ; background: white"&gt;&lt;pre style="margin: 0px"&gt;&lt;span&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;public&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; &lt;span&gt;&lt;font color="#0000ff"&gt;class&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;SpecimenContext&lt;/font&gt;&lt;/span&gt; : &lt;/font&gt;&lt;span&gt;&lt;font style="font-size: 10pt" color="#2b91af"&gt;ISpecimenContext&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;{&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;private&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#0000ff"&gt;readonly&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;ISpecimenBuilder&lt;/font&gt;&lt;/span&gt; builder;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; SpecimenContext(&lt;span&gt;&lt;font color="#2b91af"&gt;ISpecimenBuilder&lt;/font&gt;&lt;/span&gt; builder)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (builder == &lt;span&gt;&lt;font color="#0000ff"&gt;null&lt;/font&gt;&lt;/span&gt;)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;throw&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;ArgumentNullException&lt;/font&gt;&lt;/span&gt;(&lt;span&gt;&lt;font color="#a31515"&gt;"builder"&lt;/font&gt;&lt;/span&gt;);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.builder = builder;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;ISpecimenBuilder&lt;/font&gt;&lt;/span&gt; Builder&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;get&lt;/font&gt;&lt;/span&gt; { &lt;span&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.builder; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #region&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; ISpecimenContext Members&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#0000ff"&gt;object&lt;/font&gt;&lt;/span&gt; Resolve(&lt;span&gt;&lt;font color="#0000ff"&gt;object&lt;/font&gt;&lt;/span&gt; request)&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;return&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.Builder.Create(request, &lt;span&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span&gt;&lt;font style="font-size: 10pt" color="#0000ff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #endregion&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Even though the SpecimenContext class defines the Builder property, as well as a public constructor, any consumer respecting the LSP will only see the Resolve method.&lt;/p&gt;
&lt;p&gt;In fact, the Builder property on the SpecimenContext class mostly exists to support unit testing because I sometimes need to assert that a given instance of SpecimenContext contains the expected ISpecimenBuilder. This doesn’t break encapsulation since the Builder is exposed as a read-only property, and it more importantly &lt;em&gt;doesn’t pollute the API&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;To support unit testing (and whichever other clients might be interested in the encapsulated ISpecimenBuilder) we have a public property that follows all framework design guidelines. However, it’s essentially an implementation detail, so it’s not visible via the ISpecimenContext interface.&lt;/p&gt;
&lt;p&gt;When writing loosely coupled code, I’ve increasingly begun to see the &lt;em&gt;interfaces as the real API&lt;/em&gt;. Most other (even public) members are pure implementation details. If the members are public, I still demand that they follow the framework design guidelines, but I don’t consider them parts of the API. It’s a very important distinction.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The interfaces define the bulk of an application’s API. Most other types and members are implementation details.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;An important corollary is that &lt;em&gt;constructors are implementation details too&lt;/em&gt;, since they can never by part of any interfaces.&lt;/p&gt;
&lt;p&gt;In that sense we can regard interfaces as a fifth access modifier – perhaps even the most important one.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=7218be6f-54a5-460a-8da4-8c098ce7d4fc"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,7218be6f-54a5-460a-8da4-8c098ce7d4fc.aspx</comments>
      <category>Dependency Injection</category>
      <category>Software Design</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=50e30b32-9d57-4309-bbc5-9b0002965d06</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,50e30b32-9d57-4309-bbc5-9b0002965d06.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,50e30b32-9d57-4309-bbc5-9b0002965d06.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=50e30b32-9d57-4309-bbc5-9b0002965d06</wfw:commentRss>
      <title>Creating general populated lists with AutoFixture</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,50e30b32-9d57-4309-bbc5-9b0002965d06.aspx</guid>
      <link>http://blog.ploeh.dk/2011/02/08/CreatingGeneralPopulatedListsWithAutoFixture.aspx</link>
      <pubDate>Tue, 08 Feb 2011 14:53:16 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;In &lt;a href="http://blog.ploeh.dk/2011/02/07/CreatingSpecificPopulatedListsWithAutoFixture.aspx"&gt;my previous post&lt;/a&gt; I described how to customize a Fixture instance to populate lists with items instead of returning empty lists. While it’s pretty easy to do so, the drawback is that you have to do it explicitly for every type you want to influence. In this post I will follow up by describing how to enable some general conventions that simply populates all collections that the Fixture resolves.&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;This post describes &lt;a href="http://autofixture.codeplex.com/workitem/4199"&gt;a feature&lt;/a&gt; that will be available in &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; 2.1. It’s not available in AutoFixture 2.0, but is already available in the code repository. Thus, if you can’t wait for AutoFixture 2.1 you can download the source and built it.&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;Instead of having to create multiple customizations for IEnumerable&amp;lt;int&amp;gt;, IList&amp;lt;int&amp;gt;, List&amp;lt;int&amp;gt;, IEnumerable&amp;lt;string&amp;gt;, IList&amp;lt;string&amp;gt;, etc. you can simply enable these general conventions as easy as this:&lt;/p&gt; &lt;div style="font-family: ; background: white"&gt;&lt;pre style="margin: 0px"&gt;&lt;span&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; fixture = &lt;span&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;Fixture&lt;/font&gt;&lt;/span&gt;()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Customize(&lt;span&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;MultipleCustomization&lt;/font&gt;&lt;/span&gt;());&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Notice that enabling conventions for populating sequences and lists with ‘many’ items is an optional customization that you must explicitly add.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;This feature must be explicitly enabled. There are several reasons for that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It would be a breaking change if AutoFixture suddenly started to behave like this by default. 
&lt;li&gt;The MultipleCustomization targets not only concrete types such as List&amp;lt;T&amp;gt; and Collection&amp;lt;T&amp;gt;, but also interfaces such as IEnumerable&amp;lt;T&amp;gt;, IList&amp;lt;T&amp;gt; etc. Thus, if you also use &lt;a href="http://blog.ploeh.dk/2010/08/19/AutoFixtureAsAnAutomockingContainer.aspx"&gt;AutoFixture as an Auto-Mocking container&lt;/a&gt;, I wanted to provide the ability to define which customization takes precedence.&lt;/li&gt;&lt;/ul&gt;&lt;/blockquote&gt;
&lt;p&gt;With that simple customization enabled, all requested IEnumerable&amp;lt;T&amp;gt; are now populated. The following will give us a finite, but populated list of integers:&lt;/p&gt;
&lt;div style="font-family: ; background: white"&gt;&lt;pre style="margin: 0px"&gt;&lt;span&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; integers = &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.CreateAnonymous&amp;lt;&lt;span&gt;&lt;font color="#2b91af"&gt;IEnumerable&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt;&amp;gt;&amp;gt;();&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This will give us a populated List&amp;lt;int&amp;gt;:&lt;/p&gt;
&lt;div style="font-family: ; background: white"&gt;&lt;pre style="margin: 0px"&gt;&lt;span&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; list = fixture.CreateAnonymous&amp;lt;&lt;span&gt;&lt;font color="#2b91af"&gt;List&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt;&amp;gt;&amp;gt;();&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This will give us a populated Collection&amp;lt;int&amp;gt;:&lt;/p&gt;
&lt;div style="font-family: ; background: white"&gt;&lt;pre style="margin: 0px"&gt;&lt;span&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; collection = &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.CreateAnonymous&amp;lt;&lt;span&gt;&lt;font color="#2b91af"&gt;Collection&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt;&amp;gt;&amp;gt;();&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As implied above, it also handles common list interfaces, so this gives us a populated IList&amp;lt;T&amp;gt;:&lt;/p&gt;
&lt;div style="font-family: ; background: white"&gt;&lt;pre style="margin: 0px"&gt;&lt;span&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; list = fixture.CreateAnonymous&amp;lt;&lt;span&gt;&lt;font color="#2b91af"&gt;IList&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt;&amp;gt;&amp;gt;();&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The exact number of ‘many’ is as always &lt;a href="http://blog.ploeh.dk/2009/05/11/AnonymousSequencesWithAutoFixture.aspx"&gt;determined by the Fixture’s RepeatCount&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;As this code is still (at the time of publishing) in preview, I would love to get feedback on this feature.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=50e30b32-9d57-4309-bbc5-9b0002965d06"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,50e30b32-9d57-4309-bbc5-9b0002965d06.aspx</comments>
      <category>AutoFixture</category>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=902ee5a9-6115-446b-89a7-d78e7f98b689</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,902ee5a9-6115-446b-89a7-d78e7f98b689.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,902ee5a9-6115-446b-89a7-d78e7f98b689.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=902ee5a9-6115-446b-89a7-d78e7f98b689</wfw:commentRss>
      <title>Creating specific populated lists with AutoFixture</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,902ee5a9-6115-446b-89a7-d78e7f98b689.aspx</guid>
      <link>http://blog.ploeh.dk/2011/02/07/CreatingSpecificPopulatedListsWithAutoFixture.aspx</link>
      <pubDate>Mon, 07 Feb 2011 19:49:26 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;How do you get &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; to create populated lists or sequences of items? Recently I seem to have been getting this question a lot, and luckily it’s quite easy to answer.&lt;/p&gt; &lt;p&gt;Let’s first look at the standard AutoFixture behavior and API.&lt;/p&gt; &lt;p&gt;You can ask AutoFixture to create an anonymous List like this:&lt;/p&gt; &lt;div style="font-family: ; background: white"&gt;&lt;pre style="margin: 0px"&gt;&lt;span&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; list = fixture.CreateAnonymous&amp;lt;&lt;span&gt;&lt;font color="#2b91af"&gt;List&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt;&amp;gt;&amp;gt;();&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Seen from AutoFixture’s point of view, List&amp;lt;int&amp;gt; is just a class like any other. It has a default constructor, so AutoFixture just uses that and returns an instance. You get back an instance, no exceptions are thrown, but the list is empty. What if you’d rather want a populated list?&lt;/p&gt;
&lt;p&gt;There are many ways to go about this. A simple, low-level solution is to populate the list after creation:&lt;/p&gt;
&lt;div style="font-family: ; background: white"&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;fixture.AddManyTo(list);&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;However, you may instead prefer getting a populated list right away. This is also possible, but before we look at how to get there, I’d like to point out a feature that surprisingly few users notice. You can &lt;a href="http://blog.ploeh.dk/2009/05/11/AnonymousSequencesWithAutoFixture.aspx"&gt;create many anonymous specimens at once&lt;/a&gt;:&lt;/p&gt;
&lt;div style="font-family: ; background: white"&gt;&lt;pre style="margin: 0px"&gt;&lt;span&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;var&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; integers = fixture.CreateMany&amp;lt;&lt;span&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt;&amp;gt;();&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Armed with this knowledge, as well as the knowledge of &lt;a href="http://blog.ploeh.dk/2010/04/06/MappingTypesWithAutoFixture.aspx"&gt;how to map types&lt;/a&gt;, we can now create this customization to map IEnumerable&amp;lt;int&amp;gt; to CreateMany&amp;lt;int&amp;gt;:&lt;/p&gt;
&lt;div style="font-family: ; background: white"&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;fixture.Register(() =&amp;gt; fixture.CreateMany&amp;lt;&lt;span&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt;&amp;gt;());&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The Register method is really a generic method, but since we have type inference, we don’t have to write it out. However, since CreateMany&amp;lt;int&amp;gt;() returns IEnumerable&amp;lt;int&amp;gt;, this is the type we register. Thus, every time we subsequently resolve IEnumerable&amp;lt;int&amp;gt;, we will get back a populated sequence.&lt;/p&gt;
&lt;p&gt;Getting back to the original List&amp;lt;int&amp;gt; example, we can now customize it to a populated list like this:&lt;/p&gt;
&lt;div style="font-family: ; background: white"&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;fixture.Register(() =&amp;gt;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.CreateMany&amp;lt;&lt;span&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt;&amp;gt;().ToList());&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Because the ToList() extension method returns List&amp;lt;T&amp;gt;, this call registers List&amp;lt;int&amp;gt; so that we will get back a populated list of integers every time the fixture resolves List&amp;lt;int&amp;gt;.&lt;/p&gt;
&lt;p&gt;What about other collection types that don’t have a nice LINQ extension method? Personally, I never use Collection&amp;lt;T&amp;gt;, but if you wanted, you could customize it like this:&lt;/p&gt;
&lt;div style="font-family: ; background: white"&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;fixture.Register(() =&amp;gt;&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;Collection&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt;&amp;gt;(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.CreateMany&amp;lt;&lt;span&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt;&amp;gt;().ToList()));&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Since Collection&amp;lt;T&amp;gt; has a constructor overload that take IList&amp;lt;T&amp;gt; we can customize the type to use this specific overload and populate it with ‘many’ items.&lt;/p&gt;
&lt;p&gt;Finally, we can combine all this to map from collection interfaces to populated lists. As an example, we can map from IList&amp;lt;int&amp;gt; to a populated List&amp;lt;int&amp;gt; like this:&lt;/p&gt;
&lt;div style="font-family: ; background: white"&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;fixture.Register&amp;lt;&lt;span&gt;&lt;font color="#2b91af"&gt;IList&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt;&amp;gt;&amp;gt;(() =&amp;gt; &lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.CreateMany&amp;lt;&lt;span&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt;&amp;gt;().ToList());&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;When we use the Register method to map types we can no longer rely on type inference. Instead, we must explicitly register IList&amp;lt;int&amp;gt; against a delegate that creates a populated List&amp;lt;int&amp;gt;. Because List&amp;lt;int&amp;gt; implements IList&amp;lt;int&amp;gt; this compiles. Whenever this fixture instance resolves IList&amp;lt;int&amp;gt; it will create a populated List&amp;lt;int&amp;gt;.&lt;/p&gt;
&lt;p&gt;All of this describes what you can do with the strongly typed API available in AutoFixture 2.0. It’s easy and very flexible, but the only important drawback is that it’s not general. All of the customizations in this post specifically address lists and sequences of integers, but not lists of any other type. What if you would like to expand this sort of behavior to any List&amp;lt;T&amp;gt;, IEnumerable&amp;lt;T&amp;gt; etc?&lt;/p&gt;
&lt;p&gt;Stay tuned, because in &lt;a href="http://blog.ploeh.dk/2011/02/08/CreatingGeneralPopulatedListsWithAutoFixture.aspx"&gt;the next post I will describe how to do that&lt;/a&gt;.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=902ee5a9-6115-446b-89a7-d78e7f98b689"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,902ee5a9-6115-446b-89a7-d78e7f98b689.aspx</comments>
      <category>AutoFixture</category>
      <category>Unit Testing</category>
    </item>
  </channel>
</rss>
