Dependency Injection using Conventional IoC and Autofac

Dependency injection is a software design pattern that implements inversion of control for resolving dependencies.

In terms, we can refer Dependency as an object which can be served as a Service and Injection is referred as passing of a dependency to a client object that would use it. Remember that the Service is made part of client’s state.

Below example demonstrates the purpose of Inversion of Control (IoC) and Autofac (IoC container).

First of All, what is Autofac?

Autofac is an addictive Inversion of Control container for .NET 4.5.

If you look at the below code, you can clearly see that GateWay class which is a conventional IoC container is not called in Autofac whereas Autofac uses ContainerBuilder to execute Payment method.

For downloading Autofac, use Nuget Package Manager. 

  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Text;    
  5. using Autofac;    
  6.    
  7. public interface IPayment  
  8. {  
  9.     void Payment();  
  10. }  
  11.    
  12. public class CreditCardPaymet : IPayment  
  13. {  
  14.    
  15.     public void Payment()  
  16.     {  
  17.         Console.WriteLine("Redirect to Credit Card Payment");  
  18.     }  
  19. }  
  20.    
  21. public class NetBanking : IPayment  
  22. {  
  23.     public void Payment()  
  24.     {  
  25.         Console.WriteLine("Redirect to Net Banking Payment");  
  26.     }  
  27. }  
  28.    
  29. public class DebitCard : IPayment  
  30. {  
  31.     public void Payment()  
  32.     {  
  33.         Console.WriteLine("Redirect to Debit Card Payment");  
  34.     }  
  35. }  
  36.    
  37. //Inversion of Control Container  
  38. public class GateWay  
  39. {  
  40.     public IPayment objPayment = null;  
  41.    
  42.     //Constructor Injection  
  43.     public GateWay(IPayment tmpPayment)  
  44.     {  
  45.         objPayment = tmpPayment;  
  46.     }  
  47.    
  48.     public void SelectedModeOfPayment()  
  49.     {  
  50.         objPayment.Payment();  
  51.     }  
  52. }  
  53.    
  54. namespace Client    
  55. {    
  56.     class Program    
  57.     {  
  58.         static void Main(string[] args)  
  59.         {  
  60.             Console.WriteLine("Execution from conventional IoC");  
  61.             CreditCardPaymet objPayment = new CreditCardPaymet();  
  62.             GateWay objGateWay = new GateWay(objPayment);  
  63.             objGateWay.SelectedModeOfPayment();  
  64.             //Calling Autofac  
  65.             CallAutoFac();  
  66.         }  
  67.    
  68.         static void CallAutoFac()  
  69.         {  
  70.             Console.WriteLine("Execution from Autofac");  
  71.             var builder = new ContainerBuilder();  
  72.             builder.RegisterType<NetBanking>().As<IPayment>();  
  73.             var container = builder.Build();  
  74.             container.Resolve<IPayment>().Payment();  
  75.             Console.ReadLine();            
  76.         }  
  77.     }  
  78. }  

Output:

 
 
Class Diagram: