views:

90

answers:

6

When coding, I often come across the following pattern:

-A method calls another method (Fine), but the method being called/callee takes parameters, so in the wrapping method, I pass in parameters. Problem is, this dependency carrying can go on and on. How could I avoid this (any sample code appreciated)?

Thanks

+2  A: 

You can use a dependency injection framework. One such is Guice: see http://code.google.com/p/google-guice/

Paul Hankin
+1  A: 

Step 1: Instead of passing everything as separate arguments, group the arguments into a class, let's say X.

Step 2: Add getters to the class X to get the relevant information. The callee should use the getters to get the information instead of relying on parameters.

Step 3: Create an interface class of which class X inherits. Put all the getters in the interface (in C++ this is as pure virtual methods).

Step 4: Make the called methods only depend on the interface.

Patrick
A: 

IOC Containers or Inversion of Control Containers

In other words, let your container wire you dependencies, rather than you. For example, a personal favourite of mine is Ninject. Some nice examples on there (in C#) - other langagues such as Java et al have similar containers.

It's probably worth pointing out, what you're doing isn't bad. You're writing loosely coupled code by the sound of it. But unfortunately, the pain comes when wiring such depdendencies together. Stick with it though, the benefits are huge.

Finglas
A: 

Since they cannot be (easily) unit tested, most developers choose to inject objects into Views. Since the views are not (normally) used to construct other views, that is where your DI chain ends. You may have the issue (which I have run into every once in ahwile) where you need to construct objects in the correct order especially when using a DI framework like Unity where an attemt to resolve the object will deadlock. The main thing you need to worry about is circular dependency. In order to do this, read the following article:

http://stackoverflow.com/questions/2053044/can-dependency-injection-prevent-a-circular-dependency

Nate Noonen
+1  A: 

Refactoring: Introduce Parameter Object

You have a group of parameters that naturally go together?

Replace them with an object.

http://www.refactoring.com/catalog/introduceParameterObject.html

The advantage of the parameter object is that the calls passing them around don't need to change if you add/remove parameters.

(given the context of your answers, I don't think that an IoC library or dependency injection patterns are really what you're after)

Frank Schwieterman
+3  A: 

Passing a parameter along just because a lower-layer component needs it is a sign of a Leaky Abstraction. It can often be more effective to refactor dependencies to aggregate services and hide each dependency behind an interface.

Cross-cutting concerns (which are often the most common reason to pass along parameters) are best addressed by Decorators.

If you use a DI Container with interception capabilities, you can take advantage of those to implement Decorators very efficiently (some people refer to this as a container's AOP capabilities).

Mark Seemann