LA.NET [EN]

Nov 08

Before we can move on and take a look at the AdoNetDataContext component, we need to talk about one more topic: creating entities and how it might be integrated with DataContext component. The DataContext exposes a createEntity method which can be used for  creating new objects of a specific type. Internally, that method will always delegate the creation of a new entity to the function to which the createEntityMethod property points to.

As you’re probably wondering, this is something you’ll only need to configure if you need to use the DataContext features associated with identity (and even in that case, setting the createEntityMethod is optional if you’ve set the getNewIdentityMethod property).

The createEntityMethod property should reference a function which receives two parameters (a DataContext reference and a string with the “type” of the object that should be created) and initializes the metadata necessary for tracking that object. If you recall the previous posts, you’ll surely remember that we’ve added an internal __meta property to all the objects stored by the DataContext instance.

We could improve our DataContext used at the time by setting the createEntityMethod to a function which initializes the metadata associated to that object. Here’s how I’d augment the entityManager we’ve been using with a new method used for creating new entities:

createNewEntity: function (dataContext, entitySet) {
    var obj = {};
    entityManager.getNewIdentity(dataContext, obj, entitySet);
    return obj;
}


As you can see, we’re simply relying in the getNewIdentity method because it is responsible for setting the __meta object used for that purpose. Referencing entityManager instead of this might seem little strange at first, but that’s needed because the DataContext won’t call the reference through the entityManager instance (it will simply call the function through the reference it got to it during initialization:



var ctx = Sys.create.dataContext(
  {
      serviceUri: "PeopleService.svc",
      getIdentityMethod: entityManager.getEntityId,getNewIdentityMethod: entityManager.getNewIdentity,isDeferredPropertyMethod: entityManager.isDeferredProperty,
      getDeferredPropertyFetchOperationMethod: 
entityManager.getDeferredProperty, createEntityMethod: entityManager.createNewEntity } );


And that’s it. There’s really not much to say about the createEntity method. Stay tuned for more on MS AJAX.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>