I'm new to MVVM, after several week for studies, but I not quite sure how callback act between ViewModel and Model.

Let say I have a view model to get calculation result from DB, GetCalculationA is using callback, GetCalculationB is return value, but which one is better and why?

ViewModel:
public void GetCalculationResultA()
{
    _service.PrepareStuff(e =>
        {
            if (e != null) errHandler(@"err in calculation");
            _service.GetParameterA((i1, e1) =>
            {
                if (e1 != null) errHandler(@"err in calculation");
                _service.GetParameterB((i2, e2) =>
                    {
                        if (e2 != null) errHandler(@"err in calculation");
                        _service.Calc(i1, i2, (r, e3) =>
                            {
                                if (e3 != null) errHandler(@"err in calculation");
                                ResultText = r.toString();
                            });
                    });
            });
        });
}

public void GetCalculationResultB()
{
    try
    {
        _service.PrepareStuff();
        int i1 = _service.GetParameterA();
        int i2 = _service.GetParameterB();
        ResultText = _service.Calc(i1, i2);
    }
    catch
    {
        errHandler(@"err in calculation")
    }
}
有帮助吗?

解决方案

For me this looks mor like a control flow topic on async method calls. You should definitely check out C# 4.5 async and await pattern to work with async code. This helps you structure the code without the callback hell.

You can also await all results or any result of your multiple operations.

//pseudo code from the top of my head
var perperationTask = service.PrepareStuffAsync(parameter);  
var parameterATask = service.GetParameterA(value1, value2);  
var parameterBTask = service.GetParameterB(value2,value3);  

await Task.WhenAll(settingsTask, reputationTask, activityTask);  

PreperationSettings settings = perperationTask.Result;  
int parameterAResult = parameterATask.Result;  
int parameterBResult = parameterBTask.Result;

await calulation = service.CalculateAsync(parameterAResult,parameterBResult);

you should be able to wrap this in a try catch block and use classic error handling.

So async await helps to simplify async programming... read more here: Asynchronous Programming with Async and Await http://msdn.microsoft.com/en-us/library/hh191443.aspx Control Flow in Async Programs http://msdn.microsoft.com/en-us/library/hh873191.aspx

HTH

其他提示

If I understand correctly your question is really concerning error handling. For the most part, this is usually just an extension of the MVVM pattern -- you treat the errors as part of your Model. Depending on your scenario, and how your app intends to surface errors you might have an arbitrarily complex error object (or list of error objects) that the View can bind to, and possibly an event that triggers a popup in your UI.

The exact mechanics of how you call your repositories is entirely up to your application requirements. Two major patterns I've seen in my apps is the distinction between a single blocking error and a list of non-blocking errors. It seems to me that method A would lend itself to a list of non-blocking errors while B lend itself to a single blocking error.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top