Problem solve Get help with specific problems with your technologies, process and projects.

Improve dependency injection with ObjectBuilder

Reusing components and objects has obvious advantages, but it creates a myriad of dependencies. The Dependency Injection Container, part of Microsoft's ObjectBuilder, makes dependencies easier to manage.

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.

More from Patterns & Practices Summit East

Neward on communication design patterns

Toolkits, CAB and workflows

Mash-ups, Web 2.0 and developer empowerment

"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.

Dig Deeper on .NET Architecture Best Practices

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.