Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Service Locator

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.

Typically, an application contains multiple WorkItems and services. Any WorkItem can contain only a single instance of a service type, but instances of the same service type can be placed in multiple WorkItems. You want to use instances of services from multiple WorkItems. You also want the ability to override instances of a service type in a WorkItem. Each WorkItem requires the ability to obtain the correct instance of a service from its current location in the WorkItem hierarchy.

The WorkItem attempts to locate the service in its own collection of services. If it cannot locate the service type you requested, it asks its parent WorkItem to locate the service. This continues until the service is found or the root WorkItem is reached.

In the Composite UI Application Block, the Locator object has the responsibility for locating services in WorkItems or their ancestors. Each WorkItem holds an instance of the Locator object, which is created at the time the work item is initialized. If the WorkItem has a parent WorkItem, the Locator object for the child WorkItem contains a reference to the Locator object of the parent (the Locator objects are connected in their inheritance hierarchy).

The Locator object contains references to the services in the WorkItem. The following line of code returns an instance of the IMyService service. This instance can be from the myWorkItemWorkItem or one of its ancestors. The call to myWorkItem.Sevices.Get<service interface> is handled by the Locator object associated with the myWorkItemWorkItem.

IMyService service = myWorkItem.Services.Get<IMyService>();  

To add a service instance to a WorkItem, you can either explicitly create an instance and add it to the WorkItem or instruct ObjectBuilder to create the instance for you.

The following code explicitly creates a service instance.

MyService service = new MyService(); 

In the following code, ObjectBuilder constructs the instance of the service.

myWorkItem.Service.AddNew<MyService, IMyService>(); 

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft. All rights reserved.