Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVVM ViewModel lots of properties

I am new in MVVM and I am developping an application. I have a form view with a lot of property. About 50. I can not seperate these into usercontrol because I would break the mvvm principles.

I can not seperate these into model, because these contains logic. PropertyChange, Error change and these would not be poco classes, and these are not the model.

Would it be nice If I kept 60 property in a same viewmodel?

Do I think it wrong? How would you organize these?

like image 222
Lajos Avatar asked Jun 17 '13 09:06

Lajos


People also ask

Can a ViewModel have multiple views?

While a view should only have one viewmodel, a single viewmodel might be used by multiple views (imagine a wizard, for example, that has three views but all bind to the same viewmodel that drives the process).

Should ViewModels contain logic?

Short answer, Yes. The main aims of the Model-View-ViewModel (MVVM) pattern are: Permit unit testing of your view logic. These are unit tests applied to the ViewModel layer which is executed without a View associated with it.

What should a ViewModel contain?

The viewmodel may expose the model directly, or properties related to the model, for data-binding. The viewmodel can contain interfaces to services, configuration data, etc in order to fetch and manipulate the properties it exposes to the view.

Should ViewModel know about view?

In fact - the ViewModel shouldn't care about the View at all. It should simply make data available through properties, and it's up to the View to decide what it will dynamically bind to in the ViewModels. If the ViewModel wants to tell the View something this should occur implicit through Bindings.


1 Answers

I can not seperate these into usercontrol because I would break the mvvm principles.

I'm not sure what you mean by this. Essentially you'll want to use view composition and break down the view model and views into constiuent parts.

A view is a WPF UserControl (or Window), so if you're using MVVM then you're using UserControl's, it's just conceptually they are considered as views in the pattern.

I would also recommend that you use an MVVM framework if you're using the MVVM pattern, and something like Caliburn.Micro makes view composition incredibly easy.

I would also not recommend using dependency properties for view models, use INotifyPropertyChanged instead.

Most MVVM frameworks provide a base view model type which includes a lambda based method to invoke the PropertyChanged event, thus aiding refactoring.

like image 99
devdigital Avatar answered Sep 21 '22 18:09

devdigital