views:

48

answers:

2

I have the following constructor for my Class

 public MyClass(File f1, File f2, File f3, Class1 c1, Class2 c2, Class3 c3)
{
..........
}

As can be seen, it has 6 parameters. On seeing this code, one of my seniors said that instead of passing 6 parameters I should rather pass a configuration object.

I wrote the code this way because recently I have read about "Dependency injection", which says "classes must ask for what they want". So I think that passing a configuration object will be against the principle.

Is my interpretation of "Dependency injection" correct? OR Should I take my senior's advice?

+2  A: 

I don't think using a configuration object contradicts using dependency injection pattern. It is more about the form in which you inject your dependencies and a general question of whether it's better to have a function (in this case the constructor) that takes 20 parameters or combine those parameters into a class so that they are bundled together.

You are still free to use dependency injection, i.e. construct the configuration object by some factory or a container and inject it into the constructor when creating an instance of your class. Whether or not that's a good idea also depends on the particular case, there are no silver bullets ;)

Thomas Wanner
@Thomas: In our project, there is only one configuration object, which contains all the properties. My concern over using it was that, I will have to create the object of Class1, Class2 and Class3 inside my constructor. This seems to be against "Dependency injection" principle.
athena
Of course it's another question how the objects of Class1 etc. get created, but from the point of view of class MyClass they are injected dependencies whether they are passed individually or together in an object. In which constructor do you instantiate Class1 etc. - in the constructor of the configuration object ?
Thomas Wanner
Yes. All the objects required are instantiated in the constructor of the configuration object. I am confused because I do not understand how using a configuration object is different than using a factory.
athena
If they are all instantiated there then it's a kind of factory, and it's basically the object that injects dependencies into your class.
Thomas Wanner
+2  A: 

"Configuration object" is an obtuse term to apply in this situation; it frames your efforts in a purely mechanical sense. The goal is to communicate your intent to the class's consumer; let's refactor toward that.

Methods or constructors with numerous parameters indicate a loose relationship between them. The consumer generally has to make more inferences to understand the API. What is special about these 3 files together with these 3 classes? That is the information not being communicated.

This is an opportunity to create a more meaningful and intention-revealing interface by extracting an explicit concept from an implicit one. For example, if the 3 files are related because of a user, a UserFileSet parameter would clearly express that. Perhaps f1 is related to c1, f2 to c2, and f3 to c3. Declaring those associations as independent classes would halve the parameter count and increase the amount of information that can be derived from your API.

Ultimately, the refactoring will be highly dependent on your problem domain. Don't assume you should create a single object to fulfill a parameter list; try to refactor along the contours of the relationships between the parameters. This will always yield code which reflects the problem it solves more than the language used to solve it.

Bryan Watts
I second every single word of this :)
vulkanino