Services utilizing other services

Topics: CAB & Smart Client Software Factory
Mar 28, 2007 at 12:14 AM
I just ran into a little design problem. I have a service that needs access to another service, but since there isn't a WorkItem accessible from within a Service I can't access the Services collection. I can use ServiceDependency on a property but that can be tricky not knowing if the service has actually been created and added to the RootWorkItem yet.

So once again, I'm wondering if I'm missing something or if I'm breaking some basic CAB rule by trying to have Services interact with each other?

Thanks for any input you might have.
-Steve

Coordinator
Mar 28, 2007 at 4:56 AM
That's absolutly doable.

 
public class ServiceOne {
   [InjectionConstructor]
   public ServiceOne( [ServiceDependency] ServiceTwo service ) {
   }
}
 

Then you need to make sure you do add them in order:

WorkItem.Services.AddNew<ServiceTwo>();
WorkItem.Services.AddNew<ServiceOne>();

Thanks
Matias
http://staff.southworks.net/blogs/matiaswoloski
Mar 28, 2007 at 6:29 PM
Hi Matias,

Thank you for the reply. The problem I was running into was loading the Services in the wrong order, I have moved them both to the Infrastructure.Module and everything is working now.

Kind of a stupid mistake ;)
Mar 28, 2007 at 6:41 PM
I ran into this problem and solved it a similiar way. One thing that can mess up developers is using

[Service(typeof(IAccountService), AddOnDemand = false)]
to decorate service class definition. If you do that then CAB (at least in SCSF) will create that service for you. If that service depends on other services then there is no way to determine or force the order of creation.