Tuesday, January 6, 2009

What is Dependency Injection

A co-worker asked me last week, "What is dependency injection?"

Dependency Injection is a method to reduce coupling within a system. When a component you are developing needs to make a call to another component, you wouldn't want to hard-code it, so you simply define an injection point and get a framework or dependency injector to add in that dependency (almost always) at runtime.

Dependency Injection is a specific form of Inversion of Control, which is simply the idea that components should not be coupled but instead should use some means of calling another component. For example, if you wanted to decouple your system using Inversion of Control but didn't use Dependency Injection, you could instead go with Service Locators - each component makes a request to a service locator to locate the component it needs to call.

UPDATE: I found a pretty good blog post about Inversion of Control, Dependency Injection, and Service Locator here.

No comments: