-
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: add very basic dependency injection
- Loading branch information
Showing
5 changed files
with
129 additions
and
16 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,62 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace UIComponents.Core | ||
{ | ||
public class DependencyInjector | ||
{ | ||
private static readonly Dictionary<Type, object> InstantiatedInstanceDictionary | ||
= new Dictionary<Type, object>(); | ||
|
||
private readonly Dictionary<Type, object> _dependencyDictionary | ||
= new Dictionary<Type, object>(); | ||
|
||
internal void AddProvidersFromDependencies(IEnumerable<InjectDependencyAttribute> dependencyAttributes) | ||
{ | ||
foreach (var dependencyAttribute in dependencyAttributes) | ||
{ | ||
var type = dependencyAttribute.DependencyType; | ||
var providerType = dependencyAttribute.ProviderType; | ||
|
||
if (!_dependencyDictionary.ContainsKey(type)) | ||
_dependencyDictionary[type] = CreateInstance(providerType); | ||
} | ||
} | ||
|
||
private static object CreateInstance(Type providerType) | ||
{ | ||
object instance; | ||
|
||
if (InstantiatedInstanceDictionary.ContainsKey(providerType)) | ||
{ | ||
instance = InstantiatedInstanceDictionary[providerType]; | ||
} | ||
else | ||
{ | ||
instance = Activator.CreateInstance(providerType); | ||
InstantiatedInstanceDictionary[providerType] = instance; | ||
} | ||
|
||
return instance; | ||
} | ||
|
||
public void SetProvider<T>(T instance) | ||
{ | ||
_dependencyDictionary[typeof(T)] = instance; | ||
} | ||
|
||
public T Provide<T>() | ||
{ | ||
var type = typeof(T); | ||
|
||
if (!_dependencyDictionary.ContainsKey(type)) | ||
{ | ||
var value = (T) CreateInstance(type); | ||
SetProvider(value); | ||
return value; | ||
} | ||
|
||
return (T) _dependencyDictionary[type]; | ||
} | ||
} | ||
} |
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,20 @@ | ||
using System; | ||
using JetBrains.Annotations; | ||
|
||
namespace UIComponents.Core | ||
{ | ||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] | ||
[BaseTypeRequired(typeof(UIComponent))] | ||
public class InjectDependencyAttribute : Attribute | ||
{ | ||
public readonly Type DependencyType; | ||
|
||
public readonly Type ProviderType; | ||
|
||
public InjectDependencyAttribute(Type dependency, Type provider) | ||
{ | ||
DependencyType = dependency; | ||
ProviderType = provider; | ||
} | ||
} | ||
} |
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