SCSF Services

Topics: CAB & Smart Client Software Factory, Jobs & Opportunities
Mar 8, 2007 at 1:30 PM
Hi again.

Someone has a good reading about services in SCSF/CAB???

Basically I want to resolve my doubts betwee Service attribute, ServiceDependency attribute, WorkItem.Services.Get<TService>() and WorkItem.Services.Add<TService>(). And when should I use each of them...

Thanks everybody!
Developer
Mar 9, 2007 at 12:23 AM
Hi Rotten, take a look at the CAB/SCSF help (topic ServiceAttribute, ServiceDependencyAttribute) and the hands on lab (CAB Lab 01 - Introduction).

Anyway, let me try to sketch an answer:


Service Attribute

This attribute indicates that a class should be automatically registered as a service into the application's root WorkItem.

Usage:

public interface IService
{
    int GetSomeData();
}

Service(typeof(IService))
public class MyService : IService
{
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)


WorkItem.Services.Add<TService>()

Another way to register a service is programmatically. To do this you 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<MyService>(myServiceInstance); 

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 (for example in a view):

private IService myService;

ServiceDependency
public IService MyService
{
set { myService = value; }
}

With this code, we are telling CAB that we are having a dependency on the IService 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 view is added to the WorkItem, the property will automagically injected with the correct service instance.


WorkItem.Services.Get<TService>()

This is another way to retrieve a service. It is more “on-demand” than ServiceDepdency.

Imagine that you 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<IAnotherService>();
    // … 
}
When you use the ServiceDependency Attribute, CAB is calling Services.Get behind the scenes


Please let me know if this helps, but remember to check the documentation.

Cheers!

Ezequiel Jadib
http://staff.southworks.net/blogs/ejadib
Mar 9, 2007 at 7:10 AM
Thans a lot for your answer. I've already read the documents you told me, but I have not very clear conceps. With your answer and comments I'm understanding a bit more these complex.

My problem is when Services are in different modules, and, I think, I have the problem with the order creation because with Service and ServiceDependency attributes, some times I get the "Service not available at this context" error.

thanks again for your help.
Mar 9, 2007 at 10:04 AM
You have to add the service to the RootWorkItem.Services collection, than it can be found by all loaded modules.

this.WorkItem.RootWorkItem.Services.AddNew<MyService, IMyService>();

Also make sure that the modules that provide a service loaded before the module that use that service.
Mar 9, 2007 at 11:42 AM
Yes, but if I put the ServiceAttribute to a class instead Services.AddNew(), it will be added to RootWorkItem.Services collections, won't it??

Regards!