Home > Graph Management, JavaScript/EcmaScript, Runtime > Towards Better Graph Loading Via Conditional Types

Towards Better Graph Loading Via Conditional Types

One of my pet peeves is the cruft we often add to our objects to support graph loading. For example, we might create a special “lazy loading” type wrapper (e.g. Lazy) or we might create two independent fields that represent, in effect, the same thing (e.g. Employee MyManager and Guid MyManager_ID). Neither is clean, and each has its issues. I want something better.

JSON-Schema lets us do some cool things, like allowing more than one type to be valid for a given property. For example, I could allow a property to take, say, either a string or an integer. This is called a union type. Cool enough. But how could this help us with graph loading?

Consider something like this:

   "properties":{
       "MyManager":{
           "title":"MyManager",
           "type":[{"type":"string","pattern","(\w|-){22,24}"},"object"],
       }
   }

It’s subtle, but powerful. If accountObj.MyManager is a string type, then we can assume the string is a base64-encoded guid and use that ID to load the object. Once we load the object via another ajax call, we replace the id with the object. We don’t loose data, since the object would be exposing that id as a property (or at least holding it internally). The only downside is we loose our ability to provide a type schema for the MyManager property once the object is loaded.

What I *really* want is the ability to do:

   "properties":{
       "MyManager":{
           "title":"MyManager",
           "type":["http://localhost/schemas/models/employee#/id","object"],
       }
   }

This would allow us to know that the value will be a base64 guid string, but that the loaded object is an employee model. JSON-Schema doesn’t yet allow this yet. Union types cannot include these kind of external references. But the draft standard is evolving and can be extended, as can our implementations of it. The syntax used above *does* however conform to the newly submitted IETF JSON Pointer draft standard, which is an spinoff of the JSON-Schema group.

Either way, it’s a handy trick to add in your collection.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: