Dependency Injection
If you want to describe these two words, you can simply say they “Inject the Dependency”. So what’s the dependency? If we talk about the object oriented world, class is dependent on any other class.
In MVC, it is more clear as the model fetches the data from DAL layer and presents that data to the view. So we can say that model is dependent on the DAL.
So here Dependency should be simply like constructor of class A.
Public A (B b) // Constructor of A
Dependency injection allows us to loosely couple classes in layered architecture, so implementing the DI the code is more and more maintainable.
So here we try to understand DI in the MVC way. The very basic flow of MVC request is like this,
So here is where the problem begins.
Let's there is a controller and it consumes the Service from a class. In this way we can’t change the controller to consume different services easily and we need to provide “Mock” for unit testing,
We always know that decoupling the two layers is always an interface. So here also Interface plays a vital part. So we are aware of our controller to IServices Interface rather than Services class. So implementing in this way you can change the implementation of services class at any point of time.
There are basically 3 ways of Implementing Dependency Injection,
- Constructor Injection
- Property Injection
- Method Injection
Here we will mainly talk about a more common way of dependency Injection – Constructor Injection,
- public class SampleTest : ISampleTest
- {
- public int AddNewSample(int i1, int i2)
- {
- return i1 + i2;
- }
- }
- public interface ISampleTest
- {
- int AddNewSample(int i1, int i2);
- }
- public class TestController : Controller
- {
- private ISampleTest _iSamleTest;
- public TestController()
- {
- }
- public TestController(ISampleTest iSamleTest)
- {
- _iSamleTest = iSamleTest;
- }
- // GET: Test
- public ActionResult Index()
- {
- int iCount = _iSamleTest.AddNewSample(2, 4);
- ViewBag.CountNumber = iCount;
- return View();
- }}
- Loosely coupled architecture
- Unit Testing – DI enables you to have complex dependencies with the mock dependency
- Validation – DI allows you to inject additional code between the dependencies. So the developer doesn't need to write code in every class.
Here I will show with an example. Create a sample mvc project here. Create 2 classes and both classes implement an interface.
- public interface IShape
- {
- string GetshapeDetails();
- }
- public class RectangleShape : IShape
- {
- public string GetshapeDetails()
- {
- return "Rectangle has 4 sides";
- }
- }
- public class TriangleShape : IShape
- {
- public string GetshapeDetails()
- {
- return "Triangle has 3 sides";
- }
- }
- public class HomeController : Controller
- {
- private IShape _iShape;
- public HomeController(IShape iShape)
- {
- _iShape = iShape;
- }
- public ActionResult Index()
- {
- string shapedetails = _iShape.GetshapeDetails();
- ViewBag.Message = shapedetails;
- return View(shapedetails as object);
- }
- }

Now we will add the container NInject,

By adding this nuget package, we will get NInject Webcommon file in app_start,

We will write this code in NInject Webcommon
- /// <summary>
- /// Load your modules or register your services here!
- /// </summary>
- /// <param name="kernel">The kernel.</param>
- private static void RegisterServices(IKernel kernel)
- {
- kernel.Bind<IShape>().To<RectangleShape>();
- }
- private static void RegisterServices(IKernel kernel)
- {
- kernel.Bind<IShape>().To<TriangleShape>();
- }

Vignesh ManiPosted Jul 12, 2016, 5:44 AM
Nice
Nikhil SanganiPosted Jul 9, 2016, 5:22 AM
Nice
Rajeev PunhaniPosted Jul 7, 2016, 6:26 AM
Nice.
Naveen BishtPosted Jul 7, 2016, 3:31 AM
Nice easy to understand
kalu singh raoPosted Jul 7, 2016, 1:30 AM
Nice...
Vivek TripathiPosted Jul 7, 2016, 12:30 AM
Thanks Raja
RajaPosted Jul 7, 2016, 12:11 AM
Good One...