Restructure Win Forms Application to MVVP Template - Possible Future WPF Version

I recently started working on a Win Forms application that needs to be improved and restructured. There is a chance that the version of the WPF application will be executed in the future, and I will think about how to rebuild the Win Forms application into the MVVM template. The goal is that when the time comes to develop WPF, I will need to focus on creating WPF views and reusing code from a previous version.
Ive reviewed a few posts about developing a Win forms application using the MVVP pattern, and it seems you can develop a Win Forms application using the pattern.
I would like to know if the approach is appropriate and if restructuring an existing Win Forms application into an MVVM template will ease the workload when developing a version of WPF.

+4
source share
4 answers

This is not a good idea. Since the really big part of why MVVM is so good in WPF is the WPF binding framework. WinForms does not have such features, the support binding is below. This is why WinForms uses the MVP template, where Presenter is responsible for providing data for presentation. You can try to write only MVVM models, but then you will have to write quite a lot of ugly code inside your views to bind to models and update them. I would recommend sticking with MVP, as this is a pretty good approach for WinForms, it creates well-structured components, and then it’s easy to recreate the same functions in WPF and MVVM.

+4
source

Like other respondents, the problem here is the presentation level, which WinForms does not handle as easily as WPF.

However, based on my experience, many legacy WinForm applications have a lot of business data logic and data tier in form code.

I see no reason why you should not guarantee that the code will be divided into a separate data layer, business logic and graphical interface in preparation for a possible transition to WPF.

+3
source

A few months ago I wrote a small winforms application with something like MVVM, just for fun. I used my own bindings ... The binding code was placed in codebehind and looked like this:

this.Bind(src: x => x.ViewModel.SearchCriteria, dst: x => x.txtSearchCriteria.Text, mode: BindingMode.TwoWay); 

btw, I have not finished this application, but it was interesting to try :)

Summarizing:
1) is it possible? β€œYes, I tried to do it, and it worked.”
2) is it difficult to implement? - No, not very difficult.
3) do I suggest trying this in a real project? - No, it's better to use MVP instead.

+2
source

As Vladimir said, you can probably reverse engineer the Winforms project to use the MVVM template. But the question is, is it worth it?
You have to literally reinvent the wheel and wrap Winform controls in classes that support WPF binding.
Imagine that you are developing a WPF application and putting the whole business in code. It is doable, but it is not, IMO, and it is certainly ugly.

I would share my data access level so that I can use it in future WPF projects. But I would try to document the business in the application, rather than porting it to MVVM, in order to simplify the creation of a WPF application from scratch.

+2
source

Source: https://habr.com/ru/post/1383046/


All Articles