I have an application which uses Unity 2.o. The UnityWarapper class has the following two two methods.
public static IUnityContainer GetContainer(string containerName) // Create the unity container for the given container name.
public static T CreateInstance<T>(string mapName, IUnityContainer container, InterceptorType interceptorType) //reate instance of requested type and load it into the unity container.
The CreateInstance method is used to create an instance of the requested type and get the policy injection settings from the config file.
After the container is created we need to add the following extensions as per the documentation.
Would you be able to let me know the most suitable method to insert these extensions? i.e., Does it needs to be included when creating the container or the type?
Further I would like to know what is exact purpose of adding an extension to the container by specifying "AddNewExtension<Interception>()" to the container.
Adding an extension affects the container as a whole. As such, it should be done when creating a container instance, not when resolving a type. And generally they only need be done once per container. In your case, it should probably be in the "GetContainer"
method. Or more specifically, when it's actually creating your singleton instance of IUnityContainer (you are holding on to the instance rather than recreating one every time GetContainer is called, right?)
As for what extensions do, well, they extend the container. They can add registrations, they can change the way objects are resolved, or they can add entire new capabilities. It depends on the extension. For those two specific ones, the EnterpriseLibraryCoreExtension
is actually reading the Enterprise Library configuration and adding all the type registrations needed for Entlib objects to the container. It also adds a little extra behavior, adding locking on resolve calls so that the container can safely be reconfigured
in a multithreaded environment.
The Interception extension, on the other hand, adds a major new capability - to automatically generate interceptors and put in automatic pre-and post- processing for objects resolved through the container. If you're not familiar with what the Interception
extension does, I suggest you start with
the documentation and
hands-on-labs. It's a little complex, but very powerful.
I hope this clarifies the intent.
Also, just as a note - EnterpriseLibraryCoreExtension automatically adds the Interception extension, so you don't need both of them.