Dependency injection is a technique to develop an application in an independent way. Independent in the sense that every module of the application should be unique and will not depend upon other modules. They should be loosely coupled.
Tight Coupling
It means two classes or two modules are fully dependent upon each other. And changing one class object may lead to change in several areas in other class. When we are creating an object of a class and calling that class method by its object from another class, then we will say that the two classes are tightly coupled or dependent on each other.
Loose Coupling
It means two objects are independent and an object can use another object without being dependent on it. They are unique. In software development we always prefers Loosely coupling application.
Here we will see how to implement Dependency injection using Ninject.
Ninject:
Ninject is a lightning-fast, ultra-lightweight dependency injector for .NET applications. It helps you split your application into a collection of loosely-coupled, highly-cohesive pieces, and then glue them back together in a flexible manner. By using Ninject to support your software architecture, your code will become easier to write, reuse, test, and modify. To learn more about Ninject you can visit CodePlex from here.

Step 2: Select MVC template.
Step 3: Add one class Employee.cs and an interface IEmployee.cs in the project as shown in the Solution Explorer.
Step 4: Go To Manage Nuget Package and search for Ninject.Mvc3.
Step 5: After installing this Ninject we found these 3 assemblies in our References.
Now if we check the App_start folder we will find NinjectWebCommon.cs class.
Step 6: Go to your Iemployee interface and add the following abstract method.
- public interface Iemployee
- {
- string SaveData();
- }
- public class Employee:Iemployee
- {
- public string SaveData()
- {
- return "Data Saved....";
- }
- }
- [assembly: WebActivatorEx.PreApplicationStartMethod(typeof(DependencyInjection.App_Start.NinjectWebCommon), "Start")]
- [assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(DependencyInjection.App_Start.NinjectWebCommon), "Stop")]
- namespace DependencyInjection.App_Start
- {
- using System;
- using System.Web;
- using Microsoft.Web.Infrastructure.DynamicModuleHelper;
- using Ninject;
- using Ninject.Web.Common;
- public static class NinjectWebCommon
- {
- private static readonly Bootstrapper bootstrapper = new Bootstrapper();
- /// <summary>
- /// Starts the application
- /// </summary>
- public static void Start()
- {
- DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
- DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
- bootstrapper.Initialize(CreateKernel);
- }
- /// <summary>
- /// Stops the application.
- /// </summary>
- public static void Stop()
- {
- bootstrapper.ShutDown();
- }
- /// <summary>
- /// Creates the kernel that will manage your application.
- /// </summary>
- /// <returns>The created kernel.</returns>
- private static IKernel CreateKernel()
- {
- var kernel = new StandardKernel();
- try
- {
- kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
- kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
- RegisterServices(kernel);
- return kernel;
- }
- catch
- {
- kernel.Dispose();
- throw;
- }
- }
- /// <summary>
- /// Load your modules or register your services here!
- /// </summary>
- /// <param name="kernel">The kernel.</param>
- private static void RegisterServices(IKernel kernel)
- {
- kernel.Bind<Iemployee>().To<DependencyInjection.Employee>().InRequestScope();
- }
- }
- }
So our aim here is to decouple each class, so here without creating any instance, I will call another class method. Let's see how.
- public class HomeController : Controller
- {
- Iemployee iemployee;
- public HomeController(Iemployee _iemployee)
- {
- iemployee = _iemployee;
- }
- }
Now when you try to access the employee class method you can do as follows in another action method.
Here is the code of the screenshot.
- public ActionResult Index()
- {
- string result = iemployee.SaveData();
- return Content(result);
- }

So in this we saw how to implement dependency injection using Ninject.

Christian AshishPosted Feb 16, 2018, 1:27 PM
Thanks for nice article.
Gowtham RajamanickamPosted Apr 25, 2016, 2:55 AM
nice share
Kumaresh RajalingamPosted Feb 11, 2016, 9:13 PM
Nice
Humayun Kabir MamunPosted Feb 10, 2016, 11:02 PM
Nice...
Santhakumar MunuswamyPosted Feb 10, 2016, 2:02 PM
Thanks for nice article
Shubham KumarPosted Feb 10, 2016, 5:00 AM
interesting keep share
Pramod ThakurPosted Feb 10, 2016, 2:38 AM
thx for sharing...
Sibeesh VenuPosted Feb 10, 2016, 1:48 AM
Nice Share