This project is read-only.

Working with ObjectBuilder

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

Creating New Objects

Frequently, an object in your application requires 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 then 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 or register a service through configuration. To programmatically register a service, 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>(); 

To register a service through configuration, add a service configuration element to a Web.config file. In the following XML, the OrdersRepository.Services.CustomerService service is registered as a global service. (To register a service as a module service, change the scope attribute to Module.)

<compositeWeb>
  <modules>
    <module name="Customers" assemblyName="Customers" virtualPath="~/Customers">
      . . . 
      <services>
        <service registerAs="OrdersRepository.Interfaces.Services.ICustomerService, OrdersRepository.Interfaces" type="OrdersRepository.Services.CustomerService, OrdersRepository.Services" scope="Global" />
      </services>
    </module>
  </modules>
      . . .
</compositeWeb> 

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 no decorated constructor, yet there is only one constructor, it will use that constructor.

public class CustomersListViewPresenter
{
  private CustomersController _controller;

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

Last edited Nov 19, 2007 at 9:16 PM by siacomuzzi, version 1

Comments

No comments yet.