views:

114

answers:

3

Hi everyone, 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.

A: 

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.

Bill K
I agree with the first remark - yes, the initial view is just service's console log window, and the data input dialog can be thought of as another view.Hovewer, the model is algorithm driven, and I find it pretty crazy to have to partition one alorithm into parts asynchronously called by UI - and in some cases, it is not really possible.Also, from the view of maintainability, it is much better to have an algorithm in one metod. Therefore, call to GUI must be synchronous.
Tomáš Kafka
I understand exactly what you mean. I've had to use state machines in some really irritating places. Although I know you don't want to go there, there is a good chance you'll have to. If not, great, but you might want to mull it over--it's probably what you'll end up doing. A dialog box often implies releasing the thread. I will not disagree with your point that it's crazy though :)
Bill K
+1  A: 

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.

Daniel Pratt
+2  A: 

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!

Cameron MacFarland
+1 for explaining it a lot better than I did.
Daniel Pratt
Thanks, this sounds like a clean solution, I'll read the links (thanks for them!)
Tomáš Kafka