Skip to content

Instantly share code, notes, and snippets.

@dereklawless
Created June 19, 2014 13:17
Show Gist options
  • Save dereklawless/d5d8158442d70eb6c952 to your computer and use it in GitHub Desktop.
Save dereklawless/d5d8158442d70eb6c952 to your computer and use it in GitHub Desktop.
using Microsoft.Practices.ServiceLocation;
using StructureMap;
namespace App.Configuration
{
/// <summary>
/// An Inversion of Control container and service locator, for StructureMap.
/// </summary>
public class StructureMapServiceLocator : ServiceLocatorImplBase
{
#region Properties
/// <summary>
/// Gets or sets the IoC container.
/// </summary>
/// <value>The IoC container.</value>
protected IContainer Container { get; private set; }
#endregion
#region ctor
/// <summary>
/// Initializes a new instance of the <see cref="StructureMapServiceLocator"/> class.
/// </summary>
/// <param name="container">The IoC container.</param>
public StructureMapServiceLocator(IContainer container)
{
Container = container;
}
#endregion
#region ServiceLocatorImplBase Methods
protected override IEnumerable<object> DoGetAllInstances(Type serviceType)
{
return Container.GetAllInstances(serviceType).Cast<object>();
}
protected override object DoGetInstance(Type serviceType, string key)
{
return (!string.IsNullOrEmpty(key))
? Container.GetInstance(serviceType, key)
: Container.GetInstance(serviceType);
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment