PureMVC Architects Lounge

Announcements and General Discussion => General Discussion => Topic started by: zilet on April 03, 2008, 03:32:26



Title: Handling CRUD operations with RemoteObjects on large amount of data
Post by: zilet on April 03, 2008, 03:32:26
Hello,

   I can say that this is not strictly oriented to PureMVC but since I am using it it has some relations.
I am interested in the way you handle data changes for the records that is acquired from the database with a lot of records.
For instance lets say that you are working with 10 000 records (ok there can be paging but still you want to display at least 1000 records in a DataGrid).

Proxy is responsible for comunication with the database backend and operations on the data.
Considering memory consumption it is good to bind Proxy.data to the ArrayCollection which is used as a dataProvider for a DataGrid. When user makes some change in that list, proxy has to send the request to the database to execute the operation.
When the operation is successful, there are 2 approaches that I am considering:
 - reload the data again form the database - for a lot of records it can be slow
 - or change the data directly in actionscript

with the first approach if user scrolled down to watch some records he will lose its position. With the second approach it is better for the user but it requires a lot more work.
For example, on insert service has to send back newly inserted object which can be added to the Array. On update, service has to send updated object back again and then you have to search through the array for the primary key of the updated object and replace it with the new one. Similar thing happens when object is deleted.
I am interested in the best way to handle this, so I want to hear what is your experience?
Maybe better way to store objects in HashMap?


Title: Re: Handling CRUD operations with RemoteObjects on large amount of data
Post by: philipSe on April 04, 2008, 02:44:28
You refer to 2 approaches.  I do it the second way.  Proxy has the array collection as you say.  Proxy also has a map by record id to reference each record in the array, to facilitate direct update of a record.  Proxy has to maintain the map in line with each insert/delete to the array collection.
Regards
    Philip


Title: Re: Handling CRUD operations with RemoteObjects on large amount of data
Post by: zilet on April 04, 2008, 04:56:57
Thanks for the response.
Just one more thing. How you handle data mapping for pKey -> array position.
Traverse the Array?





Title: Re: Handling CRUD operations with RemoteObjects on large amount of data
Post by: philipSe on April 07, 2008, 08:01:06
Say proxy vars as follows
 datarecs :ListCollectionView = new ArrayCollection();
 idMap :Object = new Object()

For each item going into the list...
datarecs.setItemAt( rec, ix );
idMap[rec.getId()] = rec;

Get an item, given the id...
return idMap[ givenId ];

Replace an item in the list...
ix = find the index of the item in the list by searching the list
datarecs.setItemAt( rec, ix );
idMap[rec.getId()] = rec;

Remove an item from the list, given the id...
ix = find the index of the item in the list by searching the list for matching id
datarecs.removeItemAt( ix );
delete idMap[ givenId ];

Hope this helps
----Philip


Title: Re: Handling CRUD operations with RemoteObjects on large amount of data
Post by: zilet on April 07, 2008, 11:01:38
I think I found better approach for this.
Every Proxy extends RoProxy (RemoteObjectProxy :)

RoProxy has :
protected var refMap:Array = new Array;

and I have 2 functions:
:
   protected function addRecord(item:Object,token:AsyncToken):AsyncToken{
      refMap[token.message.messageId] = item;
      return token;
    }
   
    protected function removeRecord(token:AsyncToken):Object{
      var item:Object = refMap[token.message.messageId]
      refMap[token.message.messageId] = null;
      return item;
    }

and then when you call delete operation for instance :
:
  public function remove(item:Object):AsyncToken{
       return addRecord(item,service.removeByPKey(item.id));
    }

and its resulthandler:
:
private function onRemoved(evt:ResultEvent):void{
      list.removeItemAt(list.getItemIndex(removeRecord(evt.token)));
      sendNotification(DELETED);
    }

For insert operation you have to return newly inserted record and then when it returns just add it to the list, update operations don't require this way of handling if you keep the reference to the object you are updating.


Title: Re: Handling CRUD operations with RemoteObjects on large amount of data
Post by: puremvc on April 07, 2008, 11:29:42
Zilet,

I like this approach, it's very appealing. Can you work up a simple Flex utility and demo and based on it? It would really be nice to have an off-the-rack answer to this.

-=Cliff>


Title: Re: Handling CRUD operations with RemoteObjects on large amount of data
Post by: zilet on April 08, 2008, 01:50:12
No problem Cliff.

I'll send it to you mail.


Title: Re: Handling CRUD operations with RemoteObjects on large amount of data
Post by: Joel Hooks on April 11, 2008, 11:35:06
Eric Feminella's HashMap (http://code.ericfeminella.com/classes/as3/HashMap.as.html) is a nice update to using dictionaries or associative arrays for this sort of thing.


Title: Re: Handling CRUD operations with RemoteObjects on large amount of data
Post by: puremvc on April 14, 2008, 10:45:18
Eric Feminella's HashMap is a nice update to using dictionaries or associative arrays for this sort of thing.

While it's good to play nice with other frameworks and utilities when building apps or in demos, care should be taken not to place dependencies on other frameworks if at all possible in utilities, which may remain most portable to other PureMVC-supported languages by relying on the simplest language constructs possible.

For instance to, make the utility work in Python, you might have to write a hashmap class yourself, if none exists. (I don't know python myself, so there might be one, but that's not the point :) )

However in a demo, it can be good to show how to make PureMVC work with some third-party utility or framework.

-=Cliff>


Title: Re: Handling CRUD operations with RemoteObjects on large amount of data
Post by: Joel Hooks on April 15, 2008, 10:47:37
So deriving a HashMapProxy would be the PureMVC way?


Title: Re: Handling CRUD operations with RemoteObjects on large amount of data
Post by: puremvc on April 15, 2008, 10:56:52
Well, no. Using an array as a associative array / hash is the way the framework itself does things, so as not to depend on other frameworks; such as not using Flex's ArrayCollection. This makes it more portable.

This goes for utilities as well. If they can get by with only what the language offers, and then do so. If not, and its difficult or an aside to roll your own in the demo, then consider the reliance on third party utilities carefully.

If distributed on the PureMVC site, it needs to be redistributable under the Creative Commons Attribution 3.0 license. If that's possible, then there's no problem. Otherwise you may have to point people to also download the utility you're depending on from elsewhere, and that can limit the adoption of the utility.

So this isn't a 'always do it yourself' / 'not created here' type mandate, just food for thought in the creation of utilities with outside dependencies.

-=Cliff>