Skip to content

Instantly share code, notes, and snippets.

@vcsjones
Created November 26, 2010 01:03
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vcsjones/716137 to your computer and use it in GitHub Desktop.
Save vcsjones/716137 to your computer and use it in GitHub Desktop.
WP7 IoC
public static partial class IoC
{
private static readonly Dictionary<Type, Type> _registration = new Dictionary<Type, Type>();
private static readonly Dictionary<Type, object> _rot = new Dictionary<Type, object>();
private static readonly object[] _emptyArguments = new object[0];
private static readonly object _syncLock = new object();
static partial void RegisterAll();
static IoC()
{
RegisterAll();
}
public static object Resolve(Type type)
{
lock (_syncLock)
{
if (!_rot.ContainsKey(type))
{
if (!_registration.ContainsKey(type))
{
throw new Exception("Type not registered.");
}
var resolveTo = _registration[type] ?? type;
var constructorInfos = resolveTo.GetConstructors();
if (constructorInfos.Length > 1)
throw new Exception("Cannot resolve a type that has more than one constructor.");
var constructor = constructorInfos[0];
var parameterInfos = constructor.GetParameters();
if (parameterInfos.Length == 0)
{
_rot[type] = constructor.Invoke(_emptyArguments);
}
else
{
var parameters = new object[parameterInfos.Length];
foreach (var parameterInfo in parameterInfos)
{
parameters[parameterInfo.Position] = Resolve(parameterInfo.ParameterType);
}
_rot[type] = constructor.Invoke(parameters);
}
}
return _rot[type];
}
}
public static T Resolve<T>()
{
return (T) Resolve(typeof (T));
}
public static void Register<I, C>() where C:class, I
{
lock (_syncLock)
{
_registration.Add(typeof(I), typeof(C));
}
}
public static void Register<C>() where C:class
{
lock(_syncLock)
{
_registration.Add(typeof(C), null);
}
}
}
public static partial class IoC
{
static partial void RegisterAll()
{
Register<IMyInterface, IMyImplementation>();
Register<IMyComponent>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment