开发者

MVVM: One ViewModel structure for all Views vs. separate ViewModel structure per View?

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

My开发者_JAVA百科 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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜