Documentation Archive Developer
Search
PATH  Documentation > WebObjects 4.5 > WebObjects Developer's Guide


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:

An enterprise object (like any other object that uses the Foundation archiving scheme) makes itself available for archiving by declaring that it conforms to the NSCoding protocol and by implementing the protocol's two methods, encodeWithCoder: and initWithCoder:. It implements these methods like this:

// WebScript example
- encodeWithCoder:(NSCoder *)aCoder {
[EOEditingContext encodeObject:self withCoder:aCoder];
}

- initWithCoder:(NSCoder *)aDecoder {
[EOEditingContext initObject:self withCoder:aDecoder];
return self;
}
The 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:.

// Java example
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);
}
The 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.)

Table of Contents Next Section