Created
April 25, 2014 16:05
-
-
Save jarrettmeyer/11294641 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
public class StrategyQuery | |
{ | |
public string Name { get; set; } | |
} | |
public interface IStrategy | |
{ | |
bool CanHandle(StrategyQuery query); | |
void DoSomething(); | |
} | |
public interface IStrategyRegistry | |
{ | |
IStrategy GetStrategyFor(StrategyQuery query); | |
} | |
public class NinjectStrategyRegistry : IStrategyRegistry | |
{ | |
private readonly IKernel kernel; | |
public NinjectStrategyRegistry(IKernel kernel) | |
{ | |
this.kernel = kernel; | |
} | |
public IStrategy GetStrategyFor(StrategyQuery query) | |
{ | |
var instances = kernel.GetAll<IStrategy>(); | |
foreach (var instance in instances) | |
{ | |
if (instance.CanHandle(query)) | |
{ | |
return instance; | |
} | |
} | |
throw new InvalidOperationException(string.Format("Could not find strategy for {0}." query)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your strategy can do anything really. In this example, I'm using Ninject, but feel free to use any other IoC container. You can also do assembly scanning.