Questions tagged «constructor-injection»

7
我可以将构造函数参数传递给Unity的Resolve()方法吗?
我正在使用Microsoft的Unity进行依赖项注入,并且我想执行以下操作: IDataContext context = _unityContainer.Resolve<IDataContext>(); var repositoryA = _unityContainer.Resolve<IRepositoryA>(context); //Same instance of context var repositoryB = _unityContainer.Resolve<IRepositoryB>(context); //Same instance of context IDataContext context2 = _unityContainer.Resolve<IDataContext>(); //New instance var repositoryA2 = _unityContainer.Resolve<IRepositoryA>(context2); RepositoryA并且RepositoryB都有一个构造函数的IDataContext参数,我想团结与我通过它的上下文初始化存储库。另请注意,该代码IDataContext未在Unity中注册(我不需要3个实例IDataContext)。
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.