Part of the key to the publication / subscription template for updates is how to wrap information about what to do when the event fires.
When an object representing "Store X" is updated with a new name and fires an event to report this, which object uses the event?
Similarly, when City Y is added, which object should be notified of the creation?
One general approach is to have some kind of large uber-manager class that handles the whole process - it subscribes to all events and does everything.
Another approach that I used for a good effect is to create much simpler wrapper / coordinator objects that process only one part of the puzzle. I usually suffix the name of these classes with " Editor ".
So, you can have the CityEditor class, the constructor of which accepts both the City object and the TreeNode that represents this object. CityEditor will subscribe to events for both the City object and the TreeNode , and will take care of filling the TreeNode title and selecting an icon.
When the City object is updated, CityEditor responds to the triggered event by updating the TreeNode . When the City object is deleted, CityEditor ensures that the node is removed from the Treeview .
When a new Store object is added to City , CityEditor can take care of creating a StoreEditor to coordinate updates at this level. Similarly, if Employee added to the Store , the Employee instance handles Treeview updates.
source share