Avoid MVVM / data binding for small windows?

I found the view models very useful for separating the user interface and the business logic code. However, I also made the experience that for small simple windows, the view model class simply adds a lot of extra code, effectively reducing maintainability, rather than improving it.

Let me give you a simple, real-life example that I had recently: a dialog box containing two buttons (fixed text) and a custom text block.

  • Using a simple old method of programming code based on x:Name , this is a question of several lines of code (set the text, process two button presses, returning the value and closing the window) - is clean and simple.

  • The implementation of this "recommended method", i.e. creating a view model class (implementing INotifyPropertyChanged or inheriting DependencyObject) and defining commands requires a very large amount of code compared to the solution above (especially since there is no short way to define "local variable + property + increase the value of PropertyChanged" in VB) - make the solution less readable and more error prone.

So, currently I'm using a pragmatic approach to choose between the “plain old x: name” and the look model in each case. However, the wealth of blog / forum posts that claim that the view model should be used all the time makes me wonder if I have missed something.

Did I miss something?

+4
source share
5 answers

I'm doing the same thing.

Small dialogs and quick choices or informational messages are simply done in the simplest way. I try not to overload things with patterns that are not needed. The main application windows and more, as a rule, are executed using MVVM.

(So ​​either you missed nothing, or I missed the same thing.)

+1
source

I am all for MVVM, but the window you are describing can be implemented once and then filled as needed when you want to open it. Thus, the ViewModel might look like this:

 public SmallDialogTask { string Title { get; set; } string Text { get; set; } string AcceptButtonLabel { get; set; } string RejectButtonLabel { get; set; } Command AcceptCommand { get; } Command RejectCommand { get; } } 

Deploy it once and use inheritance to set up the various instances you need. As you can see from my example, this allows you to encode a lot more semantics into your class.

See also DRY , my question about the MVVM dialog box, and - just for reference - my blog post about MVVM .

+4
source

You can use data binding without defining a view model, and in most "small" windows there is no mush logic, so you do not need to be notified of changes.

Just set DataContext = this and bind data to window class properties.

Commands, on the other hand, do not have a good simple version, so I just use a good old event handling.

(I also think that MVVM is too much for many small windows, but I'm obviously in the minority on this)

+3
source

I do something similar, however, in situations where I just want a very small window that makes something very simple, I at least implement the interface and call it through the gateway so that I can call it in a test way. Something like that:

//eg. in viewmodel.cs or command.cs

 var sometextIwantUserToEnter = UIServices.GetMeSomethingThatCan().GetText(); 

IGetText will be implemented in my window, and I will do all the window display and verification of the results in the window itself in the GetText method. This makes everything isolated in the window, and I can argue that the service was called in my tests.

Hope that made sense.

+1
source

You do not need to implement INotifyPropertyChanged in the view model class. You only need to do this if you need a notification of a change that you do not have for simple dialogs.

I can’t talk about problems with VB-specifics, but in C # the code that is the result is at least simple and concise, as if it lived in window code, and often more often if you try to implement command behavior without implementation teams. (“I just set IsEnabled to true in this property installer so that the OK button is activated after entering the data,” is one of those sentences where the word “just” turns out to be a hell of a lie.)

The counterargument to this - “of course, but if I do not implement a change notification, then I can’t do X and Y when something changes the value of the property” - undermines the statement that what you are building is simple.

I find that in general, nothing is as easy as I think it will happen when I start it over. It is much easier to add change notifications and commands to the view model class than to refactor the view model class from the window code so that I can add change notifications and commands to it.

Finally, if you use simple view model classes to return simple views in all but one case, this case will bite you (or another developer) someday. There is much to be said about consistency if the cost of consistency is low.

+1
source

All Articles