OpenAccess ORM can automatically perform application-specific operations on class objects during certain lifecycle events. By implementing the IInstanceCallbacks interface for a persistence capable class, user-defined methods are called when an object of the class is read from the database, when an object is removed (deleted) from the database, or when an object is written to the database. The IInstanceCallbacks interface methods are, respectively, PostLoad(), PreDelete(), and PreStore(). You must implement all three in your class. These methods are described in detail below.
Implementing Instance Callbacks
The following example shows a persistence capable class, Person that implements the IInstanceCallbacks interface. The IInstanceCallbacks methods do nothing in this example:
C# |
Copy Code |
using OpenAccess;
[OpenAccess.Persistent]
class Person : IInstanceCallbacks
{
// . . .
void PostLoad() { return; };
void PreRemove(IObjectScope objectScope){ return; };
void PreStore() { return; };
// . . .
} |
VB .NET |
Copy Code |
Imports OpenAccess <OpenAccess.Persistent> _ Friend Class Person Implements IInstanceCallbacks
Private Sub PostLoad() Return End Sub Private Sub PreRemove(ByVal objectScope As IObjectScope) Return End Sub Private Sub PreStore() Return End Sub End Class |
The following list describes the IInstanceCallbacks interface methods. All the methods have the return type void in C#, J# and Sub in VisualBasic .NET.
IInstanceCallbacks Interface Methods
|
|
The PostLoad() method is called when the object is resolved, i.e., the object data is loaded from the database into memory. OpenAccess ORM uses delayed loading. Even if you have a reference to the object, the object data is not read from the database until some operation that requires the data is performed on the object. Only at this time is the object resolved. This means that the PostLoad() method is not necessarily called when you first get a reference to the object but rather when the object data is requested (For more information about delayed loading and how the object data is resolved, refer to Object Retrieval).
The PostLoad() method is particularly useful for initializing any transient fields whose values depend on persistent field values. For example, if your Person class has a transient field for the person's age, this can be calculated from the persistent birthdate field (and the current date) when a Person object is resolved. |
PreRemove(IObjectScope scope) |
|
This method is called in the IObjectContext.Remove() method, i.e., when the object is removed (deleted) from the database (IObjectContext is the base interface for IObjectScope and ObjectContainer. Refer to the OpenAccess ORM .NET API Reference for more information). The parameter scope can be used to propagate deletion to child objects. This propagation can also be configured in the mapping data. |
|
|
This method is called when the object is written to the database during the transaction Commit() or Checkpoint() call. The PreStore() method is called before the object is actually written to the database. You could, for example, update persistent fields based on transient data, or, based on the results of any operations performed during the PreStore() call, actions such as throwing an exception and aborting the transaction may be taken. PreStore() can also be used to hide the initialization of the identifier field (in case of GUIDs). |