<?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>Code As Dependency Configuration</title><link>http://blogs.msdn.com/ploeh/archive/2007/05/30/CodeAsDependencyConfiguration.aspx</link><description>In his article on Inversion of Control and Dependency Injection , Martin Fowler has 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</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Code As Dependency Configuration</title><link>http://blogs.msdn.com/ploeh/archive/2007/05/30/CodeAsDependencyConfiguration.aspx#2991400</link><pubDate>Wed, 30 May 2007 20:58:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2991400</guid><dc:creator>Udi Dahan</dc:creator><description>&lt;p&gt;Given that you're top level assembly has all the required concrete dependencies in order to use code instead of configuration, you could still call into something like Spring.net. (I'm just using your preconditions for code to work, in order to rebutt your &amp;quot;doesn't copy to output directory&amp;quot; argument.)&lt;/p&gt;
&lt;p&gt;There's also runtime object creation that DI frameworks provide, that are incredibly useful in scenarios like the one described here:&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://udidahan.weblogs.us/2007/04/30/generic-validation/"&gt;http://udidahan.weblogs.us/2007/04/30/generic-validation/&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;You still haven't solved the abstract factory issue with your code example. For instance, how would you do:&lt;/p&gt;
&lt;p&gt;spring.GetObjectsOfType(typeof(IValidator&amp;lt;Customer&amp;gt;));&lt;/p&gt;
&lt;p&gt;To get all the classes which should validate your customer object.&lt;/p&gt;
&lt;p&gt;Other than that, I agree that code, with its intellisense support is probably more convenient than XML for wiring dependencies.&lt;/p&gt;
</description></item><item><title>re: Code As Dependency Configuration</title><link>http://blogs.msdn.com/ploeh/archive/2007/05/30/CodeAsDependencyConfiguration.aspx#3003486</link><pubDate>Thu, 31 May 2007 10:42:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3003486</guid><dc:creator>ploeh</dc:creator><description>&lt;P&gt;Hi Udi&lt;/P&gt;
&lt;P&gt;Thank you for your comments.&lt;/P&gt;
&lt;P&gt;The point of this post was not to claim that Spring.NET doesn't support this technique, but just to describe the benefits of Code As Configuration. The reason I mentioned Spring.NET was just because this is a relatively well-known framework that (also) supports the XML configuration option, with the advantages and disadvantages this entails.&lt;/P&gt;
&lt;P&gt;I totally agree that you could use Spring.NET or other DI frameworks to achieve the same effect as the one I was aiming for - I just didn't want to make knowledge of a particular framework a precondition for reading the post, which is why I used Constructor Injection in my example.&lt;/P&gt;
&lt;P&gt;In this post, however, I wasn't trying to solve the Abstract Factory issue at all :)&lt;/P&gt;
&lt;P&gt;In the example outlined, an Abstract Factory shouldn't be allowed at all; if you want a list of IValidator&amp;lt;Customer&amp;gt;, you should take an instace of IList&amp;lt;IValidator&amp;lt;Customer&amp;gt;&amp;gt; as a constructor parameter. That's just the approach taken in this sample, and I'm not saying that you shouldn't use Abstract Factory at all, but for code consistency, I think it's a good idea to stick with a single strategy - either Constructor Injection, or Abstract Factory, or something else. In fact, my favorite is &lt;A class="" href="http://blogs.msdn.com/ploeh/archive/2007/03/25/ProviderInjection.aspx"&gt;Provider Injection&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;When used for DI, Abstract Factory is very similar to Service Locator. Although I've been very fond of Service Locator in the past, these days I prefer Constructor or Provider Injection, because these patterns lets the API communicate intent more clearly. Maybe I should write a separate blog post on this subject?&lt;/P&gt;</description></item><item><title>re: Code As Dependency Configuration</title><link>http://blogs.msdn.com/ploeh/archive/2007/05/30/CodeAsDependencyConfiguration.aspx#3008013</link><pubDate>Thu, 31 May 2007 16:34:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3008013</guid><dc:creator>Mike D</dc:creator><description>&lt;p&gt;I know this is a little OT, but it does apply to constructor injection.&lt;/p&gt;
&lt;p&gt;My question is why not have interfaces that support construtor contracts? Perhaps there is some deep reasoning and the universe might explode or there are other problems with this, &amp;nbsp;but I have seen many instances where this would be useful.&lt;/p&gt;</description></item><item><title>re: Code As Dependency Configuration</title><link>http://blogs.msdn.com/ploeh/archive/2007/05/30/CodeAsDependencyConfiguration.aspx#3008475</link><pubDate>Thu, 31 May 2007 17:18:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3008475</guid><dc:creator>ploeh</dc:creator><description>&lt;p&gt;Hi Mike D&lt;/p&gt;
&lt;p&gt;Thank you for asking - although the universe will not explode, our solar system might disintegrate if we do something like this :)&lt;/p&gt;
&lt;p&gt;Joking aside, there are at least two answers to that question: One having to do with practical constrains, and one having to do with design and architecture.&lt;/p&gt;
&lt;p&gt;Let's do the first one first:&lt;/p&gt;
&lt;p&gt;Interfaces don't support the modelling of constructors, and public constructors in abstract classes don't make any sense, so pure constructors are out of the question. Then what about factory methods? Well, there's actually the Abstract Factory design pattern, which is probably the closest you can come to specifying a contract for object creation, but that still leaves the question of how to create the abstract factory itself.&lt;/p&gt;
&lt;p&gt;Basically, there's no support for such things in the BCL.&lt;/p&gt;
&lt;p&gt;From a design perspective, it also makes a lot of sense to separate object creation from object behavior. Consider my example in this post. ClassC uses Abstraction5 which is supplied to it in its constructor. ClassB incidentally uses ClassC because it uses Abstraction3. Let's say that I wanted to change the hierarchy by replacing ClassC with a self-contained class (let's call it ClassCPrime). ClassCPrime doesn't consume Abstraction5, so it only exposes a default constructor. In other words, ClassC's constructor has a different signature than the constructor of ClassCPrime. This is what allows me to dramatically change the service hierarchy in the example.&lt;/p&gt;
&lt;p&gt;To extend this further, you may have some service implementations that are actually Singletons. Since we are not making any assumption on how objects are created, we could perfectly well pass a Singleton as a parameter for a class that uses Constructor Injection.&lt;/p&gt;
&lt;p&gt;This flexibility is very powerful, and that's why we don't have interfaces that specifies how implementations are created.&lt;/p&gt;
&lt;p&gt;This subject is rather complex and could actually benefit from a more thorough treatment, including code examples and illustrations, but I hope I managed to answer your question to at least some extent; otherwise, feel free to ask again.&lt;/p&gt;
</description></item><item><title>re: Code As Dependency Configuration</title><link>http://blogs.msdn.com/ploeh/archive/2007/05/30/CodeAsDependencyConfiguration.aspx#3019118</link><pubDate>Fri, 01 Jun 2007 09:04:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3019118</guid><dc:creator>Mike D</dc:creator><description>&lt;p&gt;Thanks for taking the time to explain, and actually I do get your point.&lt;/p&gt;
&lt;p&gt;Whatever the technical term for hitting the wall is, I hit the wall all the time. &amp;nbsp;I try and code my way out only to find myself in another corner.&lt;/p&gt;
&lt;p&gt;DI is GOOD, however we have no means of explicitly defining a contract for dependencies. We can define a contract for behavior.&lt;/p&gt;
&lt;p&gt;Everytime I find myself in this place, jumping through hoops, to do what seems to be a simple task, I can't help wonder are we missing the forest for the trees.&lt;/p&gt;
&lt;p&gt;Yes you answered my question well in the context of the example, I see the mess interface constructors could create.&lt;/p&gt;
&lt;p&gt;What if there were a way to specify that contract A requires contract B without the hoops?&lt;/p&gt;
&lt;p&gt;I really like your blog and would like to see more on decoupling layers.&lt;/p&gt;</description></item><item><title>re: Code As Dependency Configuration</title><link>http://blogs.msdn.com/ploeh/archive/2007/05/30/CodeAsDependencyConfiguration.aspx#3037965</link><pubDate>Sat, 02 Jun 2007 11:13:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3037965</guid><dc:creator>ploeh</dc:creator><description>&lt;p&gt;Hi Mike&lt;/p&gt;
&lt;p&gt;Thank you for your comments.&lt;/p&gt;
&lt;p&gt;The reason why it's beneficial not to specify that Contract A requires Contract B is exactly because of decoupling.&lt;/p&gt;
&lt;p&gt;My favorite example goes something like this: Imagine that you have on online shop with an Order class. To calculate discounts, the Order class calls into an abstract DiscountCalculator class, which is instantiated by DI in some way (pick your favorite DI technique).&lt;/p&gt;
&lt;p&gt;Imagine that you have a database-backed DisountCalculator implementation that will look up discounts for specific products in the database. This DatabaseDiscountCalculator uses an abstract Data Access Component (DAC) to access the product discount data.&lt;/p&gt;
&lt;p&gt;In essense, the Order class has a dependency on the DiscountCalculator 'contract', and the DatabaseDiscountCalculator has a dependency on the DAC 'contract'.&lt;/p&gt;
&lt;p&gt;However, it wouldn't be a good idea to explicitly model that the DiscountCalculator 'contract' requires the DAC 'contract', because you may want to create another implementation of DiscountCalculator that uses, say, a web service instead of a database; or maybe one that uses a simple algorith to calculate discounts without any data access at all.&lt;/p&gt;
&lt;p&gt;We wouldn't be able to model these extensibility configurations if we had required that DiscountCalculator requires the DAC (and only the DAC).&lt;/p&gt;
&lt;p&gt;While there are good reasons for not doing it, it IS possible to specifiy that contract A requires contract B: Just create a method on contract A along the lines of:&lt;/p&gt;
&lt;p&gt;public abstract ContracB GetContractB();&lt;/p&gt;
&lt;p&gt;There are other scenarios where this design is valuable, but not for defining layered object hierarchies.&lt;/p&gt;
</description></item><item><title>re: Code As Dependency Configuration</title><link>http://blogs.msdn.com/ploeh/archive/2007/05/30/CodeAsDependencyConfiguration.aspx#3041456</link><pubDate>Sat, 02 Jun 2007 15:12:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3041456</guid><dc:creator>Mike D</dc:creator><description>&lt;p&gt;Is there not a need for decoupling between the DC, DiscountCalculator, and the DAC/WS versions, where the discounts are stored is not it's concern. in other words each implementation of the DC would have it's own DI for which the order is unaware of.&lt;/p&gt;
&lt;p&gt;IOrder requires IDiscountCalc&lt;/p&gt;
&lt;p&gt;DACDiscountCalc requires IDataContext&lt;/p&gt;
&lt;p&gt;WSDiscountCalc requires IWebServiceContext&lt;/p&gt;
&lt;p&gt;DACDC implements IDiscountCalc&lt;/p&gt;
&lt;p&gt;WSDC implements IDiscountCalc&lt;/p&gt;
&lt;p&gt;IDiscountCalc dc =&lt;/p&gt;
&lt;p&gt;DiscountFactory.Create() { DAC || WS }&lt;/p&gt;
&lt;p&gt;IOrder o = &lt;/p&gt;
&lt;p&gt;OrderFactory.Create(dc)&lt;/p&gt;
&lt;p&gt;When I look at the DI techniques and can't help but think that the language is missing convention to handle this.&lt;/p&gt;
&lt;p&gt;One good example is generics, when introduced much of the code we write started looking a feeling right. This pretty much eliminated the need for the ( new keyword as used in overloading ) due to the lack of conariant return types. While talking about generics, I just had the need for a new() constratint that would look like this:&lt;/p&gt;
&lt;p&gt;&amp;lt;T&amp;gt;Create() : where T new(IDiscountCalc)&lt;/p&gt;
&lt;p&gt;This is a generic method that can create an object that must have a dependency on IDiscountCalc. &amp;nbsp;I had to resort do declaring an interface IDiscountCalcDependent.&lt;/p&gt;
&lt;p&gt;&amp;lt;T&amp;gt;Create() : where T : IDiscountCalcDependent&lt;/p&gt;
&lt;p&gt;Wouldn't it make more since if there were a language feature with compiler checks that allowed you to specify that A depends on B.&lt;/p&gt;
&lt;p&gt;I totally get DI constructor and setter, both need to be there, I just see the problem as the language should allow you to specify a dependency.&lt;/p&gt;
&lt;p&gt;Thanks for the discussion...&lt;/p&gt;</description></item><item><title>re: Code As Dependency Configuration</title><link>http://blogs.msdn.com/ploeh/archive/2007/05/30/CodeAsDependencyConfiguration.aspx#3050783</link><pubDate>Sun, 03 Jun 2007 00:25:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3050783</guid><dc:creator>ploeh</dc:creator><description>&lt;P&gt;Hi Mike&lt;/P&gt;
&lt;P&gt;If I understand what you are saying, you are not talking about chaining dependencies together in contracts (that's what I thought at first), but rather that you wish to be able to explicitly state that a particular implementation (Order) needs an instance of a particular abstraction (DiscountCalculator)?&lt;/P&gt;
&lt;P&gt;If this is the case, I can see that it quickly becomes quite complex when you take the Abstract Factory/Service Locator route.&lt;/P&gt;
&lt;P&gt;On the other hand, such semantics are automatically expressed by Constructor Injection, which is one of the reasons I like this pattern better.&lt;/P&gt;
&lt;P&gt;Your question inspired me to write a &lt;A class="" href="http://blogs.msdn.com/ploeh/archive/2007/06/02/StateYourDependencyIntent.aspx"&gt;post about this subject&lt;/A&gt; - I'm not sure it answers your question, but I thought it was a worthwhile subject none the less :)&lt;/P&gt;
&lt;P&gt;Please take a look and let me know if you think I understood your question :)&lt;/P&gt;</description></item><item><title>Fixture Setup With Installers</title><link>http://blogs.msdn.com/ploeh/archive/2007/05/30/CodeAsDependencyConfiguration.aspx#6579072</link><pubDate>Wed, 28 Nov 2007 17:00:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6579072</guid><dc:creator>ploeh blog</dc:creator><description>&lt;p&gt;It's no secret that I prefer unit tests over integration tests. Whenever it's possible to replace an&lt;/p&gt;
</description></item><item><title>Testing Theories Part 2 - Unit Testing with Inversion of Control Pro's/Con's</title><link>http://blogs.msdn.com/ploeh/archive/2007/05/30/CodeAsDependencyConfiguration.aspx#7781883</link><pubDate>Tue, 19 Feb 2008 01:39:56 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7781883</guid><dc:creator>Alec Lazarescu's Blog</dc:creator><description>&lt;p&gt;Inversion of Control (IoC) is often achieved by Dependency Injection and programming to interfaces and&lt;/p&gt;
</description></item><item><title>Modifying Behavior of WCF-Free Service Implementations</title><link>http://blogs.msdn.com/ploeh/archive/2007/05/30/CodeAsDependencyConfiguration.aspx#8662834</link><pubDate>Fri, 27 Jun 2008 23:55:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8662834</guid><dc:creator>ploeh blog</dc:creator><description>&lt;p&gt;In my previous post , I explained how to implement a WCF service without referencing WCF. In simple cases,&lt;/p&gt;
</description></item><item><title>Unit Testing Duplex WCF Clients</title><link>http://blogs.msdn.com/ploeh/archive/2007/05/30/CodeAsDependencyConfiguration.aspx#8709799</link><pubDate>Wed, 09 Jul 2008 00:06:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8709799</guid><dc:creator>ploeh blog</dc:creator><description>&lt;p&gt;In my previous post , I explained how to unit test a WCF service with callbacks. Since the scenario involves&lt;/p&gt;
</description></item><item><title>An Overview of Unit Testing Duplex WCF Services and Clients</title><link>http://blogs.msdn.com/ploeh/archive/2007/05/30/CodeAsDependencyConfiguration.aspx#8722660</link><pubDate>Sat, 12 Jul 2008 08:50:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8722660</guid><dc:creator>ploeh blog</dc:creator><description>&lt;p&gt;In the last couple of posts, I've demonstrated how to isolate implementation from WCF contract definition&lt;/p&gt;
</description></item></channel></rss>