May 13, 2009 at 7:50 AM
Edited May 13, 2009 at 7:51 AM
We use a static class ( in a suitably low level common assembly ) that contains 2 internal Dictionary<string, IUnityContainer>. We initialise from a .config file which populates the default dictionary, then provide a series of methods to
override the containers ( mostly for unit testing ). These get stored in the second dictionary.
Second, there is a Property to retrieve the "default" container ( container with no name ) and another method to retrieve a named container. Both of these check the override dictionary first.
Third, there is a method to Reset the internal state by clearing the second dictionaries, and optionally rebuilting the default containers from config
Lastly, we have implemented a custom unity extension to track registrations and instances to allow a CanResolve<T> extension method on IUnityContainer. This allowed us to query for whether an interface can resolve without ObjectBuilder throwing
Using this static class ensures most ( if not all ) plumbing code is removed from the developer's view and the same containers are used across our entire system making unit testing simple.
Hope this helps.