Skip to content

Instantly share code, notes, and snippets.

@jarrettmeyer
Created April 25, 2014 16:05
Show Gist options
  • Save jarrettmeyer/11294641 to your computer and use it in GitHub Desktop.
Save jarrettmeyer/11294641 to your computer and use it in GitHub Desktop.
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));
}
}
@jarrettmeyer
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment