Nov 03

The DataContext component: getting started with identities

Posted in Javascript MS AJAX      Comments Off on The DataContext component: getting started with identities

We’ve already met several interesting features of the DataContext component. Today we’ll keep going and we’ll start discussing identity. I’m not sure if you’ve noticed, but the DataContext exposes several identity related methods. For instance, the getIdentity method is used by the control to get the identity of an object.

In order for you to activate identity related features, you need to (at least) configure the component so that it knows how to get or create a new identity for an object. This is done by setting two properties:

  • getIdentityMethod: function used for getting the identity of an object. This method expects two parameters: the first references the DataContext; the second, references the entity you’re interested in getting the ID of;
  • getNewIdentityMethod: function which is used for creating a new identity for an object. This method expects three parameters: the first two match the getIdentityMethod’s parameters; the last one is an optional string which identifies the type of object that is asking for a new identity.

Setting these properties is all that is need for activating identity support for a DataContext component. Notice that using identities means  that you’ll end up using the internal _items object for storing all the objects maintained in the current DataContext component. This object is used as a dictionary, where the identity of each object is used as the key of a specific entry. In practice, this means that you can mix object of different types in the same _items dictionary (we’ll talk about this in a future post).

As you’ve probably guessed, identity is one of those features which exists (mainly) for supporting ADO.NET data service. However, nothing prevents us from exploring it, right? So let’s get starting…and we’ll start simple…Suppose you want to track changes to the array presented in the next snippet:

var people = [
    { id: 1, name: "luis", address: "fx" },{ id: 2,name: "john", address: "fx" },
    { id: 3, name: "peter", address: "fx" }

In this case, things are pretty simple, right? We’re only interested in managing simple objects (lets call them person instances) and that means that we can use the id value as the identity of each object. There’s still one little problem: what to do with new items?

In this case, and since we’re supposed to create a function for calculating new identities, we can write it so that it also sets the id of any new person object. Here’s the code needed for instantiating a DataContext component which does exactly that:

var ctx = Sys.create.dataContext(
    getIdentityMethod: function (dataContext, entity) {
    getNewIdentityMethod: function (
dataContext, entity, entitySet) {
if ( === undefined) { var contextItems = ctx.get_items(); var max = 0; for (var id in contextItems) { if (parseInt(id) > max) { max = parseInt(id); } } = max + 1; } return; } } );

With these properties sets, we can add new objects without having to worry with the ID of a new person instance:

ctx.insertEntity({ name: "joseph", address: "fx" });

The next image shows the changes made to the DataContext instance after the previous insert:


As you can see, identity activation leads to several changes:

  • new elements get added to the private _items dictionary with the new identity used as the key;
  • the new inserted object is updated: a new id property is added and set to the new identity which was calculated by the DataContext’s newIdentityMethod function;
  • the _inserts array references only the id of the item that was inserted (notice the id$4 entry in the previous image).

The example we’ve seen is really intended to get you started with identities. Unfortunately, things aren’t so easy in the real world. there will be times where you can’t really set the ID of an item (ex.: suppose it gets mapped to an auto number field of a table). There will also be other times where you need to support several “types” of objects…in these scenarios, you’ll need a more advanced way to identifying each instance.

In the next post we’ll take a look at more advanced ways of generating IDs for these scenarios. Stay tuned for more on MS AJAX.