简体   繁体   English

Unity自相关接口注册

[英]Unity Self Dependent Interface Registering

An interface depends of same type of interface to complete some certain actions. 接口依赖于相同类型的接口来完成某些特定操作。 But when I try to register it with unity I am getting 但是当我尝试统一注册时,

An unhandled exception of type 'System.StackOverflowException' occurred in Microsoft.Practices.Unity.DLL Microsoft.Practices.Unity.DLL中发生了类型为'System.StackOverflowException'的未处理异常

I think it falls into a kind of self referencing loop and fills the memory. 我认为它属于一种自引用循环,并充满了内存。

Is there something wrong to my approach. 我的方法有什么问题吗? How can I resolve it? 我该如何解决?

I have interface like this; 我有这样的界面;

  public interface IEnvironment
{
    string RootUrl { get; set; }
    string ImagesPath { get; set; }
    IEnvironment DependentEnvironment { get; set; }
}

This is representation of my running code environment such as Localhost, Production, Windows Phone Simulator etc.. 这是我正在运行的代码环境(例如Localhost,Production,Windows Phone Simulator等)的表示。

I have two classes implementing this right now; 我现在有两个类实现此目的;

class Localhost : IEnvironment
{
    public string RootUrl { get; set; }
    public string ImagesPath { get; set; }
    public IEnvironment DependentEnvironment { get; set; }

    public Localhost(IEnvironment dependentEnvironment)
    {
        ImagesPath = "images";
        DependentEnvironment = dependentEnvironment;
    }
}

and

public class WindowsPhoneSimulator : IEnvironment
    {
        public string RootUrl { get; set; }
        public string ImagesPath { get; set; }
        public IEnvironment DependentEnvironment { get; set; }
        public WindowsPhoneSimulator(IEnvironment dependentEnvironment)
        {
            ImagesPath = "/Assets/images";
            DependentEnvironment = dependentEnvironment;
        }
    }

so one environment can depend another one? 所以一个环境可以依赖另一个环境? Why? 为什么? Because for example WindowsPhoneSimulator can make api calls to localhost and when I deploy application I will change injection as ProductionEnvironment. 因为例如WindowsPhoneSimulator可以对本地主机进行api调用,所以当我部署应用程序时,我会将注入更改为ProductionEnvironment。 So it should know which environment to call. 因此,它应该知道要调用哪个环境。

The problem begins when I start to resolve my objects like; 当我开始解决我的对象时,问题就开始了。

    IocContainer.RegisterType<IEnvironment, WindowsPhoneSimulator>();

any suggestions? 有什么建议么?

You haven't shown how you are registering your types, but presumably you are just registering them with a standard RegisterType method. 您尚未显示如何注册类型,但是大概只是在使用标准的RegisterType方法注册它们。 When you register the second type, it will overwrite the first, so Unity only knows about one of your classes (the one that is registered second). 当您注册第二个类型时,它将覆盖第一个类型,因此Unity仅知道您的一个类(第二个注册的类)。 Then it makes sense as to why you get a stack overflow exception, because it is trying to create an instance of say, the WindowsPhoneSimulator class to pass into the WindowsPhoneSimulator class...which obviously it can't do. 然后就知道为什么会出现堆栈溢出异常是有道理的,因为它试图创建一个实例,例如将WindowsPhoneSimulator类传递给WindowsPhoneSimulator类……这显然是不行的。

Instead you will need to register the types in unity as "named types", then when you want to use one of them, you create a dependency override when resolving the class, to tell Unity which one you want to use. 取而代之的是,您需要将这些类型统一注册为“命名类型”,然后当您要使用其中一种类型时,在解析该类时会创建一个依赖项覆盖,以告诉Unity您要使用哪种类型。

So, register the types as named types: 因此,将类型注册为命名类型:

container.RegisterType<IEnvironment, Localhost>("Localhost")
container.RegisterType<IEnvironment, WindowsPhoneSimulator>("WindowsPhoneSimulator")

Then when you want to create the type, something like this: 然后,当您要创建类型时,如下所示:

DependencyOverride dep = new DependencyOverride(typeof(IEnvironment)
                        , container.Resolve<IEnvironment>("Localhost"));

IEnvironment obj2 = container.Resolve<IEnvironment>("WindowsPhoneSimulator", dep);

In the above example, you are first resolving the instance of the Localhost class, and then creating the dependencyOverride with that instance, and passing it into the constructor for the WindowsPhoneSimulator class. 在上面的示例中,您首先要解析Localhost类的实例,然后使用该实例创建dependencyOverride,并将其传递给WindowsPhoneSimulator类的构造函数。 So, WindowsPhoneSimulator gets passed the instance of Localhost when it is constructed. 因此,在构造WindowsPhoneSimulator时,它会传递Localhost的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM