-
-
Save bgrebil/5308186 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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