[WinRT] ViewModels Singleton

Topics: Conventions
Oct 23, 2012 at 12:13 AM

I would like to know if the viewmodels are singleton in Caliburn?  Also When you navigate to a viewmodel using the Navigation service, does it create a new viewmodel everytime?  I'm not sure I understand this part.


Oct 24, 2012 at 7:23 AM

As far as i understand, it depends on how you register it in a container: Singlton<> or PerRequest<>

Oct 25, 2012 at 2:54 AM

This is governed by a couple of things, first as kalinovsky mentioned the IOC container and how it's configured. If you've registered your view model as Singleton then it will only be created once, if you'e registered it as PerRequest then one will be created each time you navigate to the page. 

There's one gotcha in all of this, the Frame control has support for caching views. If you've done Silverlight work before you may be familiar with it's Frame control and how it works, I understand the WinRT Frame control acts in a similar manner, however if you've done a lot of Windows Phone and are used to the way views are reused there then the other way is just damn infuriating.

In short you can set NavigationCacheMode="Enabled" on your Page, the Frame control will then gladly cache that view (and its view model) for reuse later on. This means if you navigate away from a cache enabled page and navigate back to it then the view model won't be recreated (even if you have it as PerRequest). Sounds good in theory, however the caching doesn't care about things like the parameter used to navigate to the page nor the direction of the navigation. This means even forward navigation to a page will use a previously cached version. This really hits home when you have something like a "view product" page where it will use the same view and view model for every product. I'd recommend being very careful with using navigation caching. If you don't care about that then ignore the previous two paragraphs :-)

Oct 25, 2012 at 4:44 AM

That's interesting, we are building an LOB where accurate information is important, so caching is not ideal, but loading the data from the database every time you navigate to and from a page that seems slooow.   Especially if you have not yet committed data on that page and you navigate to another one, that means you would loose that data...

Is there anything else we can do?  

Oct 27, 2012 at 11:58 PM

Agreed that it's not ideal. I'm much prefer the view caching behavior exhibited by the Windows Phone frame.

I've tended to go with some sort of caching behavior, both of uncommitted changes, if really accurate data is necessary then small cache timeouts could be one answer. You'll need to tweak any strategies dependent upon your requirements.

Oct 29, 2012 at 3:41 AM

Actually by looking at other apps, we noticed a pattern, there is alway a view page with navigation, then an edit page without the navigation, so basically, leaf are editable and node are not.  We may have some kind of input in the node, but those would be commited to the database as entered.

Mar 13, 2013 at 11:26 PM

Here is a database compatible with WinRT: