How to resolve constructor parameter?

Feb 3, 2012 at 9:08 PM
Edited Feb 3, 2012 at 9:09 PM

Im new to unity framework, and i would like to know how to Resolve the constructor paramaters which are not available at the time of registration. below is my sample code. ( the assert is failing in DoWrok method)

public interface ICar
    {
        string CarName { get; set; }
    }

    public class Car : ICar
    {
        public string CarName { get; set; }
    }

 public interface IPerson
    {
        ICar MyCar { get; set; }

        void DoWork();
    }

 public class Person : IPerson
    {
        public ICar MyCar { get; set; }
       
        public Person(Car car)
        {
            MyCar = car;
        }

        public void DoWork()
        {
            Assert.IsTrue(MyCar.CarName == "Honda");
        }
    }

[TestMethod]
        public void TestUnity()
        {
            IUnityContainer container = new UnityContainer();
            container.RegisterType<ICar, Car>();
            container.RegisterType<IPerson, Person>();

            ICar newCar = container.Resolve<ICar>();
            newCar.CarName = "Honda";
            IPerson person = container.Resolve<IPerson>(new ParameterOverride("MyCar", newCar));
            person.DoWork();
        }

Feb 6, 2012 at 12:05 AM

Change

IPerson person = container.Resolve<IPerson>(new ParameterOverride("MyCar", newCar));

to

IPerson person = container.Resolve<IPerson>(new ParameterOverride("Car", newCar));

Feb 11, 2012 at 4:02 PM

Here's a blog post which shows how use the ParameterOverride (which perfectphase already described) and which also contains a custom resolver which doesn't require passing in the parameter names.

Jun 14, 2012 at 11:17 PM

miksu, the ur for your blob post link seems to not be working