New Web API DI with CW

by clayshannon

HTML

<ul class="download"><li><a href="PlatypusASPNETWebAPIIOCDICWSimpleSample.zip">Download sample - 376.8 KB</a></li></ul>

<h2>ON YOUR MARK</h2>

<p>You have to walk before you can run; and sometimes, before you can walk, you have to crawl on your hands and knees for miles until you finally achieve some semblance of enlightenment.</p>

<p>As has been said, <cite>&quot;Often those who start using DI quickly find themselves lost in a sea of confusion.&quot;</cite></p>

<p>The last three weeks have seemed like a bloody-kneecapped pilgrimage for me to understand and implement Inversion of Control (IoC) and Dependency Injection (DI). In an attempt to write the article that I wished I had when I began this quest, I will try to make this step-by-step tutorial as basic and grokkable as possible, and we will build an As-Simple-as-Possible ASP.NET Web API project that uses Castle Windsor to implement basic DI.</p>

<h2>GET SET / GET YOUR MIND RIGHT</h2>
<p>First, a little conceptual background as to why DI is not just another acronym or the latest snake oil sold to unsuspecting managers for them to foist upon their weary and wary developers: Dependency Injection is a way to write to an interface rather than an implementation – IOW, code to an interface or abstraction rather than to a concrete implementation (a class or “component”); This is a good thing, as it allows for extensibility  - you’re not “locked in” to a particular class.</p>

<p>DI (as I’ll call Dependency Injection from now on) is closely related to Inverion of Control (which I’ll refer to as IoC from now on). What this means is that instead of a class instantiating a concrete class that it itself first declares, it uses a constructor that takes an interface type as an argument, and a concrete class that implements that interface is passed to it.</p>

<h2>FLASH FORWARD/PRECAP</h2>
<p>To quickly give a rundown of what code needs to be added to a Web API project to "Windsorize" it, before going through it step by...