【问题标题】:what is asynchronous in MVVM? The Model or ViewModel. Best practise?什么是 MVVM 中的异步?模型或视图模型。最佳做法?
【发布时间】:2013-02-03 21:52:39
【问题描述】:

我正在寻找在各层之间进行异步通信的最佳实践。 我正在使用mvvm light toolkit

目前我在 model 中使用了一个后台工作程序,因为我在自动生成的代码中看到了这一点。不是后台工作人员,而是异步调用。

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(); 
}

现在我找到了在 ViewModel 中实现异步部分的AsyncDelegateCommand

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));
    }
}

我对最佳实践有点困惑?我读过很多文章。但不知何故,他们总是有不同的意见。有没有规定在正常努力下保持最佳兼容性?

值得深思

型号:

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

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

视图模型

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

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

【问题讨论】:

  • 任务模式不是更容易阅读吗?使用异步/等待..
  • 当然可以,但据我所知,它只是从 .net 4.5 开始可用?!?
  • 有一个 NuGet 包支持 4.0 和 silverlight 5。将它安装到您的项目中,它就像一个魅力! nuget.org/packages/…

标签: c# .net wpf mvvm mvvm-light


【解决方案1】:

好吧,我会说模型的影响并将其转换为视图模型是异步的。谁来做,取决于架构,它可以在视图模型本身上完成,也可以使用控制器层进行异步加载和将初始化的 VM 映射到视图。后台工作人员也是过去,您应该使用 Task 类进行并行操作。当然,当通知视图来自 VM 的更改时,不要忘记通过调度程序调用。

代码示例:

    Task<string>.Factory.StartNew(() =>
{
     string text = GetArticleText();
     Application.Current.Dispatcher.BeginInvoke(new Action(()=>MyTextProperty = text));   
});

【讨论】:

    【解决方案2】:

    我建议将异步代码放在您的 ViewModel 中,并让您的模型来存储数据。当我开始使用 MVVM 时,我学到的第一件事就是从我的模型中删除逻辑,并将其保留在我的 ViewModel 中。虽然我会说,只要所有阅读代码的人都能理解,你把代码放在哪里并不重要。

    【讨论】:

      猜你喜欢
      • 2013-03-25
      • 1970-01-01
      • 2013-05-27
      • 2012-12-23
      • 1970-01-01
      • 2012-07-31
      • 1970-01-01
      • 2011-01-20
      • 2010-10-25
      相关资源
      最近更新 更多