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?
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.
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:
Hope this helps.