Wednesday, February 6, 2013

IOC Container

My objective here was to create a lightweight container using some of the newer .NET classes available. Requirements included thread safety and lazy loading.

This is more an implementation of the service locator pattern and definitely not a fully kitted IOC container as the name suggests. For someone who requires simple service registration and location from multiple threads with the added benefit of lazy loading this may be of some use.

using System;
using System.Collections.Concurrent;
namespace IOC
{
public sealed class IocContainer
{
ConcurrentDictionary<Type,Lazy<Object>> container = new ConcurrentDictionary<Type, Lazy<Object>>();
public static readonly IocContainer Default = new IocContainer();
static IocContainer()
{
}
/// <summary>
/// Registers the service.
/// </summary>
/// <returns><c>true</c>, if service was registered, <c>false</c> otherwise.</returns>
/// <typeparam name="T">The 1st type parameter.</typeparam>
public bool RegisterService<T>() where T : new()
{
return RegisterService(typeof(T), () => new T());
}
/// <summary>
/// Registers the service.
/// </summary>
/// <returns><c>true</c>, if service was registered, <c>false</c> otherwise.</returns>
/// <param name="type">Type.</param>
/// <param name="factoryMethod">Factory method.</param>
public bool RegisterService(Type type, Func<Object> factoryMethod)
{
return container.TryAdd(type, new Lazy<Object>(factoryMethod));
}
/// <summary>
/// Finds the service.
/// </summary>
/// <returns>The service.</returns>
/// <typeparam name="T">The 1st type parameter.</typeparam>
public T FindService<T>()
{
Lazy<Object> res;
if (container.TryGetValue(typeof(T) , out res))
return (T)res.Value;
else
return default(T);
}
}
}
view raw gistfile1.cs hosted with ❤ by GitHub

Example Usage


interface IPullable
{
void Pull();
}
class Rope : IPullable
{
public void Pull()
{
Console.WriteLine("Pulled");
}
}
public static void Main (string[] args)
{
var ioc = IocContainer.Default;
Console.WriteLine("Register service = " + ioc.RegisterService(typeof(IPullable), () => new Rope()));
IPullable pullable = ioc.FindService<IPullable>();
pullable.Pull();
}
view raw gistfile1.cs hosted with ❤ by GitHub

Lets say you have an application consisting of a core project that contains code that you intend to reuse (for instance a portable class library) and a platform specific project that contains your UI. The UI project would register the Rope as the IPullable implementation (typically at startup). Now your core code can locate an implementation of IPullable whenever it needs it. When you port your project to another platform you can register a different implementation of IPullable (ElasticBand for example). Your core code remains unchanged and is non the wiser that its now pulling an elastic band instead to a rope.

No comments: