Smart Client Software Factory: Registering and Retrieving Services
In this post I will try to sketch an explanation about different ways of registering and retrieving services in SCSF / CAB.
How To: Registering Services
We have two ways of implementing the service registration.
-
Using the [Service] attribute
This attribute indicates that a class should be automatically registered as a service into the application's root WorkItem
Usage:
public interface IMyService
{
int GetSomeData();
}[Service(
typeof(IMyService))]
public class MyService : IMyService
{
public int GetSomeData()
{
return 1;
}
}With this code CAB creates an instance of this class and registers it as service during application startup. The constructor parameter tells CAB the interface key to use for the registration of the service (for location purposes).
Using WorkItem.Services.Add()
If you want to register a service programmatically, you have to call the Add or the AddNew method of the services collection of the WorkItem within you want to use the service.Usage:
RootWorkItem.Services.Add(myServiceInstance); RootWorkItem.Services.AddNew();
RootWorkItem.Services.AddNew();
How To: Retrieving Services
Now that we have registered our services, we will want to know how to retrieve them. Let's see the two ways of doing that.
-
Using the [ServiceDependency] attribute
This attribute indicates that property or parameter is a dependency on a service and should be injected when the object is added to a WorkItem.Usage:
private IMyService myService; [ServiceDependency]
public IMyService MyService
{
set { myService = value; }
}With this code, we are telling CAB that we are having a dependency on the IMyService that has already been created and is available to use. For this purpose CAB uses Inversion of Control (IOC) / Dependency Injection (DI).
So when the object is added to the Workitem, the property will be automagically injected with the correct service instance. - Using WorkItem.Services.Get()
This way is more “on-demand” than ServiceDependency.
Imagine that we have a second service AnotherService which implements IAnotherService interface and have the Service Attribute.
In our view, we are not going to have a property with the service, so we are not going to apply the ServiceDependency attribute.
Usage: (for example in a method)
public void MyMethod()
{
IAnotherService mySecondService = rootWorkItem.Services.Get();
// …
}When you use the ServiceDependency attribute, CAB is calling Services.GET behind the scenes.
I hope people having doubts about services in SCSF/CAB find this post useful.
Hi, I’m having a problem registering a SCSF service created with PIAB
http://www.codeplex.com/smartclient/Thread/View.aspx?ThreadId=10186
any Tips, Thanks
JPS
i Have Two views Named as Redview And Blue View both are shown in the single form at the Top and the bottom Possion simulatnously .
Redview Contains the two textboxes and a button controll numberX, NumberY (textbox), and “ShowAddition “(Button) when Input In the Textboxes and click on to the button show Result , its fires an event (click)and go to the Blue View witch contain the text box(txtResult) and button (btnResume_result) and shows result of the upper inputs Addition when blue views buttons clicked than buttons clicked event should automatically clear the Redviews textBox
How I Do this by Using services