Dependency Injection in ASP.NET MVC 5 with StructureMap

Dependency Injection(DI) is a design pattern that let you write loosely coupled testable code. Typically your classes depend on other classes. Take a look at the following example. The above example shows the "HomeController" of an asp.net mvc application. Within the index method, you can see that it requires to create an instance of "Student" class to get the full name of a student. It is very clear that the Index method is tightly coupled with the Student class. 
This is a disadvantage when it comes to testing HomeController. Suppose that you have other methods, with similar types of class dependencies. So, each time when you test the code, you have to pass an actual instance Student class. This is tedious. You don't have the luxury of passing a fake student class to easily test your controllers. 
So how do we decouple the Student class from HomeController? This is where DI comes in handy. You can inject dependencies in several ways. Today i will show you "constructor injection", where you simply inject the dependencies to the constructor of the class. (Ex: in the above example, inject dependencies to the constructor of the HomeController). 

Step 01 - Setup Structure Map

a). Create a new MVC5 application in Visual Studio.
b). Let's use popular  "StructreMap" Ioc container to inject dependencies externally. To setup  StructreMap, Open a package manager console powershell and type following command.
Install-Package StructureMap.MVC4
(Alternatively, you can use Nuget package manager to install with a GUI)
 
Step 02 

a). Create IStudent interface.  

b). Now Create the actual Student class/concrete class. This class implements IStudent interface.    
 
c). Go to the HomeController. Since we use constructor injection, let's create a constructor for the HomeController. It will look similar to this. 

Notice that, we are passing the IStudent interface as the parameter for the constructor. Then we saved the passed parameter into a instant variable of type IStudent. So what is the benefit of doing this?

Now you can pass any class that implements IStudent interface to our HomeController. We can create a dummy class which implements IStudent with dummy data and simply test our HomeController quite easily!

To do this, we need a way to Map our IStudent interface either with Actual Class or Dummy Class. This is facilitated by our Ioc container "StrutureMap". 

d). Goto ~/DependencyResolution -> IoC.cs file. This file is automatically created once you installed StructureMap. Change it as follows. 

Feel free to change,
x.For<IStudent>().Use<FakeStudent>(); 
You can easily replace the Actual Student class with a Dummy Class. That's it.

Cheers!

Comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete

Post a Comment

Popular posts from this blog

Real-time web with Node.js & Socket.io

First Look at AngularJS 2.0