Getting seldom used dependencies and keeping testability

In a project we’ve got controllers with functions which handles requests (request handlers) and most dependencies are given through the constructor and the method call itself. Some classes are used so seldom that it feels unnecessary to create them on every request and send them through the constructor but I don’t want to just create them (new Dependency) in the request handlers because then i can’t mock that in tests. Any ideas on a solution that’s also good for testability?

You could always pass in a factory so you can create these occasional objects only when needed, but avoid instantiating them directly within the controller. For testing you can easily mock the factory.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.