我正在嘗試遷移使用 Unity IoC 容器來使用 DryIoC 容器的 Prism WPF 應用程式。
在一個模塊中,我注入了 Unity 容器來注冊一個指定建構式的類。
unityContainer.RegisterType<IService, Service>(
typeof(Service).FullName,
new ContainerControlledLifetimeManager(),
Invoke.Constructor(
Resolve.Parameter<IDependency1>(),
Resolve.Parameter<IDependency2>()...
));
我想遷移這種型別的暫存器以使用IContainerRegistry方法進行注冊。我已經看到該IContainerRegistry介面提供了使用工廠方法注冊的方法,我可以使用它來指定建構式,但是沒有將工廠方法作為引數并命名為 register 的方法。
有人有同樣的問題嗎?也許擴展 IContainerRegistry 實作?
uj5u.com熱心網友回復:
您可以跳過IContainerRegistry并呼叫GetContainer擴展方法并使用實際的容器。
containerRegistry.GetContainer().RegisterDelegate<IDependency1, IDependency2, IService>( (a, b) => new Service(a,b), serviceKey: typeof(Service).FullName, reuse: Reuse.Singleton );
IContainerRegistry只是容器周圍的一個薄包裝器,沒有任何自己定義的行為。對于除了最瑣碎的注冊之外的任何事情,無論如何你都必須去容器。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/466557.html
