简体   繁体   English

Unity InjectionProperty 不起作用

[英]Unity InjectionProperty does not work

My configuration for a calculator dependent on its' View (GUI) is defined like so (it is then Resolved in the current context):我的计算器配置依赖于它的“视图(GUI)”定义如下(然后在当前上下文中解析):

    Dependencies.register<IView, View>(
        "standardView"
    );

    //Register: calculator release configuration
    Dependencies.register<ICalculator, Calculator>(
        "releaseCalculator",
        (new InjectionProperty(
            "IView", 
            Dependencies.resolve<IView>("standardView")
        ))
    );

    //Resolve:
    Calculator theCalculator = (Calculator)Dependencies.resolve<ICalculator>(
        "releaseCalculator"
    ); 

Where Dependencies is a wrapper for an IUnityContainer .其中 Dependencies 是IUnityContainer的包装器。

The injection-point in Calculator is: Calculator的注入点是:

private IView view;
[Dependency]
public IView IView{
    get{return view;}
    set{view = value;}
} 

If you wish to see the definition for Dependencies please note that it is absolutely equivalent to a projection of unity container except I have extended direct control on lifetime-managers.如果您希望查看Dependencies的定义,请注意它绝对等同于统一容器的投影,除非我扩展了对生命周期管理器的直接控制。

The problem is simply;问题很简单; given the above setup, the Property IView resolves to null at run-time.鉴于上述设置,Property IView在运行时解析为 null。 It is simply as though Unity just doesn't bloody work.就好像 Unity 根本行不通一样。

@Haukinger here is the wrapper: @Haukinger 这里是包装器:

using System.Collections.Generic;
using Unity;
using Unity.Lifetime;
using Unity.Registration;


namespace Calculator.Utils
{
public static class Dependencies
{
    private static IUnityContainer graph;
    private static List<KeyValuePair<string, LifetimeManager>> lifetimeManagers;


    static Dependencies(){
        //container
        graph = new UnityContainer();
        //instance managers 1 per dependency instance (implementor)
        lifetimeManagers = new List<KeyValuePair<string, LifetimeManager>>();
    }


  //STATES:
    public static void register<I, P>(string alias, params InjectionMember[] injections)
    {
        ContainerControlledTransientManager newLM = new ContainerControlledTransientManager();
        lifetimeManagers.Add(new KeyValuePair<string, LifetimeManager>(
            alias,
            newLM
        ));
        graph.RegisterType(
            typeof(I),
            typeof(P),
            alias,
            newLM,
            injections
        );
    }

    //function is identitical to Resolve
    public static T resolve<T>(string alias)
    {
        return (T)graph.Resolve(typeof(T), alias);
    }

    public static void dispose(string alias)
    {
        LifetimeManager target = (ContainerControlledTransientManager)lifetimeManagers.Find((elem)=>(elem.Key.Equals(alias))).Value;
        target.Dispose();
    }
}

} }

Named registrations are only injected when you request all registered types, ie IView[] allViews .命名注册仅在您请求所有注册类型时注入,即IView[] allViews If you request only one instance, you'll receive the default registration, in your case just null as you have no default registration.如果您只请求一个实例,您将收到默认注册,在您的情况下只是null因为您没有默认注册。

Remove the name ( standardView ) and pass null as parameter name to RegisterType and you're good to go.删除名称( standardView )并将null作为参数name传递给RegisterType ,您就可以开始了。

EDIT: this code works fine at my machine编辑:此代码在我的机器上运行良好

internal class Program
{
    static void Main( string[] args )
    {
        var container = new UnityContainer();
        container.RegisterType<IDependency, MyDependency>((string)null);
        var resolved = container.Resolve<Consumer>();
        // resolved.Dependency is an instance of MyDependency
    }
}

internal class Consumer
{
    [Dependency]
    public IDependency Dependency { get; set; }
}

public interface IDependency
{
}

internal class MyDependency : IDependency
{
}

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

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