MVVM for winforms [duplicate]

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?


Solution 1:

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!

Solution 2:

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.