Unity Constructor Injection

Mar 9, 2010 at 10:13 AM

I'm building a chain of responsbility pattern and one issue that I have is when I link up the chain I have to use the InjectionConstructor class but this has to take all of the parameters for the class rather than just the one I want to override.

This seems a bit fragile, as any refactoring of the constructors of the classes involved will break the configuration.

Is there a better way of configuring this that I'm missing?


Mar 11, 2010 at 6:09 AM

That's pretty much it, yes. We use the parameter list not only for the values, but also to know which constructor to call.


Mar 11, 2010 at 9:48 AM

Does this change in Unity 2.0, or is there an extension point so I can change the behaviour?

Mar 11, 2010 at 5:25 PM

No, this doesn't change in Unity 2.0. There is an extension point - you can provide an implementation of Microsoft.Practices.ObjectBuilder2.IConstructorSelectorPolicy to implement whatever algorithm you want, then build an InjectionMember derived class to provide that policy when configured. The extension points aren't currently documented but they are supported and will be around for the foreseeable future.