Unconstrained ParameterOverride don't seem to be behaving as described in documentation

Feb 10, 2014 at 4:44 PM
Edited Feb 11, 2014 at 10:33 AM

From :
Since the purpose of overrides is to affect the resolution of dependencies for all relevant created objects, not just the object requested in the call to Resolve, unconstrained overrides can produce errors if there are unconstrained ParameterOverride parameters that match parameters with the same name but different types on the selected constructors for objects created in a given resolve operation.
I interpreted the above quote as saying that if unconstrained ParameterOverride is configured to set constructor parameter i to 2, then any objects ( which have a constructor parameter named i ) created in a given resolve operation will have their constructor parameter i set to 2.

But as seen from the following example, my interpretation seems to be wrong:
            container.RegisterType<IB, B>(new InjectionConstructor(1));
            container.RegisterType<A>(new InjectionConstructor(
                    new B(1), 1));

            var intParameterOverride = new ParameterOverride("i", 2);
            var bParameterOverride = new ParameterOverride(
                    "b", new ResolvedParameter<IB>());
            var a = container.Resolve<A>(intParameterOverride);

            Console.WriteLine(a.i);   // 2
            Console.WriteLine(a.b.i); // I thought a.b.i would be set to 2, 
                                                // but instead is set to 1

     public class A
        public int i;
        public B b;

        public A(B b, int i)
            this.b = b;
            this.i = i;

    public class B 
        public int i;

        public B(int i)
            this.i = i;
So what then is the quote trying to convey?

Feb 12, 2014 at 6:09 AM
The issue is that in the registration of A the value of B passed in the constructor is set to new B(1). Unity stores this parameter value along with the constructor selection policy. When resolved the value B (with i = 1) is injected into A. Parameter overrides are only matched by name which can cause errors if unconstrained (as the quote mentions).

If the example is changed slightly from:
container.RegisterType<A>(new InjectionConstructor(new B(1), 1));

container.RegisterType<A>(new InjectionConstructor(new ResolvedParameter<B>(), 1));

Then the new value of 2 is injected into both values as would be expected.

So what is going on? The difference between the 2 scenarios is that in the second scenario a BuildUp is being performed on B at which point the ParameterOverride is applied. In the first scenario an override is attempted to be performed on the parameter B ("b" by name) but the name does not match the override name ("i") and because the instance is already created (by the InjectionConstructor with a value of 1) no further BuildUp is performed on B and the override value (of "2") is not injected into B.

Is this a bug? I'm not sure. On the one hand it seems a bit counter intuitive that the override value is not injected but on the other hand should Unity be injecting values into objects that it has not been told to BuildUp? I think the answer to that question is probably "no" so this behavior is "by design".

Randy Levy
Enterprise Library support engineer
Support How-to
Feb 13, 2014 at 2:04 PM
much thanks for clarifying it