I’ve recently started working on a Win Forms application which needs some improving and restructuring. There are chances that a WPF version of the application is done in the future and I’m considering restructuring the Win Forms app to the MVVM pattern. The purpose is that when the time comes to develop the WPF I would only have to focus on creating WPF views and reuse the code from the previous version.
I’ve looked at a few posts on developing a Win forms application using the MVVP pattern and it does seem possible to develop Win Forms application using the pattern.
I’d like to know if the approach is advisable and if restructuring an existing Win Forms application to a MVVM pattern would ease the work load when developing a WPF version.
MVVM is the lingua franca of WPF developers because it is well suited to the WPF platform, and WPF was designed to make it easy to build applications using the MVVM pattern (amongst others).
MVVM is a way of creating client applications that leverages core features of the WPF platform, allows for simple unit testing of application functionality, and helps developers and designers work together with less technical difficulties.
This is not a good idea in my mind. Because a really big part why MVVM is so good in WPF, is the WPF bindings infrastucture. WinForms doesn't have such abilities, binding support is way lower. That is why WinForms use MVP pattern, where Presenter is responsible for providing data to the view. You can try writing purely MVVM models, but then you'll have to write quite a lot of ugly code inside your views in order to bind to the models and update them. I would suggest stick to MVP, as this is quite a good aproach for WinForms, it creates well structured components, and then it is easy to recreate the same functionality in WPF and MVVM.
As per other respondents the problem here is the presentation layer, which WinForms doesn't handle as easily as WPF.
That said, based on my experience, many legacy WinForm applications have a lot of business and data layer logic in the forms code.
I see no reason why you shouldn't ensure that the code is separated out into distinct data layer, business logic and GUI in preparation to a possible move to WPF.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With