Simplest Web API DI CW
by clayshannon
HTML
<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.
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. Just as when you apply the brakes in your car, you don’t have to -- or want to -- directly squeeze the brake pad, but rather press your foot against a pedal. The pedal is an interface that can be swapped out, in case you want to replace the standard, boring, rectangular piece of rubber and with a snazzy chrome one shaped like a bare foot, stylized with exaggeratedly outsized toes. The brake pad just provides an extension point, to which you can attach whatever kind of foot pedal you want – it doesn’t care about its shape or color or chemical makeup. The same with interfaces and concrete classes – as long as the class you end up using “fits” the interface, all is well and it doesn’t care or know whether you have bad taste or more money than you know what to do with. Anyway, enough of that…
DI (as I’ll...