tags:

views:

1338

answers:

5
+6  Q: 

MVVM for winforms

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

A: 

MVVM Specifically fits the markup + code and lookless model in WPF and silverlight. I would not suggest it to a winforms app as I believe it would be an overkill. I do not see any benefit over MVP in a winforms app. However in WPF and silverlight it is always the preferred than MVP.

Read up on the web what MVVM is and why it came to be. That should clear it up further.

Shafqat Ahmed
A: 

I don't believe MVVM can be done in winforms(at least not without a lot of hacking). MVVM separates the view(your form) from the viewmodel(your logic).

The reason it can be done in WPF is because WPF allows loosely coupling the view from the viewmodel via databinding in xaml. This allows the ViewModel from not knowing anything about the view and still being able to function. This is a good article on MVVM basics, I believe that it will clear up several questions.

Jose
But WinForms does support binding... it is not as elegant as XAML binding, but the support IS there, and I have used it extensively in the past. The WinForms binding mechanism also "allows loosely coupling [between] the view from the viewmodel". I am not saying that MVVM is more fitting with WinForms than other patterns (such as MVP), but to say it can't be done is not quite fair of WinForms.
Brian Genisio
+1  A: 

MVVM was specifically created for WPF, in order to take advantage of WPF features like bindings and commands. Windows Forms doesn't have these features(*), so it doesn't really make sense to try to apply the MVVM pattern to a Windows Forms application... You should probably use MVC or MVP instead.

(*) It actually has some basic support for data binding, but not as powerful as in WPF...

Thomas Levesque
+4  A: 

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!

Brian Genisio
+1  A: 

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.

jbe