introduce ITransientService interface for auto registration of transient services (for DBContexts and Repositories)

This commit is contained in:
Shaun Walker
2022-07-26 09:41:42 -04:00
parent e61a6df4d7
commit 5762ce58a4
10 changed files with 35 additions and 15 deletions

View File

@ -250,7 +250,7 @@ namespace Microsoft.Extensions.DependencyInjection
var assemblies = AppDomain.CurrentDomain.GetOqtaneAssemblies();
foreach (var assembly in assemblies)
{
// dynamically register module services, contexts, and repository classes
// dynamically register module scoped services (ie. client service classes)
var implementationTypes = assembly.GetInterfaces<IService>();
foreach (var implementationType in implementationTypes)
{
@ -261,6 +261,17 @@ namespace Microsoft.Extensions.DependencyInjection
}
}
// dynamically register module transient services (ie. server DBContext, repository classes)
implementationTypes = assembly.GetInterfaces<ITransientService>();
foreach (var implementationType in implementationTypes)
{
if (implementationType.AssemblyQualifiedName != null)
{
var serviceType = Type.GetType(implementationType.AssemblyQualifiedName.Replace(implementationType.Name, $"I{implementationType.Name}"));
services.AddScoped(serviceType ?? implementationType, implementationType);
}
}
// dynamically register hosted services
var serviceTypes = assembly.GetTypes(hostedServiceType);
foreach (var serviceType in serviceTypes)