Added ability to inform DI system of types to ignore
Made use of primary constructor
This commit is contained in:
@@ -3,23 +3,19 @@ using SPTarkov.DI.Annotations;
|
|||||||
|
|
||||||
namespace SPTarkov.DI;
|
namespace SPTarkov.DI;
|
||||||
|
|
||||||
public class DependencyInjectionHandler
|
public class DependencyInjectionHandler(IServiceCollection serviceCollection)
|
||||||
{
|
{
|
||||||
private static List<Type>? _allLoadedTypes;
|
private static List<Type>? _allLoadedTypes;
|
||||||
private static List<ConstructorInfo>? _allConstructors;
|
private static List<ConstructorInfo>? _allConstructors;
|
||||||
|
|
||||||
private readonly Dictionary<string, Type> _injectedTypeNames = new();
|
private readonly Dictionary<string, Type> _injectedTypeNames = new();
|
||||||
private readonly IServiceCollection _serviceCollection;
|
|
||||||
|
|
||||||
private readonly Dictionary<string, object> _injectedValues = new();
|
private readonly Dictionary<string, object> _injectedValues = new();
|
||||||
private readonly Lock _injectedValuesLock = new();
|
private readonly Lock _injectedValuesLock = new();
|
||||||
|
|
||||||
private bool _oneTimeUseFlag;
|
private readonly HashSet<string> _typeNamesToSkip = [];
|
||||||
|
|
||||||
public DependencyInjectionHandler(IServiceCollection serviceCollection)
|
private bool _oneTimeUseFlag;
|
||||||
{
|
|
||||||
_serviceCollection = serviceCollection;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void AddInjectableTypesFromAssembly(Assembly assembly)
|
public void AddInjectableTypesFromAssembly(Assembly assembly)
|
||||||
{
|
{
|
||||||
@@ -49,6 +45,11 @@ public class DependencyInjectionHandler
|
|||||||
{
|
{
|
||||||
foreach (var type in typesToInject)
|
foreach (var type in typesToInject)
|
||||||
{
|
{
|
||||||
|
if (_typeNamesToSkip.Contains(type.Name))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
_injectedTypeNames.Add($"{type.Namespace}.{type.Name}", type);
|
_injectedTypeNames.Add($"{type.Namespace}.{type.Name}", type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -139,6 +140,11 @@ public class DependencyInjectionHandler
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void AddTypeNamesToIgnore(HashSet<string> typeNames)
|
||||||
|
{
|
||||||
|
_typeNamesToSkip.UnionWith(typeNames);
|
||||||
|
}
|
||||||
|
|
||||||
private void RegisterGenericComponents(TypeRefContainer typeRef)
|
private void RegisterGenericComponents(TypeRefContainer typeRef)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -216,10 +222,10 @@ public class DependencyInjectionHandler
|
|||||||
HandleSingletonRegistration(registrableInterface, implementationType);
|
HandleSingletonRegistration(registrableInterface, implementationType);
|
||||||
break;
|
break;
|
||||||
case InjectionType.Transient:
|
case InjectionType.Transient:
|
||||||
_serviceCollection.AddTransient(registrableInterface, implementationType);
|
serviceCollection.AddTransient(registrableInterface, implementationType);
|
||||||
break;
|
break;
|
||||||
case InjectionType.Scoped:
|
case InjectionType.Scoped:
|
||||||
_serviceCollection.AddScoped(registrableInterface, implementationType);
|
serviceCollection.AddScoped(registrableInterface, implementationType);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new ArgumentOutOfRangeException(
|
throw new ArgumentOutOfRangeException(
|
||||||
@@ -234,7 +240,7 @@ public class DependencyInjectionHandler
|
|||||||
var serviceKey = $"{implementationType.Namespace}.{implementationType.Name}";
|
var serviceKey = $"{implementationType.Namespace}.{implementationType.Name}";
|
||||||
if (registrableInterface != implementationType)
|
if (registrableInterface != implementationType)
|
||||||
{
|
{
|
||||||
_serviceCollection.AddSingleton(
|
serviceCollection.AddSingleton(
|
||||||
registrableInterface,
|
registrableInterface,
|
||||||
(serviceProvider) =>
|
(serviceProvider) =>
|
||||||
{
|
{
|
||||||
@@ -254,7 +260,7 @@ public class DependencyInjectionHandler
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_serviceCollection.AddSingleton(registrableInterface, implementationType);
|
serviceCollection.AddSingleton(registrableInterface, implementationType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user