Annoyed by tons of classes for DI in constructors of Magento 2 - is there a better way?

Check out \Magento\Framework\Model\Context, referenced in your example. What you describe is exactly what it does. Magento uses similar Context objects throughout the core to shorten DI lists.

The only thing to keep in mind is that this should not be used to hide away bad architectural decisions. You should consider whether each of the classes you need 'all over your module' is really necessary, and if so, whether there's an alternate way of organizing your code that would accomplish the same goal better. It's easy to introduce unintentional performance issues.


I'm pretty sure you're not the only one in this case and in some way I totally understand why you thought about doing this.

To me, the main issue I see with such approach is that you lose one of the main benefits of Dependency Injection which is to know straight away what your class depends on when checking the constructor.

Another important benefit of Dependency Injection is that it makes code easier to test in an automated framework. In your case that's definitely one downside.

Those are the two reasons that come up but there may be more.

EDIT: I'm just gonna to add a quote from Alan Kent (who is part of Magento) that you can find in the comments of this question:

I generally discourage throwing methods into a helper class which are not related. It is better to have separate classes that represent real purposes. Or use static methods in which case there is no need for a constructor (the calling code is responsible to get handles to needed data structures).