Created
June 19, 2014 13:17
-
-
Save dereklawless/d5d8158442d70eb6c952 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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