[英]Asp.Net Core: register implementation with multiple interfaces and lifestyle Singleton
考慮以下接口和類定義:
public interface IInterface1 { }
public interface IInterface2 { }
public class MyClass : IInterface1, IInterface2 { }
有沒有辦法用多個接口注冊MyClass
一個實例,如下所示:
...
services.AddSingleton<IInterface1, IInterface2, MyClass>();
...
並使用不同的接口解析MyClass
這個單個實例,如下所示:
IInterface1 interface1 = app.ApplicationServices.GetService<IInterface1>();
IInterface2 interface2 = app.ApplicationServices.GetService<IInterface2>();
根據定義,服務集合是ServiceDescriptor
的集合,它們是服務類型和實現類型的對。
但是,您可以通過創建自己的提供程序功能來解決此問題,如下所示(感謝 user7224827):
services.AddSingleton<IInterface1>();
services.AddSingleton<IInterface2>(x => x.GetService<IInterface1>());
更多選項如下:
private static MyClass ClassInstance;
public void ConfigureServices(IServiceCollection services)
{
ClassInstance = new MyClass();
services.AddSingleton<IInterface1>(provider => ClassInstance);
services.AddSingleton<IInterface2>(provider => ClassInstance);
}
另一種方法是:
public void ConfigureServices(IServiceCollection services)
{
ClassInstance = new MyClass();
services.AddSingleton<IInterface1>(ClassInstance);
services.AddSingleton<IInterface2>(ClassInstance);
}
我們只提供相同的實例。
您可以包裝 user7224827 的答案,以創建一個與您最初所需的 API 相匹配的不錯的擴展方法:
public static class ServiceCollectionExt
{
public static void AddSingleton<I1, I2, T>(this IServiceCollection services)
where T : class, I1, I2
where I1 : class
where I2 : class
{
services.AddSingleton<I1, T>();
services.AddSingleton<I2, T>(x => (T) x.GetService<I1>());
}
}
保留 DI 機制的另一種選擇是執行以下操作:
services.AddSingleton<MyClass>();
services.AddSingleton<Interface1>(p => p.GetRequiredService<MyClass>());
services.AddSingleton<Interface2>(x => x.GetRequiredService<MyClass>());
上面的答案很酷,以此為靈感,我對其進行了更改,以利用框架附帶的類型約束,從而避免在使用不兼容的類和接口時需要進行強制轉換以及最有用的編譯器錯誤。 在運行時,編譯器錯誤比“什么他媽的為什么是空的”更容易解決;.)
[TestClass()]
public class ServiceCollectionExtensionTests
{
interface MyInterface
{
Guid Id { get; }
}
class MyClas : MyInterface
{
Guid id = Guid.NewGuid();
public Guid Id => id;
}
[TestMethod()]
public void AddSingletonTest()
{
var service = new ServiceCollection()
.AddSingleton<MyClas>()
.ReUseSingleton<MyClas,MyInterface>()
.BuildServiceProvider();
var foo1 = service.GetService<MyClas>();
var foo2 = service.GetService<MyInterface>();
Assert.AreEqual(foo1.Id, foo2.Id);
Assert.AreSame(foo1, foo2);
}
}
“ReUseXYZ”的代碼在這里:
namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
/// Class ServiceCollectionExtension allowing to registered
/// derived implementations of a already registered service
/// to re-use the same service without having to register
/// the same class 2x ending up with 2 instances of the
/// same type in the same scope.
/// </summary>
public static class ServiceCollectionExtension
{
/// <summary>
/// Adds a singleton service of the type specified in TBase with a factory based on the registered type T that has been specified in implementation factory to the specified <see cref="Microsoft.Extensions.DependencyInjection.IServiceCollection"/>.
/// </summary>
/// <typeparam name="T">The registered type</typeparam>
/// <typeparam name="TBase">The type that T is derived from, can be the base class or base interface.</typeparam>
/// <param name="services">The services.</param>
/// <returns>the IServiceCollection used to register the interface with.</returns>
public static IServiceCollection ReUseSingleton<T, TBase>(this IServiceCollection services)
where T : TBase
where TBase : class
{
services.AddSingleton<TBase>(a => a.GetRequiredService<T>());
return services;
}
/// <summary>
/// Adds a transient service of the type specified in TBase with a factory based on the registered type T that has been specified in implementation factory to the specified <see cref="Microsoft.Extensions.DependencyInjection.IServiceCollection"/>.
/// </summary>
/// <typeparam name="T">The registered type</typeparam>
/// <typeparam name="TBase">The type that T is derived from, can be the base class or base interface.</typeparam>
/// <typeparam name="TS">The IServiceCollection instance to extend.</typeparam>
/// <param name="services">The services.</param>
/// <returns>the IServiceCollection used to register the interface with.</returns>
public static IServiceCollection ReUseTransient<T, TBase>(this IServiceCollection services)
where T : TBase
where TBase : class
{
services.AddTransient<TBase>(a => a.GetRequiredService<T>());
return services;
}
/// <summary>
/// Adds a scoped service of the type specified in TBase with a factory based on the registered type T that has been specified in implementation factory to the specified <see cref="Microsoft.Extensions.DependencyInjection.IServiceCollection"/>.
/// </summary>
/// <typeparam name="T">The registered type</typeparam>
/// <typeparam name="TBase">The type that T is derived from, can be the base class or base interface.</typeparam>
/// <typeparam name="TS">The IServiceCollection instance to extend.</typeparam>
/// <param name="services">The services.</param>
/// <returns>the IServiceCollection used to register the interface with.</returns>
public static IServiceCollection ReUseScoped<T, TBase>(this IServiceCollection services)
where T : TBase
where TBase : class
{
services.AddScoped<TBase>(a => a.GetRequiredService<T>());
return services;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.