Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVVM for winforms [duplicate]

Tags:

winforms

mvvm

Possible Duplicate:
UI Design Pattern for Windows Forms (like MVVM for WPF)

Should MVVM be used for WinForms? If so, what is the advantage over using MVP?

like image 495
SA. Avatar asked Jun 11 '09 19:06

SA.


People also ask

Can we use MVVM in WinForms?

The DevExpress MVVM Framework allows you to utilize the Model-View-ViewModel design pattern in WinForms applications.

Is MVVM deprecated?

This is the same MVVM library used by the Microsoft Store, the Photos app, and more! The MVVM Toolkit is inspired by MvvmLight, and is also the official replacement for it now that the library has been deprecated.

Is MVVM faster?

Execution time in MVVM applications is faster with an average difference of 126.21 ms, while memory usage in MVP applications is lower with an average difference of 0.92 Mb.

Do I have to use MVVM in WPF?

The Windows Presentation Framework (WPF) takes full advantage of the Model-View-ViewModel (MVVM) pattern. Though it is possible to create WPF applications without using the MVVM pattern, a little investment in learning can make building WPF applications much simpler.


2 Answers

I think that there are two answers here... really just one answer to "Should I" and one answer to "Could I".

As far as "Could I", it is certainly possible. MVVM really just relies on a view that can bind to a view model. Since WinForms supports binding, this certainly is possible. You may need to write some code to make that binding more useful in an MVVM world, but it is (at least) theoretically possible. If it worked well, the benefits would be pretty great, IMO. You could make sure that your WinForms "View" had no UI behavior, except for creating the visual objects and binding them (in code, not declarative like in XAML). WinForms objects are very difficult to test, where ViewModels are very easy to test.

As far as your real question: "Should I", that becomes much more of a project-level decision. What are your goals? If you are looking to make some rather complex UI logic testable, then you might at least look into it. Fortunately, though, there are other patterns (Model-View-Presenter, for instance) that have more community backing that also has you write a testable "presenter" class. I find ViewModels significantly easier to write unit tests against compared to Presenters, but I think that is a personal preference.

Just as an aside, the MVVM pattern is mostly another name for the "Presenter Model" pattern. You might look to see if anyone is having success with the "Presenter Model" against WinForms UIs.

Good luck!

like image 115
Brian Genisio Avatar answered Oct 25 '22 21:10

Brian Genisio


The Model-View-ViewModel (MVVM) Pattern is a design pattern. Per definition a design pattern shows a common solution in the object-oriented world and this solution can be applied in various platforms (WPF, WinForms, Java Swing, etc.). I agree that MVVM is best used with WPF because it leverages the strong binding capabilities. However, Windows Forms supports data binding as well.

The WAF Windows Forms Adapter shows how to apply the MVVM Pattern in a Windows Forms application.

like image 35
jbe Avatar answered Oct 25 '22 23:10

jbe