CommonServiceLocator should expose underlying Container

Jul 2, 2011 at 8:57 AM
Please alter the CommonServiceLocator implementation to expose the underlying container:



// --------------------------------------------------------------------------------------------------
// © Copyright 2011 by Matthew Dennis. // Released under the Microsoft Public License (Ms-PL) http://www.opensource.org/licenses/ms-pl.html // -------------------------------------------------------------------------------------------------- using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Practices.ServiceLocation; namespace Munq.CommonServiceLocator { /// <summary> /// An implementation of the Common Service Locator interface using the Munq IOC Container. /// Requires a reference Munq.CommonServiceLocator.dll. /// </summary> public class MunqCommonServiceLocator : IServiceLocator {         private readonly static Munq.IocContainer _container = new IocContainer();         /// <summary>         /// Gets the Munq IocContainer used by the MVC3 IDependencyResolver.         /// </summary>         public static IocContainer Container { get { return _container; } } /// <summary> /// Gets a list of a instances that resolve the specified type. /// </summary> /// <typeparam name="TService">The type of the service to resolve instances of.</typeparam> /// <returns>A list of instances.</returns> public IEnumerable<TService> GetAllInstances<TService>() {             return this.GetAllInstances(typeof(TService)).Cast<TService>(); } /// <summary> /// Gets a list of a instances that resolve the specified type. /// </summary> /// <param name="serviceType">The type of the service to resolve instances of.</param> /// <returns>A list of instances.</returns> public IEnumerable<object> GetAllInstances(Type serviceType) {             return _container.ResolveAll(serviceType); } /// <summary> /// Resolve the specified type using a named instance. /// </summary> /// <typeparam name="TService">The type of the service to resolve instances of.</typeparam> /// <param name="key">The name to qualify the resolution.</param> /// <returns>The resolved instance, or throws and ActivationException.</returns> public TService GetInstance<TService>(string key) {             return (TService)this.GetInstance(typeof(TService), key); } /// <summary> /// Resolve the specified type using a unnamed instance. /// </summary> /// <typeparam name="TService">The type of the service to resolve instances of.</typeparam> /// <returns>The resolved instance, or throws and ActivationException.</returns> public TService GetInstance<TService>() {             return (TService)this.GetInstance(typeof(TService), null); } /// <summary> /// Resolve the specified type using a named instance. /// </summary> /// <param name="serviceType">The type of the service to resolve instances of.</param> /// <param name="key">The name to qualify the resolution.</param> /// <returns>The resolved instance, or throws and ActivationException.</returns> public object GetInstance(Type serviceType, string key) { try {                 return _container.Resolve(key, serviceType); } catch { throw new ActivationException(); } } /// <summary> /// Resolve the specified type using a unnamed instance. /// </summary> /// <param name="serviceType">The type of the service to resolve instances of.</param> /// <returns>The resolved instance, or throws and ActivationException.</returns> public object GetInstance(Type serviceType) {             return this.GetInstance(serviceType, null); } /// <summary> /// Resolve the specified type using a unnamed instance. /// </summary> /// <param name="serviceType">The type of the service to resolve instances of.</param> /// <returns>The resolved instance, or null.</returns> public object GetService(Type serviceType) {     try     { return this.GetInstance(serviceType); } catch (ActivationException) { return null; } } } }
Coordinator
Jul 2, 2011 at 3:29 PM
Doing so means that it is no longer a replaceable implementation with another CSL. Just keep your own reference.

From: sharpelabs
Sent: Saturday, July 02, 2011 3:57 AM
To: matthew.dennis@sympatico.ca
Subject: CommonServiceLocator should expose underlying Container [Munq:263650]

From: sharpelabs

Please alter the CommonServiceLocator implementation to expose the underlying container:



// --------------------------------------------------------------------------------------------------
// © Copyright 2011 by Matthew Dennis. // Released under the Microsoft Public License (Ms-PL) http://www.opensource.org/licenses/ms-pl.html // -------------------------------------------------------------------------------------------------- using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Practices.ServiceLocation; namespace Munq.CommonServiceLocator { /// <summary> /// An implementation of the Common Service Locator interface using the Munq IOC Container. /// Requires a reference Munq.CommonServiceLocator.dll. /// </summary> public class MunqCommonServiceLocator : IServiceLocator { private readonly static Munq.IocContainer _container = new IocContainer(); /// <summary> /// Gets the Munq IocContainer used by the MVC3 IDependencyResolver. /// </summary> public static IocContainer Container { get { return _container; } } /// <summary> /// Gets a list of a instances that resolve the specified type. /// </summary> /// <typeparam name="TService">The type of the service to resolve instances of.</typeparam> /// <returns>A list of instances.</returns> public IEnumerable<TService> GetAllInstances<TService>() { return this.GetAllInstances(typeof(TService)).Cast<TService>(); } /// <summary> /// Gets a list of a instances that resolve the specified type. /// </summary> /// <param name="serviceType">The type of the service to resolve instances of.</param> /// <returns>A list of instances.</returns> public IEnumerable<object> GetAllInstances(Type serviceType) { return _container.ResolveAll(serviceType); } /// <summary> /// Resolve the specified type using a named instance. /// </summary> /// <typeparam name="TService">The type of the service to resolve instances of.</typeparam> /// <param name="key">The name to qualify the resolution.</param> /// <returns>The resolved instance, or throws and ActivationException.</returns> public TService GetInstance<TService>(string key) { return (TService)this.GetInstance(typeof(TService), key); } /// <summary> /// Resolve the specified type using a unnamed instance. /// </summary> /// <typeparam name="TService">The type of the service to resolve instances of.</typeparam> ///
[The entire original message is not included.]
Jul 3, 2011 at 10:00 AM

How? IoCContainer doesn't expose a "current" property, and your implementation of CommonServiceLocator creates the IoCContainer privately in the constructor. Unless I'm missing something there is no way to reference the underlying container?

 

Either add a additional static property to expose the container or allow the container to be passed into the constructor.

Coordinator
Jul 3, 2011 at 1:43 PM

You are correct.  I'll add both the constructor and the property.