Resolve Dependency in 3 Ways Using Ninject IoC Container

Introduction

I will assume that you have a basic understanding of Decoupled Architecture and Dependency injection in application development.

We know that DI or IoC is related to Decoupled Architecture that might provide better control and an easy way to maintain software applications. Fine, and probably you may know that there are three ways we can inject dependency using Dependency injection technique. Like:

  • Constructor Injection
  • Setter getter/property injection
  • Method injection

So, those are the possible solutions to implement Dependency injection. Now, let's get to the next concept. Is there any way to handle those types of injection in a better and smooth way? Yes, we can use an IoC container.

If IoC container is a new term to you then here is a single-line introduction to it. Though this is not a dedicated article for IoC containers, I will suggest you to refer to any good article to understand IoC container fully.

IoC container is nothing but a repository of dependent objects, we can collect all dependent objects in a single place, so that in a single go we can resolve dependencies of some objects that are dependent on those objects. Generally an IoC container are a piece of clever program that detects dependencies automatically (yes, almost magically, at least I think) .The truth to be told, if you want you can implement your own IoC container too. It's not rocket science , but people choose the best and most popular one to reduce time and get the full benefit of the IoC container.

There are many IoC containers available in the market, the choice is yours but for this article we will use Ninject, which is smart, flexible and one of the popular IoC containers.

The purpose of the article is to implement three types of DI using Niject. If you are very new to Niject then I suggest you go through my first article to learn the Ninject installation process in Visual Studio from the NuGet Package Manager.

http://www.c-sharpcorner.com/UploadFile/dacca2/implement-dependency-injection-using-ninject/}

Here is a sample implementation of the banking process. Let's discuss the example and try to understand it. In this example all classes implemented their corresponding Interface. In the BankingTransaction class we have implemented a Withdraw() function to do the withdraw operation and after completion it will Log the process and it will notify too the account holder. Have a look at the following example.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using Ninject;  
  6. using Ninject.Modules;  
  7. namespace Client  
  8. {  
  9.     public interface ILogger  
  10.     {  
  11.         void Log();  
  12.     }  
  13.     public class EventLog : ILogger  
  14.     {  
  15.         public void Log()  
  16.         {  
  17.             Console.WriteLine("event Log successful");  
  18.         }  
  19.     }  
  20.    
  21.     public interface INotifier  
  22.     {  
  23.         void SendNotification();  
  24.     }  
  25.    
  26.     public class SMSNotification : INotifier  
  27.     {  
  28.         public void SendNotification()  
  29.         {  
  30.             Console.WriteLine("Notification send by SMS");  
  31.         }  
  32.     }  
  33.    
  34.     public interface IBankingOperation  
  35.     {  
  36.         void Withdrawl();  
  37.     }  
  38.    
  39.     public class BankingOperation : IBankingOperation  
  40.     {  
  41.         public void Withdraw()  
  42.         {  
  43.             Console.WriteLine("Money Withdrawl successful");  
  44.         }  
  45.     }  
  46.    
  47.     public class BankTran  
  48.     {  
  49.         public IBankingOperation ObjBankingOperation= null;  
  50.         public INotifier ObjNotifier = null;  
  51.         public ILogger ObjLogger = null;  
  52.    
  53.         [Inject]  
  54.         public BankTran(IBankingOperation tmpBO)  
  55.         {  
  56.             ObjBankingOperation = tmpBO;  
  57.         }  
  58.    
  59.         [Inject]  
  60.         public INotifier Notifier { set { ObjNotifier = value; } }  
  61.    
  62.         [Inject]  
  63.         public void LogTransaction(ILogger tmpLog)  
  64.         {  
  65.             ObjLogger = tmpLog;  
  66.         }  
  67.    
  68.         public void MakeTransaction()  
  69.         {  
  70.             ObjBankingOperation.Withdraw();  
  71.             ObjLogger.Log();  
  72.             ObjNotifier.SendNotification();  
  73.             Console.WriteLine("Transaction Successful ");  
  74.         }  
  75.    
  76.     }  
  77.    
  78.     class Program  
  79.     {  
  80.         static void Main(string[] args)  
  81.         {  
  82.             var kernel = new StandardKernel();  
  83.             kernel.Bind<ILogger>().To<EventLog>();  
  84.             kernel.Bind<INotifier>().To<SMSNotification>();  
  85.             kernel.Bind<IBankingOperation>().To<BankingOperation>();  
  86.             BankTran tran = kernel.Get<BankTran>();  
  87.    
  88.             tran.MakeTransaction();  
  89.    
  90.             Console.ReadLine();  
  91.         }  
  92.     }  
  93. }  

 

Let's discuss the BankTran class that is the most important one for this article. This class is dependent on their other classes and they are EventLog, SMSNotification and BankingOperation. Since the object of the BankTran object is dependent on the object of those three classes, we must pass them at the time of Object creation of the BankTran class.

Now, here the IoC container comes into play. To resolve this dependency, we have kept all dependent classes in a container (in the Main() function) and resolving all Dependency in one go in this line.

  1. BankTran tran = kernel.Get<BankTran>();  

 

Now, the “tran” object can execute all the functions of its parent class. If you run the application, you will find that all functions have executed.

Client Information

Now, the question might come in your mind, if I do not use IoC then how do I solve the problem. The point is, it's not mandatory to use an IoC container to resolve dependency, we can use the traditional way too. Have a look at the following implementation.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. namespace Client  
  5. {  
  6.     public interface ILogger  
  7.     {  
  8.         void Log();  
  9.     }  
  10.     public class EventLog : ILogger  
  11.     {  
  12.         public void Log()  
  13.         {  
  14.             Console.WriteLine("event Log successful");  
  15.         }  
  16.     }  
  17.    
  18.     public interface INotifier  
  19.     {  
  20.         void SendNotification();  
  21.     }  
  22.    
  23.     public class SMSNotification : INotifier  
  24.     {  
  25.         public void SendNotification()  
  26.         {  
  27.             Console.WriteLine("Notification send by SMS");  
  28.         }  
  29.     }  
  30.    
  31.     public interface IBankingOperation  
  32.     {  
  33.         void Withdrawl();  
  34.     }  
  35.    
  36.     public class BankingOperation : IBankingOperation  
  37.     {  
  38.         public void Withdrawl()  
  39.         {  
  40.             Console.WriteLine("Money Withdrawl successful");  
  41.         }  
  42.     }  
  43.    
  44.     public class BankTran  
  45.     {  
  46.         public IBankingOperation ObjBankingOperation= null;  
  47.         public INotifier ObjNotifier = null;  
  48.         public ILogger ObjLogger = null;  
  49.                  
  50.         public BankTran(IBankingOperation tmpBO)  
  51.         {  
  52.             ObjBankingOperation = tmpBO;  
  53.         }  
  54.    
  55.          
  56.         public INotifier Notifier { set { ObjNotifier = value; } }  
  57.    
  58.          
  59.         public void LogTransaction(ILogger tmpLog)  
  60.         {  
  61.             ObjLogger = tmpLog;  
  62.         }  
  63.    
  64.         public void MakeTransaction()  
  65.         {  
  66.             ObjBankingOperation.Withdrawl();  
  67.             ObjLogger.Log();  
  68.             ObjNotifier.SendNotification();  
  69.             Console.WriteLine("Transaction Successful ");  
  70.         }  
  71.    
  72.     }  
  73.    
  74.     class Program  
  75.     {  
  76.         static void Main(string[] args)  
  77.         {  
  78.             //Creating object by passing object of BankingOperation Class(constactor injection)  
  79.             BankTran tran = new BankTran(new BankingOperation());  
  80.              
  81.             //Set property using object SMSnotification class(property injection)  
  82.             tran.Notifier = new SMSNotification();  
  83.              
  84.             //Set object on EvenLog class through function(method injection)  
  85.             tran.LogTransaction(new EventLog());  
  86.             tran.MakeTransaction();  
  87.    
  88.             Console.ReadLine();  
  89.         }  
  90.     }  
  91. }  

 

Now, compare the implementation of both Main() functions. In this approach we are specifying a dependent object explicitly depending on its injection type but in the case of IoC container we did not.

There is another advantage of IoC container; once we create a container, we can use it for many (or N) number of objects to resolve it's dependency where this facility is not available in the traditional way.

Conclusion

This is the advantage of IoC containers; to resolve dependency of objects. I hope you have understood the concept and want to give Ninject a try.

Up Next
    Ebook Download
    View all
    Learn
    View all