Files
StoreWeb/StoreHub.Framework/DependencyInjection/ServiceRegistrarProxy.cs
T
2026-08-14 18:00:58 +08:00

89 lines
3.0 KiB
C#

using MAX.Core.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
namespace StoreHub.Framework.DependencyInjection
{
/// <summary>
/// 将 <see cref="IServiceRegistrar"/> 适配到 Microsoft.Extensions.DependencyInjection
/// </summary>
internal sealed class ServiceRegistrarProxy : IServiceRegistrar
{
private readonly IServiceCollection _services;
public ServiceRegistrarProxy(IServiceCollection services)
{
_services = services;
}
public void RegisterSingleton<TService, TImplementation>() where TService : class where TImplementation : class, TService
{
_services.AddSingleton<TService, TImplementation>();
}
public void RegisterSingleton<TService>(TService instance) where TService : class
{
_services.AddSingleton(instance);
}
public void RegisterSingleton<TService>(Func<IServiceProvider, TService> factory) where TService : class
{
_services.AddSingleton(factory);
}
public void RegisterSingleton<TService, TImplementation>(object? serviceKey) where TService : class where TImplementation : class, TService
{
_services.AddKeyedSingleton<TService, TImplementation>(serviceKey);
}
public void RegisterSingleton<TService>(object? serviceKey, TService instance) where TService : class
{
_services.AddKeyedSingleton(serviceKey, instance);
}
public void RegisterSingleton<TService>(object? serviceKey, Func<IServiceProvider, object?, TService> factory) where TService : class
{
_services.AddKeyedSingleton(serviceKey, factory);
}
public void RegisterScoped<TService, TImplementation>() where TService : class where TImplementation : class, TService
{
_services.AddScoped<TService, TImplementation>();
}
public void RegisterScoped<TService>(Func<IServiceProvider, TService> factory) where TService : class
{
_services.AddScoped(factory);
}
public void RegisterScoped<TService, TImplementation>(object? serviceKey) where TService : class where TImplementation : class, TService
{
_services.AddKeyedScoped<TService, TImplementation>(serviceKey);
}
public void RegisterScoped<TService>(object? serviceKey, Func<IServiceProvider, object?, TService> factory) where TService : class
{
_services.AddKeyedScoped(serviceKey, factory);
}
public void RegisterTransient<TService, TImplementation>() where TService : class where TImplementation : class, TService
{
_services.AddTransient<TService, TImplementation>();
}
public void RegisterTransient<TService>(Func<IServiceProvider, TService> factory) where TService : class
{
_services.AddTransient(factory);
}
public void RegisterTransient<TService, TImplementation>(object? serviceKey) where TService : class where TImplementation : class, TService
{
_services.AddKeyedTransient<TService, TImplementation>(serviceKey);
}
public void RegisterTransient<TService>(object? serviceKey, Func<IServiceProvider, object?, TService> factory) where TService : class
{
_services.AddKeyedTransient(serviceKey, factory);
}
}
}