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

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?

This entry was posted in Programming. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.