UnityContainer. how to register a class constructo

2019-08-27 19:37发布

问题:

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;
        }
    }

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;
        }

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

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

Exception is: InvalidOperationException - The type String cannot be constructed. You must configure the container to supply this value.

At the time of the exception, the container was:

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

回答1:

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).

Here's how it should look like:

    [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);
    }


回答2:

Try this

int start_at = 1;
container.Register(
    Component.For<IMyComponent>().DependsOn(dependency: Dependency.OnValue<int>(start_at))
);