Church.Net.API/Church.Net.WebAPI/Extensions/ServiceCollectionExtensions.cs
2025-11-02 10:21:28 -08:00

99 lines
4.0 KiB
C#

using Microsoft.Extensions.DependencyInjection;
using System.Collections.Generic;
using System.Data;
using System;
using System.Linq;
namespace Church.Net.WebAPI.Extensions
{
public static class ServiceCollectionExtensions
{
public static IServiceCollection Remove<T>(this IServiceCollection services)
{
if (services.IsReadOnly)
{
throw new ReadOnlyException($"{nameof(services)} is read only");
}
var serviceDescriptors = services.Where(descriptor => descriptor.ServiceType == typeof(T));
if (serviceDescriptors.Any())
{
foreach (var service in serviceDescriptors)
{
services.Remove(service);
}
}
return services;
}
public static IServiceCollection OverWriteTransient<TService, TImplementation>(this IServiceCollection services) where TService : class where TImplementation : class, TService
{
services.Remove<TService>();
services.AddTransient<TService, TImplementation>();
return services;
}
public static IServiceCollection OverWriteScoped<TService, TImplementation>(this IServiceCollection services) where TService : class where TImplementation : class, TService
{
services.Remove<TService>();
services.AddScoped<TService, TImplementation>();
return services;
}
public static IServiceCollection OverWriteSingleton<TService, TImplementation>(this IServiceCollection services) where TService : class where TImplementation : class, TService
{
services.Remove<TService>();
services.AddSingleton<TService, TImplementation>();
return services;
}
//public static void AddFactory<TService, TImplementation>(this IServiceCollection services)
//where TService : class
//where TImplementation : class, TService
//{
// services.AddTransient<TService, TImplementation>();
// services.AddSingleton<Func<TService>>(x => () => x.GetService<TService>());
// services.AddSingleton<IFactory<TService>, Factory<TService>>();
//}
public static IServiceCollection AddMultiScoped<IService, TBaseImplementation>(this IServiceCollection services) where IService : class where TBaseImplementation : class, IService
{
foreach (var type in GetImplementedTypes<IService, TBaseImplementation>())
{
services.AddScoped(h => (IService)Activator.CreateInstance(type));
}
return services;
}
public static IServiceCollection AddMultiTransient<IService, TBaseImplementation>(this IServiceCollection services) where IService : class where TBaseImplementation : class, IService
{
foreach (var type in GetImplementedTypes<IService, TBaseImplementation>())
{
services.AddTransient(h => (IService)Activator.CreateInstance(type));
}
return services;
}
public static IServiceCollection AddMultiSingleton<IService, TBaseImplementation>(this IServiceCollection services) where IService : class where TBaseImplementation : class, IService
{
foreach (var type in GetImplementedTypes<IService, TBaseImplementation>())
{
services.AddSingleton(h => (IService)Activator.CreateInstance(type));
}
return services;
}
private static IEnumerable<Type> GetImplementedTypes<TService, TBaseImplementation>() where TBaseImplementation : class, TService
{
Type interfaceType = typeof(TService);
Type baseType = typeof(TBaseImplementation);
IEnumerable<Type> types = System.Reflection.Assembly.GetAssembly(baseType).GetTypes()
.Where(p => !p.IsInterface && p != baseType && interfaceType.IsAssignableFrom(p));
return types;
}
}
}