I have an Order
entity with a refund()
method that uses an abstract factory refundStrategyFactory
to create the appropriate refund strategy at run-time:
public class Order extends Entity{
public void refund(Employee emp, Quantity qty, IRefundStrategyFactory refundStartegyFactory){
//use the abstract factory to determine the appropriate strategy at run-time.
}
}
I'm trying to use dependency injection and have IRefundStrategyFactory
injected automatically into the refund()
method, but I couldn't find a way to achieve this.
I'm not using the constructor injection because Order
is an entity and we shouldn't inject services into entities unless it's going to be used temporarily as in the refund method. This is better explained by Misko Hevery's blog post To “new” or not to “new”…:
It is OK for Newable to know about Injectable. What is not OK is for the Newable to have a field reference to Injectable.
A similar answer by Mark Seemann to a related question also advocates the same idea.
Bonus point, Aren't I exposing the internals of the refund()
method by exposing its dependency on IRefundStrategyFactory
? Should I sacrifice unit testing in isolation and create the factory inside the method itself?