Walkthrough: root work item service?

Topics: CAB & Smart Client Software Factory
Nov 24, 2005 at 5:56 AM
originally posted by: BlairLeduc

In Stage 4, step 2 of "To get a reference to the WorkItem", the description says:
"Use the ServiceDependency attribute on this property so that the dependency injection feature of the underlying ObjectBuilder utility references the existing WorkItem, and passes back a reference to it"

Now, I can't seem find more information about this. Right now, all I know is if I stick a property in my ModuleInit class of type WorkItem, as I saw in the walkthrough, the root work item will be injected into my ModuleInit class, but I can't answer the question of why this works.

How would I find out what services are available? I don't mean programmically, but rather from a design point of view.

Nov 26, 2005 at 8:44 AM
originally posted by: BradWilsonMSFT

The services that are available will be a combination of the services that CAB provides by default, plus whatever your application chooses to add to the services list.

The CAB default services list can be found in two place:

In CabApplication.AddRequiredServices is the list of services for all application types:

private void AddRequiredServices()
rootWorkItem.Services.AddNew<TraceSourceCatalogService, ITraceSourceCatalogService>();
rootWorkItem.Services.AddNew<WorkItemExtensionService, IWorkItemExtensionService>();
rootWorkItem.Services.AddNew<WorkItemTypeCatalogService, IWorkItemTypeCatalogService>();
rootWorkItem.Services.AddNew<SimpleWorkItemActivationService, IWorkItemActivationService>();
rootWorkItem.Services.AddNew<WindowsPrincipalAuthenticationService, IAuthenticationService>();
rootWorkItem.Services.AddNew<ModuleLoaderService, IModuleLoaderService>();
rootWorkItem.Services.AddNew<FileCatalogModuleEnumerator, IModuleEnumerator>();
rootWorkItem.Services.AddOnDemand<DataProtectionCryptographyService, ICryptographyService>();
rootWorkItem.Services.AddNew<CommandAdapterMapService, ICommandAdapterMapService>();
rootWorkItem.Services.AddNew<UIElementAdapterFactoryCatalog, IUIElementAdapterFactoryCatalog>();

and in WindowsFormsApplication.AddServices, you can find the list of Windows Forms-specific services:

protected override void AddServices()
RootWorkItem.Services.AddNew<ControlActivationService, IControlActivationService>();
Nov 30, 2005 at 5:27 AM
originally posted by: cnathani

In the BankTeller QuickStart, the way services are injected into the SmartParts is by decorating a setter which returns the Service type with the ServiceDependency attribute. So, here it seems like the ObjectBuilder looks at the type of the property to choose which service object should be populated.

But, in the module init class, for injecting the root work item into the constructor, how does it know which service to use. Can you please elaborate on this?

Is there any brief notes available on the default services and how they can be utilized by an app?

Nov 30, 2005 at 5:50 AM
originally posted by: BlairLeduc

Thanks, Brad.

Although, I still don't understand how the ObjectBuilder knows to inject the root or parent work item when it sees a service type of WorkItem. Is there a service for that? Or is there some other mechanism?

Dec 1, 2005 at 10:00 AM
originally posted by: BradWilsonMSFT

An answer to both your questions.

The WorkItem registers itself as a service within itself, registered as type WorkItem. You can use a ServiceDependency to retrieve the WorkItem in which you are contained. In the case of the moduleinit class, those are always contained in the root WorkItem, so injecting for WorkItem gets you the root, always.
Dec 1, 2005 at 11:44 AM
originally posted by: BlairLeduc

OK, I understand now.

The work item registers itself in the InitializeFields() / LocateWorkItem() methods. The moduleinit class is injected when the module is created and added to the root work item's item collection in EnsureModuleClassesExist().

Very cool.

Thanks, Blair.