The*_*yer 2 dependency-injection .net-core
Unity、Autofac 以及可能还有相当多的其他依赖注入包都支持“键控依赖注入容器”,允许注册接口的多个实现并通过键(无论是字符串、整数、枚举还是其他任何形式)唯一地标识它们)。
然而,到目前为止我至少可以看到,.NET Core 没有这样的功能,如果我要尝试实现这样的功能,我必须采取解决方法或找到一些 hacky 解决方案。我想知道,是否有特殊原因没有在 .NET Core 中引入?
统一示例:
container.RegisterType<IService, ServiceImplementation1>("1");
container.RegisterType<IService, ServiceImplementation2>("2");
Run Code Online (Sandbox Code Playgroud)
Autofac 示例:
builder.RegisterType<ServiceImplementation1>().Keyed<IService>("1");
builder.RegisterType<ServiceImplementation2>().Keyed<IService>("2");
Run Code Online (Sandbox Code Playgroud)
Microsoft 从 .net8 开始添加了键控依赖项。
https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-8#keyed-di-services
归档时间: |
|
查看次数: |
4026 次 |
最近记录: |