Skip to content

Instantly share code, notes, and snippets.

@bgrebil
Forked from anaisbetts/funcioc.cs
Created April 4, 2013 06:01
Show Gist options
  • Save bgrebil/5308186 to your computer and use it in GitHub Desktop.
Save bgrebil/5308186 to your computer and use it in GitHub Desktop.
public class FuncServiceLocator
{
Dictionary<Tuple<Type, string>, List<Func<object>>> _registry;
public void Register(Func<object> factory, Type type, string contract = null)
{
var pair = Tuple.Create(type, contract ?? "");
if (!_registry.ContainsKey(pair)) _registry[pair] = new List<Func<object>>();
_registry[pair].Add(factory);
}
public IEnumerable<object> GetAllServices(Type type, string contract = null)
{
var pair = Tuple.Create(type, contract ?? "");
if (!_registry.ContainsKey(pair)) return Enumerable.Empty<object>();
return _registry[pair].Select(x => x());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment