Is MVVM pointless?

Is an orthodox MVVM implementation pointless? I am creating a new application and I have been considering Windows Forms and WPF. I chose WPF because it is the future and offers great flexibility. There is less code and it is easier to make significant changes to the user interface using XAML.

Since the choice for WPF is obvious, I thought that I could go all the way using MVVM as my application architecture, as it offers compatibility, separation and testability of blocks. Theoretically, it seems beautiful, like the holy grail of user interface programming. This is a brief adventure; however, turned into a real headache. As expected in practice, I found that Ive been trading one problem for another. I tend to be an obsessive programmer in that I want to do everything right so that I can get the right results and possibly become a better programmer. The MVVM pattern just failed my performance test and just turned into a big hack hack!

An obvious example is the addition of Modal dialog support. The correct way is to set the dialog box and bind it to the view model. It's difficult. To use the MVVM template, you need to distribute the code in several places across all layers of the application. You should also use esoteric programming constructors such as lamba patterns and expressions. Material that makes you look at the screen, scratching your head. It does the maintenance and debugging of a nightmare awaiting what I discovered recently. I had a good job until I got an exception the second time I called it, stating that it would not be able to display the dialog again after closing it. I had to add an event handler for the close function to the dialog box, another one in the IDialogView implementation and finally another one in the IDialogViewModel. I thought MVVM would save us from such an extravagant hacker!

There are several people with competing solutions to this problem, and they are all hacks and do not provide a clean, easily reusable, elegant solution. Most MVVM tools obscure dialog boxes, and when they access them, they are only warning fields that do not require special interfaces or view models.

I plan to abandon the MVVM viewer, at least its orthodox implementation. What do you think? Was that really a problem for you if you had? Am I just an incompetent programmer or is MVVM not what it was fanning?

+85
wpf mvvm
May 09 '10 at 17:08
source share
8 answers

Sorry if my answer got a little long, but don't blame me! Your question is also long.

Thus, MVVM is not meaningless.

A clear example is the addition of modal dialog support. the right way is to set up a dialog box and bind it to the view model. Getting it is hard to work.

Yes this is true. However, MVVM gives you the ability to separate the look of an interface from its logic. No one forces you to use it everywhere, and no one holds a gun against your forehead so that you create a separate ViewModel for everything.

Here is my solution for this specific example:
How the user interface handles a specific input is not one of the ViewModel business models. I would add code to the View.xaml.cs file, which creates a dialog box and sets the same instance of ViewModel (or something else, if necessary) as its DataContext.

To benefit from the MVVM template, you need to distribute the code in several places across all layers of the application. You should also use esoteric programming constructors such as lamba patterns and expressions.

Well, you do not need to use it in several places. Here is how I would decide:

  • Add XAML to the view, and nothing in .xaml.cs
  • Write each application logic (with the exception of things that will work directly with user interface elements) inside the ViewModel
  • All the code that the user interface must execute, but which has nothing to do with business logic, is included in the .xaml.cs files.

I think that the goal of MVVM is primarily to separate the application logic and the specific user interface, which makes it easy to modify (or completely replace) the user interface.
I use the following principle: View can know and assume everything that it wants from the ViewModel, but the ViewModel cannot know ANYTHING about the view.
WPF provides a good binding model that you can use to achieve just that.

(BTW, patterns, and lambda expressions are not esoteric if used correctly, but if you do not want to, do not use them.)

Material that makes you look at the screen, scratching your head.

Yes, I know that feeling. Exactly what I felt when I first saw MVVM. But as soon as you get it, you will no longer feel bad.

I had a window that worked perfectly ...

Why would you put the ViewModel behind the about field? There is no point in this.

Most MVVM tools paint over dialogs and when they access them, these are just warning fields that do not require special interfaces or view models.

Yes, since the fact that the user interface element is in one window or in another window or is an orbiting Mars at the moment is not a problem for ViewModels. Separation of problems

EDIT:

Here is a very good video called Create your own MVVM environment . It is worth a look.

+57
May 09 '10 at 17:27
source share

Getting this to work is difficult. In favor of the MVVM template, you should distribute the code in several places in the layers of your application. You must also use esoteric programming constructs, such as patterns and lambas.

For a regular modal dialog box? You, of course, are doing something wrong - MVVM implementation does not have to be so complicated.

Given that you are new to both MVVM and WPF, it is likely that you use suboptimal solutions everywhere and unnecessarily complicate things - at least I did this when WPF first appeared. Make sure the problem is really MVVM, not your implementation, before you give up.

MVVM, MVC, Document-View, etc. - This is an old family of templates. There are flaws, but there are no fatal flaws that you describe.

+8
May 09 '10 at 18:21
source share

I consider the problem with dialogs by cheating. My MainWindow implements the IWindowServices interface, which provides all application dialogs. My other ViewModels can then import the service interface (I use MEF, but you can just simply pass the interface through the constructors manually) and use it to accomplish what is needed. For example, here is what the interface looks like for a small application of my utility:

//Wrapper interface for dialog functionality to allow for mocking during tests public interface IWindowServices { bool ExecuteNewProject(NewProjectViewModel model); bool ExecuteImportSymbols(ImportSymbolsViewModel model); bool ExecuteOpenDialog(OpenFileDialog dialog); bool ExecuteSaveDialog(SaveFileDialog dialog); bool ExecuteWarningConfirmation(string text, string caption); void ExitApplication(); } 

This results in all Dialog actions being performed in one place, and this can be easily removed for unit testing. I follow the template that the dialog client needs to create an appropriate ViewModel, which can then be customized as needed. Execute call blocks and the subsequent client can view the contents of the ViewModel to see the Dialog results.

A cleaner MVVM design may be important for a large application where you need cleaner isolation and a more complex composition, but for small and medium sized applications, I think that a practical approach with appropriate services to identify the necessary hooks is enough.

+5
May 09 '10 at 18:25
source share

Design patterns will help you, not hinder you. A small part of a good developer is knowing when to "break the rules." If MVVM is cumbersome for the task, and you have determined that future value is not worth the effort, then do not use the template. For example, as other posters commented, why would you go through all the overhead to realize a simple one about the box?

Design patterns never suggested that they would be dogmatically followed.

+5
Aug 04 2018-11-11T00:
source share

I am in the middle of a rather complicated MVVM development using PRISM, so I already had to deal with such problems.

My personal findings:

MVVM vs MVC / PopUps and co

  • MVVM is a really great template, and in most cases it completely replaces MVC due to the powerful data binding in WPF
  • Calling a service level directly from the master is a legitimate implementation in most cases.
  • Even fairly complex List / Detail scripts can be implemented with pure MVVM thanks to the {Binding Path = /} syntax
  • However, when complex coordination is required between multiple views, the controller is required
  • You can use events; The old template, which implies storing instances of IView (or AbstractObserver) in the controller, is deprecated
  • A controller can be introduced into each presenter by an IOC container
  • Prisms The IEventAggregator service is another possible solution if the only use of the controller is event scheduling (in which case it can completely replace the controller).
  • If views are to be dynamically created, this is a very suitable task for the controller (the controller will inject an (IOC) IRegionManager in the prism)
  • Modal dialog boxes in most consolidated applications are out of date in most cases, with the exception of truly blocking operations, such as mandatory confirmations; in these cases, modal activation can be abstracted as a service called inside the controller and implemented by a specialized class, which also allows for advanced level testing at the presentation level. The controller, for example, will call IConfirmationService.RequestConfirmation ("you are sure"), which will cause the display of a modal dialog at run time and can be easily ridiculed during unit testing.
+3
Mar 08 '11 at 17:21
source share

Like the MVVM template itself. But the WPF management library that comes with support for NET 4.0 data binding is very limited, it is much better than WinForm, but still this is not enough for MVVM binding, I would say that the capacity is about 30% of what is needed for MVVM middleware . <b> Bindable MVVM: this is the UI where the ViewModel is connected using View only using data binding.
An MVVM template refers to the representation of a ViewState, it does not describe how you maintain synchronization between View and ViewModel, in WPF it is bound to data, but it can be anything. And in fact, you can use the MVVM template in any user interface tool kit that supports \ callbacks events, you can use it in pure WinAPI in WinForms (I did, and it didn’t work much more with \ callbacks events), and you even you can use it in the Text Console, for example, overwrite DoS Norton Commander using the MVVM template.

In short: MVVM is not meaningless, it's great .NET 4.0. The WPF management library is trash.

Here is a simple proof of the ViewModel concept with which you cannot bind data in pure MVVM mode using WPF.

 public class PersonsViewModel { public IList<Person> PersonList; public IList<ColumnDescription> TableColumns; public IList<Person> SelectedPersons; public Person ActivePerson; public ColumnDescription SortedColumn; } 

You cannot bind WPF DataGrid column headings to data, you cannot bind data to selected rows, etc. etc., you either do it in a simple way, or write 200 lines of XAML hacking code for these 5 lines. Simplest ViewModel. You can only imagine how things get worse with the help of complex ViewModels.
So the answer is simple if you are not writing a Hello World application using bindable MVVM in WPF is pointless. You spent most of your time thinking about hacking to bind you to the ViewModel. Data binding is nice, but be prepared to roll back to the event 70% of the time.

+1
Jan 12 '11 at 16:22
source share

I saw the same problem with many MVVM implementations when it comes to (modal) dialogs. When I look at the participants in the MVVM template, I get the feeling that something is missing to create a consistent application.

  • The view contains specific elements of the graphical user interface and determines the appearance of the user interface.
  • ViewModel represents the state and behavior of a presentation.
  • The model can be a business object from the domain level or a service that provides the necessary data.

But missing:

  • Who creates ViewModels?
  • Who is responsible for the application workflow?
  • Who mediates between ViewModels when they need to communicate with each other?

My approach is to introduce a (Use-case) Controller , which is responsible for the missing points. How this works can be seen in the WPF Application Framework (WAF) examples.

0
May 09 '10 at 18:04
source share

No, this is not meaningless, but it is difficult to wrap your head, although the template itself is ridiculously simple. There are tons of misinformation and various groups that fight properly. I think that with WPF and Silverlight you should use MVVM or you will be over coding and trying to solve problems in the new model, and the "old" technology wins the form, which just leads you to trouble. This is more like Silverlight, since everything needs to be asynchronous (hacking is possible, but you just have to choose a different platform).

Id suggests reading this article Simplifying WPF TreeView with the ViewModel template carefully to see how MVVM can be well implemented and allow you to change the winning form mentality to a new way of thinking in MVVM. In short, when you want to do something, first apply the logic to the ViewModel, not the View. Do you want to select an item? Change icon? Do not iterate over UI elements, just update model properties and bind data binding to nitty gritty.

0
Aug 18 '10 at 8:10
source share



All Articles