diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-05-07 15:36:03 +0300 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-05-07 15:36:03 +0300 |
| commit | cdba3267d2a47b3bff8cf3ec0219223e36e234ff (patch) | |
| tree | 2826e264827746eccbc1bef9d7f9bcfcb4cffb81 /Software/Visual_Studio/Tango.Core/DI | |
| parent | 453565753c012a087f402fc55eee528c132b7c8c (diff) | |
| download | Tango-cdba3267d2a47b3bff8cf3ec0219223e36e234ff.tar.gz Tango-cdba3267d2a47b3bff8cf3ec0219223e36e234ff.zip | |
Added machine studio modules order.
Implemented delayed GetInstance for TangoIOC.
Diffstat (limited to 'Software/Visual_Studio/Tango.Core/DI')
| -rw-r--r-- | Software/Visual_Studio/Tango.Core/DI/ITangoIOC.cs | 8 | ||||
| -rw-r--r-- | Software/Visual_Studio/Tango.Core/DI/TangoIOC.cs | 39 |
2 files changed, 47 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Tango.Core/DI/ITangoIOC.cs b/Software/Visual_Studio/Tango.Core/DI/ITangoIOC.cs index 3d386035d..145e9988a 100644 --- a/Software/Visual_Studio/Tango.Core/DI/ITangoIOC.cs +++ b/Software/Visual_Studio/Tango.Core/DI/ITangoIOC.cs @@ -97,6 +97,14 @@ namespace Tango.Core.DI T GetInstance<T>(); /// <summary> + /// Executes the specified action when type T is available. if already available action will be executed immediately. + /// </summary> + /// <typeparam name="T"></typeparam> + /// <param name="callback">The callback.</param> + /// <returns></returns> + void GetInstanceWhenAvailable<T>(Action<T> callback); + + /// <summary> /// Gets all instances by the specified base type. /// </summary> /// <typeparam name="T"></typeparam> diff --git a/Software/Visual_Studio/Tango.Core/DI/TangoIOC.cs b/Software/Visual_Studio/Tango.Core/DI/TangoIOC.cs index afda3d8bf..b6ee79324 100644 --- a/Software/Visual_Studio/Tango.Core/DI/TangoIOC.cs +++ b/Software/Visual_Studio/Tango.Core/DI/TangoIOC.cs @@ -45,6 +45,7 @@ namespace Tango.Core.DI } private Dictionary<Type, RegisteredType> _registeredTypes; + private List<Tuple<Type, Action<Object>>> _waitingRetrivals; /// <summary> /// Initializes a new instance of the <see cref="TangoIOC"/> class. @@ -52,6 +53,7 @@ namespace Tango.Core.DI public TangoIOC() { _registeredTypes = new Dictionary<Type, RegisteredType>(); + _waitingRetrivals = new List<Tuple<Type, Action<object>>>(); } /// <summary> @@ -166,6 +168,11 @@ namespace Tango.Core.DI if (registeredType.Instance == null) { registeredType.Instance = CreateInstance(registeredType.ImplementationType); + + //Notify waiting retrievals. + var toNotify = _waitingRetrivals.Where(x => x.Item1 == registeredType.InterfaceType).ToList(); + toNotify.ForEach(x => x.Item2(registeredType.Instance)); + _waitingRetrivals.RemoveAll(x => toNotify.Contains(x)); } return registeredType.Instance; @@ -177,6 +184,38 @@ namespace Tango.Core.DI } /// <summary> + /// Executes the specified action when type T is available. if already available action will be executed immediately. + /// </summary> + /// <typeparam name="T"></typeparam> + /// <param name="callback">The callback.</param> + /// <returns></returns> + public void GetInstanceWhenAvailable<T>(Action<T> callback) + { + GetInstanceWhenAvailable(typeof(T), (x) => callback((T)x)); + } + + /// <summary> + /// Executes the specified action when type T is available. if already available action will be executed immediately. + /// </summary> + /// <param name="callback">The callback.</param> + /// <returns></returns> + public virtual void GetInstanceWhenAvailable(Type type, Action<object> callback) + { + RegisteredType registeredType = null; + + if (_registeredTypes.TryGetValue(type, out registeredType)) + { + if (registeredType.Instance != null) + { + callback(registeredType.Instance); + return; + } + } + + _waitingRetrivals.Add(new Tuple<Type, Action<object>>(type, callback)); + } + + /// <summary> /// Determines whether the specified type is registered. /// </summary> /// <typeparam name="T"></typeparam> |
