Using BuildUp to Wire Up Objects Not Created by the Container

Unity exposes a method named BuildUp that you can use to pass existing object instances through the container in order to apply dependency injection to that object. This is an alternative to resolving the object using any of the other techniques available with Unity. However, remember that the BuildUp method cannot inject dependent objects into constructor parameters, because the object has already been created; it is not created by Unity.

The BuildUp method is useful when you do not have control of the construction of an instance, but you still want property or method call injection performed. For example, ASP.NET pages, Windows Communication Foundation (WCF) applications, and XAML code often create instances of objects and pass a reference to your code. The BuildUp method will usually return the original object after passing it through the container, although container extensions may add other features that cause the method to return a different object that is type-compatible with the existing object. For example, an injection strategy may create and return a proxy for an object or a derived object instead of the actual object.

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.

The BuildUp Method Overloads

The following table describes the overloads of the BuildUp method. The API for the Unity container contains both generic and non-generic overloads of these methods so that you can use them with languages that do not support the generics syntax.

Method

Description BuildUp

BuildUp<T>(T existing)

Passes the existing object of type T through the container and performs all configured injection upon it.

BuildUp<T>(T existing, string name)

Passes the existing object of type T with the specified name through the container and performs all configured injection upon it.

BuildUp(Type t, object existing)

Passes the existing object of type t through the container and performs all configured injection upon it.

BuildUp(Type t, object existing, string name)

Passes the existing object of type t with the specified name through the container and performs all configured injection upon it.

Note

The UnityContainer class also exposes the Teardown method, which theoretically would reverse the build-up process. However, the base implementation of this method does nothing. The implementation exists so that container extensions can execute their own custom overrides of this method if required.

Using the BuildUp Method

The following example shows how you can use the BuildUp method to apply dependency injection to existing object instances named myDataService and myLoggingService, which implement the interface IMyService. The examples use both the generic and the non-generic overloads of the container methods.

IMyService myDataService = new DataService();
IMyService myLoggingService = new LoggingService();
IMyService builtupDataService = myContainer.BuildUp<IMyService>( myDataService);
IMyService builtupLoggingService
  = (IMyService)myContainer.BuildUp(typeof(IMyService), myLoggingService);
'Usage
Dim myDataService As IMyService = New DataService()
Dim myLoggingService As IMyService = New LoggingService()
Dim builtupDataService As IMyService = myContainer.BuildUp(Of IMyService)(myDataService)
Dim builtupLoggingService As IMyService _
  = myContainer.BuildUp(GetType(IMyService), myLoggingService)

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.

The following code shows how you can execute the BuildUp method and provide a name. It uses both the generic and the non-generic overloads of the container methods.

IMyService myDataService = new DataService();
IMyService myLoggingService = new LoggingService();
IMyService builtupDataService
  = myContainer.BuildUp<IMyService>(myDataService, "Data");
IMyService builtupLoggingService
  = (IMyService)myContainer.BuildUp(typeof(IMyService), myLoggingService, "Logging");
'Usage
Dim myDataService As IMyService = New DataService()
Dim myLoggingService As IMyService = New LoggingService()
Dim builtupDataService As IMyService _
  = myContainer.BuildUp(Of IMyService)(myDataService, "Data")
Dim builtupLoggingService As IMyService _
  = myContainer.BuildUp(GetType(IMyService), myLoggingService, "Logging")

More Information

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