views:

58

answers:

2

Hi

I'm new to MVVM and need a bit of help.

My application consists of a number of different windows which display controls allowing the user to edit the data in the Business layer.

At the moment, each time the user opens a new instance of one of these windows, a ViewModel structure - classes and collections mirroring the Business layer - is created from scratch and databound to the View on the window, and is also configured to access the appropriate parts of the Business layer.

This is quite sluggush at the moment, and I have a suspicion it is because the application has to wait until all the new ViewModels are created and configured every time a window is opened. I also notice the RAM gets munched up quite quickly too.

Would it be better to have a single ViewModel structure which is created when the application starts, and then all windows and controls refer to this single instance? What is the convention for MVVM?

+1  A: 

One ViewModel per view is pretty standard. You don't want to share instances of ViewModels, since they are (usually) stateful.

I would look deeper into the sluggishness before concluding it's the creation of the ViewModel that's causing it. Profile the application with a tool, set some stopwatches, or debug the app and see what the bottleneck is.

Phil Sandler
I've had a look into this with stopwatches, the VM creation code only takes 100 ms to complete, but the Window.Show() method is taking >1000 ms. So for now I'll stick with the on-demand VM and look into why the window and its controls take a while to Show..
Caustix
A: 

do you need to recreate your viewmodels every time you access your views?

if not it seems you use view first approach, maybe you should then use a viewmodel locator?

you can also take a look at viewmodel first approach, maybe this fits more in your application.

blindmeis