Saturday, September 29, 2007

Article on Dependency Injection

Mark Monster has written an excellent article on Dependency Injection in the newest .NET Magazine (it's no MSDN magazine, but hey, its free of charge). Nonetheless, there are two things I want to mention here.

The author says that Loose coupling is mostly used in the context of SOA and web services. I don't agree. I won't deny the fact that this is also important for SOA, I think that Loose coupling comes from Object-oriented programming. A minor detail perhaps, but hey, you know me. I'm just being picky.

The second issue is when he mentions that it's absolutely necessary to have a Dependency Injection container when using Dependency Injection. This made my alarm bells go off. I completely agree when we are talking about medium-sized to larger projects here. But, I consider it to be harmful when used in smaller projects. How do you know when to use it then? As soon as you experience difficulties when not using a DI container.

Personally, I'm using Dependency Injection for a long time now, and I only have had a few projects where I really wanted to use a DI container to make my life a little easier. I believe YAGNI is an important thing in software development. I recently did an investigation on wich DI container we should use in case we need it. Turns out that Castle Windsor is very powerful and very easy to get started (its also used in the article). I didn't consider StructureMap or Spring.NET, but I guess they are equivalent to Castle Windsor.

Wrapping up this post, I want to say kudos to Mark for putting up an article on Dependency Injection. I consider this an important topic (used with or without TDD), and there are not enough developers out there who use this in there day-to-day coding efforts.

2 comments:

Mark Monster said...

Thanks for your review on my article. Good to see someone actually reads and likes it.

You're right about the issues you mention. I think I've been a little bit to resolute. In case of a small project using a DI-container is a little bit too much.

Jan Van Ryswyck said...

The fact alone that you use a db4o repository (and therefore probably also the database itself) makes me admire your courage :-). Dependency Injection is such an important topic nowadays. Articles like the one you've written are encouraging other developers to consider more loosely-coupled design and that it's no longer a OO theory thing anymore.