Object Persistence - Ajax Patterns

Object Persistence

From Ajax Patterns

(Difference between revisions)
Revision as of 01:15, 10 February 2007
24.20.71.154 (Talk | contribs)
Related Patterns
← Previous diff
Revision as of 07:54, 14 March 2007
218.166.219.74 (Talk | contribs)
Forces
Next diff →
Line 15: Line 15:
= Forces = = Forces =
-Standard object manipulation is a very natural programming technique. Persisting objects in other languages has already proven very powerful in improving code structure, for example in Java (JDO implemented through Hibernate, JPOX, etc), and simplifying the job of storing state information.+Standard object manipulation is a very natural programming technique. Persisting objects in other languages has already proven very powerful in improving code structure, for example in Java (O/R Mapping implemented through Hibernate, JPOX, etc), and simplifying the job of storing state information.
<!-- -------------------------------------------------------------------- --> <!-- -------------------------------------------------------------------- -->

Revision as of 07:54, 14 March 2007

Evidence: 1/3

Tags: Object Persistence


Contents

In A Blink

Object persistence has become an important part of many development projects because of the improved structure of coding that it provides. With object persistence, objects can be manipulated with normal programming techiques, and their state can be stored in persistent storage (on the server or client). In future sessions, the persisted objects can be restored to their previous state.


Problem

A JavaScript application stores all of it's state information in objects. Is there any way to persist this information without writing custom code to go through each object and store its state by making calls to server with the state information?

Forces

Standard object manipulation is a very natural programming technique. Persisting objects in other languages has already proven very powerful in improving code structure, for example in Java (O/R Mapping implemented through Hibernate, JPOX, etc), and simplifying the job of storing state information.


Solution

Utilize a general object persistence strategy where JavaScript objects can be manipulated and their state information can be persistently stored. In future sessions, the persisted objects can be restored to their previous state. This often provides a more natural programming experience instead of forcing programmers to develop their own techniques for saving the state of their applications.


Decisions


When to persist the object data?

Objects can be explicitly loaded and saved. Or you can use a class of persistable objects that manage get and set calls, and the data can be persisted when these calls are made.

Where to persist the data

Object data can be stored on the server through remote calls, or it can stored locally using a local storage technique such as Dojo.Storage.


Real-World Examples/Frameworks

Authenteo

Authenteo is a framework that allows entire web applications to be developed with persisted objects that accessible and manipulated from client side JavaScript.

Ajaxdo

Ajaxdo is a framework to save and load objects to a server connected to an SQL database.

Dojo Storage


Code Examples

Authenteo uses a language extension of # symbol (it does precompilation) to denote persisted fields (to allow for transient fields as well) on persistent objects. For example if you have an object that represents a list of webpage objects in a persistent object "listOfPages" we could this:

var webpage = listOfPages#[2]; // get the third page in the list
var pageName = webpage.#name; // get the name of the page
var pageContents = webpage.#content; // the contents of the page

Now we could change the name of the webpage:

webpage.#name = "New webpage name";  // This change is immediately sent to the server, and is persisted


Related Patterns

Submission Throttling is often to keeping data transfer fairly transparent to the user. All you need is a function that periodically uploads all state. Then, another function can be used to pull it down when the user logs in again - a form of Multi-Stage Download.

Local Storage

Dojo.Storage/Dojo Offline Toolkit provides the ability to store data local in persistent storage on a browser.