Unity2解决问题
伙计们。 我们正在为应用程序使用 MS Unity 2 框架。
我们有类似于下面描述的代码
public class Context:IContext
{
public IFlow Flow {get;set;}
}
public class SomeFlow:IFlow
{
public IContext Context {get;set;}
}
...
//Some code for getting IContext object
{
...
IContext context = container.Resolve<IContext>();
...
}
我们需要使用 Unity 来描述类 Context 和 SomeFlow 之间的关系。构造的问题是,当容器构造 Context 对象时,它需要创建需要 Context 对象的 SomeFlow 对象,依此类推。 在我们的例子中,SomeFlow 对象必须包含指向之前创建的 Context 对象的链接。所以接下来一定是算法:
1. Create Context object
2. Create SomeFlow object
3. Point Context.Flow to SomeFlow
4. Point SomeFlow.Context to Context
问题是我们如何统一地描述它?
guys.
We are using MS Unity 2 framework for an application.
We have code similar to described below
public class Context:IContext
{
public IFlow Flow {get;set;}
}
public class SomeFlow:IFlow
{
public IContext Context {get;set;}
}
...
//Some code for getting IContext object
{
...
IContext context = container.Resolve<IContext>();
...
}
We need to describe the relationship between the classes Context and SomeFlow using Unity. And the problem with constructing is while container is constructing Context object it needs to create SomeFlow object which requires Context object and so on.
In our case SomeFlow object must contain link to the Context object created before. So the algorithm must be next:
1. Create Context object
2. Create SomeFlow object
3. Point Context.Flow to SomeFlow
4. Point SomeFlow.Context to Context
And the question is how could we describe it with unity?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过构造函数注入来为 Flow 提供上下文,然后在构造函数中设置向后引用。这是一个简单的例子:
You can do this with constructor injection to give the Flow its context, and then in the constructor set up the backwards reference. Here's a simple example:
除了马克的回答之外,您还可以注册一个
InjectionFactory
来为您进行接线。注册以下委托可以让您不必更改应用程序设计:Alternatively to Mark's answer you can register an
InjectionFactory
that does the wiring for you. Registering the following delegate prevents you from having to change your application design: