Storing state for custom objects – Apple WebObjects 3.5 User Manual

Page 131

Advertising
background image

Storing State for Custom Objects

131

Storing State for Custom Objects

When state is stored in the server, the objects that hold state are kept intact
in memory between cycles of the request-response loop. In contrast, when
state is stored in the page, in cookies, or in the file system, objects are asked
to archive themselves (using classes and methods defined in the
Foundation framework) before being put into storage. The objects that are
part of the WebObjects and Foundation frameworks can archive
themselves, so they require no effort on your part. But if your application
has custom classes that need to store state, these classes must know how to
archive and unarchive themselves. How you implement archiving for
custom classes depends on whether your application accesses a database. If
your application accesses a database, it uses the Enterprise Objects
Framework and should use the EOEditingContext class (EditingContext
in Java) to archive objects. If your application doesn’t access a database, it
should use the NSArchiver class to archive custom objects.

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 (EditingContext
in Java) 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.

Advertising