Skip to content

Instantly share code, notes, and snippets.

@NDiiong
Created May 12, 2021 09:48
Show Gist options
  • Save NDiiong/597cc041cf86b76d06ca0753c12f4255 to your computer and use it in GitHub Desktop.
Save NDiiong/597cc041cf86b76d06ca0753c12f4255 to your computer and use it in GitHub Desktop.
public static void AddAny(this IServiceCollection collection, Assembly assembly, Func<Type, bool> implementFactory, ServiceLifetime serviceLifetime)
{
var serviceTypes = assembly.GetTypes()
.Where(x => !x.IsAbstract && x.GetInterfaces().Any(IsAnyServiceInterface))
.ToList();
serviceTypes.ForEach(x => AddService(collection, x));
bool IsAnyServiceInterface(Type type)
{
var parentInterfaceTypes = type.GetInterfaces();
return parentInterfaceTypes.Any(implementFactory);
}
void AddService(IServiceCollection collection, Type type)
{
var interfaceType = type.GetInterfaces().Single(IsAnyServiceInterface);
collection.TryAdd(new ServiceDescriptor(interfaceType, type, serviceLifetime));
}
}
public static void AddAny(this IServiceCollection collection, Type serviceType, Func<Type, bool> implementFactory, ServiceLifetime serviceLifetime)
{
AddAny(collection, serviceType.Assembly, implementFactory, serviceLifetime);
}
public static void AddAnyTransient(this IServiceCollection collection, Assembly assembly, Func<Type, bool> implementFactory)
{
collection.AddAny(assembly, implementFactory, ServiceLifetime.Transient);
}
public static void AddAnyTransient(this IServiceCollection collection, Type serviceType, Func<Type, bool> implementFactory)
{
collection.AddAny(serviceType, implementFactory, ServiceLifetime.Transient);
}
/// <summary>
/// Adds the specified descriptor to the collection if the service type hasn't already been registered.
/// </summary>
/// <param name="collection"></param>
/// <param name="serviceDescriptor"></param>
public static void TryAdd(this IServiceCollection collection, ServiceDescriptor serviceDescriptor)
{
//collection.TryAddScoped
if (!collection.Any(f => f.ServiceType == serviceDescriptor.ServiceType && f.ImplementationType == serviceDescriptor.ImplementationType))
collection.Add(serviceDescriptor);
}
/// <summary>
/// Adds the specified TService as a <see cref="ServiceLifetime.Transient"/>
/// service to the collection if the service type and implementation type hasn't already been registered.
/// </summary>
/// <typeparam name="TService"></typeparam>
/// <param name="collection"></param>
public static void TryAddTransient<TService>(this IServiceCollection collection)
where TService : class
{
TryAddTransient(collection, typeof(TService), typeof(TService));
}
/// <summary>
/// Adds the specified TService as a Microsoft.Extensions.DependencyInjection.ServiceLifetime.Transient
/// service implementation type specified in TImplementation to the collection if
/// the service type and implementation type hasn't already been registered.
/// </summary>
/// <typeparam name="TService"></typeparam>
/// <typeparam name="TImplementation"></typeparam>
/// <param name="collection"></param>
public static void TryAddTransient<TService, TImplementation>(this IServiceCollection collection)
where TService : class
where TImplementation : class, TService
{
TryAddTransient(collection, typeof(TService), typeof(TImplementation));
}
/// <summary>
/// Adds the specified service as a <see cref="ServiceLifetime.Transient"/>
/// service to the collection if the service type and implementation type hasn't already been registered.
/// </summary>
/// <param name="collection"></param>
/// <param name="service"></param>
/// <param name="implementationType"></param>
public static void TryAddTransient(this IServiceCollection collection, Type service, Type implementationType)
{
if (!collection.Any(q => q.ServiceType == service && q.ImplementationType == implementationType))
{
collection.AddTransient(service, implementationType);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment