How to get user input from the middle of a model method in a Model-View-Viewmodel?

I am writing an application that listens on a network connection, and when some data arrives, it responds back and depending on the incoming data, you may need to ask the user (show a dialog) before answering back.

I don’t know how to do this in MV-VM architecture: events and binding to observable collections are good if I just need to update the GUI based on the input, but what if I really need anwer from the user before replying back?

And to make things worse, I want to do it synchronously, because I want my answer algorithm to be in one place, and not split into several callbacks with unclear who-called-who responsibilities.

Just something like

HandleMessage(Message msg){ string reply; if (msg.type == 1) { reply = ... } else { string question = msg... reply = ShowModalDialog(question); // MVVM violation! } sender.Send(reply); } 

but I don’t want to call the view mode or viewmodel from the model, since the model must be reusable and testable - I don’t want dialogs to appear in every test run, and that would be a violation of MVVM! There are no events (they are only one-way, as far as I know, and do not have a return channel to get an answer to the origin of the event) or data binding, as this will be asynchronous.

Is this doable? This is a question that I asked several tested developers, and so far I have not received a practically useful answer. However, the need for some additional input in the middle of processing is fairly common.

Thanks!

EDIT: this is application logic, so it clearly belongs to the model, and even if this is not the case, I would like to know a solution for cases where I really need user input in the middle of the business logic subroutine in the model.

+6
wpf mvvm dialog synchronous model
source share
3 answers

This is one of those problems that MVVM does not solve on its own. One solution would be to use the service to query the user, and then use the ViewModel for this service.

In my project, we use PRISM , which, in addition to providing a framework for services, also provides other tools to facilitate the development of a graphical interface.

The following is information on how services work in PRISM.

So, in your case, I would create some kind of IOC, register a query service with it, then in the ViewModel pass in the IOC, and then use IOC to get the query service and use this to query the user. More work? Of course. But this means that you can replace the query service with another implementation for testing by simply replacing it in the IOC.

MVVM + Services = Ultimate Power!

+3
source share

I don’t know if this idea adheres strictly to the principles of MVVM, but ... I would encapsulate the functionality of the dialog as a service (using the interface). The implementation of the service will be in the user interface layer, but for testing purposes you simply "make fun of" the interface.

+1
source share

Actually, it does NOT ALL belong in the application logic.

It seems you have 2 different “views”. There is a source (data comes through the network), and the second (confirmation dialog).

The model must determine what to display the new view, display the view to display it, and then respond to input from that view.

Do not try to do everything in one step.

0
source share