Tech Junkie Blog - Real World Tutorials, Happy Coding!: ASP.NET MVC : Installing and Setting Up Ninject For Dependency Injection

Tuesday, January 8, 2019

ASP.NET MVC : Installing and Setting Up Ninject For Dependency Injection

In this post we are going to install Ninject which is a lightweight IOC container.  You can think of an IOC container as a magic toolbox that gives you the tools that you need when you need it.  Hence its called dependency injection, meaning it injects the dependencies that you need.  As with any IOC container there needs to be a mapping between the interfaces and the implementation.  That's where Ninject comes in.

First let's install Ninject with Nuget. here are the steps:

1. Open the Nuget Management Console and type in the following command

Install-Package Ninject -Version 3.2.2
Install-Package Ninject.Web.Common -Version 3.2.2
Install-Package Ninject.Mvc3 -Version  3.2.1



Don't worry about the package call Ninject.Mvc3 it works perfectly with MVC 5.


2.  Now we are going to set up Ninject by first creating an infrastructure folder.  This folder will contain all of the plumbing stuff that we would need for our application.  In the folder we are going to create an Ninject dependency resolver.  Its just what the name implies the dependency will resolve the dependencies that we need, or you call it the mappings and configurations for the dependencies.
So create a folder call "Infrastructure" and add a new file call, "DependencyMappings" inside the folder.







It implements the IDependencyResolver interface from the System.Web.Mvc namespace.  The IDependencyResolver interface gets your services and maps the dependencies to them.

It simply has two GetService(Type) and GetServices(Type) which is pretty much self explanatory.







So the code so far, for the DependencyMappings.cs file should look like the following:

using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace NorthwindCafe.Infrastructure
{
    public class DependencyMappings : IDependencyResolver
    {
        public object GetService(Type serviceType)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            throw new NotImplementedException();
        }
    }
}

Now let's implement the GetService and GetServices method

using Ninject;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace NorthwindCafe.Infrastructure
{
    public class DependencyMappings : IDependencyResolver
    {
        private IKernel _kernel;

        public DependencyMappings(IKernel kernel)
        {
            _kernel = kernel;
            Bindings();
        }


        public object GetService(Type serviceType)
        {
            return _kernel.TryGet(serviceType);
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            return _kernel.GetAll(serviceType);
        }

        private void Bindings()
        {
            throw new NotImplementedException();
        }
    }
}

The magic is in Ninject's Kernel object which has the job of resolving dependencies and instantiating the appropriate class instance.  The client does not care about the implementation is instantiated it just request a certain type of object and gets back the implementation. For now we are going to leave the Bindings method not implemented because we still have not build out our business models.

The next thing we need to do is register our DependencyMappings class to Ninject.  When we install Ninject a file was created to do just that called the NinjectWebCommon.cs file.  The method to register the dependency resolver is the RegisterServices method

        private static void RegisterServices(IKernel kernel)
        {
            DependencyResolver.SetResolver(new DependencyMappings(kernel));
        } 

Now Ninject is all setting up, we will come back to the Bindings method in the DependencyMappings file when we actually have something to map after we start building our business models.


Previous: ASP.NET MVC; Get Rid Of The Port In The URL

4 comments:

  1. When you have been looking online for JAMB 2023 examination Runs & EXPO free of charge, 2023 jamb runs, 2023 jamb cbt runs, 2023 jamb expo, 2023 jamb solutions, 2023 jamb CBT expo, jamb expo 2023/2024, jamb runs 2023/2024, 2023 2024 jamb cbt expo then you definately are inside the right region. For more information, please visit https://gistpower.com/.

    ReplyDelete

Search This Blog