From 7b697b7338f98e2339dfe2725aaadb3ee2658896 Mon Sep 17 00:00:00 2001 From: Joni Savolainen Date: Sun, 1 May 2022 13:38:10 +0300 Subject: [PATCH] feat(DependencyInjector): add constructor for constructing injector using DependencyAttributes --- Core/DependencyInjector.cs | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/Core/DependencyInjector.cs b/Core/DependencyInjector.cs index d0f7c724..824b72d1 100644 --- a/Core/DependencyInjector.cs +++ b/Core/DependencyInjector.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using JetBrains.Annotations; using UIComponents.Core.Exceptions; -using UnityEngine; namespace UIComponents.Core { @@ -36,13 +35,11 @@ public static DependencyInjector GetInjector(Type consumerType) private static DependencyInjector CreateInjector(Type consumerType) { - var injector = new DependencyInjector(); - var injectAttributes = (DependencyAttribute[]) consumerType.GetCustomAttributes(typeof(DependencyAttribute), true); - injector.PopulateFromDependencyAttributes(injectAttributes); - + var injector = new DependencyInjector(injectAttributes); + InjectorDictionary.Add(consumerType, injector); return injector; @@ -64,6 +61,20 @@ private static object CreateInstance(Type dependencyType) return instance; } + + public DependencyInjector() {} + + public DependencyInjector(IEnumerable dependencyAttributes) + { + foreach (var dependencyAttribute in dependencyAttributes) + { + var type = dependencyAttribute.DependencyType; + var providerType = dependencyAttribute.ProvideType; + + if (!DependencyDictionary.ContainsKey(type)) + DependencyDictionary[type] = CreateInstance(providerType); + } + } public void SetDependency([NotNull] T instance) where T : class { @@ -96,17 +107,5 @@ private static object CreateInstance(Type dependencyType) return true; } - - private void PopulateFromDependencyAttributes(IEnumerable dependencyAttributes) - { - foreach (var dependencyAttribute in dependencyAttributes) - { - var type = dependencyAttribute.DependencyType; - var providerType = dependencyAttribute.ProvideType; - - if (!DependencyDictionary.ContainsKey(type)) - DependencyDictionary[type] = CreateInstance(providerType); - } - } } } \ No newline at end of file