Why Cant a WorkItem Implement Services

Topics: CAB & Smart Client Software Factory
Aug 9, 2006 at 3:00 AM
originally posted by: mamu300B

Hi all!

I'm implementing a WorkItem which in turn implements 2 service interfaces by itself.
Now I'm trying to register these services by

this.Services.Add<IContentService>(this);
this.Services.Add<INavigationService>(this);

in the OnInitialized() of the WorkItem.

But the second call to Add() doesn't work! Why can't I add any instance to the Service collection?

BTW: I want to do this is because I want to replace a service implementation with another, more local one. And all SmartParts or controllers my WorkItem creates will get the more local service injected.

Many thanks in advance
-Matthias
Aug 10, 2006 at 2:27 AM
originally posted by: mamu300B

now I had a closer look at the ServiceCollection and what the method Build() does.
And it seems to me that the actual implementation with the lines

if (serviceInstance == null)
serviceInstance = BuildFirstTimeItem(typeToBuild, typeToRegisterAs, null);
else if (!container.Contains(serviceInstance))
serviceInstance = BuildFirstTimeItem(typeToBuild, typeToRegisterAs, serviceInstance);
else
BuildRepeatedItem(typeToRegisterAs, serviceInstance);

is not the right solution for handling all build situations.
E.g. it can not handle the scenario when a WorkItem will add itself as a service.

I would suggest to change the code as follows:

if (serviceInstance == null)
serviceInstance = BuildFirstTimeItem(typeToBuild, typeToRegisterAs, null);
else if (!container.Contains(serviceInstance) && !this.ContainsLocal(typeToBuild))
serviceInstance = BuildFirstTimeItem(typeToBuild, typeToRegisterAs, serviceInstance);
else
BuildRepeatedItem(typeToRegisterAs, serviceInstance);

Regards
-Matthias