Automatically update user interface when changing business objects

It is rather a design issue.

I create a tool that displays business objects in different ways (for example, tree nodes, list view items, combo fields, text fields, etc.). Each time a user changes one of them, an event occurs that signals that this business object has been changed or the collection to which it belongs has been changed. Since this business object or collection to which it belongs can be displayed in more than one place, each of these user interface elements must be updated to reflect this change. Is there an elegant decision to properly update each type of user interface element in case of a change?

I have a few ideas on how this can be done, but I would like to know if anyone has this problem, and was pleased with their solution. This is C # WinForm, but the solution can be in any language.

My current thoughts on the problem and possible solution:

This gets complicated when you want to clear event bindings (i.e. businessObject.Changed - = ObjectChanged) when your business objects become part of TreeNodeCollection / ListViewITemCollection / ComboBoxItemCollection and Clear () is called in the collection.

How about a "Service", where each object and its ui element can be registered, where the events of a business object can be listened to in one place, and each element of the user interface will be updated when events are raised? When all user interface elements are unregistered, the subscription to this object event is deleted.

, , .

?

+3
4

. :

public delegate void ObjectRefresh(BusinessObject obj);

BusinessObject:

public event ObjectRefresh;

:

if (ObjectRefresh)
    ObjectRefresh(this);

uis:

BusinessObject obj = GetBusinessObject();
obj.ObjectRefresh += this.ObjectRefresh;
...
private void ObjectRefresh(BusinessObject obj)
{
    // update UI
}

:)

+2

make sure you really need this, i.e. that this is no longer happening. For example, if your GUI objects are tied to datasets, and your business objects merge changes into a dataset, the dataset automatically notifies gui objects that the data has been changed; database gui object will automatically update

if not, then the Observer pattern as above is what you want

0
source

Source: https://habr.com/ru/post/1698130/


All Articles