Persistate

Persistent events

Hide Navigation Pane

Persistent events

Previous topic Next topic No directory for this topic  

Persistent events

Previous topic Next topic Topic directory requires JavaScript JavaScript is required for the print function Mail us feedback on this topic!  

There are six events linked to Persistent objects.  After any of these events are raised for an object, the corresponding Instance events are raised for its ObjectClass and all its base classes, starting with the least derived.  See Object class events for details.

The following list gives a summary of each of these events.  See the Persistate API documentation for full details.  See the Event Firing Sequences section for details on the order in which these and persistent events are fired in various circumstances.

The Adopted event is fired whenever the object is attached to a parent object.  This happens whenever you assign the object to a contained (not associated) member of another object, or whenever you add the object to a contained collection.   It also happens (after Disowned) during the Persistent.Move method.
The Disowned event is fired whenever the object is detached from its parent object.  This happens whenever you overwrite the contained (not associated) member containing the object with null, or another object, or  you remove the object from a contained collection. It also happens (before Adopted) during the Persistent.Move method.
The Associated event is fired whenever the object is associated to another object.  This happens whenever you assign the object to an associated scalar member of another object, or whenever you add the object to an associated collection.
The Dissociated event is fired whenever the object is disassociated from another object.  This happens whenever you overwrite an associated scalar member containing the object with null or another object, or when you remove the object from an associated collection.
The Modified event is fired whenever the object is modified using one of the generated properties, or when the OnModified() method is called manually.
The Deleted event is fired whenever the object is detached from its parent object.  This happens whenever you overwrite the contained (not associated) member containing the object with null, or another object, or  you remove the object from a contained collection.  Unlike Disowned, it is not fired by the Persistent.Move method, but it is fired whenever any newly created objects are deleted when an operation execution returns a ResultAction of Rollback.  See Synchronising with persistent storage for details of the latter.

Here is an example of the use of the Associated and Dissociated events

private static  Dictionary<Persistent, List<Persistent>> associatorLists
   = new Dictionary<Persistent, List<Persistent>>();
 
public static void TrackAssociations(Persistent obj)
{
   obj.Associated += new LinkageEventHandler(obj_Associated);
   obj.Dissociated += new LinkageEventHandler(obj_Dissociated);
}
 
public static List<Persistent> GetAssociators(Persistent obj)
{
   List<Persistent> associators;
   if (!associatorLists.TryGetValue(obj, out associators)) {
      associators = new List<Persistent>();
      associatorLists[obj] = associators;
   }
   return associators;
}
 
private static void obj_Dissociated(object sender, LinkageEventArgs args)
{
   List<Persistent> associators = GetAssociators(args.Containee);
   associators.Remove(args.Container);
}
      
private static void obj_Associated(object sender, LinkageEventArgs args)
{
   List<Persistent> associators = GetAssociators(args.Containee);
   associators.Add(args.Container);
}

This example implements association tracking for individual objects.  This can be useful if you need this only for a few objects, and only for the duration of a session, and so don't want to incur the overhead of making the entire object class Logged (see Class Category Definition)  The example is simple and does not record the member in which the association is made, or whether is a scalar or collection association.