Unity2解决问题

发布于 2024-11-11 03:51:25 字数 694 浏览 3 评论 0原文

伙计们。 我们正在为应用程序使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

潇烟暮雨 2024-11-18 03:51:25

您可以通过构造函数注入来为 Flow 提供上下文,然后在构造函数中设置向后引用。这是一个简单的例子:

public interface IContext { IFlow Flow { get; set; } }
public interface IFlow { IContext Context { get; } }

public class Context : IContext
{
    public IFlow Flow { get; set; }
}

public class SomeFlow : IFlow
{
    public SomeFlow(IContext context)
    {
        this.Context = context;
        context.Flow = this;
    }
    public IContext Context { get; set; }
}

[Test]
public void Example()
{
    IUnityContainer container = new UnityContainer();
    container.RegisterType<IContext, Context>();
    container.RegisterType<IFlow, SomeFlow>();
    var flow = container.Resolve<IFlow>();
    Assert.IsInstanceOf<Context>(flow.Context);
    Assert.IsInstanceOf<SomeFlow>(flow);
    Assert.AreSame(flow, flow.Context.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:

public interface IContext { IFlow Flow { get; set; } }
public interface IFlow { IContext Context { get; } }

public class Context : IContext
{
    public IFlow Flow { get; set; }
}

public class SomeFlow : IFlow
{
    public SomeFlow(IContext context)
    {
        this.Context = context;
        context.Flow = this;
    }
    public IContext Context { get; set; }
}

[Test]
public void Example()
{
    IUnityContainer container = new UnityContainer();
    container.RegisterType<IContext, Context>();
    container.RegisterType<IFlow, SomeFlow>();
    var flow = container.Resolve<IFlow>();
    Assert.IsInstanceOf<Context>(flow.Context);
    Assert.IsInstanceOf<SomeFlow>(flow);
    Assert.AreSame(flow, flow.Context.Flow);           
}
梦里泪两行 2024-11-18 03:51:25

除了马克的回答之外,您还可以注册一个 InjectionFactory 来为您进行接线。注册以下委托可以让您不必更改应用程序设计:

container.Register<IContext>(new InjectionFactory(c =>
{
    var context = container.Resolve<IContext>();
    var flow = container.Resolve<IFlow>();
    context.Flow = flow 
    flow.Context = context;
    return context;
}));

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:

container.Register<IContext>(new InjectionFactory(c =>
{
    var context = container.Resolve<IContext>();
    var flow = container.Resolve<IFlow>();
    context.Flow = flow 
    flow.Context = context;
    return context;
}));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文