Partager via


Using BuildUp to Wire Up Objects Not Created by the Container

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.

This scenario explores how you can directly apply dependency injection to an existing object using the BuildUp method of the Unity container.

Typical Goals

In this scenario, you use the BuildUp method to pass an object through the Unity container dependency mechanism so that it applies any specified dependencies to the object.

The BuildUp method is useful when you need to apply dependency to an object created outside of your control—for example, in an ASP.NET or a WCF application. These types of application often create instances of objects and pass a reference to your code. Therefore, you cannot use the Unity container to generate a new object instance in the same way as you would in a Windows Forms application, where you usually create all the objects you need directly in your application code.

If you have created or added extensions to the Unity container, these extensions can access and use a name that you specify when you execute the BuildUp method. This allows the extensions to change their behavior depending on the value you specify. For example, they may use the name to control how dependencies are resolved or to control features such as event wiring or interception. The actual behavior depends on the individual extension.

Solution

To apply dependencies to an existing object instance, use the BuildUp method and specify a value for the registered object type. The BuildUp method will return either of the following:

  • The original object with dependencies (if any) applied
  • A type-compatible object if a container extension changes the type—for example, by returning a proxy for the object

Note

The API for the Unity container contains both generic and non-generic overloads of most of the methods, so that you can use it with languages that do not support the Generics syntax. The examples in this topic use the generic overloads. For more information about the non-generic overloads, see Unity Application Block Methods.

To apply dependency injection to an existing object instance

  1. Using a reference to the container, call the BuildUp method and specify the concrete type of the existing object that contains dependency attributes within its class definition. You do not need to register a mapping for this. The following code shows how to apply dependency injection to an object of type CustomerService for which you have a reference to an existing instance in the variable myService. The returned object is (or is type-compatible with) the type CustomerService.

    CustomerService result = myContainer.BuildUp<CustomerService>(myService);
    
    'Usage
    Dim result As CustomerService = myContainer.BuildUp(Of CustomerService)(myService)
    

    Note

    For details of how you configure objects for dependency injection by Unity container, see Annotating Objects for Constructor Injection, Annotating Objects for Property (Setter) Injection, and Annotating Objects for Method Call Injection.

  2. You can also use the BuildUp method to apply dependency injection to an existing object where an extension uses the name to control the behavior of that extension. The following code shows how you can apply dependency injection to an object of type CustomerService for which you have a reference to an existing instance in the variable myService, using the name "Customers" to control extension behavior.

    CustomerService result = myContainer.BuildUp<CustomerService>(myService, "Customers");
    
    'Usage
    Dim result As CustomerService = myContainer.BuildUp(Of CustomerService)(myService, "Customers")
    

More Information

For more information about the techniques discussed in this scenario, see the following topics: