Translate

Wednesday, October 31, 2012

Introduction to AutoFac with a simple example (Autofac Tutorial)


Autofac is an Inversion of Control (IOC) container that was introduced in late 2007. Autofac is meant to deal with Dependency Injection.

In the console application example below, the class BootStrap uses Autofac to decide which implementation of the interface IGetdata to use.

To follow this example you need to add references to the Autofac and Autofac.Configuration dlls, which you can get from http://code.google.com/p/autofac/. I used version 2.6.3.862 for my example.




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autofac;
 
namespace AutofacExample
{
 
 
    class Program
    {
        static void Main()
        {           
            using (var container = BootStrap.Components())
            {
                var messenger = container.Resolve<IGetData>();
                messenger.GetMessage();//Displays Hi from XML
                    
            }
        }
    }
 
 
    public interface IGetData
    {
        void GetMessage();
    }
 
 
    public class GetMessageFromDatabase : IGetData
    {
        public void GetMessage()
        {
             
            Console.WriteLine("Hi from database");
        }
    }
 
 
    public class GetMessageFromXML : IGetData
    {
        public void GetMessage()
        {
            Console.WriteLine("Hi from XML");
        }
    }
 
    public class GetMessageFromTextFile : IGetData
    {
        public void GetMessage()
        {
            Console.WriteLine("Hi from Text File");
        }
    }
 
    public class BootStrap
    {
        public static IContainer Components()
        {
            var builder = new ContainerBuilder();
            builder.RegisterType<GetMessageFromXML>().As<IGetData>();
 
            return builder.Build();
        } 
 
    } 
 
}


Check this article (Autofac with MVC) out for a little bit more 
advanced example on Autofac

3 comments:

  1. Nice example but I have few questions:
    #1. What if I dont have such either or functionaly requirement, like reading from text or Xml, will it be useful to use autofac or IoC container in such case?
    #2. What if I need to call both function at the same time; meaning no if else to call, just need to call both one by one.
    #3. What benefit a coder got by implementing this code, we could have simply call it directly?

    ReplyDelete
    Replies
    1. Answer 1: Reading from text or xml was just a cooked up example to make this article simpler. A more real world example would be a logging class, or any class that needs to be loosely coupled. In the java world the whole spring framework is based on the Ioc concept. Every good developer needs to implement Ioc.
      Answer 2: If you need to call multiple implementations of a class, you will inject the appropriate implementation of that interface to the class that needs it.
      Answer 3: One of the benefits is when you need to replace implementations of a class. Think of it this way. If your android phone has a usb port for charging, all it cares is that any charging device plugged in implements the usb interface. It doesn't care who built the charger. Similarly implementing Ioc, makes "plug and play" more easier. You can easily swap out implementations of interface. This is particularly useful in unit testing, where you swap out the real implementation with a dummy implementation.

      Delete

Comments will appear once they have been approved by the moderator