<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>ploeh blog : Software Design</title><link>http://blogs.msdn.com/ploeh/archive/tags/Software+Design/default.aspx</link><description>Tags: Software Design</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>TechEd Sessions Sample Code</title><link>http://blogs.msdn.com/ploeh/archive/2007/11/09/teched-sessions-sample-code.aspx</link><pubDate>Fri, 09 Nov 2007 05:34:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6011638</guid><dc:creator>ploeh</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/ploeh/comments/6011638.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ploeh/commentrss.aspx?PostID=6011638</wfw:commentRss><wfw:comment>http://blogs.msdn.com/ploeh/rsscomments.aspx?PostID=6011638</wfw:comment><description>&lt;P&gt;Now that both of &lt;A class="" href="http://blogs.msdn.com/ploeh/archive/2007/10/10/SpeakingAtTechEdDevelopers2007.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2007/10/10/SpeakingAtTechEdDevelopers2007.aspx"&gt;my TechEd sessions&lt;/A&gt; are over, I've had some request for the sample code that I demonstrated at both sessions.&lt;/P&gt;
&lt;P&gt;The sample code I used at the interactive talk on TDD is attached to this post.&lt;/P&gt;
&lt;P&gt;The code I demonstrated at my talk on manual and dynamic mocks is available upon request. I certainly don't mind sharing it, but for reasons I don't want to go into here, I'm not going to post it for general download, so if you are interested, drop me a line (if you use the&amp;nbsp;&lt;A class="" href="http://blogs.msdn.com/ploeh/contact.aspx" mce_href="http://blogs.msdn.com/ploeh/contact.aspx"&gt;Email&lt;/A&gt; link on this blog, I'll get an email that also contains your sender address, which means you don't have to publicly post your email address here).&lt;/P&gt;
&lt;P&gt;If you didn't go to&amp;nbsp;my sessions, you can still download the sample, but it probably doesn't make as much&amp;nbsp;sense&amp;nbsp;taken out of context.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As usual, the code is provided here "as is", etc.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=6011638" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/ploeh/attachment/6011638.ashx" length="116887" type="application/x-zip-compressed" /><category domain="http://blogs.msdn.com/ploeh/archive/tags/Dependency+Injection/default.aspx">Dependency Injection</category><category domain="http://blogs.msdn.com/ploeh/archive/tags/Unit+Testing/default.aspx">Unit Testing</category><category domain="http://blogs.msdn.com/ploeh/archive/tags/Software+Design/default.aspx">Software Design</category></item><item><title>CovariantCollection</title><link>http://blogs.msdn.com/ploeh/archive/2007/09/29/CovariantCollection.aspx</link><pubDate>Sat, 29 Sep 2007 18:48:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5186429</guid><dc:creator>ploeh</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/ploeh/comments/5186429.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ploeh/commentrss.aspx?PostID=5186429</wfw:commentRss><wfw:comment>http://blogs.msdn.com/ploeh/rsscomments.aspx?PostID=5186429</wfw:comment><description>&lt;P&gt;One of the known limitations of .NET generics is the limited support for covariance. This is sometimes a challenge when it comes to collections. As an example, consider this simple abstract class:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;abstract&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;MyClass&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;protected&lt;/SPAN&gt; MyClass() { }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Okay, so that's not the most exciting class you've ever seen, but it will serve my demonstration purposes nicely. Assume that you have another class that exposes a list of MyClass objects:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;abstract&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;MyContainer&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;protected&lt;/SPAN&gt; MyContainer() { }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;abstract&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;MyClass&lt;/SPAN&gt;&amp;gt; MyClasses { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Here's a simple implementation that doesn't cause any problems at all:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;SimpleContainer&lt;/SPAN&gt; : &lt;SPAN style="COLOR: #2b91af"&gt;MyContainer&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;List&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;MyClass&lt;/SPAN&gt;&amp;gt; myClasses_;&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; SimpleContainer()&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; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.myClasses_ = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;List&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;MyClass&lt;/SPAN&gt;&amp;gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;override&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;MyClass&lt;/SPAN&gt;&amp;gt; MyClasses&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; &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;.myClasses_; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;This implementation simply encapsulates a List&amp;lt;MyClass&amp;gt;, which implements IList&amp;lt;MyClass&amp;gt;, and the compiler is happy. Now, assume that you need to write a more complex implementation of MyContainer. This class exclusively uses a list of MyDerivedClass, which derives from MyClass. For type safety reasons, you prefer to work on a list of MyDerivedClass, since you have complex internal logic and you want to ensure that you only add MyDerivedClass instances to the list. Here's an outline of what I'm describing:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;NotSoSimpleContainer&lt;/SPAN&gt; : &lt;SPAN style="COLOR: #2b91af"&gt;MyContainer&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;List&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;MyDerivedClass&lt;/SPAN&gt;&amp;gt; myClasses_;&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; NotSoSimpleContainer()&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; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.myClasses_ = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;List&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;MyDerivedClass&lt;/SPAN&gt;&amp;gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;This class must also implement the MyClasses abstract property, or it isn't going to compile. However, due to the lack of covariance support in .NET, this attempt isn't going to compile either:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: green"&gt;// Doesn't compile&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;override&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;MyClass&lt;/SPAN&gt;&amp;gt; MyClasses&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;get&lt;/SPAN&gt; { &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.myClasses_; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;The problem is that myClasses_ is an instance of List&amp;lt;MyDerivedClass&amp;gt;, and although MyDerivedClass derives from MyClass, List&amp;lt;MyDerivedClass&amp;gt; doesn't implement IList&amp;lt;MyClass&amp;gt;, but rather IList&amp;lt;MyDerivedClass&amp;gt;, which is &lt;EM&gt;an entirely different type&lt;/EM&gt;. Any similarity to other types is purely coincidental and does not imply any specific intent of the author, or something...&lt;/P&gt;
&lt;P&gt;Actually, this is quite annoying, so how can you solve this? My approach is to create a collection that wraps the original collection but exposes a different interface. It's a bit like how &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/ms132474.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/ms132474.aspx"&gt;ReadOnlyCollection&amp;lt;T&amp;gt;&lt;/A&gt; works. In lack of a better idea, I've called this class CovariantCollection&amp;lt;TConcrete, TAbstract&amp;gt;. This collection doesn't copy the original list, but rather wraps it while exposing it differently. To see how it's used, let's look at the updated NotSoSimpleContainer implementation:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;NotSoSimpleContainer&lt;/SPAN&gt; : &lt;SPAN style="COLOR: #2b91af"&gt;MyContainer&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;List&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;MyDerivedClass&lt;/SPAN&gt;&amp;gt; myClasses_;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;CovariantCollection&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;MyDerivedClass&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #2b91af"&gt;MyClass&lt;/SPAN&gt;&amp;gt; myClassesWrapper_;&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; NotSoSimpleContainer()&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; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.myClasses_ = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;List&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;MyDerivedClass&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; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.myClassesWrapper_ =&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; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;CovariantCollection&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;MyDerivedClass&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #2b91af"&gt;MyClass&lt;/SPAN&gt;&amp;gt;(&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.myClasses_);&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 style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: green"&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;override&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;MyClass&lt;/SPAN&gt;&amp;gt; MyClasses&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; &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;.myClassesWrapper_; }&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;From within NotSoSimpleContainer, you would still manipulate the original list via the myClasses_ member variable, which ensures that only instances of MyDerivedClass are being used. However, the myClassesWrapper_ member variable wraps myClasses_ and exposes it as an IList&amp;lt;MyClass&amp;gt;.&lt;/P&gt;
&lt;P&gt;Here comes&amp;nbsp;the complete implementation of CovariantCollection&amp;lt;TConcrete, TAbstract&amp;gt;. It's a bit of a long code listing, but I think it will still be more readable if I present it in one piece. I still have a few comments about the implementation below, so just scroll past the code if you don't care about all the details:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;CovariantCollection&lt;/SPAN&gt;&amp;lt;TConcrete, TAbstract&amp;gt; : &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Collection&lt;/SPAN&gt;&amp;lt;TConcrete&amp;gt;, &lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;&amp;lt;TAbstract&amp;gt; &lt;SPAN style="COLOR: blue"&gt;where&lt;/SPAN&gt; TConcrete : TAbstract&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; CovariantCollection(&lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;&amp;lt;TConcrete&amp;gt; list)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; : &lt;SPAN style="COLOR: blue"&gt;base&lt;/SPAN&gt;(list)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/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;&amp;nbsp;&amp;nbsp;&amp;nbsp; #region&lt;/SPAN&gt; IList&amp;lt;TAbstract&amp;gt; Members&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;int&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;&amp;lt;TAbstract&amp;gt;.IndexOf(TAbstract item)&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; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (item &lt;SPAN style="COLOR: blue"&gt;is&lt;/SPAN&gt; TConcrete)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&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; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.IndexOf((TConcrete)item);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&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; &lt;SPAN style="COLOR: blue"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;CovariantCollection&lt;/SPAN&gt;&amp;lt;TConcrete, TAbstract&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; CreateWrongConcreteTypeException(&lt;SPAN style="COLOR: #a31515"&gt;"item"&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;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;&amp;lt;TAbstract&amp;gt;.Insert(&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; index, TAbstract item)&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; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (!(item &lt;SPAN style="COLOR: blue"&gt;is&lt;/SPAN&gt; TConcrete))&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&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; &lt;SPAN style="COLOR: blue"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;CovariantCollection&lt;/SPAN&gt;&amp;lt;TConcrete, TAbstract&amp;gt;.&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; CreateWrongConcreteTypeException(&lt;SPAN style="COLOR: #a31515"&gt;"item"&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&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; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.Insert(index, (TConcrete)item);&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;void&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;&amp;lt;TAbstract&amp;gt;.RemoveAt(&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; index)&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; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.RemoveAt(index);&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; TAbstract &lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;&amp;lt;TAbstract&amp;gt;.&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;[&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; index]&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; &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&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; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;[index];&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&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; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&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; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (!(&lt;SPAN style="COLOR: blue"&gt;value&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;is&lt;/SPAN&gt; TConcrete))&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; {&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; &lt;SPAN style="COLOR: blue"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;CovariantCollection&lt;/SPAN&gt;&amp;lt;TConcrete, TAbstract&amp;gt;.&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; CreateWrongConcreteTypeException(&lt;SPAN style="COLOR: #a31515"&gt;"value"&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; }&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; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;[index] = (TConcrete)&lt;SPAN style="COLOR: blue"&gt;value&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #endregion&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;&amp;nbsp;&amp;nbsp;&amp;nbsp; #region&lt;/SPAN&gt; ICollection&amp;lt;TAbstract&amp;gt; Members&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;void&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ICollection&lt;/SPAN&gt;&amp;lt;TAbstract&amp;gt;.Add(TAbstract item)&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; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (!(item &lt;SPAN style="COLOR: blue"&gt;is&lt;/SPAN&gt; TConcrete))&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&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; &lt;SPAN style="COLOR: blue"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;CovariantCollection&lt;/SPAN&gt;&amp;lt;TConcrete, TAbstract&amp;gt;.&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; CreateWrongConcreteTypeException(&lt;SPAN style="COLOR: #a31515"&gt;"item"&lt;/SPAN&gt;);&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; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.Add((TConcrete)item);&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;void&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ICollection&lt;/SPAN&gt;&amp;lt;TAbstract&amp;gt;.Clear()&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; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.Clear();&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;bool&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ICollection&lt;/SPAN&gt;&amp;lt;TAbstract&amp;gt;.Contains(TAbstract item)&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; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (item &lt;SPAN style="COLOR: blue"&gt;is&lt;/SPAN&gt; TConcrete)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&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; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.Contains((TConcrete)item);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&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; &lt;SPAN style="COLOR: blue"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;CovariantCollection&lt;/SPAN&gt;&amp;lt;TConcrete, TAbstract&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; CreateWrongConcreteTypeException(&lt;SPAN style="COLOR: #a31515"&gt;"item"&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;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ICollection&lt;/SPAN&gt;&amp;lt;TAbstract&amp;gt;.CopyTo(TAbstract[] array, &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; arrayIndex)&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; TConcrete[] concreteItems = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; TConcrete[array.Length];&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.CopyTo(concreteItems, arrayIndex);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; concreteItems.CopyTo(array, 0);&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;int&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ICollection&lt;/SPAN&gt;&amp;lt;TAbstract&amp;gt;.Count&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; &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;.Count; }&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;bool&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ICollection&lt;/SPAN&gt;&amp;lt;TAbstract&amp;gt;.IsReadOnly&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; &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt; { &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; ((&lt;SPAN style="COLOR: #2b91af"&gt;ICollection&lt;/SPAN&gt;&amp;lt;TConcrete&amp;gt;)&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;).IsReadOnly; }&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;bool&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ICollection&lt;/SPAN&gt;&amp;lt;TAbstract&amp;gt;.Remove(TAbstract item)&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; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (item &lt;SPAN style="COLOR: blue"&gt;is&lt;/SPAN&gt; TConcrete)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&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; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.Remove((TConcrete)item);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&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; &lt;SPAN style="COLOR: blue"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;CovariantCollection&lt;/SPAN&gt;&amp;lt;TConcrete, TAbstract&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; CreateWrongConcreteTypeException(&lt;SPAN style="COLOR: #a31515"&gt;"item"&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;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #endregion&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;&amp;nbsp;&amp;nbsp;&amp;nbsp; #region&lt;/SPAN&gt; IEnumerable&amp;lt;TAbstract&amp;gt; Members&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: #2b91af"&gt;IEnumerator&lt;/SPAN&gt;&amp;lt;TAbstract&amp;gt; &lt;SPAN style="COLOR: #2b91af"&gt;IEnumerable&lt;/SPAN&gt;&amp;lt;TAbstract&amp;gt;.GetEnumerator()&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; &lt;SPAN style="COLOR: blue"&gt;foreach&lt;/SPAN&gt; (TAbstract item &lt;SPAN style="COLOR: blue"&gt;in&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&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; &lt;SPAN style="COLOR: blue"&gt;yield&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; item;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #endregion&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Exception&lt;/SPAN&gt; CreateWrongConcreteTypeException(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; argumentName)&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; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ArgumentException&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;.Format(&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; &lt;SPAN style="COLOR: #a31515"&gt;"This collection only supports instances of {0} (or derived types)."&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; &lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(TConcrete).AssemblyQualifiedName), argumentName);&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;You will probably notice a common pattern here. In most of the members, I need to test if the input is indeed an instance of TConcrete (MyDerivedClass in the example) and throw an exception if this is not so. If the input is okay, I delegate to the implementation provided by Collection&amp;lt;T&amp;gt;. Notice that I don't need to delegate to &lt;EM&gt;base&lt;/EM&gt;, but can delegate directly to &lt;EM&gt;this&lt;/EM&gt;, since the implementation of IList&amp;lt;TAbstract&amp;gt; is an explicit imteface implementation. Collection&amp;lt;T&amp;gt; also provides the basic wrapper functionality, so I don't need to implement this manually.&lt;/P&gt;
&lt;P&gt;You may also wonder about another implementation detail: In several cases (e.g. IndexOf), I first test the type of the input using the &lt;EM&gt;is&lt;/EM&gt; keyword, and then subsequently cast to TConcrete. Since the &lt;EM&gt;is&lt;/EM&gt; keyword causes a cast, I'm actually performing the same cast twice, which isn't terribly efficient. Normally, I'd resort to the &lt;EM&gt;as&lt;/EM&gt; keyword and then test for null, but this isn't possible in this case since TConcrete may be a value type (in which case the &lt;EM&gt;as&lt;/EM&gt; keyword is illegal).&lt;/P&gt;
&lt;P&gt;It's worth noting that CovariantCollection&amp;lt;TConcrete, TAbstract&amp;gt; implements two different generic IList interfaces: IList&amp;lt;TConcrete&amp;gt; is implemented by Collection&amp;lt;TConcrete&amp;gt;, which also means that IList&amp;lt;TAbstract&amp;gt; has to be implemented explicitly, since some of the IList members (such as the indexer) only differ in return type, and .NET doesn't allow return type covariance.&lt;/P&gt;
&lt;P&gt;You may not encounter this&amp;nbsp;collection covariance conundrum&amp;nbsp;every day, but I've personally found myself in this situation enough times that I'm happy that I have this totally reusable class to solve the problem. Now you have it too.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=5186429" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ploeh/archive/tags/Software+Design/default.aspx">Software Design</category></item><item><title>User Context</title><link>http://blogs.msdn.com/ploeh/archive/2007/08/20/UserContext.aspx</link><pubDate>Mon, 20 Aug 2007 23:38:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4479757</guid><dc:creator>ploeh</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/ploeh/comments/4479757.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ploeh/commentrss.aspx?PostID=4479757</wfw:commentRss><wfw:comment>http://blogs.msdn.com/ploeh/rsscomments.aspx?PostID=4479757</wfw:comment><description>&lt;P&gt;Besides logging, one of the most common types of &lt;A class="" href="http://blogs.msdn.com/ploeh/archive/2007/07/23/AmbientContext.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2007/07/23/AmbientContext.aspx"&gt;ambient context&lt;/A&gt; is the user. Who is the user? Was the user authenticated? What is the user allowed to do?&lt;/P&gt;
&lt;P&gt;Since being able to answer these questions are such common requirements in software development, the BCL includes the IPrincipal interface that allows you to create implementations to meet that goal. In many cases, however, you need to know more about your user than just a name, authentication flag and role. You may need separate properties for first and last name, title, the user's age, gender or color preference, contact information, relationships to other users, etc.&lt;/P&gt;
&lt;P&gt;Obviously, you can define your own user object that implements IPrincipal and IIdentity while also containing all this extra information. If you do this, here's a couple of design guidelines you should consider:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;If you have any sort of &lt;A class="" href="http://blogs.msdn.com/ploeh/archive/2006/08/24/718828.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2006/08/24/718828.aspx"&gt;volatile dependency&lt;/A&gt; in your User object, you should consider making the definition of your class abstract and otherwise abstracting away the dependencies. A simple example concerns the extraction of information. Imagine that you have a database that contains the user's first and last name, as well as a user ID. When you first create the User object, you may only know the user ID, and since you expect that not all callers will ever access the FirstName and LastName properties, you may want to implement lazy loading from the database. However, you should really make those properties abstract, so that other implementations (such as a &lt;A class="" href="http://www.martinfowler.com/bliki/TestDouble.html" mce_href="http://www.martinfowler.com/bliki/TestDouble.html"&gt;test double&lt;/A&gt;)&amp;nbsp;of your User class can implement different behavior.&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;If the distinction between IPrincipal and IIdentity doesn't make a lot of sense to you, you can easily let your User class implement both interfaces.&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;Consider creating a static convenience property for accessing the underlying user-specific store (see the example below).&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Let's look at an example illustrating these guidelines. Imagine that you have a need to track and access the user's first and last name, as well as a display name:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;abstract&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;partial&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;User&lt;/SPAN&gt; : &lt;SPAN style="COLOR: #2b91af"&gt;IPrincipal&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #2b91af"&gt;IIdentity&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;protected&lt;/SPAN&gt; User()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;abstract&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; FirstName { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;abstract&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; LastName { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;abstract&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; DisplayName { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// More members...&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;The class is abstract since I want to&amp;nbsp;give&amp;nbsp;inheritors some leeway in how they implement the behavior. In a&amp;nbsp;web application, a derived class may look up the user's properties in a table, perhaps even utilizing lazy loading. An enterprise application may look up the data in Active Directory. A federated service may extract the data from a SAML token.&amp;nbsp;Unit testers will probably want to create a stub or similar.&lt;/P&gt;
&lt;P&gt;When implementing IPrincipal and IIdentity, a question that sometimes arises regards the distinction between IPrincipal and IIdentity: Which behavior and data should be implemented where? Should I create both a UserPrincipal and a UserIdentity class? In some cases, it makes sense, while in others, it doesn't. If it doesn't make sense to you in your particular case, you can collapse the implementation of both interfaces into the same class. Notice how in the declaration of the User class, I specified that it implements both IPrincipal and IIdentity. Here's the abstract implementation:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;#region&lt;/SPAN&gt; IPrincipal Members&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: #2b91af"&gt;IIdentity&lt;/SPAN&gt; Identity&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;get&lt;/SPAN&gt; { &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&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;abstract&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;bool&lt;/SPAN&gt; IsInRole(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; role);&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;#endregion&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;#region&lt;/SPAN&gt; IIdentity Members&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;abstract&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; AuthenticationType { &lt;SPAN style="COLOR: blue"&gt;get&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;abstract&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;bool&lt;/SPAN&gt; IsAuthenticated { &lt;SPAN style="COLOR: blue"&gt;get&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;string&lt;/SPAN&gt; Name&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;get&lt;/SPAN&gt; { &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.DisplayName; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&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;#endregion&lt;/SPAN&gt;&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;The most tricky aspect is linking the two interfaces to each other via the Identity property, and as you can see, that's dead simple: Just return the&amp;nbsp;object itself.&amp;nbsp;Again, I've cheated a bit&amp;nbsp;since I've just delegated most of the rest of the interface implementation to child classes, but that makes sense, since inheritors may want to implement this functionality in quite different ways: If the backing store is AD, you can basically just wrap WindowsPrincipal and WindowsIdentity. If the backing store is a database, you may want to implement IsInRole&amp;nbsp;with a (cached) stored procedure call. And once more,&amp;nbsp;unit testers will just want to use some sort of test double.&lt;/P&gt;
&lt;P&gt;Storing the User object in a user context can obviously be done manually. On Thread.CurrentPrincipal, it's as easy as this:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Thread&lt;/SPAN&gt;.CurrentPrincipal = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;FakeUser&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"Jane"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"Doe"&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Retriving the User instance, on the other hand, requires a bit more code:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;User&lt;/SPAN&gt; u = &lt;SPAN style="COLOR: #2b91af"&gt;Thread&lt;/SPAN&gt;.CurrentPrincipal &lt;SPAN style="COLOR: blue"&gt;as&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;User&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (u == &lt;SPAN style="COLOR: blue"&gt;null&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;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;InvalidOperationException&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"Boo hiss"&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: green"&gt;// Use u&lt;/SPAN&gt;&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;It's straightforward code, but tedious to have to write over and over again. At this point, many people might be tempted to&amp;nbsp;move this code to a static method on&amp;nbsp;a helper class, but object-oriented design principles recommend that you locate behavior in the class where it belongs - in this case the User class:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;User&lt;/SPAN&gt; Current&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;get&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; &lt;SPAN style="COLOR: green"&gt;/* Possibly use further abstraction here to&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: green"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;  * enable use of HttpContext or similar, as&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: green"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;  * described in previous post. */&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;User&lt;/SPAN&gt; u = &lt;SPAN style="COLOR: #2b91af"&gt;Thread&lt;/SPAN&gt;.CurrentPrincipal &lt;SPAN style="COLOR: blue"&gt;as&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;User&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (u == &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; {&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; &lt;SPAN style="COLOR: green"&gt;// Alternatively use Null Object pattern here.&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; &lt;SPAN style="COLOR: blue"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;InvalidOperationException&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; &lt;SPAN style="COLOR: #a31515"&gt;"CurrentPrincipal is not a User instance."&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&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; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; u;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;set&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; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (&lt;SPAN style="COLOR: blue"&gt;value&lt;/SPAN&gt; == &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&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; &lt;SPAN style="COLOR: blue"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ArgumentNullException&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"value"&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&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; &lt;SPAN style="COLOR: green"&gt;/* Possibly use further abstraction here to&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: green"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;  * enable use of HttpContext or similar, as&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: green"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;  * described in previous post. */&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Thread&lt;/SPAN&gt;.CurrentPrincipal = &lt;SPAN style="COLOR: blue"&gt;value&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;The static Current property is just a convenience property that encapsulates the behavior described above. In this example, I'm just using Thread.CurrentPrincipal as is, but as described in &lt;A class="" href="http://blogs.msdn.com/ploeh/archive/2007/07/28/CallContextsVsAspNet.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2007/07/28/CallContextsVsAspNet.aspx"&gt;one of my previous posts&lt;/A&gt;, this will not work in ASP.NET. For full flexibility, you could then modify the Current property to use an abstract&amp;nbsp;context container for storing and accessing the IPrincipal instance, as is also described in that post.&lt;/P&gt;
&lt;P&gt;In this implementation, I'm also throwing an InvalidOperationException if the current IPrincipal context is not a User instance. One alternative to that would be to return a &lt;A class="" href="http://en.wikipedia.org/wiki/Null_Object_pattern" mce_href="http://en.wikipedia.org/wiki/Null_Object_pattern"&gt;Null Object&lt;/A&gt;, in this case perhaps something like this:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;internal&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;NullUser&lt;/SPAN&gt; : &lt;SPAN style="COLOR: #2b91af"&gt;User&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;internal&lt;/SPAN&gt; NullUser()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; : &lt;SPAN style="COLOR: blue"&gt;base&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; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;override&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; FirstName&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; &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;string&lt;/SPAN&gt;.Empty; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;override&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; LastName&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; &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;string&lt;/SPAN&gt;.Empty; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;override&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; DisplayName&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; &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt; { &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: #a31515"&gt;"Unknown"&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;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;override&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;bool&lt;/SPAN&gt; IsInRole(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; role)&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; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;false&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;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;override&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; AuthenticationType&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; &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt; { &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: #a31515"&gt;"Null"&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;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;override&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;bool&lt;/SPAN&gt; IsAuthenticated&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; &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;false&lt;/SPAN&gt;; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;When using&amp;nbsp;a Null Object, however, you should consider whether hiding another IPrincipal instance is the desired behavior.&amp;nbsp;If the current principal is null, it's pretty easy, but it's more difficult to decide on the correct behavior when the current instance implements IPrincipal, but doesn't derive from User. Implicitly suppressing data in this way may lead to logical bugs or unintended behavior, which is why I chose to throw an exception in the above example.&lt;/P&gt;
&lt;P&gt;The static Current property provides a convenient and safe way to code against the current user context, since it allows you to write code like this:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; firstName = &lt;SPAN style="COLOR: #2b91af"&gt;User&lt;/SPAN&gt;.Current.FirstName;&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Writing to the User instance is just as easy, if you have writable properties.&lt;/P&gt;
&lt;P&gt;A custom User class can evidently be much more complex than the above example, containing complex behavior and relationships to other users, etc. In any case, the general approach outlined here provides a few guidelines for effectively modeling your application's user context.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=4479757" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ploeh/archive/tags/Dependency+Injection/default.aspx">Dependency Injection</category><category domain="http://blogs.msdn.com/ploeh/archive/tags/Software+Design/default.aspx">Software Design</category></item><item><title>Call Contexts vs. ASP.NET</title><link>http://blogs.msdn.com/ploeh/archive/2007/07/28/CallContextsVsAspNet.aspx</link><pubDate>Sat, 28 Jul 2007 21:56:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4103892</guid><dc:creator>ploeh</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/ploeh/comments/4103892.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ploeh/commentrss.aspx?PostID=4103892</wfw:commentRss><wfw:comment>http://blogs.msdn.com/ploeh/rsscomments.aspx?PostID=4103892</wfw:comment><description>&lt;P&gt;In my former post on &lt;A class="" href="http://blogs.msdn.com/ploeh/archive/2007/07/23/AmbientContext.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2007/07/23/AmbientContext.aspx"&gt;Ambient Contexts&lt;/A&gt;, I described how you can use CallContext or Thread Local Storage (TLS) to store a context that is specific to the current context of a call. As it turns out, ASP.NET uses a &lt;A class="" href="http://piers7.blogspot.com/2005/11/threadstatic-callcontext-and_02.html" mce_href="http://piers7.blogspot.com/2005/11/threadstatic-callcontext-and_02.html"&gt;threading model that&amp;nbsp;disables this approach&lt;/A&gt;, since you are not guaranteed that a single request will be entirely executed within a single thread. That's not particularly nice of the ASP.NET platform (to put it politely), but that's the hand we've been dealt. &lt;A class="" href="http://www.lhotka.net/weblog/ASPNETThreadSwitching.aspx" mce_href="http://www.lhotka.net/weblog/ASPNETThreadSwitching.aspx"&gt;As described by Rockford Lhotka&lt;/A&gt;, your first approach should be to use HttpContext.Items when your code runs within a web application, and the CallContext or TLS when outside of ASP.NET.&lt;/P&gt;
&lt;P&gt;However, even if you create a wrapper around your storage mechanism, you are still going to carry around a reference to System.Web, which may seem... weird... if you are writing a WPF application or Windows Service or whatever. Besides, this approach is not extensible, since you can't&amp;nbsp;subsequently decide to use yet another context (perhaps you'd want to use WCF's OperationContext). If you've been working with software design for some years, it shouldn't be surprising that the answer is to add&amp;nbsp;yet another&amp;nbsp;level of indirection.&lt;/P&gt;
&lt;P&gt;The basic idea is that instead of simply checking whether HttpContext.Current is null or not, you define an abstract context container that can store your custom context in whatever call context is available.&lt;/P&gt;
&lt;P&gt;To illustrate this approach, I'll extend my &lt;A class="" href="http://blogs.msdn.com/ploeh/archive/2007/07/23/AmbientContext.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2007/07/23/AmbientContext.aspx"&gt;former example&lt;/A&gt; to store the context in an abstract container defined&amp;nbsp;by the&amp;nbsp;FeatureContextContainer class:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;abstract&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;FeatureContextContainer&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;protected&lt;/SPAN&gt; FeatureContextContainer()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;abstract&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;FeatureContext&lt;/SPAN&gt; Current { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Since I don't want to carry around any (potentially) uneccessary references to System.Web or other implementation-specific context implementations, I want to be able to define concrete FeatureContextContainers in my application configuration file (remember that&amp;nbsp;you should also consider including an &lt;A class="" href="http://blogs.msdn.com/ploeh/archive/2007/07/11/ImperativeConfiguration.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2007/07/11/ImperativeConfiguration.aspx"&gt;imperative configuration&lt;/A&gt; option). Such a configuration might look something like this:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;configuration&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp; &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;configSections&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp; &amp;nbsp; &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;section&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;featureContext&lt;/SPAN&gt;"&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;  &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;Ploeh.Samples.FeatureControl.FeatureContextConfigurationSection, Ploeh.Samples.FeatureControl&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt; /&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp; &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;configSections&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp; &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;featureContext&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;containerType&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;Ploeh.Samples.AspNetContext.HttpContextContainer, Ploeh.Samples.AspNetContext&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt; /&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;configuration&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;In this case, the HttpContextContainer class would implement FeatureContextContainer by reading and writing the context to and from HttpContext.Items. The nice thing about this approach is that while&amp;nbsp;the AspNetContext assembly has a dependency on System.Web, the rest of your application (including FeatureContext itself) doesn't.&lt;/P&gt;
&lt;P&gt;The FeatureContext class itself must be modified to use whatever context container is provided to it:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;Serializable&lt;/SPAN&gt;]&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;abstract&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;FeatureContext&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;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;FeatureContextContainer&lt;/SPAN&gt; container_ =&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;FeatureContextConfigurationSection&lt;/SPAN&gt;.GetConfigurationSection().GetContextContainer();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;protected&lt;/SPAN&gt; FeatureContext()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;FeatureContext&lt;/SPAN&gt; Current&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; &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt; { &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;FeatureContext&lt;/SPAN&gt;.container_.Current; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt; { &lt;SPAN style="COLOR: #2b91af"&gt;FeatureContext&lt;/SPAN&gt;.container_.Current = &lt;SPAN style="COLOR: blue"&gt;value&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;&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;abstract&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; Demand(&lt;SPAN style="COLOR: #2b91af"&gt;FeatureLevel&lt;/SPAN&gt; requestedLevel);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;
&lt;P&gt;Instead of directly reading and writing the context to and from the CallContext as before, FeatureContext now delegates these operations to the configured FeatureContextContainer.&lt;/P&gt;
&lt;P&gt;While it may seem that this approach now requires an application configuration file, this is actually not the case, since I implement FeatureContextConfigurationSection.GetConfigurationSection in this way:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;FeatureContextConfigurationSection&lt;/SPAN&gt; GetConfigurationSection()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;FeatureContextConfigurationSection&lt;/SPAN&gt; config = &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;ConfigurationManager&lt;/SPAN&gt;.GetSection(&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;FeatureContextConfigurationSection&lt;/SPAN&gt;.ConfigurationName)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;as&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;FeatureContextConfigurationSection&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (config != &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; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; config;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;DefaultFeatureContextConfigurationSection&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;
&lt;P&gt;Notice that if the configuration section isn't found in the application configuration file (or if no such file exists at all), I return a DefaultFeatureContextConfigurationSection instead. This internal class simply overrides the virtual GetContextContainer method:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;internal&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;DefaultFeatureContextConfigurationSection&lt;/SPAN&gt; :&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;FeatureContextConfigurationSection&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;internal&lt;/SPAN&gt; DefaultFeatureContextConfigurationSection()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; : &lt;SPAN style="COLOR: blue"&gt;base&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; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;override&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;FeatureContextContainer&lt;/SPAN&gt; GetContextContainer()&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; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;CallContextContainer&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Instead of using a configured type name to create an instance of that type, this implementation simply returns an instance of the CallContextContainer class:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;internal&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;CallContextContainer&lt;/SPAN&gt; : &lt;SPAN style="COLOR: #2b91af"&gt;FeatureContextContainer&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;const&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; contextSlotName_ = &lt;SPAN style="COLOR: #a31515"&gt;"FeatureContext"&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;internal&lt;/SPAN&gt; CallContextContainer()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; : &lt;SPAN style="COLOR: blue"&gt;base&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; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;override&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;FeatureContext&lt;/SPAN&gt; Current&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; &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&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; &lt;SPAN style="COLOR: #2b91af"&gt;FeatureContext&lt;/SPAN&gt; ctx = &lt;SPAN style="COLOR: #2b91af"&gt;CallContext&lt;/SPAN&gt;.LogicalGetData(&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; &lt;SPAN style="COLOR: #2b91af"&gt;CallContextContainer&lt;/SPAN&gt;.contextSlotName_) &lt;SPAN style="COLOR: blue"&gt;as&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;FeatureContext&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; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (ctx == &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&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; ctx = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;BasicContext&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; &lt;SPAN style="COLOR: #2b91af"&gt;CallContext&lt;/SPAN&gt;.LogicalSetData(&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;CallContextContainer&lt;/SPAN&gt;.contextSlotName_, ctx);&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; }&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; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; ctx;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&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; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&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; &lt;SPAN style="COLOR: #2b91af"&gt;CallContext&lt;/SPAN&gt;.LogicalSetData(&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; &lt;SPAN style="COLOR: #2b91af"&gt;CallContextContainer&lt;/SPAN&gt;.contextSlotName_, &lt;SPAN style="COLOR: blue"&gt;value&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&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;!--EndFragment--&gt;
&lt;P&gt;If you recall the code from my former post, this is simply the original implementation of FeatureContext moved to a FeatureContextContainer implementation. By this, we have now come full circle: The default behavior of FeatureContextContainer is the same as described in my former post, and I have introduced no breaking changes, but now the implementation is extensible, which allows you to reuse this ambient context in an ASP.NET application.&lt;/P&gt;
&lt;P&gt;In a unit testing scenario, CallContext works just fine, so you don't need to replace the default implementation in that case. This means that even if you would want to use FeatureContext in an ASP.NET application, you'd still write all your unit tests against the default implementation, and then, when you configure the entire application, you set it up with an HttpContextContainer. The HttpContextContainer class, on the other hand, is one of those software infrastructure types that you can't unit test, so if you really want to test it, you will need an integration test. On the other hand, the implementation is so simple that this will probably not be necessary.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=4103892" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ploeh/archive/tags/Dependency+Injection/default.aspx">Dependency Injection</category><category domain="http://blogs.msdn.com/ploeh/archive/tags/Software+Design/default.aspx">Software Design</category></item><item><title>Ambient Context</title><link>http://blogs.msdn.com/ploeh/archive/2007/07/23/AmbientContext.aspx</link><pubDate>Tue, 24 Jul 2007 00:06:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4002485</guid><dc:creator>ploeh</dc:creator><slash:comments>11</slash:comments><comments>http://blogs.msdn.com/ploeh/comments/4002485.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ploeh/commentrss.aspx?PostID=4002485</wfw:commentRss><wfw:comment>http://blogs.msdn.com/ploeh/rsscomments.aspx?PostID=4002485</wfw:comment><description>&lt;P&gt;These days, I'm becoming increasingly enamored with the idea of implementing cross-cutting concerns using Thread Local Storage (TLS) or the current call context. For the most&amp;nbsp;typical aspects of software, such as security and logging, the .NET framework already takes this approach:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Security can be handled by reading or writing to Thread.CurrentPrincipal&lt;/LI&gt;
&lt;LI&gt;Logging can be handled by using the methods of the Trace class. You configure the Trace class by adding or removing TraceListerners.&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;In both cases, you set up the context at the application's entry point (either imperatively or, in the case of logging, in the application's configuration), which makes it implicitly&amp;nbsp;available for all other code in the call stack. Even if you are writing code in the data access layer (which is usually quite deep in the call stack), you can still access and use this ambient context. The nice thing about this approach is that it's just there, accessible by a static property or method on a well-defined class (i.e. Thread or Trace). This means that even though you might need to be able to address your cross-cutting concern in any layer in your application, you don't need to pass it around as a parameter value in every single method you implement.&lt;/P&gt;
&lt;P&gt;Even though it's not passed to you explicitly as a parameter, you are allowed to assume that it's there; it's the responsibility of the hosting application to set up the ambient context in the correct way.&lt;/P&gt;
&lt;P&gt;Since both IPrincipal and TraceListener are abstractions (one is an interface and the other an abstract class), these implementations offer full substitutability, so you can easily create &lt;A class="" href="http://www.martinfowler.com/bliki/TestDouble.html" mce_href="http://www.martinfowler.com/bliki/TestDouble.html"&gt;test doubles&lt;/A&gt; of them when unit testing.&lt;/P&gt;
&lt;P&gt;Obviously, you can just use Thread.CurrentPrincipal and the Trace class for security and logging, but if you have your own aspect to implement, you can use a similar approach. As always, I prefer to illustrate how to do this with an example. In order to avoid the &lt;A class="" href="http://bloggingabout.net/blogs/olaf/archive/2007/04/04/aspect-oriented-programming-and-the-usual-suspects.aspx" mce_href="http://bloggingabout.net/blogs/olaf/archive/2007/04/04/aspect-oriented-programming-and-the-usual-suspects.aspx"&gt;usual suspects&lt;/A&gt; of security and logging, consider the following scenario:&lt;/P&gt;
&lt;P&gt;We are building an application where you need to be able to control which features are available to the user at any given time. This is not a question of role-based security, but rather a decision which may be made based on licensing etc. One scenario may be a trial edition with certain features disabled (but where you can upgrade the installation to a full version without reinstalling). A more advanced scenario may be a SmartClient application (perhaps in a &lt;A class="" href="http://msdn2.microsoft.com/en-us/architecture/aa699384.aspx" mce_href="http://msdn2.microsoft.com/en-us/architecture/aa699384.aspx"&gt;SaaS&lt;/A&gt; scenario)&amp;nbsp;where the user can pay to upgrade the application's feature level for a limited time.&lt;/P&gt;
&lt;P&gt;To keep things simple, we'll assume that we only need three levels of feature access:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;enum&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;FeatureLevel&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; Basic = 0,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Silver,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Gold&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;
&lt;P&gt;Since we must allow developers to create an implementation where a level of access is only available for a limited time, the API should allow a great deal of flexibility. With the pseudo-requirements set forth so far, our goal could be to enable our fellow developers to write code akin to this:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;FeatureContext&lt;/SPAN&gt;.Current.Demand(&lt;SPAN style="COLOR: #2b91af"&gt;FeatureLevel&lt;/SPAN&gt;.Silver);&lt;/PRE&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;
&lt;P&gt;The Demand method simply asserts that the requested feature level is currently enabled, and otherwise throws an exception. The FeatureContext class in itself is an abstract class:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;Serializable&lt;/SPAN&gt;]&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;abstract&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;FeatureContext&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;const&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; contextSlotName_ = &lt;SPAN style="COLOR: #a31515"&gt;"FeatureContext"&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;protected&lt;/SPAN&gt; FeatureContext()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;FeatureContext&lt;/SPAN&gt; Current&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; &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&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; &lt;SPAN style="COLOR: #2b91af"&gt;FeatureContext&lt;/SPAN&gt; ctx = &lt;SPAN style="COLOR: #2b91af"&gt;CallContext&lt;/SPAN&gt;.LogicalGetData(&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; &lt;SPAN style="COLOR: #2b91af"&gt;FeatureContext&lt;/SPAN&gt;.contextSlotName_) &lt;SPAN style="COLOR: blue"&gt;as&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;FeatureContext&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; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (ctx == &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&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; ctx = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;BasicContext&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; &lt;SPAN style="COLOR: #2b91af"&gt;CallContext&lt;/SPAN&gt;.LogicalSetData(&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;FeatureContext&lt;/SPAN&gt;.contextSlotName_, ctx);&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; }&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; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; ctx;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&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; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&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; &lt;SPAN style="COLOR: #2b91af"&gt;CallContext&lt;/SPAN&gt;.LogicalSetData(&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; &lt;SPAN style="COLOR: #2b91af"&gt;FeatureContext&lt;/SPAN&gt;.contextSlotName_, &lt;SPAN style="COLOR: blue"&gt;value&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&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;abstract&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; Demand(&lt;SPAN style="COLOR: #2b91af"&gt;FeatureLevel&lt;/SPAN&gt; requestedLevel);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;
&lt;P&gt;There are two noteworthy details in this code: The first is the abstract Demand method that needs to be implemented by a derived class (I'll get back to that).&lt;/P&gt;
&lt;P&gt;The second is the static Current property. Since it&amp;nbsp;allows any client to read and write an instance of the abstract FeatureContext, substitutability is ensured. It also ensures that you can implement the Demand method in as simple or complex fashion as you need, and put this implementation on the ambient context. To keep things simple, I've not implemented the property in a thread-safe manner, which (obviously) you should do in a real production&amp;nbsp;implementation.&lt;/P&gt;
&lt;P&gt;In this implementation,&amp;nbsp;I store the the FeatureContext in the CallContext; I could also have used TLS by using Thread.SetData, but there's a difference: Using TLS, the context only exists on the current thread; if you spawn a new thread, you must manually copy the context to the new thread if you want it to be available there as well. This happens automatically when you use the CallContext, but to store and retrieve an object via the CallContext, the class must be serializable, which is why FeatureContext is decorated with the Serializable attribute. Any derived class must also be serializable.&lt;/P&gt;
&lt;P&gt;If no current context is not already set, it defaults to BasicContext:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;Serializable&lt;/SPAN&gt;]&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;BasicContext&lt;/SPAN&gt; : &lt;SPAN style="COLOR: #2b91af"&gt;FeatureContext&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; BasicContext()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; : &lt;SPAN style="COLOR: blue"&gt;base&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; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;override&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; Demand(&lt;SPAN style="COLOR: #2b91af"&gt;FeatureLevel&lt;/SPAN&gt; requestedLevel)&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; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (requestedLevel &amp;gt; &lt;SPAN style="COLOR: #2b91af"&gt;FeatureLevel&lt;/SPAN&gt;.Basic)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&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; &lt;SPAN style="COLOR: blue"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;FeatureUseDeniedException&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&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;!--EndFragment--&gt;
&lt;P&gt;This implementation only allows basic functionality. If the requested feature level is higher than Basic, a FeatureUseDeniedException is thrown. Notice the Serializable attribute, which allows an instance of the class to be stored in the CallContext.&lt;/P&gt;
&lt;P&gt;You can implement SilverContext and GoldContext classes in similar ways, so when you want to enable Gold functionality indefinitely, you'd do something like this:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;FeatureContext&lt;/SPAN&gt;.Current = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;GoldContext&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;This will enable Gold funtionality until you explicitly change the FeatureContext to something else. If you want to enable a scenario where there's a time limit, you could create&amp;nbsp;an expiring&amp;nbsp;implementation of FeatureContext:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;Serializable&lt;/SPAN&gt;]&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ExpiringGoldContext&lt;/SPAN&gt; : &lt;SPAN style="COLOR: #2b91af"&gt;FeatureContext&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;DateTime&lt;/SPAN&gt; timeLimit_;&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; ExpiringGoldContext(&lt;SPAN style="COLOR: #2b91af"&gt;TimeSpan&lt;/SPAN&gt; allottedTime)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; : &lt;SPAN style="COLOR: blue"&gt;base&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; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.timeLimit_ = &lt;SPAN style="COLOR: #2b91af"&gt;DateTime&lt;/SPAN&gt;.Now + allottedTime;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;override&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; Demand(&lt;SPAN style="COLOR: #2b91af"&gt;FeatureLevel&lt;/SPAN&gt; requestedLevel)&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; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;DateTime&lt;/SPAN&gt;.Now &amp;gt; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.timeLimit_)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&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; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;BasicContext&lt;/SPAN&gt;().Demand(requestedLevel);&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; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&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; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;GoldContext&lt;/SPAN&gt;().Demand(requestedLevel);&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;!--EndFragment--&gt;
&lt;P&gt;When you set the current FeatureContext to ExpiringGoldContext, it will allow Gold features to be used until the time limit expires. Even when the time limit expires, the current instance remains, but its behavior changes to only allow Basic functionality. Obviously, you could optimize the implementation of ExpiringGoldContext by defining static readonly instances of BasicContext and GoldContext, but to keep things simple for the example, I just create new instances of these classes when I want to delegate implementation to them.&lt;/P&gt;
&lt;P&gt;Whenever your need to ensure that a member allows execution only if a certain functionality level is granted, you can demand the level as a guard clause in your code:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; DoSilverStuff()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;FeatureContext&lt;/SPAN&gt;.Current.Demand(&lt;SPAN style="COLOR: #2b91af"&gt;FeatureLevel&lt;/SPAN&gt;.Silver);&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: green"&gt;// Implementation here&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;To come full circle to aspect-oriented development, such a use of ambient context is an&amp;nbsp;obvious candidate for the Enterprise Library Policy Injection Application Block, which would then allow declarative use of the ambient context by decorating your members with attributes (similar to PrincipalPermissionAttribute), but I'll leave that as an exercise for the interested reader (or, perhaps,&amp;nbsp;a later post).&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=4002485" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ploeh/archive/tags/Dependency+Injection/default.aspx">Dependency Injection</category><category domain="http://blogs.msdn.com/ploeh/archive/tags/Software+Design/default.aspx">Software Design</category></item><item><title>Imperative Configuration</title><link>http://blogs.msdn.com/ploeh/archive/2007/07/11/ImperativeConfiguration.aspx</link><pubDate>Wed, 11 Jul 2007 23:56:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3758713</guid><dc:creator>ploeh</dc:creator><slash:comments>7</slash:comments><comments>http://blogs.msdn.com/ploeh/comments/3758713.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ploeh/commentrss.aspx?PostID=3758713</wfw:commentRss><wfw:comment>http://blogs.msdn.com/ploeh/rsscomments.aspx?PostID=3758713</wfw:comment><description>&lt;P&gt;How can I unit test my application with different configuration settings? A couple of months back, a reader asked me this question, and while I provided an immediate response via email, I&amp;nbsp;think that the question deserves a post of its own.&lt;/P&gt;
&lt;P&gt;In .NET, using XML-based application configuration is easy and built into the framework, so it's common for applications or libraries to depend on a configuration file. The configuration system in .NET is essentially read-only. While you can write to a config file, this will not reload the configuration settings for a running application. To reload the configuration settings, the AppDomain must be restarted.&lt;/P&gt;
&lt;P&gt;In unit testing, this is not particularly practical, since a test suite executes all test cases in a single AppDomain. As a consequence, once a configuration section is read from the configuration file, it cannot be changed by changing the declarative (that is: XML)&amp;nbsp;configuration. This is a challenge if you need to test your code with different, mutually exclusive&amp;nbsp;configuration settings.&lt;/P&gt;
&lt;P&gt;This is&amp;nbsp;another good reason for providing an imperative way to define configuration settings for your code. Essentially, file-based configuration should be treated as a fall-back mechanism that only applies if the configuration isn't supplied imperatively.&lt;/P&gt;
&lt;P&gt;As a simplistic example, imagine that you are developing a library of Newtonian mechanics to calculate projectile trajectories. Your first take on the Trajectory class looks something like this:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Trajectory&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;const&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;double&lt;/SPAN&gt; gravitationalAcceleration_ = 9.80665;&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;double&lt;/SPAN&gt; range_;&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; Trajectory(&lt;SPAN style="COLOR: blue"&gt;double&lt;/SPAN&gt; angleInDegrees,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;double&lt;/SPAN&gt; initialVelocity)&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; &lt;SPAN style="COLOR: blue"&gt;double&lt;/SPAN&gt; angleInRadians = angleInDegrees * &lt;SPAN style="COLOR: #2b91af"&gt;Math&lt;/SPAN&gt;.PI / 180;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.range_ =&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; (&lt;SPAN style="COLOR: #2b91af"&gt;Math&lt;/SPAN&gt;.Pow(initialVelocity, 2) * &lt;SPAN style="COLOR: #2b91af"&gt;Math&lt;/SPAN&gt;.Sin(angleInRadians)) /&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; &lt;SPAN style="COLOR: #2b91af"&gt;Trajectory&lt;/SPAN&gt;.gravitationalAcceleration_;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;double&lt;/SPAN&gt; Range&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; &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;.range_; }&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;!--EndFragment--&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;
&lt;P&gt;For simplicity's sake, the Trajectory class only exposes the Range property,&amp;nbsp;which provides the maximum range given the firing angle and initial velocity of the projectile. The formula used in the constructor is the formula for calculating the range, but the details of that are not terribly important besides the point that you need to know the gravitational acceleration. The constant value of 9.80665 is Earth's gravity.&lt;/P&gt;
&lt;P&gt;Now, a new requirement occurs: Some customers on Mars want to use your library to calculate trajectories, but obviously while using Mars' gravitational acceleration (which is 3.69). For that reason, you decide to move the gravitational acceleration value to a custom configuration section.&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;configuration&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp; &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;configSections&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp; &amp;nbsp; &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;section&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;newtonianConfiguration&lt;/SPAN&gt;"&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;  &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;Ploeh.Samples.Newtonian.NewtonianConfigurationSection, Ploeh.Samples.Newtonian&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt; /&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp; &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;configSections&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp; &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;newtonianConfiguration&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;gravitationalConstant&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;9.80665&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt; /&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;configuration&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;In this example, the custom configuration section is obviously very simple, but&amp;nbsp;in a more realistic scenario, it&amp;nbsp;would be much more complex. Here, we only have the single gravitationalConstant attribute, which is set to Earth's gravity.&lt;/P&gt;
&lt;P&gt;Instead of using a constant, the Trajectory constructor must now read the gravitational acceleration from the application configuration.&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; Trajectory(&lt;SPAN style="COLOR: blue"&gt;double&lt;/SPAN&gt; angleInDegrees,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;double&lt;/SPAN&gt; initialVelocity)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;NewtonianConfigurationSection&lt;/SPAN&gt; config =&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;ConfigurationManager&lt;/SPAN&gt;.GetSection(&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;NewtonianConfigurationSection&lt;/SPAN&gt;.ConfigurationName)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;as&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;NewtonianConfigurationSection&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (config == &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; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;InvalidOperationException&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"No configuration"&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;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;double&lt;/SPAN&gt; angleInRadians = angleInDegrees * &lt;SPAN style="COLOR: #2b91af"&gt;Math&lt;/SPAN&gt;.PI / 180;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.range_ =&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; (&lt;SPAN style="COLOR: #2b91af"&gt;Math&lt;/SPAN&gt;.Pow(initialVelocity, 2) * &lt;SPAN style="COLOR: #2b91af"&gt;Math&lt;/SPAN&gt;.Sin(angleInRadians)) /&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; config.GravitationalConstant;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;
&lt;P&gt;The modified constructor now loads the custom configuration section from the configuration file and throws an exception if it's not found. Otherwise, it uses the gravitational acceleration from the configuration section to calculate the range.&lt;/P&gt;
&lt;P&gt;This ought to effectively address the need to support other gravities, since the Mars customers can now configure their applications to use Mars' gravitational acceleration. The main problem that's left now is that you can't really unit test the Trajectory class with both Earth's and Mars' gravity.&lt;/P&gt;
&lt;P&gt;While you can add an app.config file to your unit test project, you can only add one. Once the configuration is read from the configuration file, it's cached and never refreshed for the lifetime of the AppDomain, so you can't change the configuration from test case to test case.&lt;/P&gt;
&lt;P&gt;In the very simple example,&amp;nbsp;one solution could be to create two different&amp;nbsp;unit test projects - one with Earth gravity, and one with Mars gravity, but if you have a more complex configuration schema with more possible&amp;nbsp;permutations, this becomes impractical very fast.&lt;/P&gt;
&lt;P&gt;The best solution is to provide an imperative override to the configuration. This is easily done in the example:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Trajectory&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;double&lt;/SPAN&gt; range_;&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; Trajectory(&lt;SPAN style="COLOR: blue"&gt;double&lt;/SPAN&gt; angleInDegrees,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;double&lt;/SPAN&gt; initialVelocity)&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; &lt;SPAN style="COLOR: #2b91af"&gt;NewtonianConfigurationSection&lt;/SPAN&gt; config =&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; &lt;SPAN style="COLOR: #2b91af"&gt;ConfigurationManager&lt;/SPAN&gt;.GetSection(&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; &lt;SPAN style="COLOR: #2b91af"&gt;NewtonianConfigurationSection&lt;/SPAN&gt;.ConfigurationName)&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; &lt;SPAN style="COLOR: blue"&gt;as&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;NewtonianConfigurationSection&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (config == &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; {&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; &lt;SPAN style="COLOR: blue"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;InvalidOperationException&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"No configuration"&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&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; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.Initialize(angleInDegrees,&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; initialVelocity, config.GravitationalConstant);&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; Trajectory(&lt;SPAN style="COLOR: blue"&gt;double&lt;/SPAN&gt; angleInDegrees,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;double&lt;/SPAN&gt; initialVelocity, &lt;SPAN style="COLOR: blue"&gt;double&lt;/SPAN&gt; gravitationalAcceleration)&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; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.Initialize(angleInDegrees, initialVelocity,&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; gravitationalAcceleration);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;double&lt;/SPAN&gt; Range&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; &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;.range_; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; Initialize(&lt;SPAN style="COLOR: blue"&gt;double&lt;/SPAN&gt; angleInDegrees,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;double&lt;/SPAN&gt; initialVelocity, &lt;SPAN style="COLOR: blue"&gt;double&lt;/SPAN&gt; gravitationalAcceleration)&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; &lt;SPAN style="COLOR: blue"&gt;double&lt;/SPAN&gt; angleInRadians = angleInDegrees * &lt;SPAN style="COLOR: #2b91af"&gt;Math&lt;/SPAN&gt;.PI / 180;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.range_ =&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; (&lt;SPAN style="COLOR: #2b91af"&gt;Math&lt;/SPAN&gt;.Pow(initialVelocity, 2) * &lt;SPAN style="COLOR: #2b91af"&gt;Math&lt;/SPAN&gt;.Sin(angleInRadians)) /&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; gravitationalAcceleration;&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 range calculation has been moved to the private Initialize method, and an overloaded constructor that takes the gravitational acceleration as a parameter has been added to the Trajectory class. This makes it very simple to test range calculations for both Earth and Mars in the same test suite:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;TestMethod&lt;/SPAN&gt;]&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; UseEarthTrajectory()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Trajectory&lt;/SPAN&gt; t = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Trajectory&lt;/SPAN&gt;(45, 250, 9.80665);&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: #2b91af"&gt;Assert&lt;/SPAN&gt;.AreEqual(4506.5515567659924923447632608029, t.Range, 0.001);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;TestMethod&lt;/SPAN&gt;]&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; UseMarsTrajectory()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Trajectory&lt;/SPAN&gt; t = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Trajectory&lt;/SPAN&gt;(45, 250, 3.69);&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: #2b91af"&gt;Assert&lt;/SPAN&gt;.AreEqual(11976.740873755886253401835401505, t.Range, 0.001);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;In a more realistic scenario with a complex configuration schema, the overloaded constructor should take a parameter representing the entire configuration hierarchy instead of just a value type as the gravitational constant from the example.&lt;/P&gt;
&lt;P&gt;Variations where you supply the configuration imperatively by setting a property or calling a method just after you've created your object, but before you begin using it, are valid alternatives in some cases.&lt;/P&gt;
&lt;P&gt;As an example, the WCF ServiceHost uses this approach, where you can either use the class without further code, in which case it will use the configuration from the configuration file, or you can add bindings and other configuration settings imperatively (with the AddServiceEndpoint method and other members).&lt;/P&gt;
&lt;P&gt;Designing your component with imperative configuration as an option not only lets you unit test different configuration scenarios much easier, but also provides more flexibility for any client consuming your code.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=3758713" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ploeh/archive/tags/Unit+Testing/default.aspx">Unit Testing</category><category domain="http://blogs.msdn.com/ploeh/archive/tags/Software+Design/default.aspx">Software Design</category></item><item><title>Using Decorators For Inter-Layer Communication</title><link>http://blogs.msdn.com/ploeh/archive/2007/06/18/UsingDecoratorsForInterLayerCommunication.aspx</link><pubDate>Mon, 18 Jun 2007 22:33:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3314348</guid><dc:creator>ploeh</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/ploeh/comments/3314348.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ploeh/commentrss.aspx?PostID=3314348</wfw:commentRss><wfw:comment>http://blogs.msdn.com/ploeh/rsscomments.aspx?PostID=3314348</wfw:comment><description>&lt;P&gt;While I hope that &lt;A class="" href="http://blogs.msdn.com/ploeh/archive/2007/06/18/WhatIsAnEntityAnyway.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2007/06/18/WhatIsAnEntityAnyway.aspx"&gt;my&amp;nbsp;previous post&lt;/A&gt; made it clear that &lt;A class="" href="http://www.martinfowler.com/eaaCatalog/dataTransferObject.html" mce_href="http://www.martinfowler.com/eaaCatalog/dataTransferObject.html"&gt;Data Transfer Objects&lt;/A&gt; are not my first choice for&amp;nbsp;transferring complex data between layers, I still owe my faithful reader(s) an outline of a better alternative.&lt;/P&gt;
&lt;P&gt;One option is to define an abstraction&amp;nbsp;(interface or base class) and pass objects implementing the abstraction between layers, using the Decorator design pattern to transform or enrich the implementation along the way. While I'm not saying this is the only, or even the best way, this approach at least has the benefit that it saves you from writing a lot of transformation and mapping code if your layer hierarchy is deep and you perform a fair amount of content enrichment along the way.&lt;/P&gt;
&lt;P&gt;Using the same scenario as my former post as a scenario,&amp;nbsp;we start out by&amp;nbsp;defining an abstract Product class (it could also have been an interface):&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;abstract&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Product&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;protected&lt;/SPAN&gt; Product()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;abstract&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; ProductId { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;abstract&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; Name { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;abstract&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;decimal&lt;/SPAN&gt; ListPrice { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;abstract&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;decimal&lt;/SPAN&gt; Discount { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;abstract&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; InventoryCount { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;One thing I aleady like better than the DTO option is that now I can model the Product with a read-only ProductId property. Creating a root Product instance in the data access layer is easy:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Product&lt;/SPAN&gt; ReadProduct(&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; productId)&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;using&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;IDataReader&lt;/SPAN&gt; r = &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.GetProductReader(productId))&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; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (!r.Read())&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&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; &lt;SPAN style="COLOR: blue"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ArgumentException&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"No such product."&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"productId"&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&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; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;DataProduct&lt;/SPAN&gt;(r);&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;!--EndFragment--&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;
&lt;P&gt;The&amp;nbsp;DataProduct class simply derives from Product. The constructor&amp;nbsp;reads the data off the data reader and into private fields that backs the properties of the class:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;internal&lt;/SPAN&gt; DataProduct(&lt;SPAN style="COLOR: #2b91af"&gt;IDataReader&lt;/SPAN&gt; productReader)&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;this&lt;/SPAN&gt;.productId_ = (&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;)productReader[&lt;SPAN style="COLOR: #a31515"&gt;"ProductId"&lt;/SPAN&gt;];&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.name_ = (&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;)productReader[&lt;SPAN style="COLOR: #a31515"&gt;"Name"&lt;/SPAN&gt;];&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.listPrice_ = (&lt;SPAN style="COLOR: blue"&gt;decimal&lt;/SPAN&gt;)productReader[&lt;SPAN style="COLOR: #a31515"&gt;"ListPrice"&lt;/SPAN&gt;];&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.discount_ = (&lt;SPAN style="COLOR: blue"&gt;decimal&lt;/SPAN&gt;)productReader[&lt;SPAN style="COLOR: #a31515"&gt;"Discount"&lt;/SPAN&gt;];&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.inventoryCount_ = (&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;)productReader[&lt;SPAN style="COLOR: #a31515"&gt;"InventoryCount"&lt;/SPAN&gt;];&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;The DataProduct class is an internal class created and returned by the data access component. It constitutes the root of a potential Decorator hierarchy.&amp;nbsp;As the Product passes up through application layers, we can add more features in each layer, and instead of writing&amp;nbsp;cloning or wrapper code each time, we can define a single, basic Decorator:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;abstract&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ProductDecorator&lt;/SPAN&gt; : &lt;SPAN style="COLOR: #2b91af"&gt;Product&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Product&lt;/SPAN&gt; innerProduct_;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;protected&lt;/SPAN&gt; ProductDecorator(&lt;SPAN style="COLOR: #2b91af"&gt;Product&lt;/SPAN&gt; p)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; : &lt;SPAN style="COLOR: blue"&gt;base&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; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.innerProduct_ = p;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;override&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; ProductId&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; &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;.innerProduct_.ProductId; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;override&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; Name&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; &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;.innerProduct_.Name; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt; { &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.innerProduct_.Name = &lt;SPAN style="COLOR: blue"&gt;value&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;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;override&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;decimal&lt;/SPAN&gt; ListPrice&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; &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;.innerProduct_.ListPrice; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt; { &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.innerProduct_.ListPrice = &lt;SPAN style="COLOR: blue"&gt;value&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;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;override&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;decimal&lt;/SPAN&gt; Discount&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; &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;.innerProduct_.Discount; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt; { &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.innerProduct_.Discount = &lt;SPAN style="COLOR: blue"&gt;value&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;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;override&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; InventoryCount&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; &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;.innerProduct_.InventoryCount; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt; { &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.innerProduct_.InventoryCount = &lt;SPAN style="COLOR: blue"&gt;value&lt;/SPAN&gt;; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;
&lt;P&gt;The nice thing about this class is that you only have to write it once, and then you can keep reusing it to add more and more features.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the next layer up from the data access layer (the business logic layer), we can just consume the Product instance served by the data access component. To add features to a Product instance, we can define another abstraction:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;abstract&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;BusinessProduct&lt;/SPAN&gt; : &lt;SPAN style="COLOR: #2b91af"&gt;ProductDecorator&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;protected&lt;/SPAN&gt; BusinessProduct(&lt;SPAN style="COLOR: #2b91af"&gt;Product&lt;/SPAN&gt; p)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; : &lt;SPAN style="COLOR: blue"&gt;base&lt;/SPAN&gt;(p)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;abstract&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;decimal&lt;/SPAN&gt; DiscountedPrice { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;abstract&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;bool&lt;/SPAN&gt; InStock { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P mce_keep="true"&gt;Creating an implementation of BusinessProduct is easy:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;SimpleBusinessProduct&lt;/SPAN&gt; : &lt;SPAN style="COLOR: #2b91af"&gt;BusinessProduct&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; SimpleBusinessProduct(&lt;SPAN style="COLOR: #2b91af"&gt;Product&lt;/SPAN&gt; p)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; : &lt;SPAN style="COLOR: blue"&gt;base&lt;/SPAN&gt;(p)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;override&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;decimal&lt;/SPAN&gt; DiscountedPrice&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; &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;.ListPrice - &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.Discount; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;override&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;bool&lt;/SPAN&gt; InStock&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; &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;.InventoryCount &amp;gt; 0; }&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 mce_keep="true"&gt;Notice that this is the same&amp;nbsp;business logic&amp;nbsp;as in my former post, but instead of dealing with arbitrary 'companion' classes or predefined DTOs, this approach is simply an extension of the Product class.&lt;/P&gt;
&lt;P mce_keep="true"&gt;Since I defined BusinessProduct as an abstract class, I can use the abstraction in the next layer (e.g. the User Inteface Process layer) without referencing any particular implementation, effectively decoupling the UIP layer from the business logic layer:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ProductView&lt;/SPAN&gt; : &lt;SPAN style="COLOR: #2b91af"&gt;BusinessProduct&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;BusinessProduct&lt;/SPAN&gt; innerProduct_;&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; ProductView(&lt;SPAN style="COLOR: #2b91af"&gt;BusinessProduct&lt;/SPAN&gt; bp)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; : &lt;SPAN style="COLOR: blue"&gt;base&lt;/SPAN&gt;(bp)&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; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.innerProduct_ = bp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;override&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;decimal&lt;/SPAN&gt; DiscountedPrice&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; &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;.innerProduct_.DiscountedPrice; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;override&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;bool&lt;/SPAN&gt; InStock&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; &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;.innerProduct_.InStock; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;virtual&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Color&lt;/SPAN&gt; Color&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; &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&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; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.InStock)&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; {&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; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Color&lt;/SPAN&gt;.Black;&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; }&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; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Color&lt;/SPAN&gt;.Red;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&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;!--EndFragment--&gt;
&lt;P mce_keep="true"&gt;Notice that this is simply a new Decorator that decorates BusinessProduct instead of simply decorating Product, but it still does this by leveraging the original ProductDecorator class (BusinessProduct derives from ProductDecorator).&lt;/P&gt;
&lt;P mce_keep="true"&gt;Each time you want to extend the functionality of an abstract data object, you simple create a new abstract Decorator that inherits from a simpler Decorator. The more application layers and the more features you need to add along the way, the more you save on this approach, since you only have to write the basic plumbing code once.&lt;/P&gt;
&lt;P mce_keep="true"&gt;While this is certainly not the only alternative to DTOs, it's at least an attractive alternative for applications with many layers and a lot of features added along the way.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=3314348" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ploeh/archive/tags/Software+Design/default.aspx">Software Design</category></item><item><title>What's An Entity, Anyway?</title><link>http://blogs.msdn.com/ploeh/archive/2007/06/18/WhatIsAnEntityAnyway.aspx</link><pubDate>Mon, 18 Jun 2007 22:29:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3287228</guid><dc:creator>ploeh</dc:creator><slash:comments>5</slash:comments><comments>http://blogs.msdn.com/ploeh/comments/3287228.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ploeh/commentrss.aspx?PostID=3287228</wfw:commentRss><wfw:comment>http://blogs.msdn.com/ploeh/rsscomments.aspx?PostID=3287228</wfw:comment><description>&lt;P&gt;These days, I seem to be encountering a lot of &lt;EM&gt;entities&lt;/EM&gt;. Not in the sense of non-corporeal beings as usually depicted in certain science fiction TV shows, but in the sense of data structures. Sometimes, they are called &lt;EM&gt;business entities&lt;/EM&gt;.&lt;/P&gt;
&lt;P&gt;Although the concept of entities differ from project to project, I think I have identified at least one common trait of all the entities I come across: They contain (structured) data, but no behavior. Usually, these entities are being consumed and manipulated by something called the &lt;EM&gt;business logic&lt;/EM&gt;. In some cases, entities are even used to transfer data from one layer of an application to the next layer (some people then call them &lt;EM&gt;data transfer objects&lt;/EM&gt;).&amp;nbsp;Since architecture diagrams with vertical columns adjacent to layers appear to be much in vogue these days, I'll use one as an example:&lt;/P&gt;
&lt;P&gt;&lt;IMG src="http://blogs.msdn.com/photos/ploeh/images/3287264/original.aspx" border=0&gt;&lt;/P&gt;
&lt;P&gt;The idea here is to have a single definition for data that spans multiple levels so that you only have to write the data structure implementation once. The code in the different layers interact with the entities: The data access layer creates and stores the entities, the business logic layer modifies the data, and the UI layer presents the data. Pretty clean architecture, right?&lt;/P&gt;
&lt;P&gt;No.&lt;/P&gt;
&lt;P&gt;So what's wrong with it? First of all, what does the name &lt;EM&gt;entity&lt;/EM&gt; tell us? Nothing, really. &lt;EM&gt;Entity&lt;/EM&gt; is a synonym for &lt;EM&gt;object&lt;/EM&gt;, but surely, the term &lt;EM&gt;business objects&lt;/EM&gt; is so last year that any self-respecting architect would never use such a term. On the other hand, an object with structure but no behavior sounds awfully familiar.&lt;/P&gt;
&lt;P&gt;Your code takes one or more structures of data as input, operate on them and outputs other structures. Fowler calls this pattern a &lt;A class="" href="http://www.martinfowler.com/eaaCatalog/transactionScript.html" mce_href="http://www.martinfowler.com/eaaCatalog/transactionScript.html"&gt;Transaction Script&lt;/A&gt;; I call it &lt;EM&gt;procedural programming&lt;/EM&gt;, and since I have had&amp;nbsp;my experiences with this programming style early in my career, I never want to go back. &lt;A class="" href="http://www.martinfowler.com/eaaCatalog/domainModel.html" mce_href="http://www.martinfowler.com/eaaCatalog/domainModel.html"&gt;Domain Model&lt;/A&gt; is where it's at.&lt;/P&gt;
&lt;P&gt;In &lt;A class="" href="http://www.martinfowler.com/books.html#eaa" mce_href="http://www.martinfowler.com/books.html#eaa"&gt;Patterns of Enterprise Application Architecture&lt;/A&gt;, Fowler wrote that "a&amp;nbsp;&lt;A class="" href="http://www.martinfowler.com/eaaCatalog/dataTransferObject.html" mce_href="http://www.martinfowler.com/eaaCatalog/dataTransferObject.html"&gt;Data Transfer Object&lt;/A&gt;&amp;nbsp;is one of those&amp;nbsp;objects our mothers&amp;nbsp;told us&amp;nbsp;never to write." While the pattern itself&amp;nbsp;is valid, it's only supposed to be used for communication across process boundaries, not across layers in the same process.&lt;/P&gt;
&lt;P&gt;If you are still not convinced about my arguments, let's take a look at an example. Imagine that you want to model a product catalog. Since&amp;nbsp;we are modeling with entities, we create Product and Category classes. Both are just dumb classes with default constructors, read/write properties,&amp;nbsp;and no behavior. To decouple data access, we also define a data access interface:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;interface&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ICatalogDataAccess&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Category&lt;/SPAN&gt; ReadCategory(&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; categoryId);&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: #2b91af"&gt;Product&lt;/SPAN&gt; ReadProduct(&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; productId);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;
&lt;P&gt;Implementing this interface is fairly straightforward, and goes something like this:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;IDataReader&lt;/SPAN&gt; r = &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.GetProductReader(productId))&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;if&lt;/SPAN&gt; (!r.Read())&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; &lt;SPAN style="COLOR: blue"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ArgumentException&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"No such product."&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"productId"&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;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Product&lt;/SPAN&gt; p = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Product&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; p.ProductId = (&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;)r[&lt;SPAN style="COLOR: #a31515"&gt;"ProductId"&lt;/SPAN&gt;];&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; p.Name = (&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;)r[&lt;SPAN style="COLOR: #a31515"&gt;"Name"&lt;/SPAN&gt;];&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; p.ListPrice = (&lt;SPAN style="COLOR: blue"&gt;decimal&lt;/SPAN&gt;)r[&lt;SPAN style="COLOR: #a31515"&gt;"ListPrice"&lt;/SPAN&gt;];&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; p.Discount = (&lt;SPAN style="COLOR: blue"&gt;decimal&lt;/SPAN&gt;)r[&lt;SPAN style="COLOR: #a31515"&gt;"Discount"&lt;/SPAN&gt;];&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; p.InventoryCount = (&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;)r[&lt;SPAN style="COLOR: #a31515"&gt;"InventoryCount"&lt;/SPAN&gt;];&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; p;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P mce_keep="true"&gt;This code is actually fairly benign - trouble only starts to appear in the business logic layer. Imagine that we need the business logic to implement the calculation of the discounted price, and whether the product is in stock (yes, rather inane business logic, I know). Since the Product entity is just a structure without behavior, it's necessary to create another class to implement this business logic:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ProductOperator&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Product&lt;/SPAN&gt; product_;&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; ProductOperator(&lt;SPAN style="COLOR: #2b91af"&gt;Product&lt;/SPAN&gt; p)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.product_ = p;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&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;decimal&lt;/SPAN&gt; DiscountedPrice&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; &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;.product_.ListPrice - &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.product_.Discount; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;bool&lt;/SPAN&gt; InStock&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; &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;.product_.InventoryCount &amp;gt; 0; }&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;!--EndFragment--&gt;
&lt;P mce_keep="true"&gt;Now you are left with the problem of how to pass this information on to the next layer.&lt;/P&gt;
&lt;P mce_keep="true"&gt;One alternative is to create an abstraction of ProductOperator (say; IProductOperator) and pass that to the next layer together with the Product entity. That approach can quickly grow quite unpleasant, since each layer adding content to the entity needs to define yet another auxiliary class to be passed along with the ProductOperator and the Product entity.&lt;/P&gt;
&lt;P mce_keep="true"&gt;Another alternative is to model the Product entity to include properties for this information from the start. That would mean that the data access component would fill in only the properties of the Product entity that comes from the database, and a variant of ProductOperator would then fill in the DiscountedPrice and InStock properties in the business logic layer:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;partial&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ProductOperator&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; ProductOperator()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; UpdateDiscountedPrice(&lt;SPAN style="COLOR: #2b91af"&gt;Product&lt;/SPAN&gt; p)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; p.DiscountedPrice = p.ListPrice - p.Discount;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; UpdateInStock(&lt;SPAN style="COLOR: #2b91af"&gt;Product&lt;/SPAN&gt; p)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; p.InStock = p.InventoryCount &amp;gt; 0;&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;!--EndFragment--&gt;
&lt;P mce_keep="true"&gt;Beware: Here be dragons.&lt;/P&gt;
&lt;P mce_keep="true"&gt;One problem with this approach is that you'd end up with a lot of properties whose values may or may not be null (DiscountedPrice and InStock, in this case), so you always need to check for null before reading and using a property value.&lt;/P&gt;
&lt;P mce_keep="true"&gt;The other problem with this design is that it railroads your components into a particular usage scenario. In the end, you model the entity in order to communicate it across your process boundary (via a UI, service interface, etc.). This boundary has a particular usage scenario; e.g. you need to show product information in a UI. Such a usage scenario then becomes the driver for the entity structure: You need to show the discounted price, so you need a property for that, etc. If you need to display product information in another screen, you include properties for this screen as well. In the end, you end up with a data structure that carries around a lot of data that may or may not be used in any particular scenario.&lt;/P&gt;
&lt;P mce_keep="true"&gt;There are lots of nicer ways to pass data between layers in extensible ways, and in a future post, I'll describe one such approach.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=3287228" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ploeh/archive/tags/Software+Design/default.aspx">Software Design</category></item><item><title>State Your Dependency Intent</title><link>http://blogs.msdn.com/ploeh/archive/2007/06/02/StateYourDependencyIntent.aspx</link><pubDate>Sun, 03 Jun 2007 00:11:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3049067</guid><dc:creator>ploeh</dc:creator><slash:comments>8</slash:comments><comments>http://blogs.msdn.com/ploeh/comments/3049067.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ploeh/commentrss.aspx?PostID=3049067</wfw:commentRss><wfw:comment>http://blogs.msdn.com/ploeh/rsscomments.aspx?PostID=3049067</wfw:comment><description>&lt;P&gt;There are several different ways to implement Dependency Injection (DI), and Martin Fowler describes four of them in his &lt;A class="" href="http://www.martinfowler.com/articles/injection.html" mce_href="http://www.martinfowler.com/articles/injection.html"&gt;excellent article on IoC/DI&lt;/A&gt;. In this article, the first three approaches (Constructor, Property, and Interface Injection) are mainly described as a background for introducing the Service Locator pattern. In Fowler's example, a generic&amp;nbsp;Service Locator is a registry (basically an in-memory table), but these days you most commonly see it implemented as a factory.&lt;/P&gt;
&lt;P&gt;Consider this simple interface:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;interface&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IMyInterface&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;void&lt;/SPAN&gt; DoStuff();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Here's a class that uses a Service Locator to get an instance of IMyInterface:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ImplicitConsumer&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; PerformOperation()&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; &lt;SPAN style="COLOR: #2b91af"&gt;IMyInterface&lt;/SPAN&gt; mi = &lt;SPAN style="COLOR: #2b91af"&gt;ServiceFactory&lt;/SPAN&gt;.Create&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;IMyInterface&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; mi.DoStuff();&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 ServiceFactory class has a static method that returns an instance of the requested interface. Although not explicitly shown, the ImplicitConsumer class has a default constructor, since no constructor is defined and the C# compiler then automatically creates one.&lt;/P&gt;
&lt;P&gt;Now, imagine yourself in a situation where you need to consume an instance of the ImplicitConsumer class and call its PerformOperation method. Also imagine that you have just been handed the class in a binary form, with documentation, but without source code. In this scenario, you would probably write code like this:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ImplicitConsumer&lt;/SPAN&gt; ic = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ImplicitConsumer&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;ic.PerformOperation();&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Writing the first line is very straightforward, since there's only one way to create a new instance of the class. Next, with the &lt;EM&gt;ic&lt;/EM&gt; instance, IntelliSense will quickly help you to find and invoke the PerformOperation method, and that's it: The code compiles and you are happy.&lt;/P&gt;
&lt;P&gt;At run-time, however, this code is going to fail, since the Service Locator has not been configured. At this point, you &lt;EM&gt;may&lt;/EM&gt; resort to the documentation, and if you are &lt;EM&gt;lucky&lt;/EM&gt;, the documentation will tell you that the PerformOperation method expects the Service Locator to be configured to return an instance of IMyInterface. If you aren't so lucky, you will have to fire up &lt;A class="" href="http://www.aisto.com/roeder/dotnet/" mce_href="http://www.aisto.com/roeder/dotnet/"&gt;Reflector&lt;/A&gt; to figure out what to do.&lt;/P&gt;
&lt;P&gt;Depending on the Service Locator's implementation, this configuration may be done in the configuration file or in code. Here's how it might look in code:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ServiceFactory&lt;/SPAN&gt;.Preset&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;IMyInterface&lt;/SPAN&gt;&amp;gt;(&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;StubMyInterface&lt;/SPAN&gt;());&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ImplicitConsumer&lt;/SPAN&gt; ic = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ImplicitConsumer&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;ic.PerformOperation();&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Here we have what looks like two pieces of totally unrelated code, yet they are very closely related at run-time. If you came by this code without prior knowledge, you'd probably&amp;nbsp;mistake the first line of code&amp;nbsp;for a piece of &lt;A class="" href="http://www.antipatterns.com/lavaflow.htm" mce_href="http://www.antipatterns.com/lavaflow.htm"&gt;lava flow&lt;/A&gt;&amp;nbsp;and delete it. If you were the author of those three lines, you might attempt to protect yourself from this risk by applying a comment to the first line, but that would be an &lt;A class="" href="http://www.butunclebob.com/ArticleS.TimOttinger.ApologizeIncode" mce_href="http://www.butunclebob.com/ArticleS.TimOttinger.ApologizeIncode"&gt;apology&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;In my opinion, an API should always strive to steer developers in the right direction. With respect to DI, the API should clearly state its intent to&amp;nbsp;consume a particular dependency. Constructor Injection does this very explicitly:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ExplicitConsumer&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IMyInterface&lt;/SPAN&gt; mi_;&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; ExplicitConsumer(&lt;SPAN style="COLOR: #2b91af"&gt;IMyInterface&lt;/SPAN&gt; mi)&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; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (mi == &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; {&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; &lt;SPAN style="COLOR: blue"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ArgumentNullException&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"mi"&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&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; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.mi_ = mi;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; PerformOperation()&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; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.mi_.DoStuff();&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;With this implementation, any developer is forced to do the right thing. There's only a single constructor, and IntelliSense will show that it expects an instance of IMyInterface. While a developer could pass null as a parameter, he or she&amp;nbsp;would do so &lt;EM&gt;in spite&lt;/EM&gt; of the knowledge that was just communicated&amp;nbsp;by IntelliSense&amp;nbsp;(and there would still&amp;nbsp;be a run-time error). There would be no need to read the documentation or fire up Reflector, because the class makes it very clear that it needs a working instance of IMyInterface to perform its work, and as a developer, you must supply it:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ExplicitConsumer&lt;/SPAN&gt; ec = &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ExplicitConsumer&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;StubMyInterface&lt;/SPAN&gt;());&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;ec.PerformOperation();&lt;/PRE&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;
&lt;P&gt;The only drawback of Constructor Injection that I have ever been able to identify is the need to initialize all dependencies at once if you have a complex hierarchy of dependencies, as described in my &lt;A class="" href="http://blogs.msdn.com/ploeh/archive/2007/05/30/CodeAsDependencyConfiguration.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2007/05/30/CodeAsDependencyConfiguration.aspx"&gt;former post&lt;/A&gt;. If lazy initialization is a necessity, you can use &lt;A class="" href="http://blogs.msdn.com/ploeh/archive/2007/03/25/ProviderInjection.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2007/03/25/ProviderInjection.aspx"&gt;Provider Injection&lt;/A&gt;, which is a variation of Constructor Injection. Although this is currently my favorite DI strategy, it's less well-known and more difficult to explain.&lt;/P&gt;
&lt;P&gt;In any case, the main point is that if a component expects to&amp;nbsp;consume a dependency which will be supplied at run-time, it should clearly state&amp;nbsp;that intent&amp;nbsp;through its API, instead of relying on out-of-band discovery mechanisms. Although the API may end up looking more complex, it's ensuring that mistakes are much harder to make. Even if there's a slightly more pronounced learning curve&amp;nbsp;to get started with&amp;nbsp;an API that uses Constructor Injection, it's easier to use in the long run. A developer with no prior knowledge of the component will sooner be able to produce code that &lt;EM&gt;compiles&lt;/EM&gt; with a component relying on a Service Locator, but he or she will sooner be able to produce code that &lt;EM&gt;works&lt;/EM&gt; with Constructor Injection.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=3049067" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ploeh/archive/tags/Dependency+Injection/default.aspx">Dependency Injection</category><category domain="http://blogs.msdn.com/ploeh/archive/tags/Software+Design/default.aspx">Software Design</category></item><item><title>Code As Dependency Configuration</title><link>http://blogs.msdn.com/ploeh/archive/2007/05/30/CodeAsDependencyConfiguration.aspx</link><pubDate>Wed, 30 May 2007 17:33:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2986805</guid><dc:creator>ploeh</dc:creator><slash:comments>13</slash:comments><comments>http://blogs.msdn.com/ploeh/comments/2986805.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ploeh/commentrss.aspx?PostID=2986805</wfw:commentRss><wfw:comment>http://blogs.msdn.com/ploeh/rsscomments.aspx?PostID=2986805</wfw:comment><description>&lt;P&gt;In his article on &lt;A class="" href="http://www.martinfowler.com/articles/injection.html" mce_href="http://www.martinfowler.com/articles/injection.html"&gt;Inversion of Control and Dependency Injection&lt;/A&gt;, Martin Fowler has&amp;nbsp;a quite interesting section towards the end where he talks about how to configure loosely coupled systems. One of his points is that in some cases, it makes more sense to tie it all together using code than by using a configuration system.&lt;/P&gt;
&lt;P&gt;When I introduce the concept of Code As Configuration for most people, they seem to instantly stop listening to what I'm saying. Configuration (the ability to change the behavior of the application without changing its code base) is good, so why would you ever want not to enable configuration? In .NET, this notion is particularly strong because System.Configuration makes it so easy to create a rich configuration object model backed by XML configuration files.&lt;/P&gt;
&lt;P&gt;With Dependency Injection, the configurable entities are types (defined in&amp;nbsp;various assemblies). Because of Dependency Injection, each library in your application can be compiled in isolation, since its &lt;A class="" href="http://blogs.msdn.com/ploeh/archive/2006/08/24/718828.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2006/08/24/718828.aspx"&gt;volatile dependencies&lt;/A&gt; are being injected at run-time.&amp;nbsp;A design pattern such as Constructor Injection is fairly simple and intuitive to use as long as you use it in isolation during development (e.g. while unit testing), but when it comes to tying everything together in an executable application, all these dependencies must be loaded into an object hierarchy such as this one:&lt;/P&gt;
&lt;P&gt;&lt;IMG src="http://blogs.msdn.com/photos/ploeh/images/2987380/original.aspx" border=0&gt;&lt;/P&gt;
&lt;P&gt;In this example, each class is defined in a separate assembly. Note that the arrows indicate usage, not dependencies. For the application to run in the desired configuration, ClassC must consume an instance of ClassE, and so forth. However, ClassC only knows about Abstraction5, which is being injected into it via its constructor:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ClassC&lt;/SPAN&gt; : &lt;SPAN style="COLOR: #2b91af"&gt;Abstraction3&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Abstraction5&lt;/SPAN&gt; a5_;&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; ClassC(&lt;SPAN style="COLOR: #2b91af"&gt;Abstraction5&lt;/SPAN&gt; a5)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; : &lt;SPAN style="COLOR: blue"&gt;base&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; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.a5_ = a5;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;protected&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;override&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; DoStuffCore()&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; &lt;SPAN style="COLOR: #2b91af"&gt;StringBuilder&lt;/SPAN&gt; builder = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;StringBuilder&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; builder.AppendLine(&lt;SPAN style="COLOR: #a31515"&gt;"I am really Class C. My dependency says:"&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; builder.Append(&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.a5_.DoStuff());&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; builder.ToString();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;The other classes are implemented in a similar way; ClassB, for example, only communicates with Abstraction3 and Abstraction4 and doesn't have any knowledge of ClassD or ClassC (let alone that in this configuration, ClassC consumes ClassE).&lt;/P&gt;
&lt;P&gt;The executable must somehow construct the correct object graph. A common approach is to use the app.config file to define this hierarchy (for an example,&amp;nbsp;&lt;A class="" href="http://www.springframework.net/" mce_href="http://www.springframework.net/"&gt;Spring.NET&lt;/A&gt; supports this option). To properly identify which type to instantiate when an abstraction is requested, an &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/aa720647(VS.71).aspx" mce_href="http://msdn2.microsoft.com/en-us/library/aa720647(VS.71).aspx"&gt;assembly-qualified type name&lt;/A&gt; is provided in the configuration file, and reflection is used to create instances of the configured types.&lt;/P&gt;
&lt;P&gt;This very common approach comes with a rather painful drawback: Since the executable application has no dependencies on any of its servers, none of the containing assemblies are automatically copied to the output directory. Although the application compiles, it crashes when executed because Fusion is unable to locate the assemblies specified in the configuration file.&lt;/P&gt;
&lt;P&gt;To work around this, you can either put all of your assemblies in the GAC or copy them to the relevant output directory, but both of these options are either repetitive or requires automation. In any case, this drawback can be a bit counter-productive during development.&lt;/P&gt;
&lt;P&gt;Another approach is simply to code the desired configuration into the top-level executable. Whether this is a viable option depends on your &lt;A class="" href="http://blogs.msdn.com/ploeh/archive/2007/05/30/ReasonsForIsolation.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2007/05/30/ReasonsForIsolation.aspx"&gt;reason for isolating&lt;/A&gt; your volatile dependencies in the first place. If your&amp;nbsp;motivation is genuinely to support plug-ins or other extensibility models, configuration-driven Dependency Injection is certainly the right approach, and hard-coding a particular configuration is not an option. For other scenarios, however, this solves the problem of managing and deploying the correct assemblies.&lt;/P&gt;
&lt;P&gt;In my example, the client will create the entire object hierarchy like this:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; Main(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;[] args)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Abstraction1&lt;/SPAN&gt; a1 = &lt;SPAN style="COLOR: #2b91af"&gt;Program&lt;/SPAN&gt;.CreateAbstraction1();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Abstraction2&lt;/SPAN&gt; a2 = &lt;SPAN style="COLOR: #2b91af"&gt;Program&lt;/SPAN&gt;.CreateAbstraction2();&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: #2b91af"&gt;Console&lt;/SPAN&gt;.WriteLine(a1.DoStuff());&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.WriteLine(a2.DoStuff());&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: #2b91af"&gt;Console&lt;/SPAN&gt;.ReadKey(&lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&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;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Abstraction1&lt;/SPAN&gt; CreateAbstraction1()&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;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ClassA&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&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;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Abstraction2&lt;/SPAN&gt; CreateAbstraction2()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Abstraction5&lt;/SPAN&gt; a5 = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ClassE&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Abstraction4&lt;/SPAN&gt; a4 = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ClassD&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Abstraction3&lt;/SPAN&gt; a3 = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ClassC&lt;/SPAN&gt;(a5);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Abstraction2&lt;/SPAN&gt; a2 = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ClassB&lt;/SPAN&gt;(a3, a4);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; a2;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Since all implementations use Constructor Injection, the client can construct the entire object&amp;nbsp;tree by starting with the leaves and working its way to the root of the tree. Notice that although this creates an object hierarchy as illustrated above, the dependency hierarchy is flattened:&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;IMG src="http://blogs.msdn.com/photos/ploeh/images/2987382/original.aspx" border=0&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;Apart from the top-level executable, each assembly only has a dependency on an assembly containing the abstractions (base classes and interfaces). Since such an assembly is considered a stable dependency, isolation has been achieved for all libraries. The only assembly with volatile dependencies is the top-level executable, but that's commonly where your UI resides, so it's not going to be testable in any case (and should be kept as minimal as possible).&lt;/P&gt;
&lt;P mce_keep="true"&gt;Since the top-level executable has all relevant dependencies for the desired configuration, all required assemblies will automatically be part of the build.&lt;/P&gt;
&lt;P mce_keep="true"&gt;Obviously, if you ever need to run the application in a different configuration, you will need to change the code, compile and redeploy, but that's not particularly different from the pure configuration scenario. Even if you use a configuration file, you will still need to compile and deploy your new or changed modules. In my opionion, it's the&amp;nbsp;overhead of actually deploying even a single file that carries the greatest cost; whether you copy one or all of your files is merely a question of network or disk transfer time.&lt;/P&gt;
&lt;P mce_keep="true"&gt;For applications where you control the entire code base and no extensibility support is required, the&amp;nbsp;Code As Configuration&amp;nbsp;technique can make development teams more productive without sacrificing significant functionality.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=2986805" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ploeh/archive/tags/Dependency+Injection/default.aspx">Dependency Injection</category><category domain="http://blogs.msdn.com/ploeh/archive/tags/Software+Design/default.aspx">Software Design</category></item><item><title>Reasons For Isolation</title><link>http://blogs.msdn.com/ploeh/archive/2007/05/30/ReasonsForIsolation.aspx</link><pubDate>Wed, 30 May 2007 13:22:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2984837</guid><dc:creator>ploeh</dc:creator><slash:comments>5</slash:comments><comments>http://blogs.msdn.com/ploeh/comments/2984837.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ploeh/commentrss.aspx?PostID=2984837</wfw:commentRss><wfw:comment>http://blogs.msdn.com/ploeh/rsscomments.aspx?PostID=2984837</wfw:comment><description>&lt;P&gt;Object-oriented applications above&amp;nbsp;some level of complexity are almost always modelled as a layered architecture. While the typical three-layer architecture remains the most widely known, n-layer architecture is also often utilized. Here's a typical design almost anyone can create (in Visio or Power Point, that is):&lt;/P&gt;
&lt;P&gt;&lt;IMG src="http://blogs.msdn.com/photos/ploeh/images/2984767/original.aspx" border=0 mce_src="http://blogs.msdn.com/photos/ploeh/images/2984767/original.aspx"&gt;&lt;/P&gt;
&lt;P&gt;There seems to be an almost universal agreement that layering is the correct things to do, but do you know &lt;EM&gt;why&lt;/EM&gt; layering is considered such an attractive design feature?&lt;/P&gt;
&lt;P&gt;When I ask this question of people, most answer that this enables them to replace a certain implementation with another implementation. What's even more interesting is that the typical &lt;EM&gt;implementation&lt;/EM&gt; of this design&amp;nbsp;is&amp;nbsp;best illustrated like this:&lt;/P&gt;
&lt;P&gt;&lt;IMG src="http://blogs.msdn.com/photos/ploeh/images/2984803/original.aspx" border=0&gt;&lt;/P&gt;
&lt;P&gt;No clear separation of logic is implemented, modules tend to trample over each others' domains, but worst of all, there's a strong hierarchy of dependencies in place: Most assemblies have dependencies on other assemblies in the solution, and few of of the projects can be compiled without also compiling all the dependent projects. The only projects that can typically compiled without&amp;nbsp;&lt;A class="" href="http://blogs.msdn.com/ploeh/archive/2006/08/24/718828.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2006/08/24/718828.aspx"&gt;volatile dependencies&lt;/A&gt; are the ones in the bottom layer, but those are the ones you most need to be independent of.&lt;/P&gt;
&lt;P&gt;When confronted with this fact, most project members don't think this is a big deal, since the ability to replace one implementation with another isn't important in their current project. To paraphrase a typical response:&amp;nbsp;"Yes, it would be &lt;EM&gt;nice&lt;/EM&gt; to be able to replace our relational database with XML files without recompiling, but we are never going to&amp;nbsp;need this, so why bother?"&lt;/P&gt;
&lt;P&gt;This attitude&amp;nbsp;suggests to me that people are missing the point.&lt;/P&gt;
&lt;P&gt;The reason why layering is important is because each module must be able to exist in isolation. This means that it should be possible to compile a project without having to load any other projects containing volatile dependencies. If you ever find that you have to load 35 projects into your solution to compile a single project, you are doing something wrong. Ideally, you should be able to define a self-contained solution containing a small subset of your projects; about five projects, and certainly no more than ten.&lt;/P&gt;
&lt;P&gt;If the ability to replace one implementation with another&amp;nbsp;is not the sole reason for isolation, then what is?&lt;/P&gt;
&lt;P&gt;Personally, I can think of at least three independent reasons:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;It's nice to have the ability to replace one implementation with another. Yes, I know I just&amp;nbsp;implied that it's wrong to think that you&amp;nbsp;don't need isolation if you don't need this feature, but I didn't say that this ability isn't valuable in some projects. In many Enterprise/Line of Business applications, this feature may not matter, but other application types (such as shrink-wrapped software) often offer some extensibility model where such a feature could be very valuable to customers.&lt;/LI&gt;
&lt;LI&gt;In large projects, it enables development teams to work independently of each other. One team can work on the data access layer while another team works on the business logic. Even when it's not important that you can replace your complex data access component when you deploy the application, it's very valuable for the business logic team to be able to replace the data access component with a simple, temporary implementation. This will allow the business logic team to work without being blocked by the data access team.&lt;/LI&gt;
&lt;LI&gt;Unit testing becomes possible. By definition, a unit is (a part of) a module isolated from its dependencies. When you can replace one implementation of a dependency with another, you can also replace it with a &lt;A class="" href="http://www.martinfowler.com/bliki/TestDouble.html" mce_href="http://www.martinfowler.com/bliki/TestDouble.html"&gt;test double&lt;/A&gt;. Since unit testing is by far the most cost-effective type of test, this property alone makes isolation very attractive.&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;If just a single of these reasons seems valuable to you, isolation should be one of your top priorities as you are implementing your design.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=2984837" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ploeh/archive/tags/Component+Development/default.aspx">Component Development</category><category domain="http://blogs.msdn.com/ploeh/archive/tags/Software+Design/default.aspx">Software Design</category></item><item><title>Authorizing Services</title><link>http://blogs.msdn.com/ploeh/archive/2007/05/20/AuthorizingServices.aspx</link><pubDate>Sun, 20 May 2007 15:05:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2674174</guid><dc:creator>ploeh</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/ploeh/comments/2674174.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ploeh/commentrss.aspx?PostID=2674174</wfw:commentRss><wfw:comment>http://blogs.msdn.com/ploeh/rsscomments.aspx?PostID=2674174</wfw:comment><description>&lt;P&gt;If you look at the default &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/system.servicemodel.serviceauthorizationmanager.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/system.servicemodel.serviceauthorizationmanager.aspx"&gt;authorization model&lt;/A&gt; for WCF, you will notice that it expects you to implement centralized authorization. While centralized and pluggable &lt;EM&gt;authentication&lt;/EM&gt; makes a lot of sense to me, that's not the case with authorization.&lt;/P&gt;
&lt;P&gt;Does it really make sense to decouple authorization from implementation? Are you only going to check access rights at the perimeter and leave the rest of your code wide-open? Can you even fully authorize an operation at the perimeter?&lt;/P&gt;
&lt;P&gt;My claim is that for a secure and maintainable solution&amp;nbsp;you should answer &lt;EM&gt;no&lt;/EM&gt; to all these questions. In this article, I'll will explain why I think so.&lt;/P&gt;
&lt;P&gt;While it makes a lot of sense to decouple authentication (who you are) from implementation, authorization (what you can do) is tightly coupled with what you are trying to do. Put in other words: Authenticating a user requires no knowledge of the context; either the user's credentials are acceptable, or they are not. Authorizing an action, on the other hand, requires knowledge of not only the identity, but also the operation being attempted. &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/system.servicemodel.serviceauthorizationmanager.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/system.servicemodel.serviceauthorizationmanager.aspx"&gt;As documented&lt;/A&gt;, the OperationContext provided to a ServiceAuthorizationManager contains information about which service and operation is being invoked, so you could certainly implement centralized authorization logic; basically, it's going to be one big switch statement.&lt;/P&gt;
&lt;P&gt;Working with centralized authorization will mean having to switch back and forth between the authorization manager and the implementation code. To me, that's not particularly productive, when you can tie authorization and implementation together as simple as this:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;PrincipalPermission&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;SecurityAction&lt;/SPAN&gt;.Demand, Role = &lt;SPAN style="COLOR: #a31515"&gt;"Administrator"&lt;/SPAN&gt;)]&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; MyMethod()&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: green"&gt;// Implementation goes here...&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;With&amp;nbsp;the PrincipalPermission attribute, you can define role-based security declaratively. This has the benefit of succinctly defining security in the most intuitive place. You could implement the same authorization logic in a centralized authorization manager, but it would require imperative code inspecting message properties. From a code maintainability perspective, decoupling authorization from implementation doesn't make a lot of sense to me.&lt;/P&gt;
&lt;P&gt;Is checking access rights only at the perimeter a good idea, then? While the case could be made for turning away unauthorized calls at the service boundary, securing a system only at its perimeter is not considered to be particularly effective. In their book &lt;EM&gt;&lt;A class="" href="http://www.microsoft.com/MSPress/books/5957.aspx" mce_href="http://www.microsoft.com/MSPress/books/5957.aspx"&gt;Writing Secure Code&lt;/A&gt;&lt;/EM&gt;, Michael Howard and David LeBlanc endorse&amp;nbsp;&lt;EM&gt;defense in depth&lt;/EM&gt; as an important principle in secure systems. Multiple checkpoints should exist at separate layers of an application. This helps thwart a would-be attacker, who would then otherwise have full control of&amp;nbsp;a system once he or she&amp;nbsp;gets past the perimiter.&lt;/P&gt;
&lt;P&gt;Pluggable authorization logic implemented at the perimiter doesn't follow this principle. What is worse is that since a WCF authorization manager is pluggable, a service operator may even accidentally deploy a service without correctly configuring the authorization manager.&lt;/P&gt;
&lt;P&gt;Another scenario deals with extensibility. Imagine that your service delegates all work to a business logic component, and that you would like to reuse the business logic in other contexts: A web site, admistrative application, ETL job, etc. In these scenarios, your business logic will probably not be hosted by WCF, but rather accessed by in-process calls, so a WCF authorization manager will&amp;nbsp;be bypassed in these cases.&lt;/P&gt;
&lt;P&gt;Authorization logic should in my opinion be implemented as close to the resource it's protecting as possible;&amp;nbsp;most likely&amp;nbsp;in the data access layer itself, so that even if an application bypasses the default business logic component, authorization is still being checked. For that reason, checking only at the perimeter is not particularly secure.&lt;/P&gt;
&lt;P&gt;As it turns out, it's not even possible to perform&amp;nbsp;any sort of authorization check at the perimiter, since additional data may be required.&amp;nbsp;A simple role-based access check as in the example given above can also be performed at the perimeter, but more complex access rules cannot. Consider this example:&lt;/P&gt;
&lt;P&gt;Imagine a service providing details about registered users. This service contains a GetUserData operation that returns user data for the requested user ID. The authorization logic for such an operation could be something like this:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;A member of the Administrator role can request data on any user.&lt;/LI&gt;
&lt;LI&gt;A member of the User role can only request his or her own data.&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;However, in this example, user IDs are particular to the application and not equal to the callers' credentials. The callers' credentials may&amp;nbsp;represent a Windows account, while users IDs are defined be a table key in a database. While there's a one-to-one&amp;nbsp;correspondence between credentials and user IDs, this relationship is defined at the data level. As such, the message available to a perimeter-based authorization manager contains the requested user ID, as well as the caller's credentials, but no data indicating any relationship between the two are available at that level.&lt;/P&gt;
&lt;P&gt;In imperative code, however, you could implement the above authorization rules like this:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;UserData&lt;/SPAN&gt; GetUserData(&lt;SPAN style="COLOR: #2b91af"&gt;Guid&lt;/SPAN&gt; userId)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;UserData&lt;/SPAN&gt; user = &lt;SPAN style="COLOR: #2b91af"&gt;DataAccess&lt;/SPAN&gt;.FindUser(userId);&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: #2b91af"&gt;PrincipalPermission&lt;/SPAN&gt; administratorPermission =&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;PrincipalPermission&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"Administrator"&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;PrincipalPermission&lt;/SPAN&gt; selfPermission =&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;PrincipalPermission&lt;/SPAN&gt;(user.DisplayName, &lt;SPAN style="COLOR: #a31515"&gt;"User"&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; administratorPermission.Union(selfPermission).Demand();&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;return&lt;/SPAN&gt; user;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;In this case it's necessary to first&amp;nbsp;get the data from the data store, since a security decision requires more data than supplied by the caller. Notice that administratorPermission allows any user to make the call if he or she is a member of the Administrator role. On the other hand, selfPermission only allows users through whose credentials are the same as the DisplayName of the user they requested. The Union of these two IPermission objects is then demanded, ensuring that at least one of them is satisfied.&lt;/P&gt;
&lt;P&gt;Authorization logic like this example cannot be implemented at the perimeter, since required data is not available.&lt;/P&gt;
&lt;P&gt;By now, I hope I have demonstrated why I think centralized authorization managers don't make a lot of sense in a complex, n-layer architecture.&lt;/P&gt;
&lt;P&gt;Note that the use of PrincipalPermission and PrincipalPermissionAttribute requires that Thread.CurrentPrincipal is populated with an instance of IPrincipal representing the caller. Using IPrincipal and thread local storage to represent and flow information about the caller is considered a .NET best practice, since the framework itself contains support for it (such as declarative role-based security).&lt;/P&gt;
&lt;P&gt;Interestingly, a WCF ServiceAuthorizationManager&amp;nbsp;is a very&amp;nbsp;correct place&amp;nbsp;to map foreign user credentials to IPrincipal objects, as I've &lt;A class="" href="http://blogs.msdn.com/ploeh/archive/2007/01/04/MappingSAMLTokensToIPrincipals.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2007/01/04/MappingSAMLTokensToIPrincipals.aspx"&gt;described in an earlier post&lt;/A&gt;.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=2674174" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ploeh/archive/tags/Services/default.aspx">Services</category><category domain="http://blogs.msdn.com/ploeh/archive/tags/Software+Design/default.aspx">Software Design</category></item></channel></rss>