Improve dependency injection with ObjectBuilder

Improve dependency injection with ObjectBuilder

Brian Eastwood, Associate Editor

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.

    Requires Free Membership to View

    When you register, you'll begin receiving targeted emails from my team of award-winning writers. Our goal is to provide a unique online resource for developers, architects and development managers tasked with building and maintaining enterprise applications using Visual Basic, C# and the Microsoft .NET platform.

    Hannah Smalltree, Editorial Director

    By submitting your registration information to SearchWinDevelopment.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchWinDevelopment.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

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.

This was first published in April 2006

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.