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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
Example Usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
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:
Post a Comment