Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is asynchronous in MVVM? The Model or ViewModel. Best practise?

I'm searching for the best practise to communicate asynchronous between the layers. I'm using the mvvm light toolkit

currently I use a backgroundworker in the model because I saw this in the autogenerated code. Not with the backgroundworker but the async call.

public void GetConfig(Action<Config, Exception> callback)
{
    BackgroundWorker backgroundWorker = new BackgroundWorker();
    backgroundWorker.DoWork += (backgroundWorkerSender, backgroundWorkerArgs) =>
    {
        try
        {
            backgroundWorkerArgs.Result = AppEnvironment.Instance.Config;
        }
        catch (Exception exception)
        {
            backgroundWorkerArgs.Result = null;
        }
    };

    backgroundWorker.RunWorkerCompleted += (backgroundWorkerSender, backgroundWorkerArgs) =>
    {
        if (backgroundWorkerArgs.Result != null)
        {
            callback((Config) backgroundWorkerArgs.Result, null);
        }
        else
        {
            /* ToDo: exceptionhandling */
        }
    };

    backgroundWorker.RunWorkerAsync(); 
}

Now I found the AsyncDelegateCommand which implements the asynchronous part in the ViewModel.

private ICommand _refreshObjectDefinitionCommand;
public ICommand RefreshObjectDefinitionCommand
{
    get
    {
        return _refreshObjectDefinitionCommand
          ?? (_refreshObjectDefinitionCommand = new AsyncDelegateCommand(delegate
              {
                  IsBusy = true;
                  _dataService.GetObjectDefinition(
                    (xmlObjectDef, errorConfig) =>
                    {
                        if (errorConfig != null)
                        {
                            /* ToDo Lenz: exceptionhandling */
                            return;
                        }

                        ObjectDefinition = xmlObjectDef;
                    });

                  _dataService.GetObjectDefinitionTreeView(
                      (treenodes, errorConfig) =>
                      {
                          if (errorConfig != null)
                          {
                              /* ToDo Lenz: exceptionhandling */
                              return;
                          }

                          TreeNodes = treenodes;
                      });
              },
                                () => _isConnected, o => IsBusy = false, exception => IsBusy = false));
    }
}

I'm a little confused about the best practise? I've read a lot of articles. But somehow they are always different opinions. Is there any provision to the best compatibility under normal effort to maintain?

Some food for thought

Model:

http://csharperimage.jeremylikness.com/2009/12/simplifying-asynchronous-calls-in.html

http://www.dzone.com/articles/mvvmlight-and-async

ViewModel

http://www.codeproject.com/Articles/123183/Asynchronus-MVVM-Stop-the-Dreaded-Dead-GUI-Problem

http://www.codeproject.com/Articles/441752/Async-MVVM-Modern-UI

like image 544
masterchris_99 Avatar asked Nov 03 '22 04:11

masterchris_99


2 Answers

Well, I would say the fecthing of model and transforming it into View Model is async. Who does it, depends on the architecture, it can be done on the view model itself or it can use controller layer for such async load ups and mapping of the initialized VM to view. Also the backgroundworkers are the past you should use Task class for parallel operations. And of course do not forget to invoke through dispatcher when notifying the view on changes from VM.

Code sample:

    Task<string>.Factory.StartNew(() =>
{
     string text = GetArticleText();
     Application.Current.Dispatcher.BeginInvoke(new Action(()=>MyTextProperty = text));   
});
like image 197
VidasV Avatar answered Nov 15 '22 05:11

VidasV


I would suggest putting the asynchronous code in your ViewModel and leave your Model to store the data. When I was starting on MVVM, one of the first thing I learned was to remove logic from my Models and instead keep it in my ViewModels. Though I would say that where you put your code doesn't matter so long as all the people reading the code can understand it.

like image 29
Caleb Kiage Avatar answered Nov 15 '22 05:11

Caleb Kiage