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.
|
This topic discusses the following tasks 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 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 CompositionContainer object.
If the service is not found, ObjectBuilder then looks at the
services in the parent CompositionContainer object.
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 Servicescollection
of the CompositionContainer within
which you want to use the service. This can be the root CompositionContainer or
a module CompositionContainer.
To use an existing service instance you have already created, use
the Add method.
moduleContainer.Services.AddNew();
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.)
"Customers" assemblyName="Customers" virtualPath="~/Customers">
. . .
"OrdersRepository.Interfaces.Services.ICustomerService, OrdersRepository.Interfaces" type="OrdersRepository.Services.CustomerService, OrdersRepository.Services" scope="Global" />
. . .
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;
}
...
}