Dependency Injection with StructureMap and ASP.NET MVC

According to this article, DI or Dependency Injection is a software design pattern, a particular case of the Inversion of Control pattern, in which one or more dependencies are injected into dependent objects. The pattern is used to create program designs that are loosely coupled and testable.

For me, what matters is the last 2 terms “loosely coupled” and “testable”. But with the number of DI tools out there which would you choose? Well, this article has the best comparison so far. And I have decided to use structuremap because its actively maintained, plenty of new features which I am going to explore later on on the next blog.  But overall? Structuremap is highly configurable.

So, let’s start.

1. Get structuremap via nuget.

nuget

 

 

2. For this article lets resolved the dependencies in our Global.asax file.

– In the below snippet on Application_Start method, Structuremap basically has an ObjectFactory class that let’s you configure your dependency resolutions. On the below snippet, I am telling structuremap to grab the concrete class CustomerProvider for my ICustomerProvider interface.

ObjectFactory.Initialize(x =>
{
     x.For<ICustomerProvider>().Use<CustomerProvider>();
});

 

3. Now time for the real use of DI.
– In the below snippet is what is called Constructor Injection. There is another way too called Property Injection. Basically, what it does is to simply remove the concrete class from being assigned to ICustomerProvider. So that later on, I can modify the concrete class’s implementation without modifying my controller.

public class HomeController : Controller
{
        ICustomerProvider provider;

        public HomeController(ICustomerProvider provider) {
            this.provider = provider;
        }

        public ActionResult CustomerList()
        {
            return PartialView(provider.GetCustomers());
        }
}

 

4. If we run the program and we encounter a parameterless constructor issue. The solution to that is override the defaultcontrollerfactory and let Structuremap instantiate the controllers.

public class StructureMapControllerFactory : DefaultControllerFactory
    {
        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            if (controllerType == null)
                return base.GetControllerInstance(requestContext, controllerType);

            return ObjectFactory.GetInstance(controllerType) as Controller;
        }
    }

– Then we modify our initialization to this.

ObjectFactory.Initialize(x =>
{
     x.For<ICustomerProvider>().Use<CustomerProvider>();
});
ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory());

On the next blog, I will cover a deeper topic on this.

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

Blog at WordPress.com.

Up ↑

%d bloggers like this: