Adding Application Code

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Unity Application Block information can be found at the Unity Application Block site.

The Unity Application Block is designed to support a range of common scenarios for resolving instances of objects that, themselves, depend on other objects or services. When adding your application code, refer to the scenarios in the Key Scenarios sections and select the ones that best suit your situation. Use the code that accompanies the scenario either as-is or adapt it as necessary.

First, prepare your application to use the Unity Application Block. The following procedure describes how to include the necessary assemblies and elements in your code.

To prepare your application

  1. Add a reference to the Unity Application Block assembly. In Visual Studio, right-click your project node in Solution Explorer, and then click Add References. Click the Browse tab and find the location of the Microsoft.Practices.Unity.dll assembly. Select the assembly, and then click OK to add the reference.

  2. Use the same procedure to set a reference to the ObjectBuilder assembly, named Microsoft.Practices.ObjectBuilder2.dll.

  3. (Optional) To use elements from the Unity Application Block without fully qualifying the element reference, add the following using statements (C#) or Imports statements (Visual Basic) to the top of your source code file as required.

    using Microsoft.Practices.Unity;
    using Microsoft.Practices.Unity.Configuration;
    using Microsoft.Practices.Unity.StaticFactory;
    
    'Usage
    Imports Microsoft.Practices.Unity
    Imports Microsoft.Practices.Unity.Configuration
    Imports Microsoft.Practices.Unity.StaticFactory
    
  4. Add your application code. For more information about how you can use the Unity Application Block in your own applications, see Key Scenarios.

Note

For Visual Basic projects, you can also use the References page of the Project Designer to manage references and imported namespaces. To access the References page, select a project node in Solution Explorer, and then click Properties on the Project menu. When the Project Designer appears, click the References tab.