Dependency Injection / Inversion of Control Patterns

Unity Application Block.

Unity with Service Locator

From MSDN, “The Unity Application Block (Unity) is a lightweight, extensible dependency injection container”

What is a Dependency Injection Container Anyway?

For those of you who work on projects that follow a composition model over an inheritance model, containers are an ideal solutin. A DI container, sometimes referred to as an inverson of control container or IOC container is a container which “contains” and manages some type of object abstraction. The container takes care of instantiations, injecting dependencies, sometimes singleton lifetime management, as well as supplying “cross-cutting” services to objects being hosted inside the container.

From MSDN, “A cross-cutting service is defined as a service that is generic enough to be applicable across different contexts, while providing specific benefits. An example of a cross-cutting service is a logging framework that logs all method calls within an application.”

Inversion Principal.

Inversion referrs to inverting or “filpping” the way you think about your program design in an object oriented way. If your programming model interrupts the normal control flow then you are probably using some sort of Inversion principal in your design.

Inversion can occur in Event-driven programming in the case of say custom ASP.Net user controls, the “bubble up” of events from the user control to the page can be thought of as a form of Inversion because it “reverses” the normal control flow which would be the page calling the control and “observing” an event on the custom control by registering and event handler on the calling page.

Another form of inversion is Dependency Injection. This is one of my favorite patterns and was first coined by Martin Fowler. Basically, if an Object needs access to a another object (we’ll call it service for this text), the object takes on the task of acquiring the service in one of 3 ways:

· Interface injection: The Service provides an interface which consumers must implement. The interface exposes specific behaviors at runtime.

· Setter injection: The dependent Object exposes a “Setter” method to inject the dependency.

· Constructor injectin: Dependencies are injected through the class constructor.

Inversion takes yet another form with the use of the Factory Pattern. In the factory pattern, the Factory takes care of any initialization steps for that object. The factory also takes care of producing different kinds of objects for the Dependend Object. The Dependent Object therefore no longer hast to depend on each type of concrete object, knowing how to construct each one, and instead looks to the factory to take care of these repetative tasks for it, thereby inverting control.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: