diff options
| author | Roy <roy.mail.net@gmail.com> | 2018-04-21 23:00:22 +0300 |
|---|---|---|
| committer | Roy <roy.mail.net@gmail.com> | 2018-04-21 23:00:22 +0300 |
| commit | 24e4a3e25a9ac161097a46b59aee637fe7bdaa50 (patch) | |
| tree | 7e6ff43ca0510fd65669b1458df2b4d8d1b4a9fe /Software/Visual_Studio/Tango.Core/DI | |
| parent | 0dec8a74239cff769836cae577fbd84824070e83 (diff) | |
| download | Tango-24e4a3e25a9ac161097a46b59aee637fe7bdaa50.tar.gz Tango-24e4a3e25a9ac161097a46b59aee637fe7bdaa50.zip | |
Started working on TangoIOC
Diffstat (limited to 'Software/Visual_Studio/Tango.Core/DI')
| -rw-r--r-- | Software/Visual_Studio/Tango.Core/DI/TangoIOC.cs | 151 | ||||
| -rw-r--r-- | Software/Visual_Studio/Tango.Core/DI/TangoInjectAttribute.cs | 13 |
2 files changed, 164 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Tango.Core/DI/TangoIOC.cs b/Software/Visual_Studio/Tango.Core/DI/TangoIOC.cs new file mode 100644 index 000000000..a987eacad --- /dev/null +++ b/Software/Visual_Studio/Tango.Core/DI/TangoIOC.cs @@ -0,0 +1,151 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.Core.DI +{ + public class TangoIOC + { + private class RegisteredType + { + public Type InterfaceType { get; set; } + public Type ImplementationType { get; set; } + public Object Instance { get; set; } + + public RegisteredType(Type interfaceType, Type implementationType, Object instance) + { + InterfaceType = interfaceType; + ImplementationType = implementationType; + Instance = instance; + } + } + + private static TangoIOC _default; + public static TangoIOC Default + { + get + { + if (_default == null) + { + _default = new TangoIOC(); + } + + return _default; + } + } + + private Dictionary<Type, RegisteredType> _registeredTypes; + + public TangoIOC() + { + _registeredTypes = new Dictionary<Type, RegisteredType>(); + } + + public void Register<T>() where T : class + { + RegisterInternal(typeof(T), typeof(T), null); + } + + public void Register<T>(T instance) + { + RegisterInternal(typeof(T), typeof(T), instance); + } + + public void Register(Type type) + { + RegisterInternal(type, type, null); + } + + public void Register(Type type, Type implementation) + { + RegisterInternal(type, implementation.GetType(), null); + } + + public void Register(Type type, Object instance) + { + RegisterInternal(type, instance.GetType(), instance); + } + + public void Register<Type, Implementation>() where Implementation : Type + { + RegisterInternal(typeof(Type), typeof(Implementation), null); + } + + public void Register<Type, Implementation>(Implementation instance) where Implementation : Type + { + RegisterInternal(typeof(Type), typeof(Implementation), instance); + } + + private void RegisterInternal(Type interfaceType, Type implementationType, Object instance) + { + _registeredTypes.Add(interfaceType, new RegisteredType(interfaceType, implementationType, instance)); + } + + public void Unregister<T>() + { + Unregister(typeof(T)); + } + + public void Unregister(Type type) + { + _registeredTypes.Remove(type); + } + + public T GetInstance<T>() + { + return (T)GetInstance(typeof(T)); + } + + public object GetInstance(Type type) + { + RegisteredType registeredType = null; + + if (_registeredTypes.TryGetValue(type, out registeredType)) + { + if (registeredType.Instance == null) + { + registeredType.Instance = InitializeInstance(registeredType.ImplementationType); + } + + return registeredType.Instance; + } + else + { + throw new InvalidOperationException("The type specified could not be located!"); + } + } + + private object InitializeInstance(Type type) + { + List<object> arguments = new List<object>(); + + ConstructorInfo constructor = type.GetConstructors().FirstOrDefault(x => x.GetParameters().Count() > 0); + + object instance = null; + + if (constructor != null) + { + foreach (var arg in constructor.GetParameters()) + { + arguments.Add(GetInstance(arg.ParameterType)); + } + + instance = Activator.CreateInstance(type, arguments.ToArray()); + } + else + { + instance = Activator.CreateInstance(type); + } + + foreach (var prop in type.GetPropertiesWithAttribute<TangoInjectAttribute>(BindingFlags.Public | BindingFlags.Instance)) + { + prop.SetValue(instance, GetInstance(prop.PropertyType)); + } + + return instance; + } + } +} diff --git a/Software/Visual_Studio/Tango.Core/DI/TangoInjectAttribute.cs b/Software/Visual_Studio/Tango.Core/DI/TangoInjectAttribute.cs new file mode 100644 index 000000000..a656c4afe --- /dev/null +++ b/Software/Visual_Studio/Tango.Core/DI/TangoInjectAttribute.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.Core.DI +{ + public class TangoInjectAttribute : Attribute + { + + } +} |
