Persistate

Deleting objects

Hide Navigation Pane

Deleting objects

Previous topic Next topic No directory for this topic  

Deleting objects

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

You delete persistent objects in one of two ways, depending on whether the object is a scalar member or in a collection.  For scalar members you simply set the property for an actual member to null.  If you create a new object and set it into an actual member that already contains an object, then this also will delete the object being overwritten.  Here is an example.

foundBorrower.PostalAddress = null;

It is important to realise that this will delete the object from the database only if it is a contained member.  For associated scalar object members, which are usually more common, this simply sets that link to null or to another object, but the original associated object still exists under its own parent, with one less reference to it.

You delete objects in contained collection members by simply removing them from the collection that they belong to.  Here is an example of the removal of an object from a collection.

Publication toRemove = The.Library.PublicationWithIsbnNumber(suppliedIsbn);
The.Library.Publications.Remove(toRemove);

The first line here returns the object we want to delete and also leaves it still in the library’s Publications collection.  The second line removes it from that collection.  Persistate will delete it from the database on the next commit - see Synchronising with persistent storage.  You can also delete elements in a contained collection by overwriting them using the single parameter indexer.  Here is an example.

The.Library.PublicationWithIsbnNumber(isbn).StockCopies[3] = null;

Persistate automatically deletes any objects contained in an object being deleted.  This recurses to all lower levels of the persistent object tree.  So in fact, what happens here is a pruning of an entire branch of the persistent tree, below the deleted object.  In the case of the first example above, Persistate deletes any StockCopy objects belonging to the deleted Publication.  (See An Example Definition File for the definition of these objects.)  On the other hand, Persistate does not delete any of the associated Lend members of these StockCopy objects since they are not contained in the StockCopy.

Caution

When you delete an object by removing it from a contained collection, Persistate does not perform any checks to see whether an object to be deleted is an associated member of one or more other objects.  You must perform this relational integrity test manually.  However, the default implementation of the Delete standard operation does perform the following AssociatorCount check, but only for objects with the log associations function.

If the class of an object being deleted is in a category which has the, then you can determine the number of associations to it as in the following example.

if (toRemove.AssociatorCount() == 0)
   The.library.publications.Remove(toRemove)
else
   ...

The call to AssociatorCount() is a relatively rapid indexed based database lookup in this case.  You cannot use this method if the object class category does not have the log associations function.

When an object is deleted, Persistate raises events associated with the deletion.  See the Persistent events section for full details of this and other events raised by Persistate.