Implement Dependency Injection Using Autofac IoC Container

Now a days I am experimenting with various IoC containers and trying to implement POC and sharing with the community. This article assumes a basic understanding of de-coupled architecture and the concept of Inversion of Control. In our previous article we talked about Unity and Ninject, you can read them here.

We know that IoC stands for Inversion of Control and it's a way to implement more independent components and to make the work easy, there are many IoC containers in the market, Autofac is one of them. In this article we will try to implement a simple example of dependency injection using Autofac. First of all create one Console application to implement the sample example and provide a reference of Autofac from the NuGet Package Manager. Here is the package.

package

Once you provide a reference, you will find the following reference in the references section of the application.
 
reference

We are now ready to implement the IoC container using autofac. Have a look at the following example. At first we have implemented two interfaces and it's corresponding concrete class.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using Autofac;  
  6.   
  7. public interface IMobileServive  
  8. {  
  9.     void Execute();  
  10. }  
  11. public class SMSService : IMobileServive  
  12. {  
  13.     public void Execute()  
  14.     {  
  15.         Console.WriteLine("SMS service executing.");  
  16.     }  
  17. }  
  18.   
  19. public interface IMailService  
  20. {  
  21.     void Execute();  
  22. }  
  23.   
  24. public class EmailService : IMailService  
  25. {  
  26.     public void Execute()  
  27.     {  
  28.         Console.WriteLine("Email service Executing.");  
  29.     }  
  30. }  
  31.   
  32. public class NotificationSender    
  33. {  
  34.     public IMobileServive ObjMobileSerivce = null;  
  35.     public IMailService ObjMailService = null;  
  36.       
  37.     //injection through constructor  
  38.     public NotificationSender(IMobileServive tmpService)  
  39.     {  
  40.         ObjMobileSerivce = tmpService;  
  41.     }  
  42.     //Injection through property  
  43.     public IMailService SetMailService  
  44.     {  
  45.         set { ObjMailService = value; }  
  46.     }  
  47.     public void SendNotification()  
  48.     {  
  49.         ObjMobileSerivce.Execute();  
  50.         ObjMailService.Execute();  
  51.     }  
  52. }  
  53.   
  54. namespace Client  
  55. {  
  56.     class Program  
  57.     {  
  58.         static void Main(string[] args)  
  59.          {  
  60.              var builder = new ContainerBuilder();  
  61.              builder.RegisterType<SMSService>().As<IMobileServive>();  
  62.              builder.RegisterType<EmailService>().As<IMailService>();  
  63.              var container = builder.Build();  
  64.   
  65.              container.Resolve<IMobileServive>().Execute();  
  66.              container.Resolve<IMailService>().Execute();  
  67.              Console.ReadLine();  
  68.          }  
  69.     }  

We have then implemented a NotificationSender class that is dependent on both MobileService and MailService.

We have injected a dependency of both classes through constructor and property. Have a look at the Main() function where we have created a repository of all dependency types and building the repository. Once we run the application we will see the following result.

result

Conclusion

There are many IoC containers available in the market and Autofac is one of them. All the containers have some pros and cons and none of them are perfect, so the choice is yours. 


Recommended Free Ebook
Similar Articles