MVVM for winforms [duplicate]

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!

Leave a Comment