Skip to content

Instantly share code, notes, and snippets.

@ChuckBryan
Created July 27, 2014 21:09
Show Gist options
  • Save ChuckBryan/9f3d507ed759d684c8f8 to your computer and use it in GitHub Desktop.
Save ChuckBryan/9f3d507ed759d684c8f8 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using Microsoft.Practices.ServiceLocation;
using StructureMap;
namespace StructureMap.ServiceLocatorAdapter {
public class StructureMapServiceLocator : ServiceLocatorImplBase {
private IContainer container;
public StructureMapServiceLocator(IContainer container) {
this.container = container;
}
/// <summary>
/// When implemented by inheriting classes, this method will do the actual work of resolving
/// the requested service instance.
/// </summary>
/// <param name="serviceType">Type of instance requested.</param>
/// <param name="key">Name of registered service you want. May be null.</param>
/// <returns>
/// The requested service instance.
/// </returns>
protected override object DoGetInstance(Type serviceType, string key) {
if (string.IsNullOrEmpty(key)) {
return container.GetInstance(serviceType);
} else {
return container.GetInstance(serviceType, key);
}
}
/// <summary>
/// When implemented by inheriting classes, this method will do the actual work of
/// resolving all the requested service instances.
/// </summary>
/// <param name="serviceType">Type of service requested.</param>
/// <returns>
/// Sequence of service instance objects.
/// </returns>
protected override IEnumerable<object> DoGetAllInstances(Type serviceType) {
foreach (object obj in container.GetAllInstances(serviceType)) {
yield return obj;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment