#TECH

Getting Started with DocumentDB

Recently I got an opportunity to explore DocumentDB. It is a managed service provided by Microsoft Azure. Below is brief introduction  and few code samples to get started with DocumentDb. As I dig dipper into it I will share my learnings further.

Relational Database has been there for a long and it is quiet useful in many different situations, but Relational technology is not always the best approach for following reasons:

  1. Scaling relational technology to work effectively across many servers can be challenging
  2. While working with data which has flat structure (JSON), Relational database does not fit the bill.

NoSQL has been introduced to address these issues. Microsoft Azure supports variety of NoSQL technologies like DocumentDB, HBase, Tables for Operational data and HDInsight for Analytical data.

DocumentDB is useful for an application working with JSON data or where the structure is not defined. DocumentDB contains collection of JSON documents which essentially contains the data as oppose to Relational technology where the data is stored in rows and columns.

If I summarize benefits/attributes of DocumentDB then here are few:

  1. DocumentDB is managed service provided by Azure.
  2. To access and modify data it uses RESTful interface.
  3. It also support Sql based query language which means you can write your favourite SQL query.
  4. It is also possible to create stored procedures and triggers that run inside DocumentDB.
  5. It supports hundreds of terabytes of documents of data in single database.

Following are the steps which will get you started with DocumentDB.

1. Create DocumentClient

Code Snippet
  1. private static DocumentClient client;
  2.         public static DocumentClient Client
  3.         {
  4.             get
  5.             {
  6.                 if (client == null)
  7.                 {
  8.                     string endpoint = ConfigurationManager.AppSettings[“endpoint”];
  9.                     string authKey = ConfigurationManager.AppSettings[“authKey”];
  10.                     Uri endpointUri = new Uri(endpoint);
  11.                     client = new DocumentClient(endpointUri, authKey);
  12.                 }
  13.                 return client;
  14.             }
  15.         }

 

2. Create Database

Code Snippet
  1. private static Database database;
  2.         public static Database Database
  3.         {
  4.             get
  5.             {
  6.                 if (database == null)
  7.                 {
  8.                     database = ReadOrCreateDatabase();
  9.                 }
  10.                 return database;
  11.             }
  12.         }
  13.         private static Database ReadOrCreateDatabase()
  14.         {
  15.             var dataBase = Client.CreateDatabaseQuery()
  16.                             .Where(d => d.Id == DatabaseId)
  17.                             .AsEnumerable()
  18.                             .FirstOrDefault();
  19.             if (dataBase == null)
  20.             {
  21.                 dataBase = Client.CreateDatabaseAsync(new Database { Id = DatabaseId }).Result;
  22.             }
  23.             return dataBase;
  24.         }

 

3. Create Collection

Code Snippet
  1. private static string collectionId;
  2.         public static String CollectionId
  3.         {
  4.             get
  5.             {
  6.                 if (string.IsNullOrEmpty(collectionId))
  7.                 {
  8.                     collectionId = ConfigurationManager.AppSettings[“collection”];
  9.                 }
  10.                 return collectionId;
  11.             }
  12.         }

 

4. Now basic repository base class can be written as below:

Code Snippet
  1. public class RepositoryBase<T> : IRepositoryBase<T> where T : class
  2.     {
  3.         /// <summary>
  4.         /// Find all documnet inside a collection
  5.         /// </summary>
  6.         /// <param name=”documentLink”></param>
  7.         /// <returns></returns>
  8.         public async Task<DocumentCollection> FindAll(string documentLink)
  9.         {
  10.             var document = await DocumentClientHelper.Client.ReadDocumentCollectionAsync(documentLink);
  11.             return document;
  12.         }
  13.         /// <summary>
  14.         /// Find a matchinng document
  15.         /// </summary>
  16.         /// <param name=”documentLink”></param>
  17.         /// <param name=”id”></param>
  18.         /// <returns></returns>
  19.         public Document FindById(string documentLink, string id)
  20.         {
  21.             return DocumentClientHelper.Client.CreateDocumentQuery(DocumentClientHelper.GetOrCreateCollectionAsync(documentLink).Result.DocumentsLink)
  22.                                 .Where(d => d.Id == id)
  23.                                 .AsEnumerable()
  24.                                 .FirstOrDefault();
  25.         }
  26.         /// <summary>
  27.         /// Save a document
  28.         /// </summary>
  29.         /// <param name=”collectionLink”></param>
  30.         /// <param name=”entity”></param>
  31.         /// <returns></returns>
  32.         public async Task<bool> Save(string collectionLink, T entity)
  33.         {
  34.             var document = await DocumentClientHelper.Client.CreateDocumentAsync(collectionLink, entity);
  35.             return document == null ? false : true;
  36.         }
  37.         /// <summary>
  38.         /// Update a document
  39.         /// </summary>
  40.         /// <param name=”collectionLink”></param>
  41.         /// <param name=”entity”></param>
  42.         /// <param name=”id”></param>
  43.         /// <returns></returns>
  44.         public async Task<bool> Update(string collectionLink, T entity, string id)
  45.         {
  46.             Document doc = FindById(DocumentClientHelper.CollectionId, id);
  47.             var document = await DocumentClientHelper.Client.ReplaceDocumentAsync(doc.SelfLink, entity);
  48.             return document == null ? false : true;
  49.         }
  50.         /// <summary>
  51.         /// Delete a document
  52.         /// </summary>
  53.         /// <param name=”documentLink”></param>
  54.         /// <param name=”id”></param>
  55.         /// <returns></returns>
  56.         public async Task<bool> Delete(string documentLink, string id)
  57.         {
  58.             Document doc = FindById(DocumentClientHelper.CollectionId, id);
  59.             var document = await DocumentClientHelper.Client.DeleteDocumentAsync(doc.SelfLink);
  60.             return document == null ? false : true;
  61.         }
  62.     }

 
Hope this helps you to get started with DocumentDB, please let me know if you face any issue while setting up DocumentDB.