Problem with singletons

Jun 29, 2012 at 8:49 PM

I'm having a problem with some of my singletons, I see the constructor in the classes is being called on every request.

I'm using a MVVM pattern in Win 8 Metro.  I've registered my classes with the IoC container in the constructor of my VML.

Do I have to use Interfaces when registering singletons? 
Are parameterized constructors ok for singletons?

 

Coordinator
Jul 2, 2012 at 2:57 PM
Edited Jul 2, 2012 at 2:58 PM

The life cycle functionality (such as Singleton) in MicroSliver is limited to interface & implementation mappings:

 

ioc.Map<ISingletonContract, SingletonConcrete>().ToSingletonScope();

...

public MyViewModel(ISingletonContract singletonContract)
{
      ...
}

 

Any time the type ISingletonContract is requested, a reference to the same instance of SingletonConcrete will be returned.  MicroSliver does not currently support 'self' mapping and life cycles.  You can not apply a life cycle or scope to an implementation without a mapping.  In the example above, a request for MyViewModel is made to the VML and subsequently the IoC container is invoked to fetch an instance of MyViewModel which will have the same reference of type ISingletonContracted injected each time MyViewModel is requested.  If, for example, you wanted to make MyViewModel a singleton, you would have to add logic to control that in your VML.

Paramaterized constructors are OK for all objects invoked via MicroSliver regardless of life cycle / scope except for primitive or value type parameters as there exists no functionality to determine what they should be set to.

Does this help?

Jul 2, 2012 at 3:46 PM

Thanks,

Switching to an interface fixed the problem.