DEV Community

loading...
Cover image for Day 48 of #100DaysOfCode: Dependency Injection in ASP.NET MVC5 using Ninject

Day 48 of #100DaysOfCode: Dependency Injection in ASP.NET MVC5 using Ninject

jenhsuan profile image Jen-Hsuan Hsieh ・2 min read

Introduction

The controller is tied to concrete implementation of service because the constructor of the HomeController has to create the instance of service.

public HomeController()
{
    _messageService = new MessageService();
}

public ActionResult About()
{
    ViewBag.Message = _messageService.GetWelcomeMessage();
    return View();
}
Enter fullscreen mode Exit fullscreen mode

There are a few advantages If we can decouple the controller with others.

  1. It'll be easier to test the controller when we want to mock the service.
  2. It'll ensure the single responsibility principle. It means that we only have to concern that how do we retrieving the data and not the concrete implementation.

Ninject is a Dependency Injection framework to break applications into loosely-coupled, highly-cohesive components, and then glue them back together.

Steps

1. Create a ASP.NET MVC project

2. Install packages

Install-Package Ninject -Version 3.3.4
Install-Package Ninject.MVC5 -Version 3.3.0
Install-Package Ninject.Web.Common.WebHost -Version 3.3.2
Enter fullscreen mode Exit fullscreen mode

3. Create an interface

public interface IMessageService
{
    string GetWelcomeMessage();
}
Enter fullscreen mode Exit fullscreen mode

4. Create a service implemented that interface

public class MessageService : IMessageService
{
    public string GetWelcomeMessage()
    {
        return "Welcome to Ninject MVC5 Example";
    }
}
Enter fullscreen mode Exit fullscreen mode

5. Edit App_Start/NinjectWebCommon.cs

It will generate NinjectWebCommon.cs after installing packages automatically. Add the following code in CreateKernel()

private static IKernel CreateKernel()
{
    var kernel = new StandardKernel();
    try
    {
        kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
        kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
        kernel.Bind<IMessageService>().To<MessageService>();
        DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
        RegisterServices(kernel);
        return kernel;
    }
    catch
    {
        kernel.Dispose();
        throw;
    }
}
Enter fullscreen mode Exit fullscreen mode

6. Edit Controllers/HomeController.cs

Now We can use dependency injection instead of creating instances in the controller.

public class HomeController : Controller
{
    private readonly IMessageService _messageService;
    public HomeController(IMessageService messageService)
    {
        _messageService = messageService;
    }
    ...
    public ActionResult About()
    {
        ViewBag.Message = _messageService.GetWelcomeMessage();

        return View();
    }
    ...
}
Enter fullscreen mode Exit fullscreen mode

That's it!

Articles

There are some of my articles and released projects. Feel free to check if you like!

References

Discussion (0)

pic
Editor guide