Re: Design pattern to sync objects



Responding to Bryan...

I'm looking for a design pattern that will keep two in-memory objects
that are based on the same data in a DB in-sync with each other during
DB updates. I have a full featured 'Person' object with the typical
fields and methods, and a light weight 'smallPerson' object that only
contains ID, FirstName and LastName fields. I use the smallPerson
object for listboxes and such where I don't need to pull all the
person's info from the DB. I use the full featured Person object when
the user wants to edit all of a person's info.

Hopefully these objects are not in the same subsystem scope if they are not siblings in a generalization. B-) There can only be one object at a time that abstracts a given problem space entity because identity must be mapped uniquely. (If they are sibling subclasses, then their attribute values can't be dependent because it would violate 3rd Normal Form.)

However, that rule is relaxed across subsystems because each subsystem maps the same problem space independently. So one can have a synchronization problem between subsystems. The standard technique for dealing with this is to select one subsystem as the "master" that owns the state data for the given class. One then uses the write accessors for the knowledge attributes in that subsystem to send an appropriate message to the other subsystems to announce a change in value. (One can obviously extend this to multiple subsystems sharing the data, including DB access.)

[FYI, full code generators will usually provide this synchronization automatically once the mapping of attributes between subsystems is provided in an MDA Marking Model. The process is known as "implicit bridging".]

Lets say I have a listbox showing a bunch of smallPerson objects and
the user clicks on one of the items to edit the person. A form pops
up which is bound to the full featured Person object. The user edits
this Person object's first name and then saves it to the DB. Now the
Person object is in sync with the DB, but the smallPerson object is
not. It will still show the old first name until I make another round
trip to the DB to refresh the objects data.
Any patterns to keep two objects based on the same data in sync?

The closest pattern is Observer. However, it really isn't relevant because the mapping of Observer to Subject is not dynamic (i.e., no registration is required). The mapping is essentially "hard-wired" by object identity. That is, the subject object identity is passed in the inter-subsystem message and that will be mapped unambiguously by the observer subsystem interface to the object with the same identity in the observer subsystem.


*************
There is nothing wrong with me that could
not be cured by a capful of Drano.

H. S. Lahman
hsl@xxxxxxxxxxxxxxxxx
Pathfinder Solutions
http://www.pathfindermda.com
blog: http://pathfinderpeople.blogs.com/hslahman
"Model-Based Translation: The Next Step in Agile Development". Email
info@xxxxxxxxxxxxxxxxx for your copy.
Pathfinder is hiring: http://www.pathfindermda.com/about_us/careers_pos3.php.
(888)OOA-PATH



.