Reusing components and objects in programming has obvious advantages, such as a tighter architecture. One downside, though, is the myriad of dependencies that results.
One answer is dependency injection, or DI, which is the task of helping child objects find each other. The process is akin to "putting objects in a bag and shaking it up and getting stuff out," said Brad Wilson, lead developer for Microsoft's patterns & practices team, at the group's recent summit at Microsoft's Boston-area offices.
The hardest way to implement DI, naturally, is hard coding. The Service Locator -- a component model inside Visual Studio and the DI system used in CAB -- is better, but it requires a dependency on the ILocator and storing constants is difficult.
Enter the Dependency Injection Container. This eliminates the need for an object to find its own dependencies through the use of containers, which create objects and fulfill dependencies for them.
This functionality is part of ObjectBuilder, a new Microsoft technology that Wilson described as "a framework for building dependency injection frameworks.
"As soon as your applications become non-trivial, it's a real problem creating objects. We decided to create a framework for all of you to build your containers," Wilson said.
The builder itself is comprised of strategies and policies, a locator and a container; this means the processes of finding items and storing items are independent.
ObjectBuilder works through strategies and policies that set what Wilson called a "chain of responsibility" for dependency characteristics such as singletons, nested containers, method invocation and instrumentation.
"The important thing about ObjectBuilder is that it's extensible," Wilson said. "If you don't like ours, you can write your own. It's perfectly acceptable."
ObjectBuilder is currently in use in Microsoft's CAB and in its Enterprise Library. Wilson said the patterns & practices group will soon post a sample of an XML-driven container on the ObjectBuilder page on GotDotNet.
More information is also available in this September 2005 MSDN article, Design Patterns: Dependency Injection.