Using Ninject in ASP.NET MVC 5 in Less than 5 Minutes

Ninject is a great Dependency Injection framework and works wonderfully with ASP.NET MVC 5. In this post, I will show you how to get Ninject running in a MVC project in less than 5 minutes.

What is Ninject?

Ninject is a light weight, versatile dependency injection framework that can be used in .NET applications. It allows you to focus on writing software instead of writing configuration and wiring code.

What is dependency injection and why is it important?

Dependency injection is the process of configuring or setting up your application to have one object supply the dependencies of another object or objects. In short, you are configuring your application to have one object inject the dependencies other objects require in a predictable manner.

Dependency injection has a few benefits:

1. It allows you to decouple the dependency from the consumer.
2. It allows you to create more testable code.

These two benefits allow us as developers to create much better code though, it isn’t automatic. You still have to make good decisions and write good code.

Ninject and ASP.NET MVC 5

You can get Ninject running in an ASP.NET MVC application really quickly. Low barrier to entry is very important because if the process is painful, you are less likely to do it. Here we go.

1. Right click the ASP.NET MVC project in solution explorer and click ‘Manage Nuget Packages…’.
2. In the Nuget Package Manager, select the Browse tab.
3. Search for Ninject.
4. Select Ninject.MVC5 and select 3.2.1 in the Version drop down beside the Install button.
5. Click the Install button.

Now, we have Ninject installed, lets learn how to use it.

1. Add a folder to the project named code.
2. Add a class named MessageService.cs to the code folder.
3. Add an interface named IMessageService.cs to the code folder.
4. Open the MessageService.cs file and replace the class declaration with the code below:

public class MessageService : IMessageService
{
    public string GetMessage()
    {
        return "Hello, World!";
    }
}

5. Open the IMessageService.cs file and replace the interface declaration with the following code:

public interface IMessageService
{
    string GetMessage();
}

6. Open the HomeController.cs file and  replace the class declaration with the following code:

public class HomeController : Controller
{
    private IMessageService _messageService = null;

    public HomeController(IMessageService messageService)
    {
        _messageService = messageService;
    }

    public ActionResult Index()
    {
        ViewBag.Message = _messageService.GetMessage();
        return View();
    }
}

7. Finally, right click he Index() action and select ‘Go To View’. Replace the HTML in the view with the following code:

<div class="jumbotron">
    <h1>@ViewBag.Message</h1>
    <p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.</p>
    <p><a href="https://asp.net" class="btn btn-primary btn-lg">Learn more &raquo;</a></p>
</div>

8. One last change. In the App_Start\NinjectWebCommon.cs file, add the following line to the RegisterServices method:

kernel.Bind<IMessageService>().To<MessageService>();

That is all there is to it. If you set a break point in the Home Controller constructor, you will see that Ninject is injecting the MessageService into that constructor in place of the IMessageService interface because of the change in bullet point 8. You should see the ‘Hello, World!’ message on the home page when it launches.

If you want to skip all the copy/pasting you can download the code from here.

Leave a Reply

Your email address will not be published.