architecture - How to send progress updates from a business/model class? -


let's have application has layered architecture. on view use mvc or mvvm. model treated domain, has part of business logic.

now let's have, in model, method takes time. complicated calculation or treatment has done each items of object example.

in ui, display progress bar , text display current step of calculation (for example listbox process history).

how that? how send model information of progress of process , how hook controller or viewmodel update progress?

i implement in following manner. business-layer process, takes long time run, raises events every indicate it's hitting specific "milestones". decide milestones signal through events , how many of them. if time-consuming process plain loop, may choose, example, raise same event again , again every 10% of items in loop. if process distinct phases, may choose raise different event each phase completed.

now, presentation layer subscribes events , acts in consequence, updating progress bar, text or whatever.

this mechanism because:

  1. the business layer stays independent of may go on in presentation layer.
  2. it easy extend bi-directional communication. can alter events presentation layer (or other subscriber) can return cancel flag business layer knows long-running process must cancelled.
  3. it admits either synchronous or asynchronous work. is, can use on blocking calls (i.e. presentation , business layer share same thread) or non-blocking calls (i.e. business layer uses background worker thread). system.componentmodel.backgroundworker class can used in latter case, it's not if want raise multiple types of events.

hope helps.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -