wabbajack/Wabbajack.DTOs/Extensions/DependencyInjectionExtensions.cs

75 lines
2.5 KiB
C#
Raw Normal View History

2021-09-27 12:42:46 +00:00
using System;
using Microsoft.Extensions.DependencyInjection;
2021-10-23 16:51:17 +00:00
namespace Wabbajack.DTOs;
public static class DependencyInjectionExtensions
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
public static IServiceCollection AddAllSingleton<T1, TBase>(this IServiceCollection services,
Func<IServiceProvider, TBase>? ctor = null)
where TBase : class, T1
where T1 : class
{
if (ctor == null)
services.AddSingleton<TBase>();
else
services.AddSingleton(ctor);
services.AddSingleton<T1, TBase>(s => s.GetService<TBase>()!);
return services;
}
public static IServiceCollection AddAllSingleton<T1, T2, TBase>(this IServiceCollection services,
Func<IServiceProvider, TBase>? ctor = null)
where TBase : class, T1, T2
where T1 : class
where T2 : class
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
if (ctor == null)
services.AddSingleton<TBase>();
else
services.AddSingleton(ctor);
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
services.AddSingleton<T1, TBase>(s => s.GetService<TBase>()!);
services.AddSingleton<T2, TBase>(s => s.GetService<TBase>()!);
return services;
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public static IServiceCollection AddAllSingleton<T1, T2, T3, TBase>(this IServiceCollection services,
Func<IServiceProvider, TBase>? ctor = null)
where TBase : class, T1, T2, T3
where T1 : class
where T2 : class
where T3 : class
{
if (ctor == null)
services.AddSingleton<TBase>();
else
services.AddSingleton(ctor);
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
services.AddSingleton<T1, TBase>(s => s.GetService<TBase>()!);
services.AddSingleton<T2, TBase>(s => s.GetService<TBase>()!);
services.AddSingleton<T3, TBase>(s => s.GetService<TBase>()!);
return services;
}
public static IServiceCollection AddAllSingleton<T1, T2, T3, T4, TBase>(this IServiceCollection services,
Func<IServiceProvider, TBase>? ctor = null)
where TBase : class, T1, T2, T3, T4
where T1 : class
where T2 : class
where T3 : class
where T4 : class
{
if (ctor == null)
services.AddSingleton<TBase>();
else
services.AddSingleton(ctor);
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
services.AddSingleton<T1, TBase>(s => s.GetService<TBase>()!);
services.AddSingleton<T2, TBase>(s => s.GetService<TBase>()!);
services.AddSingleton<T3, TBase>(s => s.GetService<TBase>()!);
services.AddSingleton<T4, TBase>(s => s.GetService<TBase>()!);
return services;
2021-09-27 12:42:46 +00:00
}
}