Accessibility

Table of Contents

Refactoring Flex applications from RPC-style data management to Data Management Services

Refactoring Restaurant Finder: Data manipulation

Create, update, and delete operations are all very similar in RPC-style applications. In create and update operations, the application will typically send an object to the server; the server creates or updates the object in the data store and then sends the updated version back to the client. Delete operations usually only send an object's unique ID to the server and then the server deletes the specified object.

The Restaurant Finder application only has code to create a review and since the update and delete code would be very similar, only the create code is listed:

private function postReview():Void
{
         ... validate user input
         ... create a review object based on user input
         var call: Object = service.addReview(review);
         call.resultHandler = addReviewResult;
}
private function addReviewResult(event: ResultEvent):Void
{
         ... update the review with the one from the server
}

One limitation of all data manipulation methods is that it would take a lot of extra code to add transactional capabilities. In order to support rollback, in case the server rejects a change for some reason, the client application must keep a copy of the initial object around until a transaction is completed. A transaction may also fail because the initial object was not the most up-to-date version. This may happen if more than one user is manipulating the same object at the same time.

Another limitation of RPC-style applications is that the client application needs to track object changes on its own. For example, if changes are batched and committed all at the same time, then special code must be written to track the objects' changes and the types of changes. More complex, error-prone code must then be written to handle the commit operation, iterating through the changes.

In Data Management Services, changes are made to a managed object and then committed. The changes can be creates, updates, or deletes. If the data is managed by Data Management Services, the changes will be tracked and, upon commit, sent to the server. If the server encounters an error when committing the changes, the client is able to roll back the changes easily or modify the data and commit again.

Here is what the above added review code may look like in Data Management Services code (assuming autoCommit is set to false):

private function createReview():Void
{
         ... validate user input
         ... create a review object based on user input
         reviews.addItem(review);
         reviewDS.commit();
}

If there were update and delete functions that would have to be migrated, they might look like this:

private function updateReview():Void
{
         ... validate user input
         ... update the review object based on user input
         // review is a reference to an object in the managed
         // reviews ArrayCollection
         review.title = newTitle.text;
         reviewDS.commit();
}
private function deleteReview():Void
{
         reviews.removeItemAt(index);
         reviewDS.commit();
}

An important part of Data Management Services involves resolving conflicts. Any commit can result in a conflict if the object being committed is not in sync with the server or if changes have been made by other clients during the editing process. Although Data Management Services detects these conflicts automatically, resolving these conflicts must be handled by the application using the appropriate logic.

In the following code, the conflictHandler function is being called when a conflict occurs. The code iterates through the conflicts and, in this instance, automatically accepts the server version of the object and prompts the user to fix the problem. In some cases this implementation would be sufficient, while in others the application may want to accept the client's changes automatically, or perhaps ask the user which version (client or server) should be accepted:

private function init():Void
{
         reviewDS.addEventListener(DataConflictEvent.CONFLICT,conflictHandler);
}

private function conflictHandler(event:DataConflictEvent):Void
{
         var conflicts:Conflicts = reviewDS.conflicts;
         while (conflicts.hasNext())
         {
                   var c:Conflict = conflicts.next();
                   c.acceptServer();
                   reviewDS.commit();
                   errorMessage = "Error saving the review";
         }
}

In simple examples like the Restaurant Finder, Data Management Services can help simplify the code somewhat. However, as applications grow in complexity, RPC-style code becomes more and more difficult to manage. With objects managed by Data Management Services, data retrieval is no longer limited to a request-response model. A data set can be retrieved and automatically kept in sync—allowing create, update, and delete operations to be performed, batched, and then committed. Low-level concerns like data locking, flagging changes, handling conflicts, and concurrency issues are all built into the framework, allowing data retrieval and manipulation code to focus exclusively on those functions.

Data Management Services overcomes many of the limitations in RPC-style data management by providing standard client and server APIs for data retrieval and manipulation. These APIs allow CRUD code to be simpler, more organized, and more maintainable. For most Flex applications, the transition to the Flex Data Management Services should be a simple one. For new applications to utilize Data Management Services, the biggest hurdle will be in learning to design the application around managed data objects. However, applications designed to use Data Management Services receive all the maintenance benefits of now-traditional persistence frameworks such as Hibernate, in addition to gaining access to an entirely new programming model specifically targeted at simplifying Flex application development.

Where to go from here

To read more about RPC and Flex Data Services, read Using RPC services in Flex Data Services 2 or visit the Data Integration and Services topic page in the Flex Developer Center.