我可以通过构造函数的参数,以统一的决心()方法? [英] Can I pass constructor parameters to Unity's Resolve() method?

查看:148
本文介绍了我可以通过构造函数的参数,以统一的决心()方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用微软的统一依赖注入和我想做的事情是这样的:

I am using Microsoft's Unity for dependency injection and I want to do something like this:

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 不与统一登记的(我不希望3个实例 IDataContext

RepositoryA and RepositoryB both have a constructor that takes an IDataContext parameter, and I want Unity to initialize the repository with the context that I pass it. Also note that IDataContext is not registered with Unity (I don't want 3 instances of IDataContext).

推荐答案

从今天起,他们已经加入此功能:

As of today they have added this functionality:

这是在最新的跌幅在这里:

It’s in the latest drop here:

<一个href="http://unity.$c$cplex.com/SourceControl/changeset/view/33899">http://unity.$c$cplex.com/SourceControl/changeset/view/33899

浅谈在这里:

<一个href="http://unity.$c$cplex.com/Thread/View.aspx?ThreadId=66434">http://unity.$c$cplex.com/Thread/View.aspx?ThreadId=66434

例如:

container.Resolve<IFoo>(new ParameterOverrides<Foo> { { "name", "bar" }, { "address", 42 } });"

这篇关于我可以通过构造函数的参数,以统一的决心()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆