77 lines
2.2 KiB
C#
77 lines
2.2 KiB
C#
namespace LaaProductionWeb.Services
|
|
{
|
|
using LaaProductionWeb.Services.Interfaces;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using System.Linq;
|
|
|
|
public static class ServicesExtensions
|
|
{
|
|
public static IServiceCollection AddTransientServices(this IServiceCollection services)
|
|
{
|
|
var transient = typeof(ITransient);
|
|
var assemblyServices = transient
|
|
.Assembly
|
|
.GetTypes()
|
|
.Where(x => !x.IsAbstract
|
|
&& transient.IsAssignableFrom(x));
|
|
|
|
foreach (var service in assemblyServices)
|
|
{
|
|
var interfaces = service
|
|
.GetInterfaces()
|
|
.Where(x => x != transient);
|
|
|
|
if (!interfaces.Any())
|
|
{
|
|
services.AddTransient(service);
|
|
|
|
continue;
|
|
}
|
|
|
|
foreach (var @interface in interfaces)
|
|
{
|
|
services.AddTransient(@interface, service);
|
|
}
|
|
}
|
|
|
|
return services;
|
|
}
|
|
|
|
public static IServiceCollection AddHttpClient(this IServiceCollection services, string baseAddress)
|
|
=> services.AddSingleton<IHttpService>(_ => new HttpService(baseAddress));
|
|
|
|
public static IServiceCollection AddSingletonServices(this IServiceCollection services)
|
|
{
|
|
var transient = typeof(ISingleton);
|
|
var assemblyServices = transient
|
|
.Assembly
|
|
.GetTypes()
|
|
.Where(x => !x.IsAbstract
|
|
&& transient.IsAssignableFrom(x));
|
|
|
|
foreach (var service in assemblyServices)
|
|
{
|
|
var interfaces = service
|
|
.GetInterfaces()
|
|
.Where(x => x != transient);
|
|
|
|
if (!interfaces.Any())
|
|
{
|
|
services.AddTransient(service);
|
|
|
|
continue;
|
|
}
|
|
|
|
foreach (var @interface in interfaces)
|
|
{
|
|
services.AddTransient(@interface, service);
|
|
}
|
|
}
|
|
|
|
return services;
|
|
}
|
|
}
|
|
}
|