扩展 Prism IContainerRegistry 以通过指定构造函数的名称进行注册

Extending Prism IContainerRegistry to register by name specifying constructor

我正在尝试将使用 Unity IoC 容器的 Prism WPF 应用程序迁移到 DryIoC 容器。

在一个模块中,我注入了 Unity 容器以注册一个 class 指定构造函数。

unityContainer.RegisterType<IService, Service>(
    typeof(Service).FullName,
    new ContainerControlledLifetimeManager(),
    Invoke.Constructor(
        Resolve.Parameter<IDependency1>(), 
        Resolve.Parameter<IDependency2>()...
    ));

我想迁移这种类型的寄存器以使用IContainerRegistry 方法进行注册。我已经看到 IContainerRegistry 接口提供了使用工厂方法注册的方法,我可以使用它来指定构造函数,但是没有以工厂方法作为参数并命名为 register 的方法。

有人遇到同样的问题吗?也许扩展 IContainerRegistry 实现?

您可以跳过 IContainerRegistry 并调用 GetContainer 扩展方法并使用实际容器。

containerRegistry.GetContainer().RegisterDelegate<IDependency1, IDependency2, IService>( (a, b) => new Service(a,b), serviceKey: typeof(Service).FullName, reuse: Reuse.Singleton );

IContainerRegistry 只是容器周围的薄包装,没有任何自己定义的行为。除了最琐碎的注册之外,您无论如何都必须转到容器。