Created
November 26, 2010 01:03
-
-
Save vcsjones/716137 to your computer and use it in GitHub Desktop.
WP7 IoC
This file contains hidden or 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
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); | |
} | |
} | |
} |
This file contains hidden or 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
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