Added ability to inform DI system of types to ignore

Made use of primary constructor
This commit is contained in:
Chomp
2025-06-29 10:54:42 +01:00
parent cf61560266
commit 95e473b22a
@@ -3,23 +3,19 @@ using SPTarkov.DI.Annotations;
namespace SPTarkov.DI;
public class DependencyInjectionHandler
public class DependencyInjectionHandler(IServiceCollection serviceCollection)
{
private static List<Type>? _allLoadedTypes;
private static List<ConstructorInfo>? _allConstructors;
private readonly Dictionary<string, Type> _injectedTypeNames = new();
private readonly IServiceCollection _serviceCollection;
private readonly Dictionary<string, object> _injectedValues = new();
private readonly Lock _injectedValuesLock = new();
private bool _oneTimeUseFlag;
private readonly HashSet<string> _typeNamesToSkip = [];
public DependencyInjectionHandler(IServiceCollection serviceCollection)
{
_serviceCollection = serviceCollection;
}
private bool _oneTimeUseFlag;
public void AddInjectableTypesFromAssembly(Assembly assembly)
{
@@ -49,6 +45,11 @@ public class DependencyInjectionHandler
{
foreach (var type in typesToInject)
{
if (_typeNamesToSkip.Contains(type.Name))
{
continue;
}
_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)
{
try
@@ -216,10 +222,10 @@ public class DependencyInjectionHandler
HandleSingletonRegistration(registrableInterface, implementationType);
break;
case InjectionType.Transient:
_serviceCollection.AddTransient(registrableInterface, implementationType);
serviceCollection.AddTransient(registrableInterface, implementationType);
break;
case InjectionType.Scoped:
_serviceCollection.AddScoped(registrableInterface, implementationType);
serviceCollection.AddScoped(registrableInterface, implementationType);
break;
default:
throw new ArgumentOutOfRangeException(
@@ -234,7 +240,7 @@ public class DependencyInjectionHandler
var serviceKey = $"{implementationType.Namespace}.{implementationType.Name}";
if (registrableInterface != implementationType)
{
_serviceCollection.AddSingleton(
serviceCollection.AddSingleton(
registrableInterface,
(serviceProvider) =>
{
@@ -254,7 +260,7 @@ public class DependencyInjectionHandler
}
else
{
_serviceCollection.AddSingleton(registrableInterface, implementationType);
serviceCollection.AddSingleton(registrableInterface, implementationType);
}
}