Skip to end of metadata
Go to start of metadata

Symptoms

A dependency exists solely to be propagated to another class, but no methods are called upon the dependent object itself. For example:

In this example, no method-calls are made upon 'service', it is simply propagated to the constructor of 'AnotherControllerImpl'. Therefore, it is not a valid dependency for 'AControllerImpl'.

Causes

DependencyInjection has been partially applied to a hierarchy of classes. This could be because some classes in the hierarchy depend upon instance state not available at container-registration time.

What To Do

Apply DependencyInjection to 'AControllerImpl' by replacing the dependency on 'SomeService' with a dependency on 'AnotherController'. If, as in the example above, 'AnotherControllerImpl' has a dependency upon some state that is not available at container-registration time, then we need to introduce a factory for creating 'AnotherController' as follows:

TODO: This is maybe a little contrieved for this example. Maybe remove or simplify. (AH).

'AnotherControllerFactory' is an interface:

It can be implemented as follows::

Now we can register both 'AControllerImpl' and 'AnotherControllerFactoryImpl' in the container. When 'AControllerImpl' is instantiated, it is supplied with an implementation of 'AnotherControlFactory' that it can use to create an 'AnotherController' instance.

Exceptions

When Migrating from executors to services, it can sometimes be difficult to avoid introducing a Propagating Dependency. In these cases, the Propogating Dependency can be considered as a good first step towards PicoFication of a set of classes. An effort should be made to complete PicoFication at some stage by making a series of further steps as described above.

Labels
  • None
  1. Feb 20, 2004

    • It wasn't obvious to me what an executor is. I had to google around for it. How about a definition?
  2. Jun 19, 2004

    It is irrelevant for this example what an executor is.