Before going inside this article I would like you to go through the following link, where you will get the basic idea about design pattern and Dependency Injection.
Let me explain once again what Dependency Injection is.
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.
Let me discuss here what Tightly Coupled and Loosely Coupled means in software development.
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 1: Create an ASP.NET Web Application.
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();
-
- }
Step 7: Go to your Employee Class and implement the abstract class method as follows, to show a simple message.
- public class Employee:Iemployee
- {
- public string SaveData()
- {
- return "Data Saved....";
-
- }
- }
Step 8: Go to the Ninjectwebcommon.cs class in App_Start and register the employee class and Iemployee interface there.
- [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();
-
-
-
-
- public static void Start()
- {
- DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
- DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
- bootstrapper.Initialize(CreateKernel);
- }
-
-
-
-
- public static void Stop()
- {
- bootstrapper.ShutDown();
- }
-
-
-
-
-
- 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;
- }
- }
-
-
-
-
-
- private static void RegisterServices(IKernel kernel)
- {
- kernel.Bind<Iemployee>().To<DependencyInjection.Employee>().InRequestScope();
-
- }
- }
- }
Now almost all the work has been finished. Now we will see how to decouple these things.
Note: 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.
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.
Step 9: In the Home Controller create a constructor and just add the following code:
- public class HomeController : Controller
- {
- Iemployee iemployee;
- public HomeController(Iemployee _iemployee)
- {
- iemployee = _iemployee;
- }
- }
So, here I am doing a Constructor Injection by creating a constructor and passing the dependency in the constructor.
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);
- }
Now just save the program and run. It will produce the following output:
So in this we saw how to implement dependency injection using Ninject.
Read more articles on Design Pattern: