/src/Engine/MvcTurbine.Unity/UnityServiceLocator.cs
https://github.com/tyronegroves/mvcturbine · C# · 292 lines · 141 code · 40 blank · 111 comment · 10 complexity · 93a2aa010856403a1224f9874551d18b MD5 · raw file
- namespace MvcTurbine.Unity {
- using System;
- using System.Collections.Generic;
- using ComponentModel;
- using Microsoft.Practices.Unity;
-
- /// <summary>
- /// Default implementation of the <seealso cref="IServiceLocator"/> contract with Unity IoC.
- /// </summary>
- /// <remarks>
- /// To learn more about Unity, please visit its website: http://www.codeplex.com/unity
- /// </remarks>
- [Serializable]
- public class UnityServiceLocator : IServiceLocator, IServiceReleaser, IServiceInjector {
- private static bool isDisposing;
-
- /// <summary>
- /// Creates an instance of the locator with an empty <seealso cref="IUnityContainer"/> instance.
- /// </summary>
- public UnityServiceLocator()
- : this(new UnityContainer()) {
- }
-
- /// <summary>
- /// Creates an instance of the locator with the specified <seealso cref="IUnityContainer"/> instance.
- /// </summary>
- /// <param name="container">Instance of <seealso cref="IUnityContainer"/> to use.</param>
- public UnityServiceLocator(IUnityContainer container) {
- if (container == null) {
- throw new ArgumentNullException("container", "The specified Unity container cannot be null.");
- }
-
- Container = container;
- }
-
- /// <summary>
- /// Gets the current <seealso cref="IUnityContainer"/> associated with this instance.
- /// </summary>
- public IUnityContainer Container { private set; get; }
-
- public IList<object> ResolveServices(Type type) {
- return new List<object>(Container.ResolveAll(type));
- }
-
- /// <summary>
- /// Gets the associated <see cref="IServiceRegistrar"/> to process.
- /// </summary>
- /// <returns></returns>
- public IServiceRegistrar Batch() {
- return new RegistrationStub();
- }
-
- /// <summary>
- /// Resolves the service of the specified type.
- /// </summary>
- /// <typeparam name="T">Type of service to resolve.</typeparam>
- /// <returns>An instance of the type, null otherwise.</returns>
- public T Resolve<T>() where T : class {
- try {
- return Container.Resolve<T>();
- }
- catch (Exception ex) {
- throw new ServiceResolutionException(typeof(T), ex);
- }
- }
-
- /// <summary>
- /// Resolves the service of the specified type by the given string key.
- /// </summary>
- /// <typeparam name="T">Type of service to resolve.</typeparam>
- /// <param name="key">Unique key to distinguish the service.</param>
- /// <returns>An instance of the type, null otherwise.</returns>
- public T Resolve<T>(string key) where T : class {
- try {
- return Container.Resolve<T>(key);
- }
- catch (Exception ex) {
- throw new ServiceResolutionException(typeof(T), ex);
- }
- }
-
- /// <summary>
- /// Resolves the service of the specified type by the given type key.
- /// </summary>
- /// <typeparam name="T">Type of service to resolve.</typeparam>
- /// <param name="type">Key type of the service.</param>
- /// <returns>An instance of the type, null otherwise.</returns>
- public T Resolve<T>(Type type) where T : class {
- try {
- return Container.Resolve(type) as T;
- }
- catch (Exception ex) {
- throw new ServiceResolutionException(type, ex);
- }
- }
-
- ///<summary>
- /// Resolves the service of the specified type by the given type key.
- ///</summary>
- ///<param name="type">Type of service to resolve.</param>
- ///<returns>An instance of the type, null otherwise</returns>
- public object Resolve(Type type) {
- try {
- return Container.Resolve(type);
- }
- catch (Exception ex) {
- throw new ServiceResolutionException(type, ex);
- }
- }
-
- /// <summary>
- /// Resolves the list of services of type <see cref="T"/> that are registered
- /// within the locator.
- /// </summary>
- /// <typeparam name="T">Type of the service to resolve.</typeparam>
- /// <returns>A list of service of type <see cref="T"/>, null otherwise.</returns>
- public IList<T> ResolveServices<T>() where T : class {
- IEnumerable<T> services = Container.ResolveAll<T>();
- return new List<T>(services);
- }
-
- /// <summary>
- /// Registers the implemation type, <paramref name="implType"/>, with the locator under
- /// the <see cref="Interface"/> service type.
- /// </summary>
- /// <typeparam name="Interface">Type of the service to register.</typeparam>
- /// <param name="implType">Implementation type to use for registration.</param>
- public void Register<Interface>(Type implType) where Interface : class {
- var key = string.Format("{0}-{1}", typeof(Interface).Name, implType.FullName);
- Container.RegisterType(typeof(Interface), implType, key);
-
- // Work-around, also register this implementation to service mapping
- // without the generated key above.
- Container.RegisterType(typeof(Interface), implType);
- }
-
- /// <summary>
- /// Registers the implemation type, <see cref="Implementation"/>, with the locator under
- /// the <see cref="Interface"/> service type.
- /// </summary>
- /// <typeparam name="Interface">Type of the service to register.</typeparam>
- /// <typeparam name="Implementation">Implementation type to use for registration.
- /// </typeparam>
- public void Register<Interface, Implementation>()
- where Implementation : class, Interface {
-
- Container.RegisterType<Interface, Implementation>();
- }
-
- /// <summary>
- /// Registers the implemation type, <see cref="Implementation"/>, with the locator under
- /// the <see cref="Interface"/> service type.
- /// </summary>
- /// <typeparam name="Interface">Type of the service to register.</typeparam>
- /// <typeparam name="Implementation">Implementation type to use for registration.
- /// </typeparam>
- /// <param name="key">Unique key to distinguish the service.</param>
- public void Register<Interface, Implementation>(string key)
- where Implementation : class, Interface {
-
- Container.RegisterType<Interface, Implementation>(key);
- }
-
- /// <summary>
- /// Registers the implementation type, <paramref name="type"/>, with the locator
- /// by the given string key.
- /// </summary>
- /// <param name="key">Unique key to distinguish the service.</param>
- /// <param name="type">Implementation type to use.</param>
- public void Register(string key, Type type) {
- Container.RegisterType(type, key);
- }
-
- /// <summary>
- /// Registers the implementation type, <paramref name="implType"/>, with the locator
- /// by the given service type, <paramref name="serviceType"/>
- /// </summary>
- /// <param name="serviceType">Type of the service to register.</param>
- /// <param name="implType">Implementation to associate with the service.</param>
- public void Register(Type serviceType, Type implType) {
- Container.RegisterType(serviceType, implType);
- }
-
- /// <summary>
- /// Registers the implementation type, <paramref name="implType"/>, with the locator
- /// by the given service type, <paramref name="serviceType"/>
- /// </summary>
- /// <param name="serviceType">Type of the service to register.</param>
- /// <param name="implType">Implementation to associate with the service.</param>
- /// <param name="key"></param>
- public void Register(Type serviceType, Type implType, string key) {
- Container.RegisterType(serviceType, implType, key);
- }
-
- /// <summary>
- /// Registers the instance of type, <typeparamref name="Interface"/>, with the locator.
- /// </summary>
- /// <typeparam name="Interface">Type of the service to register.</typeparam>
- /// <param name="instance">Instance of the type to register.</param>
- public void Register<Interface>(Interface instance) where Interface : class {
- Container.RegisterInstance(instance);
- }
-
- /// <summary>
- /// Resolves the service of the specified interface with the provided factory method.
- /// </summary>
- /// <param name="factoryMethod">The factory method which will be used to resolve this interface.</param>
- /// <returns>An instance of the type, null otherwise</returns>
- public void Register<Interface>(Func<Interface> factoryMethod) where Interface : class {
- var container = this.Container;
- Func<IUnityContainer, object> factoryFunc = c => factoryMethod.Invoke();
- container.RegisterType<Interface>(new InjectionFactory(factoryFunc));
- }
-
- /// <summary>
- /// Releases (disposes) the service instance from within the locator.
- /// </summary>
- /// <param name="instance">Instance of a service to dipose from the locator.</param>
- public void Release(object instance) {
- if (instance == null) return;
-
- Container.Teardown(instance);
- }
-
- public TService Inject<TService>(TService instance) where TService : class {
- return instance == null ? instance : (TService)Container.BuildUp(instance.GetType(), instance);
- }
-
- public void TearDown<TService>(TService instance) where TService : class {
- if (instance == null) return;
- Container.Teardown(instance);
- }
-
- /// <summary>
- /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
- /// </summary>
- /// <filterpriority>2</filterpriority>
- public void Dispose() {
- if (isDisposing) return;
- if (Container == null) return;
-
- isDisposing = true;
-
- Container.Dispose();
- Container = null;
- }
- }
-
- /// <summary>
- /// This class is for stubbing purposes only.
- /// </summary>
- internal sealed class RegistrationStub : IServiceRegistrar {
- public void Dispose() {
- }
-
- public void RegisterAll<Interface>() {
- throw new NotImplementedException();
- }
-
- public void Register<Interface>(Type implType) where Interface : class {
- throw new NotImplementedException();
- }
-
- public void Register<Interface, Implementation>() where Implementation : class, Interface {
- throw new NotImplementedException();
- }
-
- public void Register<Interface, Implementation>(string key) where Implementation : class, Interface {
- throw new NotImplementedException();
- }
-
- public void Register(string key, Type type) {
- throw new NotImplementedException();
- }
-
- public void Register(Type serviceType, Type implType) {
- throw new NotImplementedException();
- }
-
- public void Register(Type serviceType, Type implType, string key) {
- throw new NotImplementedException();
- }
-
- public void Register<Interface>(Interface instance) where Interface : class {
- throw new NotImplementedException();
- }
-
- public void Register<Interface>(Func<Interface> factoryMethod) where Interface : class {
- throw new NotImplementedException();
- }
- }
- }