It manages the processes performed on objects during construction and disposal.
attributes - [CreateNew] and [ServiceDependency]
CreateNew - This attribute tells the dependency injection system to always create a new one of whatever it is you need.
Dependency - This is a general-purpose attribute with four optional parameters: Name, NotPresentBehavior, Createype, SearchMode
methods - BuildUp and TearDown.
BuildUp(locator, type, id, instance, policies[] );
BuildUp
Creating New Objects.
[CreateNew]The CreateNew attribute instructs ObjectBuilder to instantiate and initialize an instance of a NewTransferViewPresenter when the NewTransferView is created.
public NewTransferViewPresenter Presenter
{
get { return _presenter; }
set
{
_presenter = value;
_presenter.View = this;
}
}
When the property is set, the View property of the presenter is used to connect this implementation of the INewTransferView interface to the presenter.
Locating a Service.
You can add the ServiceDependency attribute to a property.
The property specifies the type of service or interface you require.
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.
private IAccountServices _accountServices;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.
[ServiceDependency]
public IAccountServices AccountServices
{
set { _accountServices = value; }
}
public class ElectronicFundsTransferControllerRegistering a Service.
{
private IAccountServices _accountServices;
public ElectronicFundsTransferController( [ServiceDependency] IAccountServices accountServices )
{
_accountServices = accountServices;
}
...
}
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.
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.
public class CustomersListViewPresenter
{
private CustomersController _controller;
[InjectionConstructor]
public CustomersListViewPresenter ( [ServiceDependency] CustomersController controller )
{ _controller = controller;
}
...
}
No comments:
Post a Comment