Dependency Injection - Is it relevant beyond unit testing?

"What do you do if you've spent all this time convincing the powers that be that DI is absolutely essential and now . . . it isn't? That's going to hurt. Much better to just dismiss TypeMock as "too powerful" and move on. "

This is a quote from Jacob Proffitt's post on our forum regarding typemock and design for testability. I urge you to register and comment there if this is interesting to you.

The more I read Jacob's blog, and his (sometimes controversial) thoughts on Dependency injection and good design, the more I like it. He really makes me think and challenge assumptions I've held dear for a long time.

For example, is Dependency Injection really necessary when you can write tests against your code without it? Does good design necessarily involve DI? More specifically, do you need DI all over the place, or just in specific places where you know dependencies could be a problem? (I'd recommend viewing this video BTW)

I can see how DI is useful when you have different teams working on something you depend on, in which case you'd have to have a delayed implementation decision (where the actual implementation of an interface is gotten at runtime), but this can be achieved though a regular factory as well. In this interesting post, Jacob has a matrix of possible Design decisions regarding DI. Read it.

I'm going to go read more of his posts. BTW, Jacob seems to be pro-Typemock to a degree, and that raises good questions as to why.

As for the title of this post, yes I do believe DI is relevant beyond unit testing, but it is not the ultimate truth in design just as singletons may have their place, or inheritance has its place.

 

Will we throw the baby out with the bath water?

The .net world as a whole has been doing great moving towards a world of better design, better practices and more. DI is a relatively new concept in the .NET world for most people (who didn't' come from Java).
In that light, it almost scares me to try and dismiss DI or design for testability simply because of all the other good things this thinking represents.


Perhaps people aren't ready to say "yes, we know DI exists, but we know where we don't necessarily need it", because they don't know enough in this world.
perhaps bringing DI and testability concepts is good, if for no other reason than to teach people what decoupling really means, with "testing" being just the excuse to make it happen.
We have put so much effort in making testing and testability drive lots of good design decisions, that, if "unit testing" didn't catch on in the .net world, we'd have a hard time introducing them.
Perhaps (being devils advocate) it is too early to dismiss this thinking, or to drive holes in it, for fear of throwing the baby out with the bath water.
What do you think?

Published Friday, January 18, 2008 4:07 PM by RoyOsherove

Comments

Friday, January 18, 2008 5:41 PM by Jason

# re: Dependency Injection - Is it relevant beyond unit testing?

I have been following Jacob's series of posts from the beginning, and I agree with most of what he has said. I myself am fairly new to unit testing and I have been using DI just to enable mock objects to test. Being a newcomer, I of course start to wonder if it is worth making design decisions that I otherwise would not have made just to enable testing. If there will never be another implementation of a given object, is it worth creating an interface, injecting the dependency and adding additional complexity and technical debt for the sake of unit tests? This makes TypeMock.Net very attractive.

That being said, I first implemented DI in a project before I ever started unit testing. The requirements called for a framework whose underlying data sources will be changed in future releases and will come in heterogeneous formats. I chose to use DI so that other developers who use the framework would be shielded from the future changes that we had requirements for. Almost one year later, I am very happy with that decision. DI was a great tool to use, and the key point is that it was driven from actual requirements, not "Just in case."

Friday, January 18, 2008 7:05 PM by Jeremy D. Miller -- The Shade Tree Developer

# The lamest way to try to win an argument

"Well, I just always use whatever is best for the situation." or "I'm more pragmatic

Friday, January 18, 2008 8:33 PM by Jeremy D. Miller -- The Shade Tree Developer

# TypeMock isn't too powerful, and "Designing for Testability" is much more than merely mocking anyway

Roy has a post up today about TypeMock, testability, and DI . The age old (ok, it just seems like we've

Saturday, January 19, 2008 2:25 PM by Colin Jack

# re: Dependency Injection - Is it relevant beyond unit testing?

"For example, is Dependency Injection really necessary when you can write tests against your code without it? Does good design necessarily involve DI? More specifically, do you need DI all over the place, or just in specific places where you know dependencies could be a problem? (I'd recommend viewing this video BTW)"

This is a key point, there is no doubt IoC and the related loose coupling can be useful. As with everything else though if you overuse it you get klunky designs. Ultimately in many cases you don't need to be able to replace every part of your system and sometimes using the concrete class directly is just fine.

As an example I'm happy to remove the dependency on the e-mail gateway (and use Ioc/Service Locator and an interface) but I won't change all my references to rules in the domain so that they are injected. The first gives me meaningful decoupling and fast tests and I believe it does improve the design, the second one leaves me cold unless it is driven by a requirement.

Saturday, January 19, 2008 3:17 PM by Mike Suarez

# re: Dependency Injection - Is it relevant beyond unit testing?

It’s funny but I learned about the Dependency-Inversion Principle from Uncle Bob’s book before knowing anything about testing. Later on I managed connect the dots DI + Interfaces + Mocks + easy to test with the principle of DIP and went happy to bed thinking that’s a wrap.

I know nothing about TypeMock but the little I’ve played with DI has given me the feeling that I’m following a good OO practice beyond unit testing.

Sunday, January 20, 2008 8:28 AM by Steve Freeman

# re: Dependency Injection - Is it relevant beyond unit testing?

I've responded to the point of this posting on mockobjects.com but, in the meantime, I could do without this sort of comment.

What do you do if you've spent all this time convincing the powers that be that DI is absolutely essential and now . . . it isn't? That's going to hurt. Much better to just dismiss TypeMock as "too powerful" and move on.

Sunday, January 20, 2008 8:28 AM by David Fauber

# re: Dependency Injection - Is it relevant beyond unit testing?

I think DI is relevent outside of unit testing.  I refactored a class today using DI for reasons that had nothing to do with testing.  

I had a friend start to work on the code with me who wanted to change how a class was loading some data into a child object.  The child object had been being getting loaded in a constructor. We refactored so that it would be passed in as an interface instead, as that was the cleanest way to let us each prime the child object the way we wanted to without needlessly impacting other code.

Sure it will make things more testable, but I see that as a positive side effect.

Sunday, January 20, 2008 1:36 PM by Colin Jack

# re: Dependency Injection - Is it relevant beyond unit testing?

"It’s funny but I learned about the Dependency-Inversion Principle from Uncle Bob’s book before knowing anything about testing. Later on I managed connect the dots DI + Interfaces + Mocks + easy to test with the principle of DIP and went happy to bed thinking that’s a wrap."

Same, but like anything else it can be taken too far. To me some coupling isn't a problem, as long as the coupling isn't too strong and isn't on things that you are worred about.

I keep using this sort of example, but if my domain class is calling out to a DAO/repository then extract an interface and use DI/Service Locator. I wouldn't do the same if my domain class wanted to use a domain rule.

Sunday, January 20, 2008 6:00 PM by ISerializable - Roy Osherove's Blog

# The Case For Typemock

I just wrote an email to someone regarding Typemock . We were discussing why they don't think Typemock

Sunday, January 20, 2008 6:37 PM by Community Blogs

# The Case For Typemock

I just wrote an email to someone regarding Typemock . We were discussing why they don't think Typemock

Monday, January 21, 2008 11:05 PM by Finds of the Week - Jan 20, 2008 » Chinh Do

# Finds of the Week - Jan 20, 2008 » Chinh Do

Pingback from  Finds of the Week - Jan 20, 2008 » Chinh Do

Wednesday, February 13, 2008 4:52 AM by Greg Young [MVP]

# Mocks are a code smell

Now that the title has grabbed your attention no I don't really think that mocks are a code smell

Wednesday, February 13, 2008 9:28 AM by Greg Young [MVP]

# Mocks are a code smell

Mocks are overused, why deal with mocking dependencies when you can avoid them?