MSDN Magazine - March 2008 - (Page 60) dictionary to store mapping information between interfaces and objects implementing those interfaces. We need a way to initially populate the dictionary, which is done by the Register (object obj) method (see Figure 4). Note that the Register method does not need to be on the IDependencyResolver interface because only the creator of the SimpleDependencyResolver will register dependencies. Typically this is done by a helper class called in the Main method during application startup. How does CompanyService find the SimpleDependencyResolver so that it can locate its dependencies? We could pass an IDependencyResolver into every class that needs it, but this quickly becomes cumbersome. The easiest solution is to place the configured SimpleDependencyResolver instance into a globally accessible location, which you can do using the static gateway pattern. (You could have also used the singleton pattern, but singletons are notoriously difficult to test. They are one of the biggest causes of tightly coupled code that’s difficult to test, as they’re little more than global variables in disguise. Avoid them if possible.) Let’s take a look at the static gateway, which I’ll call IoC. (Another possible name is DependencyResolver, but IoC is shorter.) The static methods on IoC match the methods on IDepenEven if your architecture is dencyResolver. (Note that layered, you may still have IoC doesn’t implement IDependencyResolver because tight coupling, which can static classes cannot impleimpede the testing and ment interfaces.) There is evolution of your app. also an Initialize method that accepts the real IDependencyResolver. The IoC static gateway simply forwards all Resolve requests to the configured IDependencyResolver: public class IoC { private static IDependencyResolver s_Inner; public static void Initialize(IDependencyResolver resolver) { s_Inner = resolver; } public static T Resolve () { return s_Inner.Resolve (); } Full-Fledged IoC Containers A simple IoC container such as SimpleDependencyResolver enables you to stitch together loosely coupled components. However, it lacks many of the features present in full-fledged IoC containers, including: • Wider configuration options, such as XML, code, or script • Lifetime management, such as singleton, transient, per-thread, or pooled • Auto-wiring dependencies • The ability to wire in new functionality Let’s discuss each of these features in more depth. I will use Castle Windsor, a widely used open source IoC container, as a concrete example. Many containers can be configured through an external XML file. For example, Windsor can be configured as follows: } During application startup, you initialize IoC with the configured SimpleDependencyResolver. You can now replace poor man’s dependency injection with IoC.Resolve in the default constructor: public InvoiceService() : this(IoC.Resolve (), IoC.Resolve (), IoC.Resolve ()) { } XML configuration is advantageous, as it can be modified without recompiling the application, though it does often require an application restart for the changes to be applied. It is not without its disadvantages though, as XML configuration tends to be very verbose; errors aren’t detected until runtime; and generic types are declared using the CLR’s backtick notation, rather than the more familiar C# generic notation. (Company.Application.IValidatorOf is written as Company.Application.IValidatorOf`1[[Company.Application.Invoice, Company.Application]], Company.Application.) In addition to XML, you can configure Windsor using C# or any other Microsoft .NET Framework-compliant language. If you isolated your configuration code in a separate assembly, changing configuration would mean simply recompiling the configuration assembly and restarting the application. You can script Windsor configuration using Binsor, which is a domain-specific language (DSL) built specifically for configuring Windsor. Binsor allows you to write your configuration files in Boo. (Boo is a statically typed CLR language that focuses on language and compiler extensibility, thus making it well suited for writing DSLs.) In Binsor, the previous XML configuration file could be rewritten as: import JamesKovacs.IoCArticle Component(“Foo”, IFoo, Foo) Note that you do not need to synchronize access to the inner IDependencyResolver, as it is only read but never updated, after application startup. The IoC class provides another benefit—it acts as an anticorruption layer in your application. If you want to use a different IoC container, you simply need to implement an adapter that implements IDependencyResolver. Even though IoC will be used extensively throughout your application, you have not coupled yourself to any particular container. 60 msdnmagazine Dependency Injection Things get more interesting when you realize that Boo is a fullfledged programming language, which means that you can use Binsor to automatically register types in Windsor without having to manually add component registrations, as you would with XML-based configuration: import System.Reflection serviceAssembly = Assembly.Load(“JamesKovacs.IoCArticle.IoCContainer”) for type in serviceAssembly.GetTypes(): continue if type.IsInterface or type.IsAbstract or type.GetInterfaces().Length == 0 Component(type.FullName, type.GetInterfaces()[0], type) Even if you’re not familiar with Boo, the intent of the code should
Table of Contents Feed for the Digital Edition of MSDN Magazine - March 2008 MSDN Magazine - March 2008 Contents Toolbox CLR Inside Out Data Points Advanced Basics Office Space Introducing ASP.NET MVC Loosen Up CI Server Performance Office Development Test Run Security Briefs Extreme ASP.NET Foundations .NET Matters {End Bracket} MSDN Magazine - March 2008 MSDN Magazine - March 2008 - (Page Intro) MSDN Magazine - March 2008 - Contents (Page Cover1) MSDN Magazine - March 2008 - Contents (Page Cover2) MSDN Magazine - March 2008 - Contents (Page 1) MSDN Magazine - March 2008 - Contents (Page 2) MSDN Magazine - March 2008 - Contents (Page 3) MSDN Magazine - March 2008 - Contents (Page 4) MSDN Magazine - March 2008 - Contents (Page 5) MSDN Magazine - March 2008 - Contents (Page 6) MSDN Magazine - March 2008 - Contents (Page 7) MSDN Magazine - March 2008 - Contents (Page 8) MSDN Magazine - March 2008 - Contents (Page 9) MSDN Magazine - March 2008 - Contents (Page 10) MSDN Magazine - March 2008 - Toolbox (Page 11) MSDN Magazine - March 2008 - Toolbox (Page 12) MSDN Magazine - March 2008 - Toolbox (Page 13) MSDN Magazine - March 2008 - Toolbox (Page 14) MSDN Magazine - March 2008 - CLR Inside Out (Page 15) MSDN Magazine - March 2008 - CLR Inside Out (Page 16) MSDN Magazine - March 2008 - CLR Inside Out (Page 17) MSDN Magazine - March 2008 - CLR Inside Out (Page 18) MSDN Magazine - March 2008 - CLR Inside Out (Page 19) MSDN Magazine - March 2008 - CLR Inside Out (Page 20) MSDN Magazine - March 2008 - Data Points (Page 21) MSDN Magazine - March 2008 - Data Points (Page 22) MSDN Magazine - March 2008 - Data Points (Page 23) MSDN Magazine - March 2008 - Data Points (Page 24) MSDN Magazine - March 2008 - Data Points (Page 25) MSDN Magazine - March 2008 - Data Points (Page 26) MSDN Magazine - March 2008 - Advanced Basics (Page 27) MSDN Magazine - March 2008 - Advanced Basics (Page 28) MSDN Magazine - March 2008 - Advanced Basics (Page 29) MSDN Magazine - March 2008 - Advanced Basics (Page 30) MSDN Magazine - March 2008 - Advanced Basics (Page 31) MSDN Magazine - March 2008 - Advanced Basics (Page 32) MSDN Magazine - March 2008 - Office Space (Page 33) MSDN Magazine - March 2008 - Office Space (Page 34) MSDN Magazine - March 2008 - Office Space (Page 35) MSDN Magazine - March 2008 - Office Space (Page 36) MSDN Magazine - March 2008 - Office Space (Page 37) MSDN Magazine - March 2008 - Office Space (Page 38) MSDN Magazine - March 2008 - Office Space (Page 39) MSDN Magazine - March 2008 - Office Space (Page 40) MSDN Magazine - March 2008 - Office Space (Page 41) MSDN Magazine - March 2008 - Introducing ASP.NET MVC (Page 42) MSDN Magazine - March 2008 - Introducing ASP.NET MVC (Page 43) MSDN Magazine - March 2008 - Introducing ASP.NET MVC (Page 44) MSDN Magazine - March 2008 - Introducing ASP.NET MVC (Page 45) MSDN Magazine - March 2008 - Introducing ASP.NET MVC (Page 46) MSDN Magazine - March 2008 - Introducing ASP.NET MVC (Page 47) MSDN Magazine - March 2008 - Introducing ASP.NET MVC (Page 48) MSDN Magazine - March 2008 - Introducing ASP.NET MVC (Page 49) MSDN Magazine - March 2008 - Introducing ASP.NET MVC (Page 50) MSDN Magazine - March 2008 - Introducing ASP.NET MVC (Page 51) MSDN Magazine - March 2008 - Introducing ASP.NET MVC (Page 52) MSDN Magazine - March 2008 - Introducing ASP.NET MVC (Page 53) MSDN Magazine - March 2008 - Loosen Up (Page 54) MSDN Magazine - March 2008 - Loosen Up (Page 55) MSDN Magazine - March 2008 - Loosen Up (Page 56) MSDN Magazine - March 2008 - Loosen Up (Page 57) MSDN Magazine - March 2008 - Loosen Up (Page 58) MSDN Magazine - March 2008 - Loosen Up (Page 59) MSDN Magazine - March 2008 - Loosen Up (Page 60) MSDN Magazine - March 2008 - Loosen Up (Page 61) MSDN Magazine - March 2008 - Loosen Up (Page 62) MSDN Magazine - March 2008 - Loosen Up (Page 63) MSDN Magazine - March 2008 - Loosen Up (Page 64) MSDN Magazine - March 2008 - Loosen Up (Page 65) MSDN Magazine - March 2008 - Loosen Up (Page 66) MSDN Magazine - March 2008 - Loosen Up (Page 67) MSDN Magazine - March 2008 - Loosen Up (Page 68) MSDN Magazine - March 2008 - Loosen Up (Page 69) MSDN Magazine - March 2008 - CI Server (Page 70) MSDN Magazine - March 2008 - CI Server (Page 71) MSDN Magazine - March 2008 - CI Server (Page 72) MSDN Magazine - March 2008 - CI Server (Page 73) MSDN Magazine - March 2008 - CI Server (Page 74) MSDN Magazine - March 2008 - CI Server (Page 75) MSDN Magazine - March 2008 - CI Server (Page 76) MSDN Magazine - March 2008 - CI Server (Page 77) MSDN Magazine - March 2008 - CI Server (Page 78) MSDN Magazine - March 2008 - CI Server (Page 79) MSDN Magazine - March 2008 - CI Server (Page 80) MSDN Magazine - March 2008 - Performance (Page 81) MSDN Magazine - March 2008 - Performance (Page 82) MSDN Magazine - March 2008 - Performance (Page 83) MSDN Magazine - March 2008 - Performance (Page 84) MSDN Magazine - March 2008 - Performance (Page 85) MSDN Magazine - March 2008 - Performance (Page 86) MSDN Magazine - March 2008 - Performance (Page 87) MSDN Magazine - March 2008 - Performance (Page 88) MSDN Magazine - March 2008 - Office Development (Page 89) MSDN Magazine - March 2008 - Office Development (Page 90) MSDN Magazine - March 2008 - Office Development (Page 91) MSDN Magazine - March 2008 - Office Development (Page 92) MSDN Magazine - March 2008 - Office Development (Page 93) MSDN Magazine - March 2008 - Office Development (Page 94) MSDN Magazine - March 2008 - Office Development (Page 95) MSDN Magazine - March 2008 - Office Development (Page 96) MSDN Magazine - March 2008 - Test Run (Page 97) MSDN Magazine - March 2008 - Test Run (Page 98) MSDN Magazine - March 2008 - Test Run (Page 99) MSDN Magazine - March 2008 - Test Run (Page 100) MSDN Magazine - March 2008 - Test Run (Page 101) MSDN Magazine - March 2008 - Test Run (Page 102) MSDN Magazine - March 2008 - Test Run (Page 103) MSDN Magazine - March 2008 - Test Run (Page 104) MSDN Magazine - March 2008 - Test Run (Page 105) MSDN Magazine - March 2008 - Test Run (Page 106) MSDN Magazine - March 2008 - Security Briefs (Page 107) MSDN Magazine - March 2008 - Security Briefs (Page 108) MSDN Magazine - March 2008 - Security Briefs (Page 109) MSDN Magazine - March 2008 - Security Briefs (Page 110) MSDN Magazine - March 2008 - Extreme ASP.NET (Page 111) MSDN Magazine - March 2008 - Extreme ASP.NET (Page 112) MSDN Magazine - March 2008 - Extreme ASP.NET (Page 113) MSDN Magazine - March 2008 - Extreme ASP.NET (Page 114) MSDN Magazine - March 2008 - Extreme ASP.NET (Page 115) MSDN Magazine - March 2008 - Extreme ASP.NET (Page 116) MSDN Magazine - March 2008 - Extreme ASP.NET (Page 117) MSDN Magazine - March 2008 - Extreme ASP.NET (Page 118) MSDN Magazine - March 2008 - Foundations (Page 119) MSDN Magazine - March 2008 - Foundations (Page 120) MSDN Magazine - March 2008 - Foundations (Page 121) MSDN Magazine - March 2008 - Foundations (Page 122) MSDN Magazine - March 2008 - Foundations (Page 123) MSDN Magazine - March 2008 - Foundations (Page 124) MSDN Magazine - March 2008 - Foundations (Page 125) MSDN Magazine - March 2008 - Foundations (Page 126) MSDN Magazine - March 2008 - Foundations (Page 127) MSDN Magazine - March 2008 - Foundations (Page 128) MSDN Magazine - March 2008 - .NET Matters (Page 129) MSDN Magazine - March 2008 - .NET Matters (Page 130) MSDN Magazine - March 2008 - .NET Matters (Page 131) MSDN Magazine - March 2008 - {End Bracket} (Page 132) MSDN Magazine - March 2008 - {End Bracket} (Page Cover3) MSDN Magazine - March 2008 - {End Bracket} (Page Cover4)
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.