Adding Persistence to an Existing Hierarchy
- 2 minutes to read
The simplest way to persist an object is to inherit it from the XPObject class (see Creating a Persistent Object). Alternatively, an object can be inherited from the XPBaseObject class. However, this variant requires the implementation of an identity (key) field. Inheriting an object from the aforementioned classes simplifies things, but it's sometimes necessary to persist an object with a different base. In this instance there are two possible options to choose from:
- Changing the Base Inheritance - By implementing the IXPObject and IComparable interfaces and adding a Session-specific constructor into the object class definition. See the How to: Add Persistence to an Existing Hierarchy by Changing the Base Inheritance topic for step-by-step instructions on how this can be carried out.
Using Session-less Persistent Objects - By marking a class with the PersistentAttribute and using the Session object to control any persistent operations with the object. This is the simplest way to add XPO into legacy applications, since the base inheritance and class hierarchies are not modified in any way. See the How to: Add Persistence to an Existing Hierarchy by Using Session-less Persistent Objects topic for details on this technique.
As for collections and arrays which define one-to-many associations between classes, the XPCollection must be used to store/load collections of related objects.