Last active
September 11, 2020 04:55
-
-
Save NDiiong/0356d1a7b3ab030db56c4b070cc8bb48 to your computer and use it in GitHub Desktop.
Remove IServiceCollection
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
public static partial class ServiceCollectionExtensions | |
{ | |
//Ref: https://stackoverflow.com/questions/49087739/net-core-register-raw-generic-with-different-number-of-parameters | |
public static IServiceCollection Scan(this IServiceCollection serviceCollection, IEnumerable<Assembly> assemblies, Type interfaceType, ServiceLifetime lifetime) | |
{ | |
foreach (var type in assemblies.SelectMany(x => x.GetTypes().Where(t => t.IsClass && !t.IsAbstract))) | |
{ | |
foreach (var i in type.GetInterfaces()) | |
{ | |
// Check for generic | |
if (i.IsGenericType && i.GetGenericTypeDefinition() == interfaceType) | |
{ | |
var genericInterfaceType = interfaceType.MakeGenericType(i.GetGenericArguments()); | |
if (!serviceCollection.Any(f => f.ServiceType == genericInterfaceType && f.ImplementationType == type)) | |
serviceCollection.Add(new ServiceDescriptor(genericInterfaceType, type, lifetime)); | |
} | |
// Check for non-generic | |
else if (!i.IsGenericType && i == interfaceType) | |
{ | |
if (!serviceCollection.Any(f => f.ServiceType == interfaceType && f.ImplementationType == type)) | |
serviceCollection.Add(new ServiceDescriptor(interfaceType, type, lifetime)); | |
} | |
} | |
} | |
return serviceCollection; | |
} | |
} |
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
public static partial class ServiceCollectionExtensions | |
{ | |
private static void RegisterAllTypes<T>(this IServiceCollection services, Assembly[] assemblies, ServiceLifetime lifetime = ServiceLifetime.Transient) | |
{ | |
var typesFromAssemblies = assemblies | |
.SelectMany(a => a.DefinedTypes.Where(x => !x.IsAbstract && x.GetInterfaces().Contains(typeof(T)))); | |
foreach (var type in typesFromAssemblies) | |
{ | |
services.Add(new ServiceDescriptor(typeof(T), type, lifetime)); | |
} | |
} | |
public static IServiceCollection AddDomainServices(this IServiceCollection services) | |
{ | |
services.RegisterAllTypes<IValidator>(new[] { typeof(DomainServiceRegistration).Assembly }); | |
return services; | |
} | |
} |
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
Type[] interfaceTypes = { | |
typeof(INumberValidator), | |
}; | |
private static void RegisterAllTypes(this IServiceCollection services, Assembly[] assemblies, Type[] interfaceTypes, ServiceLifetime lifetime = ServiceLifetime.Transient) | |
{ | |
foreach (var typeInterface in interfaceTypes) | |
{ | |
var typesFromAssemblies = assemblies | |
.SelectMany(a => a.DefinedTypes.Where(x => !x.IsAbstract && x.GetInterfaces().Contains(typeInterface))); | |
foreach (var type in typesFromAssemblies) | |
{ | |
services.Add(new ServiceDescriptor(typeInterface, type, lifetime)); | |
} | |
} | |
} |
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
public static partial class ServiceCollectionExtensions | |
{ | |
public static IServiceCollection Remove<T>(this IServiceCollection services) | |
{ | |
var serviceDescriptor = services.FirstOrDefault(descriptor => descriptor.ServiceType == typeof(T)); | |
if (serviceDescriptor != null) services.Remove(serviceDescriptor); | |
return services; | |
} | |
private static IServiceCollection Remove(this IServiceCollection services, params Type[] types) | |
{ | |
var serviceDescriptor = services.FirstOrDefault(descriptor => types.Contains(descriptor.ServiceType)); | |
if (serviceDescriptor != null) | |
services.Remove(serviceDescriptor); | |
return services; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment