helloquence oqmzwnd3thu unsplash 5

Dear Martin,

This is a letter to Martin Fowler, which I also publish in my blog.

I am rather wondered why you don’t do any mention in your article

https://www.martinfowler.com/articles/injection.html

about the following problem:

Real classes which use dependency injection very often may need to pass some of their dependencies to implementation classes which they depend on.

This may lead to increase of the number of dependencies and make code somehow hard to write and read. Another problem is that while implementation of a class changes (say to make it more advanced), new dependencies may appear leading to such inconveniences as changing constructors’ signatures.

I feel that one of the best solutions is to group related dependencies into a record (if to use Pascal term) or a structure (if to use C/C++ term), like as this fragment of my ongoing open source project written in Python:

class ExecutionContext(object):
    def __init__(self, logger, translations):
        """
        :param logger: logger
        :param translations: usually should be gettext.GNUTranslations
        """
        self.logger = logger
        self.translations = translations

This way I can pass one object ExecutionContext instead of two objects (the logger and the translations) into constructors. These objects are to be then grouped into even greater objects, etc.

Do you think it is a good idea?