<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.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>Peter Ritchie's MVP Blog : Patterns</title><link>http://msmvps.com/blogs/peterritchie/archive/tags/Patterns/default.aspx</link><description>Tags: Patterns</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>A Upcoming Pandemic of Domain Anaemia</title><link>http://msmvps.com/blogs/peterritchie/archive/2009/01/29/a-upcoming-pandemic-of-domain-anaemia.aspx</link><pubDate>Thu, 29 Jan 2009 17:10:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1666992</guid><dc:creator>PeterRitchie</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/peterritchie/rsscomments.aspx?PostID=1666992</wfw:commentRss><comments>http://msmvps.com/blogs/peterritchie/archive/2009/01/29/a-upcoming-pandemic-of-domain-anaemia.aspx#comments</comments><description>&lt;p&gt;There&amp;#39;s a well-known anti-pattern called the anaemic domain model[1][2].&amp;nbsp; This anti-pattern basically says domain entities, chronically, have little or no behaviour (remember, object-oriented design is about attributes &lt;strong&gt;and&lt;/strong&gt; behaviour).&lt;/p&gt;
&lt;p&gt;It should be obvious that a domain model that isn&amp;#39;t truly object oriented is a domain model with a problem.&amp;nbsp; But, let&amp;#39;s look at other reasons why the Anaemic Domain Model is an anti-pattern.&amp;nbsp; Your Domain is the nexus, the essence, of your system.&lt;/p&gt;
&lt;p&gt;An anaemic domain model is basically a reporting system.&amp;nbsp; Each &amp;quot;Entity&amp;quot; becomes, essentially, a query.&amp;nbsp; This is fine, reporting systems are necessary and prevalent.&amp;nbsp; But, to shoe-horn a domain model on top of this leads away from good reporting patterns that could add value and increases complexity, needlessly.&amp;nbsp; The designers spend most of their time trying to force entities on the system, without recognizing the basic reporting nature of the system.&amp;nbsp; This usually leads to &amp;quot;reports&amp;quot; that have to pull in multiple domain &amp;quot;entities&amp;quot; to generate the report--rehydringing data into an entity (usually through some sort of ORM) with no value added.&amp;nbsp; i.e. an ORM that will manage the child-parent relationship (and either pre-load or lazy-load aggregates) doesn&amp;#39;t provide much value here.&lt;/p&gt;
&lt;p&gt;The worst case scenario with an anaemic domain model is that there really is behaviour there; but it&amp;#39;s not handled in the domain entities; it&amp;#39;s handled in a different layer.&amp;nbsp; This is a problem because this circumvents the whole point of a domain model and layering.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;One indication of anaemia is that most of the domain classes&amp;nbsp; simply contain attributes.&amp;nbsp; Anyone familiar with patterns should recognize this as a Data Transfer Object, not a Domain Entity.&amp;nbsp; There&amp;#39;s nothing wrong with DTOs, they&amp;#39;re very important in almost all systems with any sort of complexity; but they&amp;#39;re not Domain Entities.&amp;nbsp; Let&amp;#39;s be truthful, there are systems with little or no behaviour in the domain; and that&amp;#39;s not a bad thing.&amp;nbsp; Systems like this likely don&amp;#39;t need a Domain Model and may not need techniques like Domain Driven Design.&amp;nbsp; The quicker people recognize that, the quicker they can be using a more appropriate architecture and design.&amp;nbsp; In some extreme cases the anaemic-domain-entity-DTOs service other DTOs&lt;/p&gt;
&lt;p&gt;Now, where am I going with this?&amp;nbsp; Well, there&amp;#39;s been a series of guidance out of Microsoft Patterns and Practice about some application &amp;quot;patterns&amp;quot;.&lt;/p&gt;
&lt;p&gt;First, let me describe what a pattern is.&amp;nbsp; A pattern is a way of &amp;quot;documenting a solution to a design problem&amp;quot; [3].&amp;nbsp; First, for it to be a pattern, it needs to detail the problem and it&amp;#39;s context, then provide a solution.&amp;nbsp; The latest &amp;quot;patterns&amp;quot; from P&amp;amp;P do not detail the problem or a context.&amp;nbsp; They&amp;#39;re simply architectural descriptions.&lt;/p&gt;
&lt;p&gt;Now the association between the Anaemic Domain Model and the latest P&amp;amp;P guidance.&amp;nbsp; In 3 of the 5 recently publish &amp;quot;patterns&amp;quot; the following is detail is included: &amp;quot;A Domain Entity pattern is used to define business entities that contain data only.&amp;quot;&amp;nbsp; This is the very definition of an Anaemic Domain Model.&amp;nbsp; Plus, in the RIA pattern the following, contradictory, detail is included: &amp;quot;Domain entities are responsible for implementing business rules.&amp;nbsp; Entities from the domain model represent business objects that contain data and implement behavior [sic]. In other words, the business objects are responsible for implementing business operations and interacting with other business objects.&amp;quot;&lt;/p&gt;
&lt;p&gt;This is disconcerting because historically sample code and guidance from Microsoft is simply reused without thought.&amp;nbsp; This leads to poorly designed and architected applications, and the .NET community as a whole is seen as one that produces poor-quality code and design.&amp;nbsp; Without context about the problems these patterns try to solve, they will be misused&amp;mdash;likely forced upon contexts and situations where they don&amp;rsquo;t fit, simply because &amp;ldquo;they&amp;rsquo;re from Microsoft&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;[1] &lt;a href="http://www.martinfowler.com/bliki/AnemicDomainModel.html" title="MF Bliki- AnemicDomainModel"&gt;MF Bliki- AnemicDomainModel&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;[2] &lt;a href="http://en.wikipedia.org/wiki/Anemic_Domain_Model" title="Anemic Domain Model - Wikipedia, the free encyclopedia"&gt;Anemic Domain Model - Wikipedia, the free encyclopedia&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;[3] &lt;a href="http://en.wikipedia.org/wiki/Design_pattern_(computer_science)" title="Design pattern (computer science) - Wikipedia, the free encyclopedia"&gt;Design pattern (computer science) - Wikipedia, the free encyclopedia&lt;/a&gt;&lt;/p&gt;
&lt;div style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:18e4120a-82b3-45b3-8d5c-9449fa380fad" class="wlWriterSmartContent"&gt;Technorati Tags: &lt;a rel="tag" href="http://technorati.com/tags/Microsoft+Patterns+and+Practicies"&gt;Microsoft Patterns and Practicies&lt;/a&gt;,&lt;a rel="tag" href="http://technorati.com/tags/Anti-patterns"&gt;Anti-patterns&lt;/a&gt;,&lt;a rel="tag" href="http://technorati.com/tags/Anaemic"&gt;Anaemic&lt;/a&gt;,&lt;a rel="tag" href="http://technorati.com/tags/Anemic"&gt;Anemic&lt;/a&gt;,&lt;a rel="tag" href="http://technorati.com/tags/Domain+Model"&gt;Domain Model&lt;/a&gt;,&lt;a rel="tag" href="http://technorati.com/tags/Microsoft"&gt;Microsoft&lt;/a&gt;&lt;/div&gt;
&lt;div style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:bf1c39d4-6db7-4130-b9ff-995a8a4cb99f" class="wlWriterSmartContent"&gt;del.icio.us Tags: &lt;a rel="tag" href="http://del.icio.us/popular/Microsoft+Patterns+and+Practicies"&gt;Microsoft Patterns and Practicies&lt;/a&gt;,&lt;a rel="tag" href="http://del.icio.us/popular/Anti-patterns"&gt;Anti-patterns&lt;/a&gt;,&lt;a rel="tag" href="http://del.icio.us/popular/Anaemic"&gt;Anaemic&lt;/a&gt;,&lt;a rel="tag" href="http://del.icio.us/popular/Anemic"&gt;Anemic&lt;/a&gt;,&lt;a rel="tag" href="http://del.icio.us/popular/Domain+Model"&gt;Domain Model&lt;/a&gt;&lt;/div&gt;
&lt;div style="text-align:right;margin:0px;padding:4px 0px 4px 0px;" class="wlWriterHeaderFooter"&gt;&lt;a href="http://digg.com/submit?url=http%3a%2f%2fmsmvps.com%2fblogs%2fpeterritchie%2farchive%2f2009%2f01%2f29%2fa-upcoming-pandemic-of-domain-anaemia.aspx&amp;amp;title=A+Upcoming+Pandemic+of+Domain+Anaemia"&gt;&lt;img border="0" width="100" src="http://digg.com/img/badges/100x20-digg-button.png" alt="Digg This" height="20" style="border:0;" title="Digg This" /&gt;&lt;/a&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http://msmvps.com/blogs/peterritchie/archive/2009/01/29/a-upcoming-pandemic-of-domain-anaemia.aspx"&gt;&lt;img border="0" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://msmvps.com/blogs/peterritchie/archive/2009/01/29/a-upcoming-pandemic-of-domain-anaemia.aspx&amp;amp;bgcolor=0080C0&amp;amp;fgcolor=FFFFFF&amp;amp;border=000000&amp;amp;cbgcolor=D4E1ED&amp;amp;cfgcolor=000000" alt="DotNetKick This" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1666992" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/.NET+Development/default.aspx">.NET Development</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/Software+Development/default.aspx">Software Development</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/Design_2F00_Coding+Guidance/default.aspx">Design/Coding Guidance</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/Microsoft/default.aspx">Microsoft</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/Patterns/default.aspx">Patterns</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/OOD/default.aspx">OOD</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/AntiPattern/default.aspx">AntiPattern</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/DDD/default.aspx">DDD</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/ALT.NET/default.aspx">ALT.NET</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/Software+Development+Practices/default.aspx">Software Development Practices</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/Software+Development+Guidance/default.aspx">Software Development Guidance</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/Microsoft+Patterns+and+Practices/default.aspx">Microsoft Patterns and Practices</category></item><item><title>Pontificating Virtual Parameterized Constructors in C#</title><link>http://msmvps.com/blogs/peterritchie/archive/2008/11/18/pontificating-virtual-parameterized-constructors-in-c.aspx</link><pubDate>Tue, 18 Nov 2008 17:55:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1654469</guid><dc:creator>PeterRitchie</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/peterritchie/rsscomments.aspx?PostID=1654469</wfw:commentRss><comments>http://msmvps.com/blogs/peterritchie/archive/2008/11/18/pontificating-virtual-parameterized-constructors-in-c.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://blogs.msdn.com/tomholl/archive/2008/11/18/constructors-and-inheritance-why-is-this-still-so-painful.aspx"&gt;Tom Hollander recently posted&lt;/a&gt; about a change he required to the Enterprise Library for date/time validation.&amp;nbsp; He had to create a new class (rather than modify the Enterprise Library) that derived from another, defective class.&amp;nbsp; One of his complaints was that in order to effectively implement the base class he had to also write matching constructors that simply called the base class.&amp;nbsp; His suggestion was effectively to add the concept of virtual parameterized constructors to C#.&amp;nbsp; I detail &amp;ldquo;parameterized constructors&amp;rdquo; because C# already effectively has virtual default constructors.&amp;nbsp; In the following example the base constructor (Form()) is automatically called by the derivative:&lt;/p&gt;
&lt;div style="font-size:10pt;background:white;color:black;font-family:courier new;"&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MyForm&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;Form&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; MyForm()&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Virtual parameterized constructors are not new, and from a mere language standpoint this seems reasonable.&amp;nbsp; Pragmatically though, I believe, this is another story.&amp;nbsp; It seems logical to be able to simply inherit the parameterized constructors of the base class; but, there are so many times that this isn&amp;#39;t the case or some generally accepted principles that would be contravened by a language addition like this. &lt;/p&gt;
&lt;p&gt;Let&amp;#39;s first look at the &lt;a href="http://en.wikipedia.org/wiki/Open/closed_principle"&gt;open/closed principle&lt;/a&gt; (OCP).&amp;nbsp; The OCP suggests classes should be open for extension but closed for modification.&amp;nbsp; Robert Martin suggests [1] properly designed class hierarchies that obey OCP implement an abstraction; i.e. derive from an abstract class or implement an interface.&amp;nbsp; For example: &lt;/p&gt;
&lt;div style="font-size:10pt;background:white;color:black;font-family:courier new;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;interface&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IShape&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Draw(&lt;span style="color:#2b91af;"&gt;Graphics&lt;/span&gt; graphics);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Rectangle&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;IShape&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:#008000;"&gt;//...&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Draw(&lt;span style="color:#2b91af;"&gt;Graphics&lt;/span&gt; graphics)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:#808080;"&gt;///&lt;/span&gt;&lt;span style="color:#008000;"&gt;...&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Second, let&amp;#39;s look at the &amp;quot;&lt;a href="http://www.ubookcase.com/book/Addison.Wesley/CPP.Coding.Standards.101.Rules.Guidelines.and.Best.Practices/0321113586/ch34lev1sec2.html"&gt;prefer composition over inheritance&lt;/a&gt;&amp;quot; principle.&amp;nbsp; The effect of a language change like this on a design that prefers composition should be fairly obvious.&amp;nbsp; Here&amp;#39;s an example of this principle: &lt;/p&gt;
&lt;div style="font-size:10pt;background:white;color:black;font-family:courier new;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;interface&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IPolygon&lt;/span&gt; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Draw(&lt;span style="color:#2b91af;"&gt;Graphics&lt;/span&gt; graphics);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;sealed&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Polygon&lt;/span&gt; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;readonly&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Point&lt;/span&gt;[] points;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Polygon(&lt;span style="color:#2b91af;"&gt;Point&lt;/span&gt;[] points) {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:#0000ff;"&gt;this&lt;/span&gt;.points = points;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Draw(&lt;span style="color:#2b91af;"&gt;Graphics&lt;/span&gt; graphics) {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:#0000ff;"&gt;for&lt;/span&gt;(&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt; i = 1; i &amp;lt; points.Length; i++) {&lt;/p&gt;
&lt;p 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; graphics.DrawLine(&lt;span style="color:#2b91af;"&gt;Pens&lt;/span&gt;.Black, points[i-1], points);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Rectangle&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;IPolygon&lt;/span&gt; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;readonly&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Polygon&lt;/span&gt; polygon;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Rectangle(&lt;span style="color:#2b91af;"&gt;Point&lt;/span&gt; location, &lt;span style="color:#2b91af;"&gt;Size&lt;/span&gt; size) {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:#2b91af;"&gt;Point&lt;/span&gt;[] points = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Point&lt;/span&gt;[5];&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; points[4] = points[0] = location;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; points[1] = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Point&lt;/span&gt;(location.X + size.Width, location.Y);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; points[2] = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Point&lt;/span&gt;(location.X + size.Width, location.Y + size.Height);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; points[3] = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Point&lt;/span&gt;(location.X, location.Y + size.Height);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; polygon = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Polygon&lt;/span&gt;(points);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Draw(&lt;span style="color:#2b91af;"&gt;Graphics&lt;/span&gt; graphics) {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; polygon.Draw(graphics);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Obviously there is no way to use virtual parameterized constructors here.&lt;/p&gt;
&lt;p&gt;Clearly, designs that take into account OCP and prefer-composition-over-inheritance would not benefit from a &amp;quot;virtual parameterized constructor&amp;quot; language addition. &lt;/p&gt;
&lt;p&gt;Finally, let&amp;#39;s look at why a class might have many constructors causing such friction for derivatives.&amp;nbsp; There&amp;#39;s many reasons why a class might have many constructors.&amp;nbsp; I believe all are indications of a poorly designed class.&amp;nbsp; My first thought would be that many constructors is a result of a large class and that the large-class-code-smell should be an indication for redesign.&amp;nbsp; A large class could be in an indication of a motherclass; but in either case this is likely a single responsibility principle (SRP) violation and the class is doing much more than it should and be redesigned.&amp;nbsp; If the class isn&amp;#39;t large but has many constructors, this was likely done not in response to how the class should/would be used but to cover every possible way of constructing the type.&amp;nbsp; This would then be a &lt;a href="http://en.wikipedia.org/wiki/You_Ain&amp;#39;t_Gonna_Need_It"&gt;YAGNI&lt;/a&gt; violation and the number of constructors should simply be pared down. &lt;/p&gt;
&lt;p&gt;But, what about when you have to deal with poorly design hierarchies and don&amp;#39;t have the ability to modify them?&amp;nbsp; A valid point; but, simply for the lack of friction of writing pass-through constructors I don&amp;#39;t think adding to the language to support poorly designed classes is a good for the language or its developers. &lt;/p&gt;
&lt;p&gt;While an addition like virtual parameterized constructors seems benign, its limited actual usefulness makes the effort not worth the reward.&amp;nbsp; Plus, it introduces greater abilities to create poorly designed types. &lt;/p&gt;
&lt;p&gt;[1] &lt;a href="http://www.objectmentor.com/resources/articles/ocp.pdf"&gt;http://www.objectmentor.com/resources/articles/ocp.pdf&lt;/a&gt; &lt;/p&gt;
&lt;div style="text-align:left;margin:0px;padding:4px 4px 4px 4px;" class="wlWriterHeaderFooter"&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http://msmvps.com/blogs/peterritchie/archive/2008/11/18/pontificating-virtual-parameterized-constructors-in-c.aspx&amp;amp;title=Pontificating%20Virtual%20Parameterized%20Constructors%20in%20C"&gt;&lt;img border="0" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://msmvps.com/blogs/peterritchie/archive/2008/11/18/pontificating-virtual-parameterized-constructors-in-c.aspx&amp;amp;bgcolor=0080C0&amp;amp;fgcolor=FFFFFF&amp;amp;border=000000&amp;amp;cbgcolor=D4E1ED&amp;amp;cfgcolor=000000" alt="DotNetKicks Image" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1654469" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/Software+Development/default.aspx">Software Development</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/Pontification/default.aspx">Pontification</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/Patterns/default.aspx">Patterns</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/OOD/default.aspx">OOD</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/AntiPattern/default.aspx">AntiPattern</category></item><item><title>Bridges and Factories</title><link>http://msmvps.com/blogs/peterritchie/archive/2007/12/21/bridges-and-factories.aspx</link><pubDate>Fri, 21 Dec 2007 15:45:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1409857</guid><dc:creator>PeterRitchie</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/peterritchie/rsscomments.aspx?PostID=1409857</wfw:commentRss><comments>http://msmvps.com/blogs/peterritchie/archive/2007/12/21/bridges-and-factories.aspx#comments</comments><description>&lt;p&gt;In my &lt;a class="" href="http://msmvps.com/blogs/peterritchie/archive/2007/12/13/dependancy-injection.aspx"&gt;previous post&lt;/a&gt;, I talked about Dependency Injection (DI).&amp;nbsp; One&amp;nbsp;implementation&amp;nbsp;of DI is using interface-oriented design to abstract a class from an injected dependency via an interface.&amp;nbsp; This is one possible implementation of the Bridge pattern.&amp;nbsp; Depending only upon an interface and classes to implement that interface opens up some very interesting possibilities.&lt;/p&gt;
&lt;p&gt;The tried-and-true&amp;nbsp;means of creating an object is to simply directly use it&amp;#39;s constructor, for 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;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:Courier New;"&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;IPerson&lt;/span&gt; person = &lt;span style="COLOR:blue;"&gt;new&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;Employee&lt;/span&gt;(&lt;span style="COLOR:#a31515;"&gt;&amp;quot;Peter&amp;quot;&lt;/span&gt;, &lt;span style="COLOR:#a31515;"&gt;&amp;quot;Ritchie&amp;quot;&lt;/span&gt;);&lt;/pre&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is nice and simple; but we&amp;#39;re still coupled to &lt;font face="courier new,courier"&gt;Employee&lt;/font&gt; despite using an &lt;font face="courier new,courier"&gt;IPerson&lt;/font&gt; object thereafter.&amp;nbsp; The above code hasn&amp;#39;t truly eliminated the dependency upon the &lt;font face="courier new,courier"&gt;Employee&lt;/font&gt; class.&amp;nbsp;Moving all that type of logic to a single method encapsulates the logic into one, reusable, place.&amp;nbsp; This is the Factory pattern, for 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;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; person = &lt;span style="COLOR:#2b91af;"&gt;Person&lt;/span&gt;.Create(&lt;span style="COLOR:#a31515;"&gt;&amp;quot;Peter&amp;quot;&lt;/span&gt;, &lt;span style="COLOR:#a31515;"&gt;&amp;quot;Ritchie&amp;quot;&lt;/span&gt;);&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;font face="courier new,courier"&gt;Create&lt;/font&gt; method, a &amp;quot;Factory Method&amp;quot; on a concrete (likely static) class &lt;font face="courier new,courier"&gt;Person&lt;/font&gt;,&amp;nbsp;would know how to create an &lt;font face="courier new,courier"&gt;IPerson&lt;/font&gt; object.&amp;nbsp; At this point using a &lt;font face="courier new,courier"&gt;IPerson&lt;/font&gt; object is completely abstracted from a specific&amp;nbsp;concrete type&amp;nbsp;and its creation.&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:Courier New;"&gt;&lt;pre style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; InviteToMeeting(person);&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;font face="courier new,courier"&gt;InviteToMeeting&lt;/font&gt; is completely independent of the concrete type implementing &lt;font face="courier new,courier"&gt;IPerson&lt;/font&gt; (and it can actually be a private type) and how it was created or where it came from.&amp;nbsp; This abstraction leaves &lt;font face="courier new,courier"&gt;IPerson&lt;/font&gt; objects free to be created however they need to be.&amp;nbsp; The benefits of Dependency Injection, Bridge, and Factory patterns&amp;nbsp;are synergistic--together they offer more than if they were apart.&amp;nbsp; In the above example they&amp;#39;re created as Employee objects; but because their creation is completely encapsulated it could also be affected by policy.&amp;nbsp; Policy that clients of the factory would have no dependency upon.&lt;/p&gt;
&lt;p&gt;In the simplest case Person.Create could always just create Employee objects; but because creation is hidden from the client the factory method could use other logic to decide what classes to use.&amp;nbsp; Person.Create could be implemented this way:&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:Lucida Console Modified;"&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;IPerson&lt;/span&gt; Create ( &lt;span style="COLOR:#2b91af;"&gt;String&lt;/span&gt; firstName, &lt;span style="COLOR:#2b91af;"&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;if&lt;/span&gt; (&lt;span style="COLOR:#2b91af;"&gt;Configuration&lt;/span&gt;.IsAdministrator)&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;new&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;Employee&lt;/span&gt;(firstName, lastName);&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;else&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;new&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;Subordinate&lt;/span&gt;(firstName, lastName);&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;/div&gt;
&lt;p&gt;Now we&amp;#39;re injecting policy into the creation process.&amp;nbsp; Somewhere, likely&amp;nbsp;not in source code,&amp;nbsp;whether the current user is running as an administrator.&amp;nbsp; This state is used in a policy to decide what type of IPerson object to create.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1409857" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/.NET+Development/default.aspx">.NET Development</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/Patterns/default.aspx">Patterns</category></item><item><title>Dependency Injection</title><link>http://msmvps.com/blogs/peterritchie/archive/2007/12/13/dependancy-injection.aspx</link><pubDate>Thu, 13 Dec 2007 15:42:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1395819</guid><dc:creator>PeterRitchie</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/peterritchie/rsscomments.aspx?PostID=1395819</wfw:commentRss><comments>http://msmvps.com/blogs/peterritchie/archive/2007/12/13/dependancy-injection.aspx#comments</comments><description>&lt;p&gt;Dependency injection (DI) is a form of inversion of control.&amp;nbsp; There seems to be a tendency in some circles to refer to dependency injection as inversion of control (IoC).&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Dependency injection is a form of abstraction by removing physical dependencies between classes and potentially assemblies.&amp;nbsp; This abstraction can have many different motives.&amp;nbsp; One motive is for Aspect Oriented Software Development (ASOD) where you&amp;#39;re abstracting shared (or cross-cutting) concerns into independent classes.&amp;nbsp; Another motive is for Test-Driven Development (TDD) where you want to be able to test each class independently of its dependencies--in which case injected dependencies are &amp;quot;mocked&amp;quot;--technically this is still AOSD because you&amp;#39;re separating the testing concern.&lt;/p&gt;
&lt;p&gt;Dependency injection in .NET is most frequently implemented using interface-oriented design/programming, where a dependency of a class is on an interface.&amp;nbsp; Any class implementing that interface is then a candidate for being &amp;quot;injected&amp;quot; into the other class.&amp;nbsp;There&amp;#39;s many ways to inject dependency, one is at time of construction the other is at time of operation.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;There are many examples of dependency injection at time of operation in the .NET Framework.&amp;nbsp; Normally when an interface is involved, dependency injection is also involved.&amp;nbsp; Serialization is a good example.&amp;nbsp; Serialization in .NET allows programmers to serialize to&amp;nbsp;pretty much anything.&amp;nbsp; &amp;quot;Anything&amp;quot; being a stream.&amp;nbsp; Serialization abstracts the destination from the act of serializing and allows programmers to inject a dependency on any type of destination (media, resource, mock, etc.) simply by implementing &lt;font face="courier new,courier"&gt;IStream&lt;/font&gt;.&amp;nbsp; For example, for any given class in .NET I can &amp;quot;serialize&amp;quot; it to any stream:&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:Courier New;"&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;MyType&lt;/span&gt; myType = &lt;span style="COLOR:blue;"&gt;new&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;MyType&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:#2b91af;"&gt;IFormatter&lt;/span&gt; formatter = &lt;span style="COLOR:#2b91af;"&gt;Configuration&lt;/span&gt;.CreateFormatter(&lt;span style="COLOR:blue;"&gt;typeof&lt;/span&gt;(myType));&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;using&lt;/span&gt; (&lt;span style="COLOR:#2b91af;"&gt;Stream&lt;/span&gt; stream = &lt;span style="COLOR:blue;"&gt;new&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;MemoryStream&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; formatter.Serialize(stream, myType);&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:green;"&gt;// TODO: something with stream.&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;/div&gt;
&lt;p&gt;In the above example, &lt;font face="courier new,courier"&gt;IFormatter.Serialize(Stream, Object)&lt;/font&gt;, uses dependency injection at time of operation to serialize to a &lt;font face="courier new,courier"&gt;MemoryStream&lt;/font&gt; object.&amp;nbsp; In no way is &lt;font face="courier new,courier"&gt;IFormatter.Serialize&lt;/font&gt; dependant on &lt;font face="courier new,courier"&gt;MemoryStream&lt;/font&gt;.&amp;nbsp; The same call could be written as follows:&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:Courier New;"&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;using&lt;/span&gt;(&lt;span style="COLOR:#2b91af;"&gt;Stream&lt;/span&gt; stream = &lt;span style="COLOR:blue;"&gt;new&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;FileStream&lt;/span&gt;(&lt;span style="COLOR:#a31515;"&gt;&amp;quot;file.dat&amp;quot;&lt;/span&gt;, &lt;span style="COLOR:#2b91af;"&gt;FileMode&lt;/span&gt;.Create))&lt;/pre&gt;&lt;pre style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&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; formatter.Serialize(stream, myType);&lt;/pre&gt;&lt;pre style="MARGIN:0px;"&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:green;"&gt;// TODO: something with stream.&lt;/span&gt;&lt;/pre&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The above form of DI is interface (and class) based, in that you need to implement a specific&amp;nbsp;interface to inject as a dependency.&amp;nbsp; Another form of DI is via delegates.&amp;nbsp; You can abstract a particular class from other concerns simply by accepting a delegate.&amp;nbsp; Any class that wants to be injected simply needs to implement a method with the same signature as the required delegate.&amp;nbsp; Events are an example of this.&amp;nbsp; Any class that raises events is completely independent of any other classes that want to act as observers and subscribe to these events.&amp;nbsp; Subscribers are, in effect,&amp;nbsp;are preforming dependency injection by subscribing to an event.&amp;nbsp; This form of DI is not limited to DI,&amp;nbsp;it can be any delegate.&amp;nbsp;&amp;nbsp;In&amp;nbsp;.NET 2.0 there were many examples of this introduced.&amp;nbsp; &lt;font face="courier new,courier"&gt;List.ForEach&lt;/font&gt;, for 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;namespace&lt;/span&gt; Examples&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; System.Collections.Generic;&lt;/pre&gt;&lt;pre style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;public&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;class&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;Program&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;static&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;void&lt;/span&gt; Main()&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;List&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;string&lt;/span&gt;&amp;gt; names = &lt;span style="COLOR:blue;"&gt;new&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="COLOR:blue;"&gt;string&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:green;"&gt;// TODO: populate names&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; names.ForEach(Print);&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;static&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;void&lt;/span&gt; Print(&lt;span style="COLOR:#2b91af;"&gt;String&lt;/span&gt; name)&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;Console&lt;/span&gt;.WriteLine(name);&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;p&gt;In this example, &lt;font face="courier new,courier"&gt;List&amp;lt;T&amp;gt;&lt;/font&gt;, of course, is&amp;nbsp;completely independent from &lt;font face="courier new,courier"&gt;Program&lt;/font&gt; and&amp;nbsp;&lt;font face="courier new,courier"&gt;Program&lt;/font&gt; is free to be static because there are no interface implementation requirements.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1395819" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/.NET+Development/default.aspx">.NET Development</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/Software+Development/default.aspx">Software Development</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/Patterns/default.aspx">Patterns</category></item></channel></rss>