Rashim's Blog

Define your own IoC container

Posted on: January 10, 2012

Last year I was assigned to develop a IoC container for R&D purposes which make me to start working with the IoC Cointainer

Anyway…
I share with you part of it as I think that, it might help you a lot to develop your own container.

The Inversion of Control (IoC) and Dependency Injection (DI) patterns are all about eliminating dependencies from your code. Dependency injection is also usually well-known as “inversion of control” who’s Principle is – “Don’t call us, we’ll call you”. So simply we can say that IoC is the principle whereas DI is the way of implementation. There are three popular ways of inversion of control namely

  1.  Constructor Injection
  2.  Method Injection
  3.  Property Injection

Well,I am not going to discuss about IoC or DI rather i wanna to show how to implement our own IoC Container. However if you wanna to know in details about IoC and DI please have a look on the Link[^]

Lets move to our code portion,

public static class IoC
{
    private static readonly IDictionary<Type, Type> types = new Dictionary<Type, Type>();
    private static readonly IDictionary<Type, object> typeInstances = new Dictionary<Type, object>();
    public static void Register<TContract, TImplementation>()
    {
        types[typeof(TContract)] = typeof(TImplementation);
    }
    public static void Register<TContract, TImplementation>(TImplementation instance)
    {
        typeInstances[typeof(TContract)] = instance;
    }
    public static T Resolve<T>()
    {
        return (T)Resolve(typeof(T));
    }
    public static object Resolve(Type contract)
    {
        if (typeInstances.ContainsKey(contract))
        {
            return typeInstances[contract];
        }
        else
        {
            Type implementation = types[contract];
            ConstructorInfo constructor = implementation.GetConstructors()[0];
            ParameterInfo[] constructorParameters = constructor.GetParameters();
            if (constructorParameters.Length == 0)
            {
                return Activator.CreateInstance(implementation);
            }
            List<object> parameters = new List<object>(constructorParameters.Length);
            foreach (ParameterInfo parameterInfo in constructorParameters)
            {
                parameters.Add(Resolve(parameterInfo.ParameterType));
            }
            return constructor.Invoke(parameters.ToArray());
        }
    }
}

Now from your bootStrapper class you need to register your class like,

 Service svc = new Service();
 IoC.Register<IService, Service>(svc);

On a subsequent Classes you just need to inject the Interface or class that you register like,

private IService currentSvc = null;
public LoginServices(IService  submittedService)
{
   this.currentSvc =submittedService;
}

Very very Simple!!!! We love simple, don’t we?

Updated:19th March2012

Example: Say this is my Interface,

public interface ILogger
    {
        void Log(string submittedMsg);
    }

The Logger class implements this interface.

public class Logger : ILogger
{
    public void Log(string submittedMsg)
    {
        if (this != null && !string.IsNullOrEmpty(submittedMsg))
        {
            Console.WriteLine(string.Format("[{0}] {1}\r\n", DateTime.Now, submittedMsg));
            Console.Read();
        }
    }
}

In this ConsoleLogger class I inject the ILogger to its constructor,

public class ConsoleLogger
{
    private ILogger logger = null;

    public ConsoleLogger(ILogger submittedLogger)
    {
        this.logger = submittedLogger;
        this.DisplayMessage();
    }

    public void DisplayMessage()
    {
        logger.Log("Hi!!");
    }
}

Register and Resolve options,

class Program
{
    static void Main(string[] args)
    {
        IoC.Register<ILogger, Logger>();

        IoC.Register<ConsoleLogger, ConsoleLogger>();
        ConsoleLogger objCOnsoleLogger = IoC.Resolve<ConsoleLogger>();
    }
}
About these ads

1 Response to "Define your own IoC container"

Awesome :) Liked it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 37 other followers

%d bloggers like this: