Resolution with parameters and fallbacks

Apr 2, 2013 at 9:27 PM
Forgive me if this has been addressed already.

I have an interface IPricer that I want to resolve. There are 2 enum parameters that characterize implementations, Scope and Lifetime. I'd like to be able to register implementations each of which handles one combination of Scope and Lifetime. I'd like to be able to resolve with fallbacks - ie if asked to resolve for some combination of Scope and Lifetime that doesn't have an implementation registered, there's some mechanism for falling back to another registration perhaps with some sort of wildcarding.

Is there some way to accomplish this with Unity?
Apr 3, 2013 at 4:17 PM
You can create named registrations that use specific enum values (using InjectionConstructor). It sounds like you want to use the enum values as part of the key so you would probably have to come up with some sort of naming convention. However, there is no out of the box "fallback" logic.

Probably the easiest (and a clean) approach would be to create an IPricer factory that handles all the logic for determining how to set the state and/or instantiate an IPricer.

Another approach would be to create a UnityContainerExtension that performs all the logic.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Apr 3, 2013 at 6:49 PM
Thank you for responding. Can you give me an idea what additional classes I would have to create besides the Extension one?
Apr 4, 2013 at 6:57 AM
You will need to create a UnityContainerExtension and also implement a BuilderStrategy probably in the PreCreation stage. This recent question has an example that might help. It's using an IBuilderPolicy to maintain state but I don't think you would need that so that can be ignored.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to