Working with ObjectBuilder

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.
To create client business applications using current Microsoft technologies, see patterns & practices' Prism.

This topic discusses the following tasks that you can perform with ObjectBuilder:

  • Creating new objects
  • Locating a service
  • Registering a service
  • Registering a constructor

Creating New Objects

Frequently, an object in your application will require an instance of another object. For example, consider the NewTransferView in the reference implementation. The reference implementation uses the MVP pattern to decouple business logic from the user interface code. This means that the NewTransferView requires a reference to an instance of a presenter object (in this case, an instance of the NewTransferViewPresenter class).

The NewTransferView class requires an instance of the NewTransferViewPresenter class, but does not contain code to instantiate an instance. Instead, the NewTransferView class uses the CreateNew attribute.

[CreateNew]
public NewTransferViewPresenter Presenter
{
  get { return _presenter; }
  set
  {
    _presenter = value;
    _presenter.View = this;
  }
}

The CreateNew attribute instructs ObjectBuilder to instantiate and initialize an instance of a NewTransferViewPresenter when the NewTransferView is created. When the property is set, the View property of the presenter is used to connect this implementation of the INewTransferView interface to the presenter (the View property is defined in the Presenter base class).

Locating a Service

You can add the ServiceDependency attribute to a property in your class to declaratively obtain a reference to a service. The property specifies the type of service or interface you require, as shown in the following code. When this attribute is present, ObjectBuilder locates an instance of the service, and passes back a reference to it. To locate the service, ObjectBuilder first looks in the current WorkItem. If the service is not found, ObjectBuilder looks at the services in the parent WorkItem. If the service is not found, ObjectBuilder throws an exception.

private IAccountServices  _accountServices;

[ServiceDependency]
public IAccountServices AccountServices
{
  set { _accountServices = value; }
}

Frequently, the ServiceDependency attribute is used for the arguments in a constructor. This means that ObjectBuilder will instantiate the required services when it creates the dependent object.

public class ElectronicFundsTransferController
{
  private IAccountServices  _accountServices;

  public ElectronicFundsTransferController
    (
      [ServiceDependency] IAccountServices accountServices
    )
  {
    _accountServices = accountServices;
  }
  ...
}

Registering a Service

You can programmatically register a service. To do this, call the Add method or AddNew method of the Services collection of the WorkItem within which you want to use the service. This can be the root WorkItem or a module WorkItem. To use an existing service instance you have already created, use the Add method.

moduleWorkItem.Services.AddNew<AccountServices, IAccountServices>();

Registering a Constructor

A class can contain more than one constructor. ObjectBuilder first looks for any constructor decorated with the [InjectionConstructor] attribute (there can be only one of these) and uses this constructor, if found. If there is a constructor, but no decorated constructor, it will use that constructor.

public class CustomersListViewPresenter
{
  private CustomersController _controller;

  [InjectionConstructor]
  public CustomersListViewPresenter
    (
      [ServiceDependency] CustomersController controller
    )
  {
    _controller = controller;
  }
  ...
}