Notification on new type mapping registration

Feb 5, 2010 at 2:29 PM


is there any way to receive a notification after IUnityContainer.RegisterType<> has created a new type mapping? The scenario is that I want to track availability of a service implementation (to be independent of the order of service registration). Is there a way to do that?



Feb 6, 2010 at 4:31 AM

You can use a container extension to do this - extensions can register for an event that gets fired every time RegisterType is called.


Feb 6, 2010 at 6:44 AM

thanks for your reply.

I wrote such an extension, but I found that I have to register it with every child container that I'm creating (and i'm using lost of child containers). This requires all places now to register this extension, which is not a very good design and may create maintainability problems in the future.

It would be nice if there was a way to register global extensions (or extensions which are automatically migrated from parent to child), or have a way to receive notifications about registration directly from the container.

I would like to emphasize again that I'm trying to solve the problem of load order. I do not want (and cannot) guarantee any module loading order (especially with an externally specified catalog), and therefore I cannot guarantee that when a service is created, the required dependencies are registered as well. Do you have other solutions for this problem?




Feb 7, 2010 at 4:01 AM

Expect a "ChildContainerCreated" event to be added to the extension context for Unity 2.0. Haven't gotten it done yet, but it's coming.

As far as load order is concerned, the easiest approach I find is not to get anything out the container until you're done loading. Solves the 80% case for me. The remaining 20%, well, of course, it depends.

(Can you tell I used to be a consultant? :-) )



Feb 7, 2010 at 12:33 PM

Alright, thanks for that, would solve our problem then (together with extension). Otherwise I'm left with terrible reordering hacks at different levels of containers - not the best thing for a purely composite app.