Table of Contents Previous Section
Archiving Custom Objects in a Database Application
If your application accesses a database, it uses the Enterprise Objects framework and should use the EOEditingContext class to archive objects. An editing context manages a graph of enterprise objects that represent records fetched from a database. You send messages to the editing context to fetch objects from the database, insert or delete objects, and save the data from the changed objects back to the database. (See the Enterprise Objects Framework Developer's Guide for more information.)In WebObjects, applications that use the Enterprise Objects Framework must enlist the help of the EOEditingContext class to archive enterprise objects. The primary reason is so that EOEditingContext can keep track, from one database transaction to the next, of the objects it is designed to manage. But using an EOEditingContext for archiving also benefits your application in these other ways:
- During archiving, an EOEditingContext stores only as much information about its enterprise objects as is needed to reconstitute the object graph at a later time. For example, unmodified objects are stored as simple references that will allow the EOEditingContext to recreate the object from the database at a later time. Thus, your application can store state very efficiently by letting an EOEditingContext archive your enterprise objects.
- During unarchiving, an EOEditingContext can recreate individual objects in the graph only as they are needed by the application. This approach can significantly improve an application's perceived performance.
// WebScript exampleThe Java packages provide a different archiving mechanism; your Java classes should implement the java.io.Serializable interface. This interface consists of two methods: writeObject, which roughly corresponds to encodeWithCoder:; and readObject, which roughly corresponds to initWithCoder:.
- encodeWithCoder:(NSCoder *)aCoder {
[EOEditingContext encodeObject:self withCoder:aCoder];
}
- initWithCoder:(NSCoder *)aDecoder {
[EOEditingContext initObject:self withCoder:aDecoder];
return self;
}
// Java exampleThe enterprise object simply passes on responsibility for archiving and unarchiving itself to the EOEditingContext class, by invoking the encodeObject:withCoder: and initObject:withCoder: methods in WebScript or Objective-C (writeObject and readObject in Java) and passing a reference to itself (self or this) as one of the arguments. The editing context takes care of the rest. (See the EOEditingContext class specification in the Enterprise Objects Framework Reference for more information.)
private void writeObject(java.io.ObjectOutputStream out)
throws IOException {
EOEditingContext.writeObjectToStream(this, out);
}
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException{
EOEditingContext.initObjectFromStream(this, in);
}
Table of Contents Next Section