OnBuiltUp getting called twice for me service

Topics: CAB & Smart Client Software Factory
May 2, 2006 at 10:12 AM
originally posted by: sklett

This may be a dumb question, but I just noticed that my service class seems to be built by the ObjectBuilder twice. I have the Service attribute. I remember reading in the docs (I think) that the ObjectBuilder will use the Singleton strategy on a service class. So, I'm wondering, is this normal behavior? I do in fact need my service to be a Singleton, is there something else that I need to do?

I'm using ServiceDependency in my presenters to get a reference to the service. I tried to step through the building process, but to be honest I had a hard time watching what was building what.

Any suggestions? Do I need to give more info?

Thanks for any help,
Steve
May 2, 2006 at 12:16 PM
originally posted by: BradWilsonMSFT

The Service attribute does not guarantee singleton behavior. However, ServiceDependency should not be causing extra objects to be created. Can you provide a simple repro?
May 2, 2006 at 4:05 PM
originally posted by: sklett

I just made a simple class:
Service
public class SingletonTest : IBuilderAware
{
void IBuilderAware.OnBuiltUp(string id)
{
OnBuiltUp(id);
}
void IBuilderAware.OnTearingDown()
{
OnTearingDown();
}

protected virtual void OnBuiltUp(string id){}
protected virtual void OnTearingDown(){}
}

I then added this service in my ModuleController class:
SingletonTest singletonTest =
WorkItem.Services.AddNew<SingletonTest, SingletonTest>();

I then added a dependency in one of my presenters:
protected SingletonTest _singletonTest = null;
ServiceDependency
public SingletonTest SingletTest
{
set { _singletonTest = value; }
get { return _singletonTest; }
}


This resulted in the following execution path:
1) OnBuiltUp
2) ModuleController.Services.AddNew<>
3) OnBuiltUp
4) Presenter DI property


(I hope that makes sense)
So what I thought was a singleton indeed is not. I added a member variable and initialized it to 333, then I assigned that value to 111 in the OnBuiltUp method. On the second call to OnBuiltUp the test variable was back to 333.

Am I doing something wrong?
May 2, 2006 at 4:31 PM
originally posted by: matiaswoloski

Hi,

You are adding the service twice. When CAB loads a module, it looks for all the Service decorated classes in the assembly module. If it finds any, it will do RootWorkItem.Services.Add.
Then you are adding the service again, now in the child workitem (ControlledWorkItem<ModuleController>)
SingletonTest singletonTest =
WorkItem.Services.AddNew<SingletonTest, SingletonTest>();

So, either you use the Service attribute or use the AddNew in the module. Using Service will scope the service globally, using AddNew inside of the ModuleController will scope the service to the Module.

Matias
http://staff.southworks.net/blogs/matiaswoloski
May 2, 2006 at 5:13 PM
originally posted by: sklett

Hi Matias,

Thanks for the suggestion, that was the exact problem. I should have known that I think...

Everything is working well now!

Have a good night,
Steve