How to use Dependency Injection with Static Methods?

Viewed 25240

Imagine there is a Customer class with an instance Load() method.

When the Load() method is called, it retrieves order details by e.g.

var orders = Order.GetAll(customerId, ...);

GetAll() is a static method of the Order class and the input parameters are fields defined in the Customer class.

As you can see, Order is a dependency of the Customer class, however, I can't just create an IOrder and inject it there as interfaces can't have static methods.

Therefore, the question is how could I introduce dependency injection in this example?

I don't want to make GetAll() an instance method since it's a static method and need to keep it that way.

For example, I have used utility classes in my design, most of which just contain static methods.

3 Answers
Related