Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does MVVM violate DRY?

It seems that ViewModels that I make look suspiciously like other classes and they seem to require a lot of code repetition, e.g. in a current project I have:

  • SmartForm: Model that represents a data form to fill in, has properties:
    • IdCode
    • Title
    • Description
    • collection of SmartFormFields
    • etc.
  • SmartFormControlView View
  • SmartFormControlViewModel ViewModel
    • IdCode
    • Title
    • Description
    • collection of SmartFormFields
    • etc.

So my ViewModel is basically the same as my Model, just with all the OnPropertyChanged features for binding with the View.

It seems as I refactor and extend this that every little change I make to my model, I have to make a mirror change to the ViewModel.

This seems to violate a basic rule of patterns Don't Repeat Yourself.

Am I implementing the MVVM pattern incorrectly or is it just an inherent characteristic of MVVM that there is always a 1-to-1 repetition going on between Model and ViewModel?

like image 845
Edward Tanguay Avatar asked Jun 08 '09 08:06

Edward Tanguay


People also ask

Is MVVM slow?

MVVM Done Right is Slow In a large application, you might need to loop through the data multiple times to make sure it has all recalculated correctly. If you just use the framework and let the framework deal with your sloppy code, this can make the system incredibly slow.

Should I always use MVVM?

For trivial projects MVVM is unnecessary. Using only the View is sufficient. For simple projects, the ViewModel/Model split may be unnecessary, and just using a Model and a View is good enough. Model and ViewModel do not need to exist from the start and can be introduced when they are needed.

What is the main purpose of MVVM Architecture?

Rationale. MVVM was designed to remove virtually all GUI code ("code-behind") from the view layer, by using data binding functions in WPF (Windows Presentation Foundation) to better facilitate the separation of view layer development from the rest of the pattern.

What is the use of ViewModel in MVVM?

The ViewModel class is designed to store and manage UI-related data in a lifecycle conscious way. The ViewModel class allows data to survive configuration changes such as screen rotations.


1 Answers

I personally don't think it violates DRY since the model and view-model (I prefer the term presenter) don't point to the same information. For instance your VM and M both have a Title property, but your VM's Title property could also include validation, whereas your model's Title property could assume validity.

While it's true that The VM may contain all of the properties of the model, there is also the possibility of having validations (e.g. Title must be non-blank), data-dependencies, bindable UI-specific properties (icons, colors, brushes, etc.) which aren't part of the view.

Essentially all UI patterns have similar "duplication" in the way you state it: namely cascading modifications. Try changing a model in MVC without changing the controller.

That being said, MVVM (or any UI pattern designed to separate UI, logic, and state) can be overly tedious for simple cases such as your example. When logic becomes little more than state-pass through, the value separating the controller/presenter/view-model decreases.

In your specific case, if there really isn't any logic, validation, or UI specific properties that your VM isn't surfacing, and your model doesn't have to be persisted, serialized, or made backwards compatible with an existing structure (or adding the logic to do so in your VM is trivial), I would strongly consider combining the M and VM to avoid creating properties whose sole purpose is to get/set the underlying model's properties.

like image 109
micahtan Avatar answered Oct 02 '22 15:10

micahtan