How to cleanly get user input from the middle of model's method in Model-View-Viewmodel architecture?

StackOverflow https://stackoverflow.com/questions/884831

Question

I am writing an app that listens on a network connection, and when some data arrive, it replies back, and depending on incoming data, it may need to ask user (show dialog) before replying back.

I don't know how to do this cleanly in M-V-VM architecture: the events and binding to observable collections are nice if I need to just update GUI based on incoming data, but what if I actually need an anwer from user before replying back?

And to make things worse, I want to do it synchronously, because I want my reply algorithm to be at one place, not partitioned into multiple callbacks with unclear 'who-calls-who' responsibilities.

Simply, 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 view or viewmodel from model, as model needs to be reusable and testable - I don't want popping dialogs in every test run, and it would be violation of MVVM! No events (they are just one-way as far as i know, and have no backwards channel to get reply to event origin) or databinding, as it would be asynchronous.

Is this doable? This is a question I asked several test driven development propagators, and so far, I didn't get practically usable answer. Yet, a need for some additional input in the middle of processing is fairly common.

Thanks!

EDIT: this is application logic, so it clearly belongs to model, and even if in this case it didn't, I'd like to know the solution for cases when I really need user's input in the middle of business logic routine in model.

Was it helpful?

Solution

This is one of those problems that MVVM doesn't solve on it's own. One solution would be to use a service to query the user and then have the ViewModel use that service.

In my project we're using PRISM which besides providing a services framework also provides other tools for making GUI development easier.

Here's a writeup of how services work in PRISM.

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

MVVM + Services = Ultimate Power!

OTHER TIPS

I don't know if this idea is in strict keeping with the tenets of MVVM, but...I would encapsulate the dialog functionality as a service (referenced via an interface). The implementation of the service would be in the UI layer, but for testing purposes you would just "mock" the interface.

Actually, it doesn't ALL belong in the application logic.

It seems like you have 2 different "views". There is the initial one (data coming in over the net), and a second one (confirmation dialog).

The model needs to determine that a new view needs to be displayed, signal the view to display it, then later respond to the input from that view.

Don't try to do it all in one step.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top