Is there an elegant way to deal with dependency injection for the strategy pattern?
For instance, I have my dependency injector that I register a few different strategies with, like so:
container.Register<IMakeCoffee, MakeLatte>();
container.Register<IMakeCoffee, MakeEspresso>();
container.Register<IMakeCoffee, MakeCappuccino>();
And then when I want to access one of these strategies, I inject it into my constructor like:
public Barista(IEnumerable<IMakeCoffee> coffeeStrategies) {}
But once I have that list of all the strategies, how can I say I want this specific strategy? What I'm doing now is forcing each instance of IMakeCoffee
to have a CoffeeType enum that I can key off of to determine what type the strategy is.
public Coffee MakeCoffee(CoffeeType coffeeType)
{
var strat = coffeeStrategies.FirstOrDefault(s => s.CoffeeType == coffeeType);
return strat.MakeCoffee();
}
Is there a more elegant way to uniquely identify a strategy? The enum business seems cumbersome.
For reference, I am using Prism for a Xamarin Forms app, but I felt this question was framework independent.