Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View and ViewModel getting too large

Tags:

mvvm

wpf

While adding extra functionality to the main view I have in my application, I've realized that the amount of code will soon become a problem (currently around 600 lines of code in my viewmodel and I still have plenty to add).

I've been looking around for articles on how to split/design your view into smaller components, but I haven't found a satisfying solution. One person suggested using child viewmodels but that presented with other problems (dependency between the viewmodels).

I've thought of using user controls, but there is nothing on the View that I use on other Views so it kind of defeats the purpose of user controls.

What is a correct approach in this situation?

Thanks, Adrian

like image 509
Adrian Avatar asked Jun 06 '12 08:06

Adrian


2 Answers

If you want to split a view into component parts, then you need to do view composition. If you are building an MVVM app, then you should really be using an MVVM framework. Something like Caliburn.Micro makes view composition incredibly easy.

There doesn't necessarily have to be dependencies between the view models, they should only know about what they need in order to produce their view. This could be a subset of the business object that the parent view model contains. As the parent view model will have references to all of the child view models, it can pass the relevant parts of the business object to them at the point of their construction.

like image 92
devdigital Avatar answered Sep 23 '22 14:09

devdigital


I would agree with using Caliburn Micro.

However, to play devil's advocate you could split out your ViewModel File into separate files (same class name) and use the partial keyword before the class keyword. Its generally tidier and one step-away (non-breaking precursor) from breaking-up into separate classes.

like image 26
Meirion Hughes Avatar answered Sep 19 '22 14:09

Meirion Hughes