小编Imm*_*ity的帖子

如何在棱镜 IContainerRegistry 中为 2 个接口注册相同的实例(= 单例)

我有一个Prism应用程序,可以Unity进行依赖注入。

我有一个类具有以下继承:

ISomeService --> ISomeMoreSpecializedService --> SomeMoreSpecializedService
Run Code Online (Sandbox Code Playgroud)
  • ISomeService用于一些非特定于应用程序的模块。
  • ISomeMoreSpecializedService正在扩展之前的接口,添加一些特定于应用程序的概念
  • SomeMoreSpecializedService 是应该在特定软件中使用的唯一实现
  • (还有ISomeService的抽象实现)

我的专业服务如下:

public class SomeMoreSpecializedService : SomeService, ISomeMoreSpecializedService
{
    public SomeMoreSpecializedService(ISomeDependency dependency){
    }
}
Run Code Online (Sandbox Code Playgroud)

我在棱镜模块中:

public class MyModule : IModule
{
    public void RegisterTypes(IContainerRegistry containerRegistry)
    {
        containerRegistry.RegisterSingleton<ISomeService, SomeMoreSpecializedService>();
        containerRegistry.RegisterSingleton<ISomeMoreSpecializedService, SomeMoreSpecializedService>();
    }

    public void OnInitialized(IContainerProvider containerProvider)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

但我的问题是我收到了两个不同的单例。我该怎么做?

之前我正在构建一个new SomeMoreSpecializedService(),然后在做RegisterInstance。这到目前为止一直有效,但现在我需要注入一些依赖项。

c# prism unity-container

3
推荐指数
1
解决办法
1374
查看次数

标签 统计

c# ×1

prism ×1

unity-container ×1