-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: rework dependency injection, create AssetDatabaseAssetLoader
BREAKING CHANGE: InjectDependencyAttribute has been renamed to DependencyAttribute. The provider argument has been renamed to provide. The majority of DependencyInjector has been reworked completely.
- Loading branch information
Showing
10 changed files
with
164 additions
and
93 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using UnityEditor; | ||
|
||
namespace UIComponents.Core | ||
{ | ||
public class AssetDatabaseAssetLoader : IAssetLoader | ||
{ | ||
public T LoadAsset<T>(string assetPath) where T : UnityEngine.Object | ||
{ | ||
return AssetDatabase.LoadAssetAtPath<T>(assetPath); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System; | ||
|
||
namespace UIComponents.Core | ||
{ | ||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] | ||
public class DependencyAttribute : Attribute | ||
{ | ||
public readonly Type DependencyType; | ||
|
||
public readonly Type ProvideType; | ||
|
||
public DependencyAttribute(Type dependency, Type provide) | ||
{ | ||
DependencyType = dependency; | ||
ProvideType = provide; | ||
} | ||
} | ||
} |
File renamed without changes.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,104 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
|
||
namespace UIComponents.Core | ||
{ | ||
public class DependencyInjector | ||
{ | ||
private static readonly Dictionary<Type, object> InstantiatedInstanceDictionary | ||
internal static readonly Dictionary<Type, DependencyInjector> InjectorDictionary = | ||
new Dictionary<Type, DependencyInjector>(); | ||
|
||
internal static readonly Dictionary<Type, object> InstantiatedInstanceDictionary | ||
= new Dictionary<Type, object>(); | ||
|
||
private readonly Dictionary<Type, object> _dependencyDictionary | ||
internal readonly Dictionary<Type, object> DependencyDictionary | ||
= new Dictionary<Type, object>(); | ||
|
||
internal void AddProvidersFromDependencies(IEnumerable<InjectDependencyAttribute> dependencyAttributes) | ||
public static void SetDependency<TConsumer, TDependency>(TDependency provider) | ||
where TConsumer : class | ||
where TDependency : class | ||
{ | ||
foreach (var dependencyAttribute in dependencyAttributes) | ||
{ | ||
var type = dependencyAttribute.DependencyType; | ||
var providerType = dependencyAttribute.ProviderType; | ||
|
||
if (!_dependencyDictionary.ContainsKey(type)) | ||
_dependencyDictionary[type] = CreateInstance(providerType); | ||
} | ||
var injector = GetInjector(typeof(TConsumer)); | ||
|
||
injector.SetDependency(provider); | ||
} | ||
|
||
public static TDependency GetDependency<TConsumer, TDependency>() | ||
where TConsumer : class | ||
where TDependency : class | ||
{ | ||
var injector = GetInjector(typeof(TConsumer)); | ||
|
||
return injector.Provide<TDependency>(); | ||
} | ||
|
||
public static DependencyInjector GetInjector(Type consumerType) | ||
{ | ||
if (InjectorDictionary.ContainsKey(consumerType)) | ||
return InjectorDictionary[consumerType]; | ||
|
||
return CreateInjector(consumerType); | ||
} | ||
|
||
private static object CreateInstance(Type providerType) | ||
private static DependencyInjector CreateInjector(Type consumerType) | ||
{ | ||
var injector = new DependencyInjector(); | ||
|
||
var injectAttributes = (DependencyAttribute[]) | ||
consumerType.GetCustomAttributes(typeof(DependencyAttribute), true); | ||
|
||
injector.PopulateFromDependencyAttributes(injectAttributes); | ||
|
||
InjectorDictionary.Add(consumerType, injector); | ||
|
||
return injector; | ||
} | ||
|
||
private static object CreateInstance(Type dependencyType) | ||
{ | ||
object instance; | ||
|
||
if (InstantiatedInstanceDictionary.ContainsKey(providerType)) | ||
if (InstantiatedInstanceDictionary.ContainsKey(dependencyType)) | ||
{ | ||
instance = InstantiatedInstanceDictionary[providerType]; | ||
instance = InstantiatedInstanceDictionary[dependencyType]; | ||
} | ||
else | ||
{ | ||
instance = Activator.CreateInstance(providerType); | ||
InstantiatedInstanceDictionary[providerType] = instance; | ||
instance = Activator.CreateInstance(dependencyType); | ||
InstantiatedInstanceDictionary[dependencyType] = instance; | ||
} | ||
|
||
return instance; | ||
} | ||
|
||
public void SetProvider<T>(T instance) | ||
public void SetDependency<T>(T instance) where T : class | ||
{ | ||
_dependencyDictionary[typeof(T)] = instance; | ||
DependencyDictionary[typeof(T)] = instance; | ||
} | ||
|
||
public T Provide<T>() | ||
public T Provide<T>() where T : class | ||
{ | ||
var type = typeof(T); | ||
|
||
if (!_dependencyDictionary.ContainsKey(type)) | ||
|
||
if (DependencyDictionary.ContainsKey(type)) | ||
return (T) DependencyDictionary[type]; | ||
|
||
Debug.LogWarningFormat("Could not get dependency {0}", type.Name); | ||
|
||
return null; | ||
} | ||
|
||
private void PopulateFromDependencyAttributes(IEnumerable<DependencyAttribute> dependencyAttributes) | ||
{ | ||
foreach (var dependencyAttribute in dependencyAttributes) | ||
{ | ||
var value = (T) CreateInstance(type); | ||
SetProvider(value); | ||
return value; | ||
var type = dependencyAttribute.DependencyType; | ||
var providerType = dependencyAttribute.ProvideType; | ||
|
||
if (!DependencyDictionary.ContainsKey(type)) | ||
DependencyDictionary[type] = CreateInstance(providerType); | ||
} | ||
|
||
return (T) _dependencyDictionary[type]; | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace UIComponents.Core | ||
{ | ||
public interface IAssetLoader | ||
{ | ||
public T LoadAsset<T>(string assetPath) where T : UnityEngine.Object; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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