I currently have a static OrderEvaluatorFactory which is used to create various kinds of Order Evaluators.
A new kind Order Evaluator should now use an order processor which is only available through Dependency Injection.
The factory class currently looks like this. It makes use of an attribute to search the desired type for instanciation and then uses System.Activator.CreateInstance() to create an instance.
public static class OrderEvaluatorFactory
{
private static Dictionary<Type, Type> orderEvaluatorTypesByOrderTypes = null;
public static IOrderEvaluator CreateFor(OrderType ot)
{
if (orderEvaluatorTypesByOrderTypes == null)
{
orderEvaluatorTypesByOrderTypes = Utilities.TypeFinder<OrderTypeEvaluatorForAttribute, Type>.GetTypesByAttribute(
typeof(OrderEvaluatorFactory).Assembly, type => typeof(IOrderEvaluator).IsAssignableFrom(type),
attribute => attribute.For);
}
Type orderEvaluatorType = evaluatorTypesByOrderTypes[ot.GetType()];
if (evaluatorType == null)
throw new Exception("No evaluator type found for " + ot.GetType());
return (IOrderEvaluator)Activator.CreateInstance(evaluatorType);
}
}
And this is how the evaluator looks, which should make use of dependency injection:
[OrderTypeEvaluatorFor(typeof(SuperFastDeliveryOrder))]
public class SuperFastDeliveryOrderEvaluator : IOrderEvaluator
{
private readonly IOrderProcessor orderProcessor;
public SuperFastDeliveryOrderEvaluator(IOrderProcessor orderProcessor)
{
this.orderProcessor = orderProcessor;
}
public bool Eval(Customer cstm)
{
// omitted...
return true;
}
}
This is the only evaluator that (currently) needs IOrderProcessor. So I don't want to pass the object to simply to all other types as well (e.g. as parameter).
The problem I am now faced with is that my constructor (with IOrderProcessor) is not getting called (as I believe Activator.CreateInstance() only calls the default constructor) and as said I don't want to use an overload to pass an IOrderProcessor to every object because not all object need one.
I guess I have now a bit a mess, because the factory pattern (especially a static factory) does not go well with DI. Unfortunately I don't see a nice way to solve this issue.
I guess I could make the factory non-static and add it to my DI container, allowing me to pass IOrderProcessor to this factory but the Activator would still remain.
But how could I replace the Activator with something more suited for DI that doesn't require me to hardcode the instanciation for every currently known Evaluator type?