Skip to content

Instantly share code, notes, and snippets.

@anaisbetts
Created March 24, 2013 22:36
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anaisbetts/5233880 to your computer and use it in GitHub Desktop.
Save anaisbetts/5233880 to your computer and use it in GitHub Desktop.
Literally everything I ever wanted out of an IoC container
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