UnityContainer。如何使用参数注册类构造函数 [英] UnityContainer. how to register a class constructor with parameters

查看:164
本文介绍了UnityContainer。如何使用参数注册类构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Microsoft Unity Container的新手,所以我的问题可能微不足道。
我只能使用一个无参数的构造函数来注册和解析类的实例。现在,我想学习如何使用简单的构造函数注册和解析类。这是我的课程。

Im new with Microsoft Unity Container, so my question may be trivial. I got to register and resolve instances for classes with only one parameterless constructor. Now I wish to learn how to register and resolve a class with a simple constructor. Here is my class.

public class MyClass
{
    private string a1;

    public string A1
        {
        get { return a1; }
        set { a1 = value; }
        }
    private int a2;

    public int A2
        {
        get { return a2; }
        set { a2 = value; }
        }
    public MyClass()
    { this.a1 = "aaaaa";
    this.a2 = 1;
    }

    public MyClass(string a1,int a2)
        {
        this.a1 = a1;
        this.a2 = a2;
        }
    }

我必须注册并解析MyClass()构造函数;然后,我尝试注册第二个

I got to register and resolve the MyClass() constructor; then, I tried to register the second

        //App.xaml.cs
        container = new UnityContainer();
        container.RegisterType<MainView>();
        ............
        container.RegisterType<string>("A1");
        container.RegisterType<int>("A2");
        container.RegisterType<MyClass>("MyClassConstructor",new InjectionConstructor(new ResolvedParameter<string>("A1"),new ResolvedParameter<int>("A2")));

        //NavigationService.cs, create an instance of MyClass("mystring",100) and bind it to a window
        public Window ShowMainView()
        {
        var window = Container.Resolve<MainView>();

        MyClass myClass = Container.Resolve<MyClass>("MyClassConstructor", new ParameterOverrides { { "A1", "mystring" }, { "A2", 100 } });
        window.DataContext = myClass ;
        window.Show();
        return window;
        }

当容器尝试创建MyClass实例时,出现此错误:

when the container tries to create the MyClass instance, I got this error:

解决依赖关系失败,请键入= MySolution.MyProject.ViewModels.MyClass,名称= MyClassConstructor。
异常发生在:解决时。

Resolution of the dependency failed, type = "MySolution.MyProject.ViewModels.MyClass", name = "MyClassConstructor". Exception occurred while: while resolving.

在发生异常时,容器为:

At the time of the exception, the container was:

解析MySolution.MyProject.ViewModels.MyClass,MyClassConstructor
解析构造函数MySolution.MyProject.ViewModels.MyClass(System.String a1,System.Int32 a2)的参数 a1
解析System.String,A1

Resolving MySolution.MyProject.ViewModels.MyClass,MyClassConstructor Resolving parameter "a1" of constructor MySolution.MyProject.ViewModels.MyClass(System.String a1, System.Int32 a2) Resolving System.String,A1

请帮我了解它的工作原理。谢谢

please, help me to understand how it works. thanks

推荐答案

在注册方面,您做得太多了。

但是,更重要的是,您需要指定构造函数参数名称(小写),而不是属性名称(大写)。

You're doing too much on the registration side.
More importantly though, you need to specify the constructor parameter names (which are in lower case), not the property names (which are in upper case).

这里是这样的:

    [TestMethod]
    public void Should_Be_Able_To_Pass_Parameters_To_Ctor()
    {
        var container = new UnityContainer();

        container.RegisterType<MyClass>();

        var myClass = container.Resolve<MyClass>( 
            new ParameterOverrides { { "a1", "mystring" }, { "a2", 100 } });

        Assert.IsNotNull(myClass);
        Assert.AreEqual("mystring", myClass.A1);
        Assert.AreEqual(100, myClass.A2);
    }

这篇关于UnityContainer。如何使用参数注册类构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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