<?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>Thu, 02 Feb 2012 20:37:40 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=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>4</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>11</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>8</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>3</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>15</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>9</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>
      <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>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=bd87a8f8-dba8-44f5-944b-53b295b99f31</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,bd87a8f8-dba8-44f5-944b-53b295b99f31.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,bd87a8f8-dba8-44f5-944b-53b295b99f31.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=bd87a8f8-dba8-44f5-944b-53b295b99f31</wfw:commentRss>
      <slash:comments>9</slash:comments>
      <title>The BCL already has a Maybe monad</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,bd87a8f8-dba8-44f5-944b-53b295b99f31.aspx</guid>
      <link>http://blog.ploeh.dk/2011/02/04/TheBCLAlreadyHasAMaybeMonad.aspx</link>
      <pubDate>Fri, 04 Feb 2011 13:11:34 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;During the last couple of weeks I’ve been very interested in using a Maybe monad with &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt;’s Kernel code, but although many examples can be found on the internet, they remain samples. &lt;a href="http://abdullin.com/journal/2009/10/6/zen-development-practices-c-maybe-monad.html"&gt;Rinat Abdullin&lt;/a&gt; and &lt;a href="http://weblogs.asp.net/zowens/archive/2009/09/04/maybe-monad-my-c-version.aspx"&gt;Zack Owens&lt;/a&gt; both posted samples, but I particularly like &lt;a href="http://mikehadlow.blogspot.com/"&gt;Mike Hadlow&lt;/a&gt;’s s&lt;a href="http://mikehadlow.blogspot.com/2011/01/monads-in-c1-introduction.html"&gt;eries about Monads in C#&lt;/a&gt; because he also explains how to &lt;a href="http://mikehadlow.blogspot.com/2011/01/monads-in-c-5-maybe.html"&gt;use LINQ with monads such as the Maybe monad&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;As I really wanted a Maybe monad for AutoFixture, I first thought about simply implementing it directly in the AutoFixture source. However, I found it too arbitrary to put such a general purpose programming construct into a specific library such as AutoFixture. My next thought was to create a small open source project just for that single purpose, but then I though about the problem a bit more…&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;The BCL sort of already has a Maybe monad – you just need to recognize it as such.&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;What is a Maybe monad really? If you really distill it, it’s just a type that either contains a value, or doesn’t contain a value. In other words, it’s a type that represents a particular range: a set with either zero or one items. That’s just a special case of a more general range or collection, and we already have LINQ covering those constructs.&lt;/p&gt; &lt;p&gt;Here it is: the Maybe monad from the BLC (encapsulated in a nice extension method):&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;static&lt;/font&gt;&lt;/span&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;LightweightMaybe&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;public&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;IEnumerable&lt;/font&gt;&lt;/span&gt;&amp;lt;T&amp;gt; Maybe&amp;lt;T&amp;gt;(&lt;span&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt; T 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;&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;new&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;Obviously, this method returns a Maybe with a value, but we can just as easily represent Nothing with an empty array.&lt;/p&gt;
&lt;p&gt;With my ‘new’ Maybe monad, I can now write code like this (where &lt;em&gt;request&lt;/em&gt; is a System.Object instance):&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;return&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; (&lt;span&gt;&lt;font color="#0000ff"&gt;from&lt;/font&gt;&lt;/span&gt; t &lt;span&gt;&lt;font color="#0000ff"&gt;in&lt;/font&gt;&lt;/span&gt; request.Maybe().OfType&amp;lt;&lt;span&gt;&lt;font color="#2b91af"&gt;Type&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&gt;&lt;font color="#0000ff"&gt;let&lt;/font&gt;&lt;/span&gt; typeArguments = t.GetGenericArguments()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="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;where&lt;/font&gt;&lt;/span&gt; typeArguments.Length == 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;amp;&amp;amp; &lt;span&gt;&lt;font color="#0000ff"&gt;typeof&lt;/font&gt;&lt;/span&gt;(&lt;span&gt;&lt;font color="#2b91af"&gt;IList&lt;/font&gt;&lt;/span&gt;&amp;lt;&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; == t.GetGenericTypeDefinition()&lt;/font&gt;&lt;/pre&gt;&lt;pre style="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;select&lt;/font&gt;&lt;/span&gt; context.Resolve(&lt;span&gt;&lt;font color="#0000ff"&gt;typeof&lt;/font&gt;&lt;/span&gt;(&lt;span&gt;&lt;font color="#2b91af"&gt;List&lt;/font&gt;&lt;/span&gt;&amp;lt;&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; .MakeGenericType(typeArguments)))&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .DefaultIfEmpty(&lt;span&gt;&lt;font color="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span&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; .SingleOrDefault();&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You may think that this looks dense, but before that the code looked 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; type = request &lt;span&gt;&lt;font color="#0000ff"&gt;as&lt;/font&gt;&lt;/span&gt; &lt;span&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;span&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;if&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; (type == &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;{&lt;/font&gt;&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="#0000ff"&gt;new&lt;/font&gt;&lt;/span&gt; &lt;span&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;}&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; typeArguments = type.GetGenericArguments();&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;if&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; (typeArguments.Length != 1)&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;return&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;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;}&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;if&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; (&lt;span&gt;&lt;font color="#0000ff"&gt;typeof&lt;/font&gt;&lt;/span&gt;(&lt;span&gt;&lt;font color="#2b91af"&gt;IList&lt;/font&gt;&lt;/span&gt;&amp;lt;&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; type.GetGenericTypeDefinition())&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;return&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;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;}&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;return&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt; context.Resolve(&lt;span&gt;&lt;font color="#0000ff"&gt;typeof&lt;/font&gt;&lt;/span&gt;(&lt;span&gt;&lt;font color="#2b91af"&gt;List&lt;/font&gt;&lt;/span&gt;&amp;lt;&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; .MakeGenericType(typeArguments));&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Notice that in this more traditional approach involving Guard Clauses, I have to construct a new NoSpecimen object in three different places, thus violating the DRY principle. I like not having all those if/return blocks in the code.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=bd87a8f8-dba8-44f5-944b-53b295b99f31"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,bd87a8f8-dba8-44f5-944b-53b295b99f31.aspx</comments>
      <category>Software Design</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=412d27b8-4d12-4ee2-8a7e-e970147187bc</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,412d27b8-4d12-4ee2-8a7e-e970147187bc.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,412d27b8-4d12-4ee2-8a7e-e970147187bc.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=412d27b8-4d12-4ee2-8a7e-e970147187bc</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <title>Scalable doesn’t mean fast</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,412d27b8-4d12-4ee2-8a7e-e970147187bc.aspx</guid>
      <link>http://blog.ploeh.dk/2011/01/24/ScalableDoesntMeanFast.aspx</link>
      <pubDate>Mon, 24 Jan 2011 12:03:16 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;Recently I spent a couple of days with &lt;a href="http://twitter.com/#!/tjementum"&gt;Thomas Jespersen&lt;/a&gt; who’s working towards a launch of &lt;a href="http://www.spiir.dk/"&gt;spiir.dk&lt;/a&gt; – on Windows Azure. The reason I got to talk to him was to see if I could help with some performance issues he had with Azure Table Storage.&lt;/p&gt; &lt;p&gt;The scenario is really simple: the application needs to load all of a user’s bank transactions into memory to enable pretty advanced sorting and filtering. That sounds like a lot, but really isn’t more than approximately 200 kB of data retrieved through a &lt;em&gt;single&lt;/em&gt; query – so: there are no 1+N problems in play here, but even so it originally took more than two seconds. That’s a bit long to wait before you can even start rendering a web page.&lt;/p&gt; &lt;p&gt;By tweaking his partitioning strategy and using parallel queries, Thomas managed to bring down the data retrieval time to approximately one second. Although stress testing indicated that this duration was very stable, even under load, it is still too slow. So we met to see what could be done. &lt;/p&gt; &lt;p&gt;Thomas had done a great job tweaking the query, so I couldn’t really suggest some sort of secret API that would make it run significantly faster. Basically, we have to deal with Azure storage being based on REST and that there are a lot of things about run-time behavior we cannot control. Apart from designing a proper partitioning strategy, we can’t add indexes to Azure Table Storage.&lt;/p&gt; &lt;p&gt;It was time to take a different approach.&lt;/p&gt; &lt;p&gt;As far as I can tell, Windows Azure is designed to be &lt;em&gt;very&lt;/em&gt; scalable. However, just because scalability implies that you can handle an insane amount of work within acceptable time frames, it doesn’t mean that you can &lt;em&gt;extrapolate&lt;/em&gt; it to mean that under a light load, everything will be lightning fast. That’s not the case at all.&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;Scalability means that performance characteristics remain &lt;em&gt;stable&lt;/em&gt; from light to heavy load.&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;Consequently this means that if performance is adequate under heavy load, it will also be adequate under a light load. Azure Storage is first and foremost designed to be scalable, and as a second priority, as fast as possible.&lt;/p&gt; &lt;p&gt;As Thomas discovered, Azure Table Storage isn’t particularly fast.&lt;/p&gt; &lt;p&gt;It may be a masochistic side of me that I’m not otherwise aware of, but I actually appreciate that. It makes us reassess our most basic assumptions.&lt;/p&gt; &lt;p&gt;The data that Thomas needs to read isn’t particularly dynamic, so what if we take a snapshot of it? In short, we loaded all of a user’s data into memory and serialized it to Azure Blob Storage.&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;Loading the same data from a binary serialized Blob took only 1/6 of the time it did to load it from Table Storage.&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;As it turns out, Thomas doesn’t even need all the columns from the Table to populate the view, so we could even make the serialized Blob smaller yet.&lt;/p&gt; &lt;p&gt;At this point, however, we now have two representations of the same data: The original data in Table Storage, and a &lt;em&gt;persistent cache&lt;/em&gt; in Blob Storage. The remaining challenge is to figure out how to keep these in sync.&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;This may seem like a hack, but is really represents a paradigm shift. Letting go of ACID opens up a lot of new opportunities.&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;Actually, I spend most of the next day trying to convince Thomas that &lt;a href="http://abdullin.com/cqrs"&gt;CQRS&lt;/a&gt; would be the best approach, or that we could at least pick up some of the techniques from asynchronous, messaging based architectures, but that’s another story.&lt;/p&gt; &lt;p&gt;The morale here is that on Azure, things may be slower than you are used to, but storage is (relatively) cheap, so denormalization can save you a lot of execution time.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=412d27b8-4d12-4ee2-8a7e-e970147187bc"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,412d27b8-4d12-4ee2-8a7e-e970147187bc.aspx</comments>
      <category>Azure</category>
      <category>Software Design</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=b5de0372-7630-4bb0-8a0a-fb818f8a1a67</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,b5de0372-7630-4bb0-8a0a-fb818f8a1a67.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,b5de0372-7630-4bb0-8a0a-fb818f8a1a67.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=b5de0372-7630-4bb0-8a0a-fb818f8a1a67</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <title>My Christmas challenge has a winner</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,b5de0372-7630-4bb0-8a0a-fb818f8a1a67.aspx</guid>
      <link>http://blog.ploeh.dk/2011/01/01/MyChristmasChallengeHasAWinner.aspx</link>
      <pubDate>Sat, 01 Jan 2011 13:53:33 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;A week ago I concluded Microsoft Denmark’s 2010 .NET Community Christmas Calendar with a challenge about &lt;a href="http://blog.ploeh.dk/2010/12/24/ChallengeResolveClosedTypesWithMEF.aspx"&gt;resolving closed types with MEF&lt;/a&gt;. As 2011 came around, the deadline ended, so it’s now time to pick the winner.&lt;/p&gt; &lt;p&gt;I didn’t get a lot of entries, which can be interpreted in at least one (or more) of the following ways:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;The challenge was too difficult  &lt;li&gt;The challenge wasn’t interesting  &lt;li&gt;The prize wasn’t attractive  &lt;li&gt;People had other things to do during the holidays&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Whatever the reason, it made my task of picking a winner that much easier. The best Danish entry came from &lt;a href="http://danielvg.dk/"&gt;Daniel Volder Guarnieri&lt;/a&gt; who cheated a bit by partially hard-coding the composition of Mayonnaise into the ContainerBuilder. As I wrote in the original challenge, there are many ways to tackle the challenge, and one was to take the unit tests very literally :)&lt;/p&gt; &lt;p&gt;However, honorable mention must go to &lt;a href="http://code.dortikum.net/"&gt;Boyan Mihaylov&lt;/a&gt; who participated just for the honor. He took a more general approach similar to &lt;a href="http://fluentmef.codeplex.com/"&gt;Fluent MEF&lt;/a&gt;. This involves implementing a completely new &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.composition.primitives.composablepartcatalog.aspx"&gt;ComposablePartCatalog&lt;/a&gt; with associated &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.composition.primitives.composablepartdefinition.aspx"&gt;ComposablePartDefinition&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.composition.primitives.composablepart.aspx"&gt;ComposablePart&lt;/a&gt; implementations – not a trivial undertaking.&lt;/p&gt; &lt;p&gt;Kudos to Boyan and congratulations to Daniel. My thanks for your submissions, and a happy new year to all my readers!&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=b5de0372-7630-4bb0-8a0a-fb818f8a1a67"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,b5de0372-7630-4bb0-8a0a-fb818f8a1a67.aspx</comments>
      <category>Dependency Injection</category>
      <category>Miscellaneous</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=2f570521-8df8-4b6a-bcce-6e1bfea1a6ee</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,2f570521-8df8-4b6a-bcce-6e1bfea1a6ee.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,2f570521-8df8-4b6a-bcce-6e1bfea1a6ee.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=2f570521-8df8-4b6a-bcce-6e1bfea1a6ee</wfw:commentRss>
      <title>Challenge: Resolve closed types with MEF</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,2f570521-8df8-4b6a-bcce-6e1bfea1a6ee.aspx</guid>
      <link>http://blog.ploeh.dk/2010/12/24/ChallengeResolveClosedTypesWithMEF.aspx</link>
      <pubDate>Fri, 24 Dec 2010 09:29:06 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;For my international reader, a bit of context is in order for this post: Microsoft Denmark sponsors a series of Christmas challenges known as the Microsoft Christmas Calendar. Different bloggers alternate hosting a coding challenge for the day, and Microsoft sponsors the prizes.&lt;/p&gt; &lt;p&gt;Today I have the honor of hosting the last challenge for 2010. Contestants from the Danish .NET community have the opportunity to win a &lt;a href="http://www.thinkgeek.com/homeoffice/kitchen/c2b1/"&gt;Molecular Gastronomy Starter Kit&lt;/a&gt; – if any of my international readers feel like joining in, they are welcome, but (probably) not eligible for the prize.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;The challenge&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;The &lt;a href="http://mef.codeplex.com/"&gt;Managed Extensibility Framework&lt;/a&gt; (MEF) enables us to compose applications by annotating types and members with [Import] and [Export] attributes, but what can you do when you have types without these attributes and you can’t add the attributes?&lt;/p&gt; &lt;p&gt;For example, how can we compose Mayonnaise from these three classes?&lt;/p&gt;&lt;!--
{\rtf\ansi{\fonttbl{\f0 Consolas;}}{\colortbl;\red0\green0\blue255;\red43\green145\blue175;\red163\green21\blue21;}\f0 \fs19 \cf1 public\cf0  \cf1 sealed\cf0  \cf1 class\cf0  \cf2 EggYolk\cf0  \{ \}\par \par \cf1 public\cf0  \cf1 sealed\cf0  \cf1 class\cf0  \cf2 OliveOil\cf0  \{ \}\par \par \cf1 public\cf0  \cf1 sealed\cf0  \cf1 class\cf0  \cf2 Mayonnaise\cf0 \par \{\par     \cf1 private\cf0  \cf1 readonly\cf0  \cf2 EggYolk\cf0  eggYolk;\par     \cf1 private\cf0  \cf1 readonly\cf0  \cf2 OliveOil\cf0  oil;\par \par     \cf1 public\cf0  Mayonnaise(\cf2 EggYolk\cf0  eggYolk, \cf2 OliveOil\cf0  oil)\par     \{\par         \cf1 if\cf0  (eggYolk == \cf1 null\cf0 )\par         \{\par             \cf1 throw\cf0  \cf1 new\cf0  \cf2 ArgumentNullException\cf0 (\cf3 "eggYolk"\cf0 );\par         \}\par         \cf1 if\cf0  (oil == \cf1 null\cf0 )\par         \{\par             \cf1 throw\cf0  \cf1 new\cf0  \cf2 ArgumentNullException\cf0 (\cf3 "oil"\cf0 );\par         \}\par \par         \cf1 this\cf0 .eggYolk = eggYolk;\par         \cf1 this\cf0 .oil = oil;\par     \}\par \par     \cf1 public\cf0  \cf2 EggYolk\cf0  EggYolk\par     \{\par         \cf1 get\cf0  \{ \cf1 return\cf0  \cf1 this\cf0 .eggYolk; \}\par     \}\par \par     \cf1 public\cf0  \cf2 OliveOil\cf0  Oil\par     \{\par         \cf1 get\cf0  \{ \cf1 return\cf0  \cf1 this\cf0 .oil; \}\par     \}\par \}\par }
--&gt; &lt;div style="font-family: consolas; background: none repeat scroll 0% 0% 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;sealed&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;EggYolk&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: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;sealed&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;OliveOil&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: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;sealed&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Mayonnaise&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: rgb(43, 145, 175);"&gt;EggYolk&lt;/span&gt; eggYolk;&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: rgb(43, 145, 175);"&gt;OliveOil&lt;/span&gt; oil;&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; Mayonnaise(&lt;span style="color: rgb(43, 145, 175);"&gt;EggYolk&lt;/span&gt; eggYolk, &lt;span style="color: rgb(43, 145, 175);"&gt;OliveOil&lt;/span&gt; oil)&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; (eggYolk == &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: rgb(43, 145, 175);"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"eggYolk"&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; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (oil == &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: rgb(43, 145, 175);"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"oil"&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;this&lt;/span&gt;.eggYolk = eggYolk;&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;.oil = oil;&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: rgb(43, 145, 175);"&gt;EggYolk&lt;/span&gt; EggYolk&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;get&lt;/span&gt; { &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.eggYolk; }&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: rgb(43, 145, 175);"&gt;OliveOil&lt;/span&gt; Oil&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;get&lt;/span&gt; { &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.oil; }&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 challenge is to come up with a good solution to that problem. Here are the formal rules:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The unit test suite at the end of this post must pass. 
&lt;/li&gt;&lt;li&gt;You are not allowed to edit the unit tests. 
&lt;/li&gt;&lt;li&gt;You are only allowed to add &lt;em&gt;one&lt;/em&gt; (1) &lt;em&gt;using&lt;/em&gt; directive to the unit test file to reference the namespace of your proposed solution. 
&lt;/li&gt;&lt;li&gt;You must work from the Visual Studio 2010 solution attached to this post. Add a new project that contains your solution. Send me the solution in a .zip file to enter the contest. 
&lt;/li&gt;&lt;li&gt;You are allowed to implement your solution in &lt;em&gt;any language&lt;/em&gt; you would like as long as it compiles and runs from Visual Studio 2010 Premium. 
&lt;/li&gt;&lt;li&gt;The winner is chosen by my subjective judgment, but I will emphasize clean code and design. A tip: attempt to get as good scores as possible from Visual Studio’s &lt;em&gt;Code Analysis&lt;/em&gt; and &lt;em&gt;Code Metrics&lt;/em&gt;. Good scores does not guarantee that you win, but bad scores will most likely ensure that you don’t. 
&lt;/li&gt;&lt;li&gt;Since many of you are on Christmas vacation the deadline is this year. As long as you submit a solution in 2010 (Danish time) you’re a contestant.&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;There are lots of different ways to skin this cat, so I’m looking forward to your submissions to see all your creative solutions.&lt;/p&gt;
&lt;p&gt;This unit test suite is the specification:&lt;/p&gt;&lt;!--
{\rtf\ansi{\fonttbl{\f0 Consolas;}}{\colortbl;\red0\green0\blue255;\red43\green145\blue175;}\f0 \fs19 \cf1 using\cf0  System.ComponentModel.Composition.Hosting;\par \cf1 using\cf0  Ploeh.Samples.MeffyXmas.MenuModel;\par \cf1 using\cf0  Xunit;\par \par \cf1 namespace\cf0  Ploeh.Samples.MeffyXmas.MefMenu.UnitTest\par \{\par     \cf1 public\cf0  \cf1 class\cf0  \cf2 ContainerBuilderFacts\cf0 \par     \{\par         [\cf2 Fact\cf0 ]\par         \cf1 public\cf0  \cf1 void\cf0  DefaultContainerCorrectlyResolvesOliveOil()\par         \{\par             \cf2 CompositionContainer\cf0  container = \cf1 new\cf0  \cf2 ContainerBuilder\cf0 ()\par                 .Build();\par             \cf1 var\cf0  oil = container.GetExportedValue&amp;lt;\cf2 OliveOil\cf0 &amp;gt;();\par             \cf2 Assert\cf0 .NotNull(oil);\par         \}\par \par         [\cf2 Fact\cf0 ]\par         \cf1 public\cf0  \cf1 void\cf0  DefaultContainerCorrectlyResolvesEggYolk()\par         \{\par             \cf2 CompositionContainer\cf0  container = \cf1 new\cf0  \cf2 ContainerBuilder\cf0 ()\par                 .Build();\par             \cf1 var\cf0  yolk = container.GetExportedValue&amp;lt;\cf2 EggYolk\cf0 &amp;gt;();\par             \cf2 Assert\cf0 .NotNull(yolk);\par         \}\par \par         [\cf2 Fact\cf0 ]\par         \cf1 public\cf0  \cf1 void\cf0  DefaultContainerCorrectlyResolvesMayonnaise()\par         \{\par             \cf2 CompositionContainer\cf0  container = \cf1 new\cf0  \cf2 ContainerBuilder\cf0 ()\par                 .Build();\par             \cf1 var\cf0  mayo = container.GetExportedValue&amp;lt;\cf2 Mayonnaise\cf0 &amp;gt;();\par             \cf2 Assert\cf0 .NotNull(mayo);\par         \}\par \par         [\cf2 Fact\cf0 ]\par         \cf1 public\cf0  \cf1 void\cf0  DefaultContainerReturnsSingletonMayonnaise()\par         \{\par             \cf2 CompositionContainer\cf0  container = \cf1 new\cf0  \cf2 ContainerBuilder\cf0 ()\par                 .Build();\par             \cf1 var\cf0  mayo1 = container.GetExportedValue&amp;lt;\cf2 Mayonnaise\cf0 &amp;gt;();\par             \cf1 var\cf0  mayo2 = container.GetExportedValue&amp;lt;\cf2 Mayonnaise\cf0 &amp;gt;();\par             \cf2 Assert\cf0 .Same(mayo1, mayo2);\par         \}\par \par         [\cf2 Fact\cf0 ]\par         \cf1 public\cf0  \cf1 void\cf0  WithTransientMayonnaiseReturnTransientMayonnaise()\par         \{\par             \cf2 CompositionContainer\cf0  container = \cf1 new\cf0  \cf2 ContainerBuilder\cf0 ()\par                 .WithNonSharedMayonnaise()\par                 .Build();\par             \cf1 var\cf0  mayo1 = container.GetExportedValue&amp;lt;\cf2 Mayonnaise\cf0 &amp;gt;();\par             \cf1 var\cf0  mayo2 = container.GetExportedValue&amp;lt;\cf2 Mayonnaise\cf0 &amp;gt;();\par             \cf2 Assert\cf0 .NotSame(mayo1, mayo2);\par         \}\par \par         [\cf2 Fact\cf0 ]\par         \cf1 public\cf0  \cf1 void\cf0  TransientMayonnaiseByDefaultContainsSingletonEggYolk()\par         \{\par             \cf2 CompositionContainer\cf0  container = \cf1 new\cf0  \cf2 ContainerBuilder\cf0 ()\par                 .WithNonSharedMayonnaise()\par                 .Build();\par             \cf1 var\cf0  mayo1 = container.GetExportedValue&amp;lt;\cf2 Mayonnaise\cf0 &amp;gt;();\par             \cf1 var\cf0  mayo2 = container.GetExportedValue&amp;lt;\cf2 Mayonnaise\cf0 &amp;gt;();\par             \cf2 Assert\cf0 .Same(mayo1.EggYolk, mayo2.EggYolk);\par         \}\par \par         [\cf2 Fact\cf0 ]\par         \cf1 public\cf0  \cf1 void\cf0  TransientMayonnaiseByDefaultContainsSingletonOil()\par         \{\par             \cf2 CompositionContainer\cf0  container = \cf1 new\cf0  \cf2 ContainerBuilder\cf0 ()\par                 .WithNonSharedMayonnaise()\par                 .Build();\par             \cf1 var\cf0  mayo1 = container.GetExportedValue&amp;lt;\cf2 Mayonnaise\cf0 &amp;gt;();\par             \cf1 var\cf0  mayo2 = container.GetExportedValue&amp;lt;\cf2 Mayonnaise\cf0 &amp;gt;();\par             \cf2 Assert\cf0 .Same(mayo1.Oil, mayo2.Oil);\par         \}\par \par         [\cf2 Fact\cf0 ]\par         \cf1 public\cf0  \cf1 void\cf0  TransientMayonnaiseCanHaveTransientEggYolk()\par         \{\par             \cf2 CompositionContainer\cf0  container = \cf1 new\cf0  \cf2 ContainerBuilder\cf0 ()\par                 .WithNonSharedMayonnaise()\par                 .WithNonSharedEggYolk()\par                 .Build();\par             \cf1 var\cf0  mayo1 = container.GetExportedValue&amp;lt;\cf2 Mayonnaise\cf0 &amp;gt;();\par             \cf1 var\cf0  mayo2 = container.GetExportedValue&amp;lt;\cf2 Mayonnaise\cf0 &amp;gt;();\par             \cf2 Assert\cf0 .NotSame(mayo1.EggYolk, mayo2.EggYolk);\par         \}\par \par         [\cf2 Fact\cf0 ]\par         \cf1 public\cf0  \cf1 void\cf0  TransientMayonnaiseCanHaveSingletonOil()\par         \{\par             \cf2 CompositionContainer\cf0  container = \cf1 new\cf0  \cf2 ContainerBuilder\cf0 ()\par                 .WithNonSharedMayonnaise()\par                 .WithNonSharedEggYolk()\par                 .Build();\par             \cf1 var\cf0  mayo1 = container.GetExportedValue&amp;lt;\cf2 Mayonnaise\cf0 &amp;gt;();\par             \cf1 var\cf0  mayo2 = container.GetExportedValue&amp;lt;\cf2 Mayonnaise\cf0 &amp;gt;();\par             \cf2 Assert\cf0 .Same(mayo1.Oil, mayo2.Oil);\par         \}\par \par         [\cf2 Fact\cf0 ]\par         \cf1 public\cf0  \cf1 void\cf0  TransientMayonnaiseCanHaveTransientOil()\par         \{\par             \cf2 CompositionContainer\cf0  container = \cf1 new\cf0  \cf2 ContainerBuilder\cf0 ()\par                 .WithNonSharedMayonnaise()\par                 .WithNonSharedOil()\par                 .Build();\par             \cf1 var\cf0  mayo1 = container.GetExportedValue&amp;lt;\cf2 Mayonnaise\cf0 &amp;gt;();\par             \cf1 var\cf0  mayo2 = container.GetExportedValue&amp;lt;\cf2 Mayonnaise\cf0 &amp;gt;();\par             \cf2 Assert\cf0 .NotSame(mayo1.Oil, mayo2.Oil);\par         \}\par \par         [\cf2 Fact\cf0 ]\par         \cf1 public\cf0  \cf1 void\cf0  TransientMayonnaiseCanHaveSingletonEggYolk()\par         \{\par             \cf2 CompositionContainer\cf0  container = \cf1 new\cf0  \cf2 ContainerBuilder\cf0 ()\par                 .WithNonSharedMayonnaise()\par                 .WithNonSharedOil()\par                 .Build();\par             \cf1 var\cf0  mayo1 = container.GetExportedValue&amp;lt;\cf2 Mayonnaise\cf0 &amp;gt;();\par             \cf1 var\cf0  mayo2 = container.GetExportedValue&amp;lt;\cf2 Mayonnaise\cf0 &amp;gt;();\par             \cf2 Assert\cf0 .Same(mayo1.EggYolk, mayo2.EggYolk);\par         \}\par \par         [\cf2 Fact\cf0 ]\par         \cf1 public\cf0  \cf1 void\cf0  PureTransientMayonnaiseIsTransient()\par         \{\par             \cf2 CompositionContainer\cf0  container = \cf1 new\cf0  \cf2 ContainerBuilder\cf0 ()\par                 .WithNonSharedMayonnaise()\par                 .WithNonSharedEggYolk()\par                 .WithNonSharedOil()\par                 .Build();\par             \cf1 var\cf0  mayo1 = container.GetExportedValue&amp;lt;\cf2 Mayonnaise\cf0 &amp;gt;();\par             \cf1 var\cf0  mayo2 = container.GetExportedValue&amp;lt;\cf2 Mayonnaise\cf0 &amp;gt;();\par             \cf2 Assert\cf0 .NotSame(mayo1, mayo2);\par         \}\par \par         [\cf2 Fact\cf0 ]\par         \cf1 public\cf0  \cf1 void\cf0  PureTransientMayonnaiseHasTransientEggYolk()\par         \{\par             \cf2 CompositionContainer\cf0  container = \cf1 new\cf0  \cf2 ContainerBuilder\cf0 ()\par                 .WithNonSharedMayonnaise()\par                 .WithNonSharedEggYolk()\par                 .WithNonSharedOil()\par                 .Build();\par             \cf1 var\cf0  mayo1 = container.GetExportedValue&amp;lt;\cf2 Mayonnaise\cf0 &amp;gt;();\par             \cf1 var\cf0  mayo2 = container.GetExportedValue&amp;lt;\cf2 Mayonnaise\cf0 &amp;gt;();\par             \cf2 Assert\cf0 .NotSame(mayo1.EggYolk, mayo2.EggYolk);\par         \}\par \par         [\cf2 Fact\cf0 ]\par         \cf1 public\cf0  \cf1 void\cf0  PureTransientMayonnaiseHasTransientOil()\par         \{\par             \cf2 CompositionContainer\cf0  container = \cf1 new\cf0  \cf2 ContainerBuilder\cf0 ()\par                 .WithNonSharedMayonnaise()\par                 .WithNonSharedEggYolk()\par                 .WithNonSharedOil()\par                 .Build();\par             \cf1 var\cf0  mayo1 = container.GetExportedValue&amp;lt;\cf2 Mayonnaise\cf0 &amp;gt;();\par             \cf1 var\cf0  mayo2 = container.GetExportedValue&amp;lt;\cf2 Mayonnaise\cf0 &amp;gt;();\par             \cf2 Assert\cf0 .NotSame(mayo1.Oil, mayo2.Oil);\par         \}\par     \}\par \}\par }
--&gt;
&lt;div style="font-family: consolas; background: none repeat scroll 0% 0% white; color: black; font-size: 10pt;"&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt; System.ComponentModel.Composition.Hosting;&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt; Ploeh.Samples.MeffyXmas.MenuModel;&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt; Xunit;&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;namespace&lt;/span&gt; Ploeh.Samples.MeffyXmas.MefMenu.UnitTest&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: blue;"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ContainerBuilderFacts&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: rgb(43, 145, 175);"&gt;Fact&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;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; DefaultContainerCorrectlyResolvesOliveOil()&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: rgb(43, 145, 175);"&gt;CompositionContainer&lt;/span&gt; container = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ContainerBuilder&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; .Build();&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; oil = container.GetExportedValue&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;OliveOil&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: rgb(43, 145, 175);"&gt;Assert&lt;/span&gt;.NotNull(oil);&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: rgb(43, 145, 175);"&gt;Fact&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;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; DefaultContainerCorrectlyResolvesEggYolk()&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: rgb(43, 145, 175);"&gt;CompositionContainer&lt;/span&gt; container = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ContainerBuilder&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; .Build();&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; yolk = container.GetExportedValue&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;EggYolk&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: rgb(43, 145, 175);"&gt;Assert&lt;/span&gt;.NotNull(yolk);&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: rgb(43, 145, 175);"&gt;Fact&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;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; DefaultContainerCorrectlyResolvesMayonnaise()&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: rgb(43, 145, 175);"&gt;CompositionContainer&lt;/span&gt; container = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ContainerBuilder&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; .Build();&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; mayo = container.GetExportedValue&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Mayonnaise&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: rgb(43, 145, 175);"&gt;Assert&lt;/span&gt;.NotNull(mayo);&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: rgb(43, 145, 175);"&gt;Fact&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;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; DefaultContainerReturnsSingletonMayonnaise()&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: rgb(43, 145, 175);"&gt;CompositionContainer&lt;/span&gt; container = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ContainerBuilder&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; .Build();&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; mayo1 = container.GetExportedValue&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Mayonnaise&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;var&lt;/span&gt; mayo2 = container.GetExportedValue&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Mayonnaise&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: rgb(43, 145, 175);"&gt;Assert&lt;/span&gt;.Same(mayo1, mayo2);&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: rgb(43, 145, 175);"&gt;Fact&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;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; WithTransientMayonnaiseReturnTransientMayonnaise()&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: rgb(43, 145, 175);"&gt;CompositionContainer&lt;/span&gt; container = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ContainerBuilder&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; .WithNonSharedMayonnaise()&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; .Build();&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; mayo1 = container.GetExportedValue&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Mayonnaise&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;var&lt;/span&gt; mayo2 = container.GetExportedValue&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Mayonnaise&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: rgb(43, 145, 175);"&gt;Assert&lt;/span&gt;.NotSame(mayo1, mayo2);&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: rgb(43, 145, 175);"&gt;Fact&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;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; TransientMayonnaiseByDefaultContainsSingletonEggYolk()&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: rgb(43, 145, 175);"&gt;CompositionContainer&lt;/span&gt; container = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ContainerBuilder&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; .WithNonSharedMayonnaise()&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; .Build();&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; mayo1 = container.GetExportedValue&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Mayonnaise&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;var&lt;/span&gt; mayo2 = container.GetExportedValue&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Mayonnaise&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: rgb(43, 145, 175);"&gt;Assert&lt;/span&gt;.Same(mayo1.EggYolk, mayo2.EggYolk);&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: rgb(43, 145, 175);"&gt;Fact&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;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; TransientMayonnaiseByDefaultContainsSingletonOil()&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: rgb(43, 145, 175);"&gt;CompositionContainer&lt;/span&gt; container = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ContainerBuilder&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; .WithNonSharedMayonnaise()&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; .Build();&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; mayo1 = container.GetExportedValue&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Mayonnaise&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;var&lt;/span&gt; mayo2 = container.GetExportedValue&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Mayonnaise&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: rgb(43, 145, 175);"&gt;Assert&lt;/span&gt;.Same(mayo1.Oil, mayo2.Oil);&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: rgb(43, 145, 175);"&gt;Fact&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;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; TransientMayonnaiseCanHaveTransientEggYolk()&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: rgb(43, 145, 175);"&gt;CompositionContainer&lt;/span&gt; container = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ContainerBuilder&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; .WithNonSharedMayonnaise()&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; .WithNonSharedEggYolk()&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; .Build();&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; mayo1 = container.GetExportedValue&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Mayonnaise&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;var&lt;/span&gt; mayo2 = container.GetExportedValue&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Mayonnaise&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: rgb(43, 145, 175);"&gt;Assert&lt;/span&gt;.NotSame(mayo1.EggYolk, mayo2.EggYolk);&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: rgb(43, 145, 175);"&gt;Fact&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;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; TransientMayonnaiseCanHaveSingletonOil()&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: rgb(43, 145, 175);"&gt;CompositionContainer&lt;/span&gt; container = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ContainerBuilder&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; .WithNonSharedMayonnaise()&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; .WithNonSharedEggYolk()&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; .Build();&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; mayo1 = container.GetExportedValue&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Mayonnaise&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;var&lt;/span&gt; mayo2 = container.GetExportedValue&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Mayonnaise&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: rgb(43, 145, 175);"&gt;Assert&lt;/span&gt;.Same(mayo1.Oil, mayo2.Oil);&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: rgb(43, 145, 175);"&gt;Fact&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;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; TransientMayonnaiseCanHaveTransientOil()&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: rgb(43, 145, 175);"&gt;CompositionContainer&lt;/span&gt; container = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ContainerBuilder&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; .WithNonSharedMayonnaise()&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; .WithNonSharedOil()&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; .Build();&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; mayo1 = container.GetExportedValue&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Mayonnaise&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;var&lt;/span&gt; mayo2 = container.GetExportedValue&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Mayonnaise&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: rgb(43, 145, 175);"&gt;Assert&lt;/span&gt;.NotSame(mayo1.Oil, mayo2.Oil);&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: rgb(43, 145, 175);"&gt;Fact&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;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; TransientMayonnaiseCanHaveSingletonEggYolk()&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: rgb(43, 145, 175);"&gt;CompositionContainer&lt;/span&gt; container = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ContainerBuilder&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; .WithNonSharedMayonnaise()&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; .WithNonSharedOil()&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; .Build();&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; mayo1 = container.GetExportedValue&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Mayonnaise&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;var&lt;/span&gt; mayo2 = container.GetExportedValue&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Mayonnaise&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: rgb(43, 145, 175);"&gt;Assert&lt;/span&gt;.Same(mayo1.EggYolk, mayo2.EggYolk);&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: rgb(43, 145, 175);"&gt;Fact&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;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; PureTransientMayonnaiseIsTransient()&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: rgb(43, 145, 175);"&gt;CompositionContainer&lt;/span&gt; container = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ContainerBuilder&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; .WithNonSharedMayonnaise()&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; .WithNonSharedEggYolk()&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; .WithNonSharedOil()&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; .Build();&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; mayo1 = container.GetExportedValue&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Mayonnaise&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;var&lt;/span&gt; mayo2 = container.GetExportedValue&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Mayonnaise&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: rgb(43, 145, 175);"&gt;Assert&lt;/span&gt;.NotSame(mayo1, mayo2);&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: rgb(43, 145, 175);"&gt;Fact&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;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; PureTransientMayonnaiseHasTransientEggYolk()&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: rgb(43, 145, 175);"&gt;CompositionContainer&lt;/span&gt; container = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ContainerBuilder&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; .WithNonSharedMayonnaise()&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; .WithNonSharedEggYolk()&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; .WithNonSharedOil()&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; .Build();&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; mayo1 = container.GetExportedValue&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Mayonnaise&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;var&lt;/span&gt; mayo2 = container.GetExportedValue&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Mayonnaise&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: rgb(43, 145, 175);"&gt;Assert&lt;/span&gt;.NotSame(mayo1.EggYolk, mayo2.EggYolk);&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: rgb(43, 145, 175);"&gt;Fact&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;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; PureTransientMayonnaiseHasTransientOil()&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: rgb(43, 145, 175);"&gt;CompositionContainer&lt;/span&gt; container = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ContainerBuilder&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; .WithNonSharedMayonnaise()&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; .WithNonSharedEggYolk()&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; .WithNonSharedOil()&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; .Build();&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; mayo1 = container.GetExportedValue&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Mayonnaise&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;var&lt;/span&gt; mayo2 = container.GetExportedValue&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Mayonnaise&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: rgb(43, 145, 175);"&gt;Assert&lt;/span&gt;.NotSame(mayo1.Oil, mayo2.Oil);&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;ContainerBuilder is the class you must implement, so the unit tests don’t compile until you make them. Meffy xmas!&lt;/p&gt;
&lt;p&gt;&lt;img style="background-image: none; border-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px;" title="clip_image002" alt="clip_image002" src="http://blog.ploeh.dk/content/binary/Windows-Live-Writer/ResolveclosedtypeswithMEF_BD03/clip_image002_3.jpg" border="0" height="182" width="640"&gt;&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=2f570521-8df8-4b6a-bcce-6e1bfea1a6ee"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,2f570521-8df8-4b6a-bcce-6e1bfea1a6ee.aspx</comments>
      <category>Dependency Injection</category>
      <enclosure url="http://blog.ploeh.dk/content/binary/2f570521-8df8-4b6a-bcce-6e1bfea1a6ee/MeffyChristmas2010.zip" type="application/x-zip-compressed" length="2089050" />
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=dc02b92a-dcd9-482f-9d34-ab03dcaaead9</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,dc02b92a-dcd9-482f-9d34-ab03dcaaead9.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,dc02b92a-dcd9-482f-9d34-ab03dcaaead9.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=dc02b92a-dcd9-482f-9d34-ab03dcaaead9</wfw:commentRss>
      <slash:comments>15</slash:comments>
      <title>The TDD Apostate</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,dc02b92a-dcd9-482f-9d34-ab03dcaaead9.aspx</guid>
      <link>http://blog.ploeh.dk/2010/12/22/TheTDDApostate.aspx</link>
      <pubDate>Wed, 22 Dec 2010 13:57:56 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;I’ve been doing Test-Driven Development since 2003. I still do, I still love it, and I still expect to be doing it in the future. Over the years, I’ve repeatedly returned to the discussion of whether TDD should be regarded as Test-Driven &lt;em&gt;Development&lt;/em&gt; or Test-Driven &lt;em&gt;Design&lt;/em&gt;. For a long time I’ve been of the conviction that TDD is both of those. Not so any longer.&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;TDD is not a good design methodology.&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;Over the years I’ve written tons of code with TDD. I’ve written code where tests &lt;em&gt;blindly drove&lt;/em&gt; the design, and I’ve written code where the design was the result of a long period of deliberation, and the tests were only the manifestations of already well-formed ideas.&lt;/p&gt; &lt;p&gt;I can safely say that the code where tests alone drove the design never turned out particularly well. Although it was testable and, after a fashion, ‘loosely coupled’, it was still Spaghetti Code in the sense that it lacked overall consistency and &lt;a href="http://blog.ploeh.dk/2010/12/03/TowardsBetterAbstractions.aspx"&gt;good abstractions&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;On the other hand, I’m immensely pleased with code like &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; 2.0, which was mostly the result of hours of careful contemplation riding my bike to and from work. It was still written test-first, but the design was well thought out in advance.&lt;/p&gt; &lt;p&gt;This made me think: did I just fail (repeatedly) at Test-Driven Design, or is the overall concept a fallacy?&lt;/p&gt; &lt;p&gt;That’s a pretty hard question to answer; what constitutes good design? In the following, let’s assume that the SOLID principles is a pretty good indicator of good design. If so, does test-first drive us towards SOLID design?&lt;/p&gt; &lt;p&gt;&lt;strong&gt;TDD versus the Single Responsibility Principle&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Does TDD ensure the application of the &lt;a href="http://en.wikipedia.org/wiki/Single_responsibility_principle"&gt;Single Responsibility Principle&lt;/a&gt; (SRP)? This question is easy to answer and the answer is a resounding &lt;em&gt;NO!&lt;/em&gt; Nothing prevents us from test-driving a God Class. I’ve seen many examples, and I’ve been guilty of it myself.&lt;/p&gt; &lt;p&gt;Constructor Injection is a much better help because it &lt;a href="http://blog.ploeh.dk/2010/02/02/RefactoringToAggregateServices.aspx"&gt;makes SRP violations so painful&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;The score so far: 0 points to TDD.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;TDD versus the Open/Closed Principle&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Does TDD ensure that we follow the &lt;a href="http://en.wikipedia.org/wiki/Open/closed_principle"&gt;Open/Closed Principle&lt;/a&gt; (OCP)? This is a bit harder to answer. I’ve previously argued that &lt;a href="http://blog.ploeh.dk/2009/06/05/TestabilityIsReallyTheOpenClosedPrinciple.aspx"&gt;Testability is just another name for OCP&lt;/a&gt;, so that would in itself imply that TDD drives OCP. However, the issue is more complex than that, because there are several different ways we can address the OCP:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Inheritance  &lt;li&gt;Composition&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;According to &lt;a href="http://www.osherove.com/"&gt;Roy Osherove&lt;/a&gt;’s book &lt;a href="http://affiliate.manning.com/idevaffiliate.php?id=1150_91"&gt;The Art of Unit Testing&lt;/a&gt;, the &lt;em&gt;Extract and Override &lt;/em&gt;technique is a common unit testing trick. Personally, I rarely use it, but if used it will indirectly drive us a bit towards OCP via inheritance.&lt;/p&gt; &lt;p&gt;However, we all know that we should &lt;em&gt;favor composition over inheritance&lt;/em&gt;, so does TDD drive us in that direction? As I alluded to previously, TDD does tend to drive us towards the use of &lt;a href="http://xunitpatterns.com/Test%20Double.html"&gt;Test Doubles&lt;/a&gt;, which we can view as one way to achieve OCP via composition.&lt;/p&gt; &lt;p&gt;However, another favorite composition technique of mine is to &lt;a href="http://blog.ploeh.dk/2010/04/07/DependencyInjectionIsLooseCoupling.aspx"&gt;add functionality with a Decorator&lt;/a&gt;. This is only possible if the original type implements an interface that can be decorated. It’s possible to write a test that forces a &lt;a href="http://xunitpatterns.com/SUT.html"&gt;SUT&lt;/a&gt; to implement an interface, but TDD as a technique in itself does not drive us in that direction.&lt;/p&gt; &lt;p&gt;Grudgingly, however, I most admit that TDD still scores half a point against OCP, for a total score so far of ½ point.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;TDD versus the Liskov Substitution Principle&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Does TDD drive us towards adhering to the &lt;a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle"&gt;Liskov Substitution Princple&lt;/a&gt; (LSP)? Perhaps, but probably not.&lt;/p&gt; &lt;p&gt;Black box testing can’t protect us against the SUT attempting to downcast its dependencies, but at least it doesn’t particularly pull us in that direction either. When it comes to the SUT’s treatment of a dependency, TDD pulls in neither direction.&lt;/p&gt; &lt;p&gt;Can we test-drive interface implementations that inadvertently violate the LSP? Yes, easily. As I discussed in a previous post, the use of &lt;a href="http://martinfowler.com/bliki/HeaderInterface.html"&gt;Header Interfaces&lt;/a&gt; pulls us towards LSP violations. &lt;a href="http://blog.ploeh.dk/2010/12/02/InterfacesAreNotAbstractions.aspx"&gt;The more members an interface has, the more likely are LSP violations&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;TDD can definitely drive us towards Header Interfaces (although they tend to hurt in the long run). I've seen this happen numerous times, and I’ve been there myself. TDD doesn’t properly encourage LSP adherence.&lt;/p&gt; &lt;p&gt;The score this round: 0 points for TDD, for a running total of ½ point.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;TDD versus the Interface Segregation Principle&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Does TDD drive us towards the &lt;a href="http://en.wikipedia.org/wiki/Interface_segregation_principle"&gt;Interface Segregation Principle&lt;/a&gt; (ISP)? No. It’s pretty easy to test-drive a SUT towards a Header Interface, just as we can test-drive towards a God Class.&lt;/p&gt; &lt;p&gt;Another 0 points for TDD. The score is still ½ point to TDD.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;TDD versus the Dependency Inversion Principle&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Does TDD drive us towards the &lt;a href="http://en.wikipedia.org/wiki/Dependency_inversion_principle"&gt;Dependency Inversion Principle&lt;/a&gt; (DIP)? Yes, it does.&lt;/p&gt; &lt;p&gt;The whole drive towards Testability – the ability to replace dependencies with Test Doubles – drives us exactly in the same direction as the DIP.&lt;/p&gt; &lt;p&gt;Since we tend to mistake such mechanistic loose coupling with proper application design, this probably explains why we, for so long, have confused TDD with good design. However, although I view loose coupling as a prerequisite for good design, &lt;a href="http://blog.ploeh.dk/2010/12/02/InterfacesAreNotAbstractions.aspx"&gt;it is by no means enough&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;For those that still keep score, TDD scores 1 point against DIP, for a total of 1½ points.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;TDD does not ensure SOLID&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;With 1½ out of 5 possible points I have stated my case. I am convinced that TDD itself does not drive us towards SOLID design. It’s definitely possible to &lt;em&gt;use&lt;/em&gt; test-first techniques to drive towards SOLID designs, but that will always be an extra effort that &lt;em&gt;supplements&lt;/em&gt; TDD; it’s not something that is inherently built into TDD.&lt;/p&gt; &lt;p&gt;Obviously you could argue that SOLID in itself is not the end-all, be-all of proper API design. I would agree. However, based on my experience with TDD, I think the conclusion holds. TDD does not drive us towards good design. It is not a design technique.&lt;/p&gt; &lt;p&gt;I still write code test-first because I find it more &lt;em&gt;productive&lt;/em&gt;, but I make design decisions out of band. I’m a Test-Driven Design Apostate.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=dc02b92a-dcd9-482f-9d34-ab03dcaaead9"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,dc02b92a-dcd9-482f-9d34-ab03dcaaead9.aspx</comments>
      <category>Software Design</category>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=1e1eaf87-e48f-4f89-9bfc-4f882203f8c5</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,1e1eaf87-e48f-4f89-9bfc-4f882203f8c5.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,1e1eaf87-e48f-4f89-9bfc-4f882203f8c5.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=1e1eaf87-e48f-4f89-9bfc-4f882203f8c5</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <title>On Role Interfaces, the Reused Abstractions Principle and Service Locators</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,1e1eaf87-e48f-4f89-9bfc-4f882203f8c5.aspx</guid>
      <link>http://blog.ploeh.dk/2010/12/18/OnRoleInterfacesTheReusedAbstractionsPrincipleAndServiceLocators.aspx</link>
      <pubDate>Sat, 18 Dec 2010 14:21:17 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;As a comment to my previous post about &lt;a href="http://blog.ploeh.dk/2010/12/02/InterfacesAreNotAbstractions.aspx"&gt;interfaces being no guarantee for abstractions&lt;/a&gt;, Danny asks some interesting questions. In particular, his questions relate to &lt;a href="http://www.udidahan.com/"&gt;Udi Dahan&lt;/a&gt;’s presentation &lt;a href="http://www.infoq.com/presentations/Making-Roles-Explicit-Udi-Dahan"&gt;Intentions &amp;amp; Interfaces: Making patterns concrete&lt;/a&gt; (also known as &lt;em&gt;Making Roles Explicit&lt;/em&gt;). Danny writes:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;it would seem that Udi recommends creating interfaces for each "role" the domain object plays and using a Service Locator to find the concrete implementation ... or in his case the concrete FetchingStrategy used to pull data back from his ORM. This sounds like his application would have many 1:1 abstractions.&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;Can this be true, or can we consolidate &lt;a href="http://martinfowler.com/bliki/RoleInterface.html"&gt;Role Interfaces&lt;/a&gt; with the &lt;a href="http://parlezuml.com/blog/?postid=934"&gt;Reused Abstractions Principle (RAP)&lt;/a&gt; – preferably without resorting to a &lt;a href="http://blog.ploeh.dk/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx"&gt;Service Locator&lt;/a&gt;? Yes, of course we can.&lt;/p&gt; &lt;p&gt;In Udi Dahan’s talks, we see various examples where he queries a Service Locator for a Role Interface. If the Service Locator returns an instance he uses it; otherwise, he falls back to some sort of default behavior. Here is my interpretation of Udi Dahan’s slides:&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;void&lt;/font&gt;&lt;/span&gt; Persist(&lt;span&gt;&lt;font color="#2b91af"&gt;Customer&lt;/font&gt;&lt;/span&gt; entity)&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; validator = &lt;span&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.serviceLocator&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Get&amp;lt;&lt;span&gt;&lt;font color="#2b91af"&gt;IValidator&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span&gt;&lt;font color="#2b91af"&gt;Customer&lt;/font&gt;&lt;/span&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; &lt;span&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; (validator != &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; validator.Validate(entity);&lt;/font&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&gt;&lt;font style="font-size: 10pt" color="#008000"&gt;// Save entity in actual store&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 is actually not very pretty object-oriented code, but I have Udi Dahan suspected of choosing this implementation to better communicate the essence of how to use Role Interfaces. However, a more proper implementation would have a default (or Null Object) implementation of the Role Interface, and then the special implementation.&lt;/p&gt;
&lt;p&gt;If we assume that a NullValidator exists, we can require that the Service Locator can always serve up a proper instance of IValidator&amp;lt;Customer&amp;gt;. This enables us to simplify the Persist method to something 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;void&lt;/font&gt;&lt;/span&gt; Persist(&lt;span&gt;&lt;font color="#2b91af"&gt;Customer&lt;/font&gt;&lt;/span&gt; entity)&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; validator = &lt;span&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.serviceLocator&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Get&amp;lt;&lt;span&gt;&lt;font color="#2b91af"&gt;IValidator&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span&gt;&lt;font color="#2b91af"&gt;Customer&lt;/font&gt;&lt;/span&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; validator.Validate(entity);&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&gt;&lt;font style="font-size: 10pt" color="#008000"&gt;// Save entity in actual store&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;Either the Service Locator returns a specialized CustomerValidator, or it returns the NullValidator. In any case, this assumption enables us to leverage the &lt;a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle"&gt;Liskov Substitution Principle&lt;/a&gt; and refactor the conditional logic to polymorphism.&lt;/p&gt;
&lt;p&gt;In other words: every single time we discover the need to extract a Role Interface, we should end up with &lt;em&gt;at least&lt;/em&gt; two implementations: the Null Object and the &lt;a href="http://martinfowler.com/eaaCatalog/specialCase.html"&gt;Special Case&lt;/a&gt;. Thus the RAP is satisfied.&lt;/p&gt;
&lt;p&gt;As a last refactoring, we can also get rid of the Service Locator. Instead, we can use Constructor Injection to inject IValidator&amp;lt;Customer&amp;gt; directly into the Persistence 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;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;CustomerPersistence&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; &lt;span&gt;&lt;font color="#2b91af"&gt;IValidator&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span&gt;&lt;font color="#2b91af"&gt;Customer&lt;/font&gt;&lt;/span&gt;&amp;gt; validator;&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; CustomerPersistence(&lt;span&gt;&lt;font color="#2b91af"&gt;IValidator&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span&gt;&lt;font color="#2b91af"&gt;Customer&lt;/font&gt;&lt;/span&gt;&amp;gt; v)&lt;/font&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; (v == &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;"..."&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;.validator = v;&lt;/font&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="#0000ff"&gt;void&lt;/font&gt;&lt;/span&gt; Persist(&lt;span&gt;&lt;font color="#2b91af"&gt;Customer&lt;/font&gt;&lt;/span&gt; entity)&lt;/font&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;.validator.Validate(entity);&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&gt;&lt;font style="font-size: 10pt" color="#008000"&gt;// Save entity in actual store&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;Thus, the use of Role Interfaces in no way hinges on using a Service Locator, and everything is good again :)&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=1e1eaf87-e48f-4f89-9bfc-4f882203f8c5"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,1e1eaf87-e48f-4f89-9bfc-4f882203f8c5.aspx</comments>
      <category>Dependency Injection</category>
      <category>Software Design</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=172e0e51-adc9-4cbb-ae00-55f8bf173a17</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,172e0e51-adc9-4cbb-ae00-55f8bf173a17.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,172e0e51-adc9-4cbb-ae00-55f8bf173a17.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=172e0e51-adc9-4cbb-ae00-55f8bf173a17</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <title>Towards better abstractions</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,172e0e51-adc9-4cbb-ae00-55f8bf173a17.aspx</guid>
      <link>http://blog.ploeh.dk/2010/12/03/TowardsBetterAbstractions.aspx</link>
      <pubDate>Fri, 03 Dec 2010 13:19:48 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;In my &lt;a href="http://blog.ploeh.dk/2010/12/02/InterfacesAreNotAbstractions.aspx"&gt;previous post&lt;/a&gt; I discussed why the use of interfaces doesn’t &lt;em&gt;guarantee&lt;/em&gt; that we work against good abstractions. In this post I will look at some guidelines that might be helpful in defining better abstractions.&lt;/p&gt; &lt;p&gt;One important trait of a useful abstraction is that we can create many different implementations of it. This is the &lt;a href="http://parlezuml.com/blog/?postid=934"&gt;Reused Abstractions Principle (RAP)&lt;/a&gt;. This is particularly important because composition and separation of concerns often result in such reuse. Every time we use &lt;a href="http://en.wikipedia.org/wiki/Null_Object_pattern"&gt;Null Objects&lt;/a&gt;, &lt;a href="http://en.wikipedia.org/wiki/Decorator_pattern"&gt;Decorators&lt;/a&gt; or &lt;a href="http://en.wikipedia.org/wiki/Composite_pattern"&gt;Composites&lt;/a&gt;, we &lt;a href="http://blog.ploeh.dk/2010/04/07/DependencyInjectionIsLooseCoupling.aspx"&gt;reuse the same abstraction to compose an application from separate classes&lt;/a&gt; that all adhere to the &lt;a href="http://en.wikipedia.org/wiki/Single_responsibility_principle"&gt;Single Responsibility Principle&lt;/a&gt;. For example, Decorators are an excellent way to implement &lt;a href="http://en.wikipedia.org/wiki/Cross-cutting_concern"&gt;Cross-Cutting Concerns&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;The RAP gives us a way to &lt;em&gt;identify&lt;/em&gt; good abstractions after the fact, but doesn’t say much about the &lt;em&gt;traits&lt;/em&gt; that make up a good, composable interface.&lt;/p&gt; &lt;p&gt;On the other hand, I find that the &lt;em&gt;composability&lt;/em&gt; of an interface is a pretty good indicator of its potential for reuse. While we can create Decorators from just about any interface, creating meaningful Null Objects or Composites are much harder. As we &lt;a href="http://blog.ploeh.dk/2010/12/02/InterfacesAreNotAbstractions.aspx"&gt;previously saw&lt;/a&gt;, bad abstractions often prevent us from implementing a meaningful Composite.&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;Being able to implement a meaningful Composite is a good indication of a sound interface.&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;This understanding is equivalent to the realization associated with the concept of &lt;em&gt;Closure of Operations&lt;/em&gt; from &lt;a href="http://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215"&gt;Domain-Driven Design&lt;/a&gt;. As soon as we achieve this, a lot of very intuitive, almost arithmetic-like APIs tend to follow. It becomes much easier to &lt;em&gt;compose&lt;/em&gt; various instances of the abstraction.&lt;/p&gt; &lt;p&gt;With Composite as an indicator of good abstractions, here are some guidelines that should enable us to define more useful interfaces.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;ISP&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;The more members an interface has, the more difficult it is to create a Composite of it. Thus, the &lt;a href="http://en.wikipedia.org/wiki/Interface_segregation_principle"&gt;Interface Segregation Principle&lt;/a&gt; is a good guide, as it points us towards small interfaces. By extrapolation, the best interface would be an interface with a single member.&lt;/p&gt; &lt;p&gt;That’s a good start, but even such an interface could be problematic if it’s a &lt;a href="http://en.wikipedia.org/wiki/Leaky_abstraction"&gt;Leaky Abstraction&lt;/a&gt; or a Shallow Interface. Still, let us assume that we aim for such &lt;a href="http://martinfowler.com/bliki/RoleInterface.html"&gt;Role Interfaces&lt;/a&gt; and move on to see what other guidelines are available to us.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Commands&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/Command_pattern"&gt;Commands&lt;/a&gt;, and by extension any interface that consists of all void methods, are imminently composable. To implement a Null Object, just ignore the input and do nothing. To implement a Composite, just pass on the input to each contained instance.&lt;/p&gt; &lt;p&gt;A Command is the epitome of the &lt;a href="http://en.wikipedia.org/wiki/Hollywood_Principle"&gt;Hollywood Principle&lt;/a&gt; because &lt;em&gt;telling&lt;/em&gt; is the only thing you can do. There’s no way to &lt;em&gt;ask&lt;/em&gt; a Command about anything when the method returns void. Commands also guarantee the &lt;a href="http://en.wikipedia.org/wiki/Law_of_Demeter"&gt;Law of Demeter&lt;/a&gt;, because there’s no way you can ‘dot’ across a void :)&lt;/p&gt; &lt;p&gt;If a Command takes one or more input parameters, they must all stay clear of Shallow Interfaces and Leaky Abstractions. If these conditions are satisfied, a Command tends to be a very good abstraction. However, sometimes we just need return values.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Closure of Operations&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;We already briefly discussed Closure of Operations. In C# we can describe this concept as any method that fits this signature in some way:&lt;/p&gt; &lt;div style="font-family: ; background: white"&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;T DoIt(T x);&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;An interface that returns the same type as the input type(s) exhibit Closure of Operations. There may be more than one input parameter as long as they are all of the same type.&lt;/p&gt;
&lt;p&gt;The interesting thing about Closure of Operations is that any interface with that quality is easily implemented as a Null Object (just return the input). A sort of Composite is often also possible because we can pass the input to each instance in the Composite and use some sort of aggregation or selection algorithm to return a result.&lt;/p&gt;
&lt;p&gt;Even if the return type doesn’t easily lend itself towards aggregation, you can often implement a coalescing behavior with a Composite by returning the first non-null instance returned by the contained instances.&lt;/p&gt;
&lt;p&gt;Interfaces that exhibits Closure of Operations tend to be good abstractions, but it’s not always possible to design APIs like that.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Reduction of Input&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Sometimes we can keep some of the benefits from Closure of Operations even though a pure model isn’t possible. Any method that returns a type that is a subset of the input types also tends to be composable.&lt;/p&gt;
&lt;p&gt;One variation is something 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;T1 DoIt(T1 x, T2 y, T3 z);&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In this sort of interface, the return type is the same as the first parameter. When creating Null Objects or Composites, we can generally just do as we did with pure Closure of Operations and ignore the other parameters.&lt;/p&gt;
&lt;p&gt;Another variation is a method 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;T1 DoIt(&lt;span&gt;&lt;font color="#2b91af"&gt;Foo&lt;/font&gt;&lt;/span&gt;&amp;lt;T1, T2, T3&amp;gt; foo);&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;where Foo is defined 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;Foo&lt;/font&gt;&lt;/span&gt;&amp;lt;T1, T2, T3&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;public&lt;/font&gt;&lt;/span&gt; T1 X { &lt;span&gt;&lt;font color="#0000ff"&gt;get&lt;/font&gt;&lt;/span&gt;; &lt;span&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;span&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; T2 Y { &lt;span&gt;&lt;font color="#0000ff"&gt;get&lt;/font&gt;&lt;/span&gt;; &lt;span&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;span&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/span&gt; T3 Z { &lt;span&gt;&lt;font color="#0000ff"&gt;get&lt;/font&gt;&lt;/span&gt;; &lt;span&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;In this case we can still reduce the input to create the output by simply selecting and returning foo.X and ignoring the other properties.&lt;/p&gt;
&lt;p&gt;Still, we may not always be able to define APIs such as these.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Composable return types&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Sometimes (perhaps even most of the times) we can’t mold our APIs into any of the above shapes because we inherently need to &lt;em&gt;map&lt;/em&gt; one type into another type:&lt;/p&gt;
&lt;div style="font-family: ; background: white"&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;T2 Map(T1 x);&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;To keep such a method composable, we must then make sure that the output type itself is composable. This would allow us to implement a Composite by wrapping each return value from the contained instances into a Composite of the return type.&lt;/p&gt;
&lt;p&gt;Likewise, we could create a Null Object by returning another Null Object for the return type.&lt;/p&gt;
&lt;p&gt;In theory, we could repeat this design process to create a big chain of composable types, as long as the last type terminates the chain by fitting into one of the above shapes. However, this can quickly become unwieldy, so we should go to great efforts to make those chains as short as possible.&lt;/p&gt;
&lt;p&gt;It should be noted that every type that implements IEnumerable fits pretty well into this category. A Null Object is simply an empty sequence, and a Composite is simply a sequence with multiple items. Thus, interfaces that return enumerables tend to be good abstractions.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;There are many well-known variations of good interface design. The above guiding principles looks only at a small, interrelated set. In fact, we can regard both Commands and Closure of Operations as degenerate cases of Reduction of Input. We should strive to create interfaces that directly fit into one of these categories, and when that isn’t possible, at least interfaces that return types that fit into those categories.&lt;/p&gt;
&lt;p&gt;Keeping interfaces small and focused makes this possible in the first place.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=172e0e51-adc9-4cbb-ae00-55f8bf173a17"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,172e0e51-adc9-4cbb-ae00-55f8bf173a17.aspx</comments>
      <category>Software Design</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=fc5635d2-de43-41c0-bf9c-0dfea0078a57</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,fc5635d2-de43-41c0-bf9c-0dfea0078a57.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,fc5635d2-de43-41c0-bf9c-0dfea0078a57.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=fc5635d2-de43-41c0-bf9c-0dfea0078a57</wfw:commentRss>
      <slash:comments>14</slash:comments>
      <title>Interfaces are not abstractions</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,fc5635d2-de43-41c0-bf9c-0dfea0078a57.aspx</guid>
      <link>http://blog.ploeh.dk/2010/12/02/InterfacesAreNotAbstractions.aspx</link>
      <pubDate>Thu, 02 Dec 2010 13:03:04 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;One of the first sound bites from the beloved book &lt;a href="http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612/"&gt;Design Patterns&lt;/a&gt; is this:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;&lt;em&gt;Program to an interface, not an implementation&lt;/em&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p align="left"&gt;It would seem that a corollary is that we can measure the quality of our code on the number of interfaces; the more, the better. However, that’s not how it feels in reality when you are trying to figure out whether to use an IFooFactory, IFooPolicy, IFooPolicyFactory or perhaps even an IFooFactoryFactory.&lt;/p&gt; &lt;p align="left"&gt;Do you extract interfaces from your classes to enable loose coupling? If so, you probably have a &lt;a href="http://martinfowler.com/bliki/InterfaceImplementationPair.html"&gt;1:1 relationship between your interfaces and the concrete classes that implement them&lt;/a&gt;. That’s &lt;a href="http://simpleprogrammer.com/2010/11/02/back-to-basics-what-is-an-interface/"&gt;probably not a good sign&lt;/a&gt;, and violates the &lt;a href="http://parlezuml.com/blog/?postid=934"&gt;Reused Abstractions Principle (RAP)&lt;/a&gt;. I’ve been guilty of this and didn’t like the result.&lt;/p&gt; &lt;blockquote&gt; &lt;p align="left"&gt;Having only one implementation of a given interface is a code smell.&lt;/p&gt;&lt;/blockquote&gt; &lt;p align="left"&gt;Programming to an interface does not guarantee that we are coding against an abstraction. Interfaces are not abstractions. Why not?&lt;/p&gt; &lt;p align="left"&gt;An interface is just a language construct. In essence, it’s just a &lt;em&gt;shape&lt;/em&gt;. It’s like a power plug and socket. In Europe we use one kind, and the US uses another, but it’s only &lt;em&gt;by convention&lt;/em&gt; that we transmit 230V through European sockets and 110V through US sockets. Although plugs only fit in their respective sockets, nothing prevents us from sending 230V through a US plug/socket combination.&lt;/p&gt; &lt;p align="left"&gt;&lt;a href="http://blogs.msdn.com/b/kcwalina/"&gt;Krzysztof Cwalina&lt;/a&gt; already pointed this out in 2004: &lt;a href="http://blogs.msdn.com/b/kcwalina/archive/2004/10/24/246947.aspx"&gt;interfaces are not contracts&lt;/a&gt;. If they aren’t even contracts, then how can they be abstractions?&lt;/p&gt; &lt;p&gt;Interfaces &lt;em&gt;can&lt;/em&gt; be used as abstractions, but using an interface is in itself no guarantee that we are dealing with an abstraction. Rather, we have the following relationship between interfaces and abstractions:&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="Abstraction and interface sets" border="0" alt="Abstractions, interfaces and their intersection" src="http://blog.ploeh.dk/content/binary/Windows-Live-Writer/Interfaces-are-not-abstractions_D282/image_5.png" width="240" height="135"&gt;&lt;/p&gt; &lt;p&gt;There are basically two sets: a set of abstractions and a set of interfaces. In the following we will discuss the set of interfaces that does &lt;em&gt;not&lt;/em&gt; intersect the set of abstractions, saving the intersection for &lt;a href="http://blog.ploeh.dk/2010/12/03/TowardsBetterAbstractions.aspx"&gt;another blog post&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;There are many ways an interface can turn out to be a poor abstraction. The following is an incomplete list:&lt;/p&gt; &lt;p&gt;&lt;strong&gt;LSP Violations&lt;/strong&gt;&lt;/p&gt; &lt;p align="left"&gt;Violating the &lt;a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle"&gt;Liskov Substitution Principle&lt;/a&gt; is a pretty obvious sign that the interface in use is a poor abstraction. This may be most obvious when the consumer of the interface needs to downcast an instance to properly work with it.&lt;/p&gt; &lt;p align="left"&gt;However, as &lt;a href="http://www.objectmentor.com/omTeam/martin_r.html"&gt;Uncle Bob&lt;/a&gt; &lt;a href="http://www.objectmentor.com/resources/articles/lsp.pdf"&gt;points out&lt;/a&gt;, even an interface as simple as this seemingly innocuous rectangle ‘abstraction’ contains potential dangers:&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;IRectangle&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;int&lt;/font&gt;&lt;/span&gt; Width { &lt;span&gt;&lt;font color="#0000ff"&gt;get&lt;/font&gt;&lt;/span&gt;; &lt;span&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;span&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt; Height { &lt;span&gt;&lt;font color="#0000ff"&gt;get&lt;/font&gt;&lt;/span&gt;; &lt;span&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 align="left"&gt;The issue becomes apparent when you attempt to let a Square class implement IRectangle. To protect the invariants of Square, you can’t allow the Width and Height properties to differ. You have a couple of options, none of which are very good:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;div align="left"&gt;Update both Width and Height to the same value when one of them are being written.&lt;/div&gt;
&lt;li&gt;
&lt;div align="left"&gt;Ignore the write operation when the caller attempts to assign an invalid value.&lt;/div&gt;
&lt;li&gt;
&lt;div align="left"&gt;Throw an exception when the caller attempts to assign a Width which is different from the Height (and vice versa).&lt;/div&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;p align="left"&gt;From the point of view of a consumer of the IRectangle interface, all of these options would at the very least violate the &lt;a href="http://en.wikipedia.org/wiki/Principle_of_least_astonishment"&gt;Principle of Least Astonishment&lt;/a&gt;, and throwing exceptions would definitely cause the consumer to behave differently when consuming Square instances as opposed to ‘normal’ rectangles.&lt;/p&gt;
&lt;p align="left"&gt;The problem stems from the fact that the operations have side effects. Invoking one operation changes the state of a seemingly unrelated piece of data. The more members we have, the greater the risk is, so the &lt;a href="http://en.wikipedia.org/wiki/Interface_segregation_principle"&gt;Interface Segregation Principle&lt;/a&gt; can, to a certain extent, help.&lt;/p&gt;
&lt;p align="left"&gt;&lt;strong&gt;Header Interfaces&lt;/strong&gt;&lt;/p&gt;
&lt;p align="left"&gt;Since a higher number of members increases the risk of unexpected side effects and temporal coupling it should come as no surprise that interfaces mechanically extracted from &lt;em&gt;all&lt;/em&gt; members of a a concrete class are poor abstractions.&lt;/p&gt;
&lt;p align="left"&gt;As always, Visual Studio makes it very easy to do the wrong thing by offering the &lt;em&gt;Extract Interface&lt;/em&gt; refactoring feature.&lt;/p&gt;
&lt;p&gt;We call such interfaces &lt;a href="http://martinfowler.com/bliki/HeaderInterface.html"&gt;Header Interfaces&lt;/a&gt; because they resemble C++ header files. They tend to simply state the same thing twice without apparent benefit. This is particularly true when you have only a single implementation, which tends to be very likely for interfaces with many members.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Shallow Interfaces&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When you use the &lt;em&gt;Extract Interface&lt;/em&gt; refactoring feature in Visual Studio, even if you don’t extract every member, the resulting interface is &lt;em&gt;shallow&lt;/em&gt; because it doesn’t recursively extract interfaces from the concrete types exposed by the extracted members.&lt;/p&gt;
&lt;p&gt;An example I’ve seen more than once involves extracting an interface from a LINQ to SQL or LINQ to Entities context in order to define a Repository interface. As an example, here’s an interface extracted from a very simple LINQ to Entities context:&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;IPostingContext&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;void&lt;/font&gt;&lt;/span&gt; AddToPostings(&lt;span&gt;&lt;font color="#2b91af"&gt;Posting&lt;/font&gt;&lt;/span&gt; posting);&lt;/font&gt;&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;ObjectSet&lt;/font&gt;&lt;/span&gt;&amp;lt;&lt;span&gt;&lt;font color="#2b91af"&gt;Posting&lt;/font&gt;&lt;/span&gt;&amp;gt; Postings { &lt;span&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;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;At first glance this may look useful, but it isn’t. Even though it’s an interface, it’s still tightly coupled to a specific object context. Not only does ObjectSet&amp;lt;T&amp;gt; reference the Entity Framework, but the Posting class is defined by a very specific, auto-generated Entity context.&lt;/p&gt;
&lt;p&gt;The interface may give you the impression of working against loosely coupled code, but you can’t easily (if at all) implement a different IPostingContext with a radically different data access technology. You’ll be stuck with this particular PostingContext.&lt;/p&gt;
&lt;p&gt;If you must extract an interface, you’ll need to do it recursively.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Leaky Abstractions&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Another way we can create problems for ourselves is when our interfaces leak implementation details. A good example can be found in the &lt;a href="http://systemwrapper.codeplex.com/"&gt;SystemWrapper&lt;/a&gt; project that provides extracted interfaces for various BCL types, such as &lt;a href="http://msdn.microsoft.com/en-us/library/system.io.fileinfo.aspx"&gt;System.IO.FileInfo&lt;/a&gt;. Those interfaces may enable mocking, but we shouldn’t expect to ever be able to create another implementation of SystemWrapper.IO.IFileInfoWrap. In other words, those interfaces aren’t very useful.&lt;/p&gt;
&lt;p&gt;Another example is this attempt at defining a Repository interface:&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;IFooRepository&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;string&lt;/font&gt;&lt;/span&gt; ConnectionString { &lt;span&gt;&lt;font color="#0000ff"&gt;get&lt;/font&gt;&lt;/span&gt;; &lt;span&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;span&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;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Exposing a ConnectionString property strongly indicates that the repository is implemented on top of a database; this knowledge leaks through. If we wanted to implement the repository based on a web service, we might be able to repurpose the the ConnectionString property to a service URL, but it would be a hack at best – and how would we define security settings in that scenario?&lt;/p&gt;
&lt;p&gt;Exposing a &lt;em&gt;FileName&lt;/em&gt; property on an interface that represents an abstract resource is another example of a &lt;a href="http://en.wikipedia.org/wiki/Leaky_abstraction"&gt;Leaky Abstraction&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Leaky Abstractions like these are often difficult to reuse. As an example, it would be difficult to implement a &lt;a href="http://en.wikipedia.org/wiki/Composite_pattern"&gt;Composite&lt;/a&gt; out of the above IFooRepository – how do you aggregate a ConnectionString?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In short, using interfaces in no way guarantees that we operate with appropriate abstractions. Thus, the proliferation of interfaces that typically follow from TDD or use of DI may not be the pure goodness we tend to believe.&lt;/p&gt;
&lt;p&gt;Creating good abstractions is difficult and requires skill. In a &lt;a href="http://blog.ploeh.dk/2010/12/03/TowardsBetterAbstractions.aspx"&gt;future post&lt;/a&gt;, I’ll look at some principles that we can use as guides.&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=fc5635d2-de43-41c0-bf9c-0dfea0078a57"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,fc5635d2-de43-41c0-bf9c-0dfea0078a57.aspx</comments>
      <category>Software Design</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=535c4930-7362-4699-8fcc-d4715e964ef2</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,535c4930-7362-4699-8fcc-d4715e964ef2.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,535c4930-7362-4699-8fcc-d4715e964ef2.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=535c4930-7362-4699-8fcc-d4715e964ef2</wfw:commentRss>
      <title>Integrating AutoFixture with ObjectHydrator</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,535c4930-7362-4699-8fcc-d4715e964ef2.aspx</guid>
      <link>http://blog.ploeh.dk/2010/11/22/IntegratingAutoFixtureWithObjectHydrator.aspx</link>
      <pubDate>Mon, 22 Nov 2010 06:42:37 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;Back in the days of &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; 1.0 I occasionally got the feedback that although people liked the engine and its features, they didn’t like the data it generated. I think they particularly &lt;a href="http://twitter.com/#!/hakanforss/status/7607978333"&gt;didn’t like&lt;/a&gt; &lt;a href="http://blog.ploeh.dk/2009/04/02/CreatingStringsWithAutoFixture.aspx"&gt;all the Guids&lt;/a&gt;, but &lt;a href="http://hakanforss.wordpress.com/"&gt;Håkon Forss&lt;/a&gt; &lt;a href="http://twitter.com/#!/hakanforss/status/7598441621"&gt;suggested&lt;/a&gt; combining &lt;a href="http://objecthydrator.codeplex.com/"&gt;Object Hydrator&lt;/a&gt;’s data generator with AutoFixture.&lt;/p&gt; &lt;p&gt;In fact, this suggestion made me realize that AutoFixture 1.0’s engine wasn’t extensible enough, which again prompted me to build AutoFixture 2.0. Now that AutoFixture 2.0 is out, what would be more fitting than to examine whether we can do what Håkon suggested?&lt;/p&gt; &lt;p&gt;It turns out to be pretty easy to customize AutoFixture to use Object Hydrator’s data generator. The main part is creating a custom ISpecimenBuilder that acts as an &lt;a href="http://en.wikipedia.org/wiki/Adapter_pattern"&gt;Adapter&lt;/a&gt; of Object Hydrator:&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;HydratorAdapter&lt;/font&gt;&lt;/span&gt; : &lt;/font&gt;&lt;span&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&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;IMap&lt;/font&gt;&lt;/span&gt; map;&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; HydratorAdapter(&lt;span&gt;&lt;font color="#2b91af"&gt;IMap&lt;/font&gt;&lt;/span&gt; map)&lt;/font&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; (map == &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;"map"&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;.map = map;&lt;/font&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; ISpecimenBuilder 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; Create(&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&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&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; pi = request &lt;span&gt;&lt;font color="#0000ff"&gt;as&lt;/font&gt;&lt;/span&gt; &lt;span&gt;&lt;font color="#2b91af"&gt;PropertyInfo&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;if&lt;/font&gt;&lt;/span&gt; (pi == &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;return&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;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&gt;&lt;font color="#0000ff"&gt;if&lt;/font&gt;&lt;/span&gt; ((!&lt;span&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/span&gt;.map.Match(pi))&lt;/font&gt;&lt;/pre&gt;&lt;pre style="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;this&lt;/font&gt;&lt;/span&gt;.map.Type != pi.PropertyType))&lt;/font&gt;&lt;/pre&gt;&lt;pre style="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;return&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;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&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;.map.Mapping(pi).Generate();&lt;/font&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;The IMap interface is defined by Object Hydrator, ISpecimenBuilder and NoSpecimen are AutoFixture types and the rest are BCL types.&lt;/p&gt;
&lt;p&gt;Each HydratorAdapter adapts a single IMap instance. The IMap interface only works with PropertyInfo instances, so the first thing to do is to examine the request to figure out whether it’s a request for a PropertyInfo at all. If this is the case and the map matches the request, we ask it to generate a specimen for the property.&lt;/p&gt;
&lt;p&gt;To get all of Object Hydrator’s maps into AutoFixture, we can now define this customization:&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;ObjectHydratorCustomization&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&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;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; ICustomization 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;void&lt;/font&gt;&lt;/span&gt; Customize(&lt;span&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&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt;&lt;/span&gt; builders = &lt;span&gt;&lt;font color="#0000ff"&gt;from&lt;/font&gt;&lt;/span&gt; m &lt;span&gt;&lt;font color="#0000ff"&gt;in&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;DefaultTypeMap&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span&gt;&lt;font color="#0000ff"&gt;select&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;HydratorAdapter&lt;/font&gt;&lt;/span&gt;(m);&lt;/font&gt;&lt;/pre&gt;&lt;pre style="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(&lt;/font&gt;&lt;/pre&gt;&lt;pre style="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;CompositeSpecimenBuilder&lt;/font&gt;&lt;/span&gt;(builders));&lt;/font&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;The ObjectHydratorCustomization simply projects all maps from Object Hydrator’s DefaultTypeMap into instances of HydratorAdapter and adds these as customizations to the fixture.&lt;/p&gt;
&lt;p&gt;This enables us to use Object Hydrator with any Fixture instance 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; 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;ObjectHydratorCustomization&lt;/font&gt;&lt;/span&gt;());&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;To prove that this works, here’s a dump of a Customer type created in this way:&lt;/p&gt;
&lt;div style="font-family: ; background: white"&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;span&gt;&lt;font color="#a31515"&gt;"Id"&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; &lt;span&gt;&lt;font color="#a31515"&gt;"FirstName"&lt;/font&gt;&lt;/span&gt;: &lt;span&gt;&lt;font color="#a31515"&gt;"Raymond"&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;span&gt;&lt;font color="#a31515"&gt;"LastName"&lt;/font&gt;&lt;/span&gt;: &lt;span&gt;&lt;font color="#a31515"&gt;"Reeves"&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;span&gt;&lt;font color="#a31515"&gt;"Company"&lt;/font&gt;&lt;/span&gt;: &lt;span&gt;&lt;font color="#a31515"&gt;"Carrys Candles"&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;span&gt;&lt;font color="#a31515"&gt;"Description"&lt;/font&gt;&lt;/span&gt;: &lt;span&gt;&lt;font color="#a31515"&gt;"Lorem ipsum dolor sit"&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;span&gt;&lt;font color="#a31515"&gt;"Locations"&lt;/font&gt;&lt;/span&gt;: 53,&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp; &lt;span&gt;&lt;font color="#a31515"&gt;"IncorporatedOn"&lt;/font&gt;&lt;/span&gt;: &lt;span&gt;&lt;font color="#a31515"&gt;"\/Date(1376154940000+0200)\/"&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;span&gt;&lt;font color="#a31515"&gt;"Revenue"&lt;/font&gt;&lt;/span&gt;: 33.57,&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp; &lt;span&gt;&lt;font color="#a31515"&gt;"WorkAddress"&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="#a31515"&gt;"AddressLine1"&lt;/font&gt;&lt;/span&gt;: &lt;span&gt;&lt;font color="#a31515"&gt;"32373 BALL Lane"&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="#a31515"&gt;"AddressLine2"&lt;/font&gt;&lt;/span&gt;: &lt;span&gt;&lt;font color="#a31515"&gt;"29857 DEER PARK Dr."&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="#a31515"&gt;"City"&lt;/font&gt;&lt;/span&gt;: &lt;span&gt;&lt;font color="#a31515"&gt;"fullerton"&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="#a31515"&gt;"State"&lt;/font&gt;&lt;/span&gt;: &lt;span&gt;&lt;font color="#a31515"&gt;"NM"&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="#a31515"&gt;"PostalCode"&lt;/font&gt;&lt;/span&gt;: &lt;span&gt;&lt;font color="#a31515"&gt;"27884"&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="#a31515"&gt;"Country"&lt;/font&gt;&lt;/span&gt;: &lt;/font&gt;&lt;span&gt;&lt;font style="font-size: 10pt" color="#a31515"&gt;"GI"&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;font style="font-size: 10pt"&gt;&amp;nbsp; &lt;span&gt;&lt;font color="#a31515"&gt;"HomeAddress"&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="#a31515"&gt;"AddressLine1"&lt;/font&gt;&lt;/span&gt;: &lt;span&gt;&lt;font color="#a31515"&gt;"66377 NORTH STAR Pl."&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="#a31515"&gt;"AddressLine2"&lt;/font&gt;&lt;/span&gt;: &lt;span&gt;&lt;font color="#a31515"&gt;"33406 MAY Dr."&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="#a31515"&gt;"City"&lt;/font&gt;&lt;/span&gt;: &lt;span&gt;&lt;font color="#a31515"&gt;"miami"&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="#a31515"&gt;"State"&lt;/font&gt;&lt;/span&gt;: &lt;span&gt;&lt;font color="#a31515"&gt;"MD"&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="#a31515"&gt;"PostalCode"&lt;/font&gt;&lt;/span&gt;: &lt;span&gt;&lt;font color="#a31515"&gt;"18361"&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="#a31515"&gt;"Country"&lt;/font&gt;&lt;/span&gt;: &lt;/font&gt;&lt;span&gt;&lt;font style="font-size: 10pt" color="#a31515"&gt;"PH"&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;font style="font-size: 10pt"&gt;&amp;nbsp; &lt;span&gt;&lt;font color="#a31515"&gt;"Addresses"&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;span&gt;&lt;font color="#a31515"&gt;"HomePhone"&lt;/font&gt;&lt;/span&gt;: &lt;span&gt;&lt;font color="#a31515"&gt;"(388)538-1266"&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;span&gt;&lt;font color="#a31515"&gt;"Type"&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;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Without Object Hydrator’s data generators, this would have looked like this instead:&lt;/p&gt;
&lt;div style="font-family: ; background: white"&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;span&gt;&lt;font color="#a31515"&gt;"Id"&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; &lt;span&gt;&lt;font color="#a31515"&gt;"FirstName"&lt;/font&gt;&lt;/span&gt;: &lt;span&gt;&lt;font color="#a31515"&gt;"FirstNamebf53cb4c-3aae-4963-bb0c-ad0219293736"&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;span&gt;&lt;font color="#a31515"&gt;"LastName"&lt;/font&gt;&lt;/span&gt;: &lt;span&gt;&lt;font color="#a31515"&gt;"LastName079f7ab2-d026-48c5-8cfb-76e0568d1d79"&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;span&gt;&lt;font color="#a31515"&gt;"Company"&lt;/font&gt;&lt;/span&gt;: &lt;span&gt;&lt;font color="#a31515"&gt;"Company9ffe4640-2534-4ef7-b066-fb6bbe3a668c"&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;span&gt;&lt;font color="#a31515"&gt;"Description"&lt;/font&gt;&lt;/span&gt;: &lt;span&gt;&lt;font color="#a31515"&gt;"Descriptionf5843974-b14b-4bce-b3cc-63ad6aaf3ab2"&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;span&gt;&lt;font color="#a31515"&gt;"Locations"&lt;/font&gt;&lt;/span&gt;: 2,&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp; &lt;span&gt;&lt;font color="#a31515"&gt;"IncorporatedOn"&lt;/font&gt;&lt;/span&gt;: &lt;span&gt;&lt;font color="#a31515"&gt;"\/Date(1290169587222+0100)\/"&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;span&gt;&lt;font color="#a31515"&gt;"Revenue"&lt;/font&gt;&lt;/span&gt;: 1.0,&lt;/font&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;font style="font-size: 10pt"&gt;&amp;nbsp; &lt;span&gt;&lt;font color="#a31515"&gt;"WorkAddress"&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="#a31515"&gt;"AddressLine1"&lt;/font&gt;&lt;/span&gt;: &lt;span&gt;&lt;font color="#a31515"&gt;"AddressLine1f4d50570-423e-4a74-8348-1c54402ffe48"&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="#a31515"&gt;"AddressLine2"&lt;/font&gt;&lt;/span&gt;: &lt;span&gt;&lt;font color="#a31515"&gt;"AddressLine2031fe3e2-40c1-4ec3-b445-e88c213457e9"&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="#a31515"&gt;"City"&lt;/font&gt;&lt;/span&gt;: &lt;span&gt;&lt;font color="#a31515"&gt;"Citycd33fce3-66bb-457d-8f99-98a16c0c5bf1"&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="#a31515"&gt;"State"&lt;/font&gt;&lt;/span&gt;: &lt;span&gt;&lt;font color="#a31515"&gt;"State40bebd6d-6073-4421-8a74-e910ff9d09e3"&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="#a31515"&gt;"PostalCode"&lt;/font&gt;&lt;/span&gt;: &lt;span&gt;&lt;font color="#a31515"&gt;"PostalCode1da93f22-799b-4f6b-a5ce-f4816f8bbb05"&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="#a31515"&gt;"Country"&lt;/font&gt;&lt;/span&gt;: &lt;/font&gt;&lt;span&gt;&lt;font style="font-size: 10pt" color="#a31515"&gt;"Countryfa2ad951-ce0c-42a4-ab55-c077b6e03f00"&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;font style="font-size: 10pt"&gt;&amp;nbsp; &lt;span&gt;&lt;font color="#a31515"&gt;"HomeAddress"&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="#a31515"&gt;"AddressLine1"&lt;/font&gt;&lt;/span&gt;: &lt;span&gt;&lt;font color="#a31515"&gt;"AddressLine145cbffeb-d7a9-4778-b297-d010c30b7614"&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="#a31515"&gt;"AddressLine2"&lt;/font&gt;&lt;/span&gt;: &lt;span&gt;&lt;font color="#a31515"&gt;"AddressLine2e86d6476-5bdc-4940-a8ee-975bf3f65d49"&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="#a31515"&gt;"City"&lt;/font&gt;&lt;/span&gt;: &lt;span&gt;&lt;font color="#a31515"&gt;"City6ae3aab9-7c73-4768-ae7d-a6ea515c816a"&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="#a31515"&gt;"State"&lt;/font&gt;&lt;/span&gt;: &lt;span&gt;&lt;font color="#a31515"&gt;"State56de6222-fd84-46b0-ace0-c6098dbd0681"&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="#a31515"&gt;"PostalCode"&lt;/font&gt;&lt;/span&gt;: &lt;span&gt;&lt;font color="#a31515"&gt;"PostalCodeca1af9af-a97b-4966-b156-cfbebd6d5e38"&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="#a31515"&gt;"Country"&lt;/font&gt;&lt;/span&gt;: &lt;/font&gt;&lt;span&gt;&lt;font style="font-size: 10pt" color="#a31515"&gt;"Country6960eebe-fe6f-4b63-ad73-7ba6a2b95791"&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;font style="font-size: 10pt"&gt;&amp;nbsp; &lt;span&gt;&lt;font color="#a31515"&gt;"Addresses"&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;span&gt;&lt;font color="#a31515"&gt;"HomePhone"&lt;/font&gt;&lt;/span&gt;: &lt;span&gt;&lt;font color="#a31515"&gt;"HomePhone623f9d6f-febe-4c9f-87f8-e90d7e57eb46"&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;span&gt;&lt;font color="#a31515"&gt;"Type"&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;}&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;One limitation of Object Hydrator is that it requires the classes to have default constructors. AutoFixture doesn’t have that constraint, and to prove that I defined the only available Customer constructor 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; Customer(&lt;span&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&lt;/span&gt; id)&lt;/font&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;With AutoFixture, this is not a problem and the Customer instance is created as described above.&lt;/p&gt;
&lt;p&gt;With the extensibility model of AutoFixture 2.0 I am pleased to be able to verify that Håkon Forss (and others) can now have the best of both worlds :)&lt;/p&gt;&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=535c4930-7362-4699-8fcc-d4715e964ef2"/&gt;&lt;/div&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,535c4930-7362-4699-8fcc-d4715e964ef2.aspx</comments>
      <category>AutoFixture</category>
    </item>
  </channel>
</rss>
