I had a project in which we used Unity, and I watched a video about StructureMap, and I really liked the idea of registration from the very beginning.
So, I created the following interface:
/// <summary> /// An interface which must be implemented to create a configurator class for the UnityContainer. /// </summary> public interface IUnityContainerConfigurator { /// <summary> /// This method will be called to actually configure the container. /// </summary> /// <param name="destination">The container to configure.</param> void Configure(IUnityContainer destination); }
And aggregates have a default Configurator class. We also wrapped our Unity IoC with a static class so that we can call IoC.Resolve<T> , and I just added the following functions to this shell:
/// <summary> /// Configure the IoC /// </summary> public static class Configure { /// <summary> /// Configure the IoC using by calling the supplied configurator. /// </summary> /// <typeparam name="TConfigurator">The configurator to use</typeparam> public static void From<TConfigurator>() where TConfigurator : IUnityContainerConfigurator, new() { From(new TConfigurator()); } /// <summary> /// Configure the IoC using by calling the supplied configurator. /// </summary> /// <param name="configurationInterface">The configurator instance to use</param> public static void From(IUnityContainerConfigurator configurationInterface) { configurationInterface.Configure(instance); } // other configuration. }
So, in the initialization form, either the program or the website I just called:
IoC.Configure.From<BLL.DefaultMapping>();
There is a class in BLL similar to this:
public class DefaultMapping:IUnityContainerConfigurator { public void Configure(IUnityContainer destination) { destionation.RegisterType<IRepository, SQLRepository>();
The only drawback is that all of your layers are associated with the selected IoC container.
Update . After this answer, I posted an article on my blog containing Unity wrapper .
Davy landman
source share