Story Details for articles

2 - DocumentDB - Golf Tracker Article - Techniques

Date Posted:
5/26/2015 6:32:28 PM
Date Updated:
5/26/2015 6:32:28 PM
0/0 votes
DocumentDB, WebApi
Programming Language:
C#, JavaScript
DocumentDB, AngularJS, WebApi
documentdb, angularjs, webapi
Demo site:
Home Page:

Data Management Made Easy

In order to store JSON documents of different shapes into the same collection, there has to be a way to query for a collection of documents by a particular type.  The easiest way to do this is simply that, including an attribute to the JSON document called "type" and labeling it based on the type of document that it is.

The JSON document might look like this:

02.  "Name": "Joe Golfer",
03.  "Handicap": 15,
04.  "IsPlus": false,
05.  "Rounds": [
06.    {
07.      "Score": 87,
08.      "NetScore": 70,
09.      "DatePlayed": "2015-05-15T19:05:07.026Z",
10.      "DateEpoch": 1431716707,
11.      "GolfCourse": {
12.        "GolfClubName": "Desert Willow",
13.        "GolfCourseName": "Mountain View",
14.        "TeePlayed": {
15.          "TeeName": "Blue",
16.          "Gender": "Mens",
17.          "Length": 6470,
18.          "Slope": 128,
19.          "Rating": 71.5,
20.          "Par": 72
21.        }
22.      }
23.    }
24.  ],
25.  "id": "4df3342e-4312-4f87-b5a8-8c182bd5ca60",
26.  "type": "golfer"

In this case I have two types of documents, a Golfer, and Golf Clubs.  So I can then simply add the "type" attribute to the JSON document and label it either "golfer" or "golfclub".  But really, that would be a pain to have to remember to include that "type" attribute every time I'm creating a new document and I won't be working with JSON documents in my WebApi project, I'll be working with .Net classes.  There's an easy solution for that.


I've created an EntityBase class that holds the Id field for each document, plus the "type" property that will be loaded with the value for the derived class.  Let me show you what I mean.

The Golfer Class

The golfer class contains any number of properties, but I don't want to worry about whether or not it has the "type" property, so I just inherit from the EntityBase class, and set a value in the base constructor, and I'm done!

Here's a look at the golfer C# class:

public class Golfer : EntityBase
    /// <summary>
    /// Pass the lowercase string name of the class to the base class.
    /// This is used in the repository for storage and querying, to organize
    /// documents by this type name.
    /// </summary>
    public Golfer()
        : base("golfer")
    public string Name { get; set; }
    public decimal Handicap { get; set; }
    public bool IsPlus { get; set; }
    public List<Round> Rounds { get; set; }

You'll see that it inherits from the EntityBase class and simply passes in the name of the class itself in lowercase to the base constructor, and that's all that's necessary.

When it comes time to adding additional properties to this class, I can just do it and I'll be done.  The class is still going to be part of the "golfer" type and it will be returned as a collection based on that type.

Here's a look at the EntityBase class:

/// <summary>
/// This is the base class that all root entity classes inherit.  This will
/// allow the type of entity to be passed in which is used for the
/// Where predicate in the RepositoryBase class.
/// </summary>
public class EntityBase
    private readonly string _type;
    /// <summary>
    /// All root entities inherit this base class.
    /// </summary>
    /// <param name="type">The name of the type of entity (lowercase).</param>
    public EntityBase(string type)
        this._type = type;
    /// <summary>
    /// This is need for querying in the RepositoryBase. Used by DocumentDB.
    /// </summary>
    [JsonProperty(PropertyName = "id")]
    public string Id { get; set; }
    /// <summary>
    /// This type field will be used to organize the documents by "type" in
    /// DocumentDB in a single-collection scenario.  The type is just the lowercase
    /// name of the derived class.
    /// </summary>
    public string type { get { return _type; } }

When the "golfer" type value is passed into the EntityBase constructor, it gets returned as the value for the "type" property.  And when the .Net Golfer class is serialized by the JSON serializer to be stored in DocumentDB, the "type" property just goes along with it and it serialized along with all the other properties and values.

It's important to know that you only need to inherit the EntityBase class on the "root" class.  Any of the embedded classes should not inherit from the EntityBase class.

This isn't all that's needed to make this work, but it's the basis.  In the Visual Studio Deep Dive article, I'll go over how it's implemented in the RepositoryBase class.

Continue on to see a demonstration of how the client operations work.


    No comments yet.


User Name: