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.
This scenario explores how you can directly apply dependency injection to an existing object using the BuildUp method of the Unity container.
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.
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:
To apply dependency injection to an existing object instance
CustomerService result = myContainer.BuildUp<CustomerService>(myService);
Dim result As CustomerService = myContainer.BuildUp(Of CustomerService)(myService)
CustomerService result = myContainer.BuildUp<CustomerService>(myService, "Customers");
Dim result As CustomerService = myContainer.BuildUp(Of CustomerService)(myService, "Customers")
For more information about the techniques discussed in this scenario, see the following topics: