Should Global Toolkit use Unity?

Coordinator
Feb 5, 2009 at 6:10 PM
As much as I want to keep the toolkit free of 3rd party dependancies, I don't want to write my own container just for the GT project.  Specifically, I want a service locator that can resolve a CurrencyInfo provider and Currency conversion data provider.   I'm thinking about using unity, and am looking for any objections to doing so before I integrate it into the codebase. 
Coordinator
Feb 6, 2009 at 2:30 AM
Edited Feb 6, 2009 at 2:33 AM
I tried to implement a solution that would address both concerns.  Global Toolkit will have a default implementation of ServiceLocator which will resolve some of the default providers.  You will be able to replace GlobalToolkit's service locator with your own service locator implementation if you want.  If I wanted to use the UnityContainer in my application, I could do this:

IUnityContainer container = new UnityContainer();
//register the default currency provider as a singleton.
container.RegisterType<ICurrencyProvider, DefaultCurrencyProvider>(new ContainerControlledLifetimeManager());
GlobalToolkitContext.ServiceLocator = new UnityServiceLocator(container);

The UnityServiceLocator would be a really simple shim which looks like this:

    class UnityServiceLocator : IServiceLocator
    {
        private IUnityContainer container;

        public UnityServiceLocator(IUnityContainer container)
        {
            this.container = container;
        }

        #region IServiceLocator Members

        public T Resolve<T>()
        {
            return container.Resolve<T>();
        }

        #endregion
    }

This solution allows you to use Unity or Spring or anything else without putting a dependency in Global Toolkit on those frameworks.  The ServiceLocator can be set either programatically (like above) or in the configuration file.

<globalToolkit serviceLocator="<service locator type here>">
....
</globalToolkit>