This week there have been two posts covering dependency inversion and going too far with constructor injection.  In the first post Robert C Martin (Uncle Bob) cautions against littering your code with calls to your IoC container. 

I think these frameworks are great tools. But I also think you should carefully restrict how and where you use them.

The post goes on to discuss how to keep the code which builds dependencies contained in one place so that you are not littering your code with calls to the container framework or coupling yourself to other classes using new() statements.

The second post by Jeffrey Palmero takes this a little further and is the one that really got me thinking.  Palmero sets up an example where a class requires two interface dependencies in its constructor.  However, in the code, both dependencies are not always used.  This was a key thing to note, because Palmero’s point was,

The anti-pattern is that one of these constructor arguments is really not a class dependency.

In his example, one of the interface dependencies required in the constructor is only used in some cases.  This was a huge “duh” for me because I know there are several times when I have required everything the class might use in the constructor. 

I think the key here is balance.  Require the real dependencies up front, but also allow for other dependencies to be resolved by blending the use of a container with a factory type pattern.  Keep all the dirty details of your dependent objects and how they are resolved hidden and encapsulated in one place.  This would allow you to switch containers or stop using one all together.

** Update **

A couple readers have posted comments and I would love to hear more.  Be sure to give them a read!