mirror of
https://github.com/Stone-Red-Code/DynamicInjector.git
synced 2026-09-06 07:46:19 +02:00
- Add Scopes
This commit is contained in:
@@ -6,14 +6,29 @@ internal class Prgramm
|
|||||||
{
|
{
|
||||||
private static void Main()
|
private static void Main()
|
||||||
{
|
{
|
||||||
InjectionContainer injector = new InjectionContainer();
|
InjectionContainer container = new InjectionContainer();
|
||||||
injector.RegisterGlobal<IFirstTestService, FirstTestService>();
|
container.RegisterScoped<IFirstTestService, FirstTestService>();
|
||||||
injector.RegisterLocal<SecondTestService>();
|
container.RegisterLocal<SecondTestService>();
|
||||||
|
|
||||||
TestClass? testClass = injector.Resolve<TestClass>();
|
using (LifetimeScope scope = container.BeginScope())
|
||||||
|
{
|
||||||
|
TestClass? testClass = scope.Resolve<TestClass>();
|
||||||
|
testClass.Test();
|
||||||
|
int res = scope.Invoke<TestClass, int, SecondTestService>(testClass, (i) => i.SecondTest);
|
||||||
|
|
||||||
testClass.Test();
|
Console.WriteLine(res);
|
||||||
|
scope.Invoke<TestClass>(testClass, (i) => i.SecondTest);
|
||||||
|
scope.Invoke<TestClass>(testClass, (i) => i.SecondTest);
|
||||||
|
}
|
||||||
|
|
||||||
injector.Invoke<TestClass>(testClass, (c) => c.Test);
|
Console.WriteLine();
|
||||||
|
|
||||||
|
using (LifetimeScope scope = container.BeginScope())
|
||||||
|
{
|
||||||
|
scope.Resolve<TestClass>().Test();
|
||||||
|
scope.Resolve<TestClass>().Test();
|
||||||
|
scope.Resolve<TestClass>().Test();
|
||||||
|
scope.Resolve<TestClass>().Test();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -16,7 +16,6 @@ internal class TestClass
|
|||||||
|
|
||||||
public void Test()
|
public void Test()
|
||||||
{
|
{
|
||||||
testService.Print();
|
|
||||||
SecondTestService?.Output();
|
SecondTestService?.Output();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace DynamicInjector
|
||||||
|
{
|
||||||
|
public interface IContainer
|
||||||
|
{
|
||||||
|
public object Invoke<TInstance>(TInstance instance, Func<TInstance, Delegate> method);
|
||||||
|
|
||||||
|
public object Resolve(Type type);
|
||||||
|
|
||||||
|
public T Resolve<T>();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,10 +4,11 @@ using System.Reflection;
|
|||||||
|
|
||||||
namespace DynamicInjector
|
namespace DynamicInjector
|
||||||
{
|
{
|
||||||
public class InjectionContainer
|
public class InjectionContainer : IContainer
|
||||||
{
|
{
|
||||||
private readonly Dictionary<string, object> globalServices = new Dictionary<string, object>();
|
private readonly Dictionary<string, object> globalServices = new Dictionary<string, object>();
|
||||||
private readonly Dictionary<string, Type> localServices = new Dictionary<string, Type>();
|
private readonly Dictionary<string, Type> localServices = new Dictionary<string, Type>();
|
||||||
|
private readonly Dictionary<string, Type> scopedServices = new Dictionary<string, Type>();
|
||||||
|
|
||||||
public void RegisterGlobal<TService>(string? name = null)
|
public void RegisterGlobal<TService>(string? name = null)
|
||||||
{
|
{
|
||||||
@@ -36,7 +37,22 @@ namespace DynamicInjector
|
|||||||
|
|
||||||
public void RegisterLocal<TService, TImplementation>(string? name = null) where TImplementation : TService
|
public void RegisterLocal<TService, TImplementation>(string? name = null) where TImplementation : TService
|
||||||
{
|
{
|
||||||
localServices.Add(name ?? typeof(TService).FullName, typeof(TService));
|
localServices.Add(name ?? typeof(TService).FullName, typeof(TImplementation));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RegisterScoped<TService>(string? name = null)
|
||||||
|
{
|
||||||
|
scopedServices.Add(name ?? typeof(TService).FullName, typeof(TService));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RegisterScoped<TService, TImplementation>(string? name = null) where TImplementation : TService
|
||||||
|
{
|
||||||
|
scopedServices.Add(name ?? typeof(TService).FullName, typeof(TImplementation));
|
||||||
|
}
|
||||||
|
|
||||||
|
public LifetimeScope BeginScope()
|
||||||
|
{
|
||||||
|
return new LifetimeScope(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public T Resolve<T>()
|
public T Resolve<T>()
|
||||||
@@ -45,6 +61,11 @@ namespace DynamicInjector
|
|||||||
}
|
}
|
||||||
|
|
||||||
public object Resolve(Type type)
|
public object Resolve(Type type)
|
||||||
|
{
|
||||||
|
return InternalResolve(type, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal object InternalResolve(Type type, LifetimeScope? scope)
|
||||||
{
|
{
|
||||||
ConstructorInfo[]? constructorInfos = type.GetConstructors();
|
ConstructorInfo[]? constructorInfos = type.GetConstructors();
|
||||||
|
|
||||||
@@ -71,11 +92,11 @@ namespace DynamicInjector
|
|||||||
|
|
||||||
foreach (ParameterInfo parameterInfo in parameterInfos)
|
foreach (ParameterInfo parameterInfo in parameterInfos)
|
||||||
{
|
{
|
||||||
if (CheckIfServiceExists(parameterInfo.Name, out object? serviceInstance))
|
if (CheckIfServiceExists(parameterInfo.Name, scope, out object? serviceInstance))
|
||||||
{
|
{
|
||||||
arguments.Add(serviceInstance!);
|
arguments.Add(serviceInstance!);
|
||||||
}
|
}
|
||||||
else if (CheckIfServiceExists(parameterInfo.ParameterType.FullName, out serviceInstance))
|
else if (CheckIfServiceExists(parameterInfo.ParameterType.FullName, scope, out serviceInstance))
|
||||||
{
|
{
|
||||||
arguments.Add(serviceInstance!);
|
arguments.Add(serviceInstance!);
|
||||||
}
|
}
|
||||||
@@ -94,11 +115,11 @@ namespace DynamicInjector
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (CheckIfServiceExists(propertyInfo.Name, out object? serviceInstance))
|
if (CheckIfServiceExists(propertyInfo.Name, scope, out object? serviceInstance))
|
||||||
{
|
{
|
||||||
propertyInfo.SetValue(instance, serviceInstance);
|
propertyInfo.SetValue(instance, serviceInstance);
|
||||||
}
|
}
|
||||||
else if (CheckIfServiceExists(propertyInfo.PropertyType.FullName, out serviceInstance))
|
else if (CheckIfServiceExists(propertyInfo.PropertyType.FullName, scope, out serviceInstance))
|
||||||
{
|
{
|
||||||
propertyInfo.SetValue(instance, serviceInstance);
|
propertyInfo.SetValue(instance, serviceInstance);
|
||||||
}
|
}
|
||||||
@@ -112,6 +133,11 @@ namespace DynamicInjector
|
|||||||
}
|
}
|
||||||
|
|
||||||
public object Invoke<TInstance>(TInstance instance, Func<TInstance, Delegate> method)
|
public object Invoke<TInstance>(TInstance instance, Func<TInstance, Delegate> method)
|
||||||
|
{
|
||||||
|
return InternalInvoke(instance, method, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal object InternalInvoke<TInstance>(TInstance instance, Func<TInstance, Delegate> method, LifetimeScope? scope)
|
||||||
{
|
{
|
||||||
Delegate delegateMethod = method.Invoke(instance);
|
Delegate delegateMethod = method.Invoke(instance);
|
||||||
MethodInfo methodInfo = delegateMethod.Method;
|
MethodInfo methodInfo = delegateMethod.Method;
|
||||||
@@ -126,11 +152,11 @@ namespace DynamicInjector
|
|||||||
|
|
||||||
foreach (ParameterInfo parameterInfo in parameterInfos)
|
foreach (ParameterInfo parameterInfo in parameterInfos)
|
||||||
{
|
{
|
||||||
if (CheckIfServiceExists(parameterInfo.Name, out object? serviceInstance))
|
if (CheckIfServiceExists(parameterInfo.Name, scope, out object? serviceInstance))
|
||||||
{
|
{
|
||||||
arguments.Add(serviceInstance!);
|
arguments.Add(serviceInstance!);
|
||||||
}
|
}
|
||||||
else if (CheckIfServiceExists(parameterInfo.ParameterType.FullName, out serviceInstance))
|
else if (CheckIfServiceExists(parameterInfo.ParameterType.FullName, scope, out serviceInstance))
|
||||||
{
|
{
|
||||||
arguments.Add(serviceInstance!);
|
arguments.Add(serviceInstance!);
|
||||||
}
|
}
|
||||||
@@ -143,8 +169,17 @@ namespace DynamicInjector
|
|||||||
return delegateMethod.DynamicInvoke(arguments.ToArray());
|
return delegateMethod.DynamicInvoke(arguments.ToArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool CheckIfServiceExists(string typeName, out object? serviceInstance)
|
private bool CheckIfServiceExists(string typeName, LifetimeScope? scope, out object? serviceInstance)
|
||||||
{
|
{
|
||||||
|
foreach (KeyValuePair<string, Type> serviceType in localServices)
|
||||||
|
{
|
||||||
|
if (typeName == serviceType.Key)
|
||||||
|
{
|
||||||
|
serviceInstance = InternalResolve(localServices[serviceType.Key], scope);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
foreach (KeyValuePair<string, object> serviceType in globalServices)
|
foreach (KeyValuePair<string, object> serviceType in globalServices)
|
||||||
{
|
{
|
||||||
if (typeName == serviceType.Key)
|
if (typeName == serviceType.Key)
|
||||||
@@ -154,11 +189,25 @@ namespace DynamicInjector
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (KeyValuePair<string, Type> serviceType in localServices)
|
if (scope is null)
|
||||||
|
{
|
||||||
|
serviceInstance = null;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (KeyValuePair<string, Type> serviceType in scopedServices)
|
||||||
{
|
{
|
||||||
if (typeName == serviceType.Key)
|
if (typeName == serviceType.Key)
|
||||||
{
|
{
|
||||||
serviceInstance = Resolve(localServices[serviceType.Key]);
|
if (scope.Services.ContainsKey(serviceType.Key))
|
||||||
|
{
|
||||||
|
serviceInstance = scope.Services[serviceType.Key];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
serviceInstance = InternalResolve(scopedServices[serviceType.Key], scope);
|
||||||
|
scope.Services.Add(serviceType.Key, serviceInstance);
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace DynamicInjector
|
||||||
|
{
|
||||||
|
public class LifetimeScope : IContainer, IDisposable
|
||||||
|
{
|
||||||
|
private readonly InjectionContainer injectionContainer;
|
||||||
|
|
||||||
|
public Dictionary<string, object> Services { get; } = new Dictionary<string, object>();
|
||||||
|
|
||||||
|
public LifetimeScope(InjectionContainer injectionContainer)
|
||||||
|
{
|
||||||
|
this.injectionContainer = injectionContainer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T Resolve<T>()
|
||||||
|
{
|
||||||
|
return (T)Resolve(typeof(T));
|
||||||
|
}
|
||||||
|
|
||||||
|
public object Resolve(Type type)
|
||||||
|
{
|
||||||
|
return injectionContainer.InternalResolve(type, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public object Invoke<TInstance>(TInstance instance, Func<TInstance, Delegate> method)
|
||||||
|
{
|
||||||
|
return injectionContainer.InternalInvoke(instance, method, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
foreach (object? service in Services.Values)
|
||||||
|
{
|
||||||
|
if (service is IDisposable disposableService)
|
||||||
|
{
|
||||||
|
disposableService.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,79 +4,79 @@ namespace DynamicInjector
|
|||||||
{
|
{
|
||||||
public static class MethodInjectionContainerExtensions
|
public static class MethodInjectionContainerExtensions
|
||||||
{
|
{
|
||||||
public static void Invoke<TService>(this InjectionContainer injectionContainer, Func<TService, Delegate> method)
|
public static void Invoke<T>(this IContainer injectionContainer, Func<T, Delegate> method)
|
||||||
{
|
{
|
||||||
injectionContainer.Invoke(injectionContainer.Resolve<TService>(), method);
|
injectionContainer.Invoke(injectionContainer.Resolve<T>(), method);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TReturn Invoke<TInstance, TReturn>(this InjectionContainer injectionContainer, TInstance instance, Func<TInstance, Func<TReturn>> method)
|
public static TResult Invoke<T, TResult>(this IContainer injectionContainer, T instance, Func<T, Func<TResult>> method)
|
||||||
{
|
{
|
||||||
return (TReturn)injectionContainer.Invoke(instance, method);
|
return (TResult)injectionContainer.Invoke(instance, method);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TReturn Invoke<TInstance, TReturn, T1>(this InjectionContainer injectionContainer, TInstance instance, Func<TInstance, Func<T1, TReturn>> method)
|
public static TResult Invoke<T, TResult, T1>(this IContainer injectionContainer, T instance, Func<T, Func<T1, TResult>> method)
|
||||||
{
|
{
|
||||||
return (TReturn)injectionContainer.Invoke(instance, method);
|
return (TResult)injectionContainer.Invoke(instance, method);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TReturn Invoke<TInstance, TReturn, T1, T2>(this InjectionContainer injectionContainer, TInstance instance, Func<TInstance, Func<T1, T2, TReturn>> method)
|
public static TResult Invoke<T, TResult, T1, T2>(this IContainer injectionContainer, T instance, Func<T, Func<T1, T2, TResult>> method)
|
||||||
{
|
{
|
||||||
return (TReturn)injectionContainer.Invoke(instance, method);
|
return (TResult)injectionContainer.Invoke(instance, method);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TReturn Invoke<TInstance, TReturn, T1, T2, T3>(this InjectionContainer injectionContainer, TInstance instance, Func<TInstance, Func<T1, T2, T3, TReturn>> method)
|
public static TResult Invoke<T, TResult, T1, T2, T3>(this IContainer injectionContainer, T instance, Func<T, Func<T1, T2, T3, TResult>> method)
|
||||||
{
|
{
|
||||||
return (TReturn)injectionContainer.Invoke(instance, method);
|
return (TResult)injectionContainer.Invoke(instance, method);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TReturn Invoke<TInstance, TReturn, T1, T2, T3, T4>(this InjectionContainer injectionContainer, TInstance instance, Func<TInstance, Func<T1, T2, T3, T4, TReturn>> method)
|
public static TResult Invoke<T, TResult, T1, T2, T3, T4>(this IContainer injectionContainer, T instance, Func<T, Func<T1, T2, T3, T4, TResult>> method)
|
||||||
{
|
{
|
||||||
return (TReturn)injectionContainer.Invoke(instance, method);
|
return (TResult)injectionContainer.Invoke(instance, method);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TReturn Invoke<TInstance, TReturn, T1, T2, T3, T4, T5>(this InjectionContainer injectionContainer, TInstance instance, Func<TInstance, Func<T1, T2, T3, T4, T5, TReturn>> method)
|
public static TResult Invoke<T, TResult, T1, T2, T3, T4, T5>(this IContainer injectionContainer, T instance, Func<T, Func<T1, T2, T3, T4, T5, TResult>> method)
|
||||||
{
|
{
|
||||||
return (TReturn)injectionContainer.Invoke(instance, method);
|
return (TResult)injectionContainer.Invoke(instance, method);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TReturn Invoke<TInstance, TReturn, T1, T2, T3, T4, T5, T6>(this InjectionContainer injectionContainer, TInstance instance, Func<TInstance, Func<T1, T2, T3, T4, T5, T6, TReturn>> method)
|
public static TResult Invoke<T, TResult, T1, T2, T3, T4, T5, T6>(this IContainer injectionContainer, T instance, Func<T, Func<T1, T2, T3, T4, T5, T6, TResult>> method)
|
||||||
{
|
{
|
||||||
return (TReturn)injectionContainer.Invoke(instance, method);
|
return (TResult)injectionContainer.Invoke(instance, method);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TReturn Invoke<TService, TReturn>(this InjectionContainer injectionContainer, Func<TService, Func<TReturn>> method)
|
public static TResult Invoke<T, TResult>(this IContainer injectionContainer, Func<T, Func<TResult>> method)
|
||||||
{
|
{
|
||||||
return (TReturn)injectionContainer.Invoke(injectionContainer.Resolve<TService>(), method);
|
return (TResult)injectionContainer.Invoke(injectionContainer.Resolve<T>(), method);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TReturn Invoke<TService, TReturn, T1>(this InjectionContainer injectionContainer, Func<TService, Func<T1, TReturn>> method)
|
public static TResult Invoke<T, TResult, T1>(this IContainer injectionContainer, Func<T, Func<T1, TResult>> method)
|
||||||
{
|
{
|
||||||
return (TReturn)injectionContainer.Invoke(injectionContainer.Resolve<TService>(), method);
|
return (TResult)injectionContainer.Invoke(injectionContainer.Resolve<T>(), method);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TReturn Invoke<TService, TReturn, T1, T2>(this InjectionContainer injectionContainer, Func<TService, Func<T1, T2, TReturn>> method)
|
public static TResult Invoke<T, TResult, T1, T2>(this IContainer injectionContainer, Func<T, Func<T1, T2, TResult>> method)
|
||||||
{
|
{
|
||||||
return (TReturn)injectionContainer.Invoke(injectionContainer.Resolve<TService>(), method);
|
return (TResult)injectionContainer.Invoke(injectionContainer.Resolve<T>(), method);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TReturn Invoke<TService, TReturn, T1, T2, T3>(this InjectionContainer injectionContainer, Func<TService, Func<T1, T2, T3, TReturn>> method)
|
public static TResult Invoke<T, TResult, T1, T2, T3>(this IContainer injectionContainer, Func<T, Func<T1, T2, T3, TResult>> method)
|
||||||
{
|
{
|
||||||
return (TReturn)injectionContainer.Invoke(injectionContainer.Resolve<TService>(), method);
|
return (TResult)injectionContainer.Invoke(injectionContainer.Resolve<T>(), method);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TReturn Invoke<TService, TReturn, T1, T2, T3, T4>(this InjectionContainer injectionContainer, Func<TService, Func<T1, T2, T3, T4, TReturn>> method)
|
public static TResult Invoke<T, TResult, T1, T2, T3, T4>(this IContainer injectionContainer, Func<T, Func<T1, T2, T3, T4, TResult>> method)
|
||||||
{
|
{
|
||||||
return (TReturn)injectionContainer.Invoke(injectionContainer.Resolve<TService>(), method);
|
return (TResult)injectionContainer.Invoke(injectionContainer.Resolve<T>(), method);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TReturn Invoke<TService, TReturn, T1, T2, T3, T4, T5>(this InjectionContainer injectionContainer, Func<TService, Func<T1, T2, T3, T4, T5, TReturn>> method)
|
public static TResult Invoke<T, TResult, T1, T2, T3, T4, T5>(this IContainer injectionContainer, Func<T, Func<T1, T2, T3, T4, T5, TResult>> method)
|
||||||
{
|
{
|
||||||
return (TReturn)injectionContainer.Invoke(injectionContainer.Resolve<TService>(), method);
|
return (TResult)injectionContainer.Invoke(injectionContainer.Resolve<T>(), method);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TReturn Invoke<TService, TReturn, T1, T2, T3, T4, T5, T6>(this InjectionContainer injectionContainer, Func<TService, Func<T1, T2, T3, T4, T5, T6, TReturn>> method)
|
public static TResult Invoke<T, TResult, T1, T2, T3, T4, T5, T6>(this IContainer injectionContainer, Func<T, Func<T1, T2, T3, T4, T5, T6, TResult>> method)
|
||||||
{
|
{
|
||||||
return (TReturn)injectionContainer.Invoke(injectionContainer.Resolve<TService>(), method);
|
return (TResult)injectionContainer.Invoke(injectionContainer.Resolve<T>(), method);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user