如何从业务/模型类发送最新进展? [英] How to send progress updates from a business/model class?

查看:105
本文介绍了如何从业务/模型类发送最新进展?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我们有一个具有分层的体系结构的应用程序。在视图我们使用MVC或者MVVM。该模型作为域处理,具有业务逻辑的很大一部分。

Let's say we have an application that has a layered architecture. On the view we use a MVC or MVVM. The model is treated as the domain, it has a good part of the business logic.

现在让我们说我们有,在模型中,需要一些时间的方法。复杂的计算,或有工作要做,以例如对象的每个项目的处理。

Now let's say we have, in the model, a method that takes some time. A complicated calculation or a treatment that has to be done to each items of an object for example.

在用户界面中,我们要显示一个进度条和文本,将(与所有的历史过程,例如一个列表框)显示计算的当前步骤。

In the UI, we would like to display a progress bar and a text that would display the current step of the calculation (for example a listbox with all the process history).

你会怎么做呢?如何从模型发送的处理的进展以及信息挂钩控制器或视图模型,这样它会更新进度

How would you do that? How to send from the model the information of the progress of the process and how to hook up the Controller or ViewModel so that it will update the progress?

推荐答案

我经常会以下列方式实现这一点。我的业务层的过程,这需要很长的时间来运行,引发事件,每隔一段时间,以表明它的具体击中里程碑。你决定什么里程碑,通过各种活动和有多少人发出信号。如果你的耗时的过程是一个简单的循环中,可以选择,例如,一次又一次提高相同的事件在循环中的项目的每一个的10%。如果是用不同的阶段过程中,你可能会选择提高不同的事件,因为每个阶段完成。

I often implement this in the following manner. My business-layer process, which takes a long time to run, raises events every so often to indicate that it's hitting specific "milestones". You decide what milestones to signal through events and how many of them. If your time-consuming process is a plain loop, you may choose, for example, to raise the same event again and again every 10% of the items in the loop. If it is a process with distinct phases, you may choose to raise a different event as each phase is completed.

现在,您的presentation层订购了后果的事件和行为,更新进度条,文本或什么的。

Now, your presentation layer subscribes to those events and acts in consequence, updating the progress bar, the text or whatever.

这机制是很好的,因为:

This mechanism is good because:


  1. 业务层保持独立的东西可能会最多在presentation层。

  2. 这是很容易延长双向通信。您可以轻松地改变事件,以便使业务层知道,一个长期运行的过程中必须取消presentation层(或任何其它用户)可以返回一个取消标志。

  3. 它承认同步或异步工作。也就是说,你可以用它阻塞调用(即你的presentation和业务层共享同一个线程)或非阻塞调用(即业务层使用一个后台工作线程)。的 System.ComponentModel.BackgroundWorker 的类可被用在后一种情况下,但它不是,如果你想提高多种类型的事件太美好了。

  1. The business layer stays independent of what may go on up in the presentation layer.
  2. It is easy to extend for bi-directional communication. You can easily alter events so that the presentation layer (or any other subscriber) can return a cancel flag so that the business layer knows that a long-running process must be cancelled.
  3. It admits either synchronous or asynchronous work. That is, you can use it on blocking calls (i.e. your presentation and business layer share the same thread) or non-blocking calls (i.e. your business layer uses a background worker thread). The System.ComponentModel.BackgroundWorker class can be used in the latter case, but it's not too good if you want to raise multiple types of events.

希望这有助于。

这篇关于如何从业务/模型类发送最新进展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆