Notification when new IBuildPlanPolicy is set?

Oct 8, 2010 at 7:19 AM


Me again.. I'm working on a custom extension for Unity and I'm looking for a way to get some notification when an IBuildPlanPolicy is registered as a result of a call to RegisterType/RegisterInstance.

So far I found the ExtensionContext.Registering event but that does not give me the policy that will be added as a result of that call. There are no CollectionChanged notifications (or something similar) in IPolicyList either.

Any ideas anyone?



Oct 8, 2010 at 3:57 PM

There really isn't any facility along these lines.

Why do you want this, what are you actually trying to do? With more information I may be able to come up with another way to accomplish the same thing.


Oct 8, 2010 at 6:17 PM
Edited Oct 9, 2010 at 4:14 PM

I have a working prototype of a contextual binding engine like NInject's. When I register types with that engine after I registered a default binding everything works fine. I get the default mapping as a fallback when no other mapping kicks in.

But when I register a default mapping after I registered types with the contextual binding engine I loose my contextual bindings and only the default mapping is used.

I use a custom IBuildPlanPolicy to implement this behavior and believed the container acts in the same way, but I can't find the right spot.

[EDIT] Solved the problem. My mistake was to use an IBuildPlanPolicy while I should have used an IBuildKeyMappingPolicy instead. Works like a charm now :-) [/EDIT]

Oct 13, 2010 at 3:55 PM

Good, glad to heard you solved the problem. I was just going to post that I hadn't forgotten you, but I have a conference (the p&p Symposium) next week and a ton of prep still to do.


Oct 13, 2010 at 4:55 PM

I figured out where to put contextual type mappings and lifetime management. Now I need to find the right place to hook up context based sets of injection members for instance creation. If you have any hints where that might be I will be glad to hear about it.

Good luck with your conference!

Oct 13, 2010 at 6:32 PM

You want to look for the various *SelectorPolicy objects - ConstructorSelectorPolicy, PropertySelectorPolicy, and MethodSelectorPolicy. These are used to decide which members to call as part of the injection process, and also generate other objects called Resolvers which are used to provide the values that are actually injected.

Injected members are only chosen once per build key, the first time it's requested, so be aware of that.


Oct 13, 2010 at 7:17 PM

Ah, so thats what ClearExistingBuildPlan is for. If you have previously registered a mapping with the same build key you need to remove the settings made by the SelectorPolicy. That definitely helps! Thank you very much! :-)