1
Vote

Even if I register a class as a singleton in WinRT, a new instance is created every time

description

I have a Metro style MVVM application and I've used MetroSliver to resolve view models and dependancies.
The problem is that, even if I've registered a ViewModel a singleton, a new instance is returned every time the page connected to that view model is displayed.

Here is my scenario:
  • I open the application: the MainViewModel (connected to the main page) is invoked and the constructor is invoked.
  • I go into a detail page
  • I go back into the main page: the MainViewModel constructor is invoked again, while it shouldn't happen since I've registered MainViewModel as a singleton

comments

jbillmann wrote Jun 21, 2012 at 2:01 AM

I don't think I completely understand your issue.

Just to be clear, scopes (such as singleton) are only available to interface/implementation mappings.

For example:

ioc.Map().ToSingletonScope();

Anytime a request to the MicroSliver IoC is made that requires type ISingletonContract, the same object instance of type SingletonConcrete would be returned.

MicroSliver does not currently support 'self' mapping and scopes. In other words, you can not apply scope to an implementation without a mapping. If you're view model was being injected, you would see the same instance of those objects should you have applied a scope (singleton) to those mappings.

If this is your problem, let me know. It's definitely something I can consider for the next release.

jbillmann wrote Jul 10, 2012 at 2:05 PM

Please see this discussion on singletons in WinRT: http://microsliver.codeplex.com/discussions/361452