My Hopes for Azure DocumentDB Client
I have been playing around with Azure DocumentDB and took to twitter to vent about the client .net library. I was quickly asked to clarify my frustrations.
Here is an example of what it currently takes to get a document:
let client = new DocumentClient(uri, authKey)
let db = client.CreateDatabaseQuery().Where(fun x -> x.Id = "ItemDB").AsEnumerable().FirstOrDefault()
match db with
| null -> failwith "DB not found"
| _ ->
let collection = client.CreateDocumentCollectionQuery(db.CollectionsLink).Where(fun c -> c.Id = "Items").AsEnumerable().FirstOrDefault()
match collection with
| null -> failwith "Collection not found"
| _ ->
let item = client.CreateDocumentQuery<Item>(collection.DocumentsLink).Where(fun d -> d.Id = "1").AsEnumerable().FirstOrDefault()
I tried to simplify .Where(fun …).AsEnumerable().FirstOrDefault() down to just .FirstOrDefault(fun …) but it threw an error. So I stuck with what was in the examples.
What I want is:
let client = new DocumentClient(uri, authKey, "ItemDB")
let item = client.Query<Item>().FirstOrDefault(fun i -> i.Id = "1")
I feel like this would solve the 80% use case. The database can be part of the client and if you need to switch or access another database just create a new client. Next, I feel like the majority people that are not going to use the collection or will not care. It will probably just be a plural of the document type name. You can extract information from the type provided in the Query<T> method to get a collection and it also allows you to do the json deserializing, and provide the type for Linq query.
Let’s take a look at creating a new document and collection.
let item = { Id = "testing"}
let client = new DocumentClient(new Uri(endpoint), authKey)
let db = client.CreateDatabaseQuery().Where(fun x -> x.Id = "ItemDB").AsEnumerable().FirstOrDefault()
match db with
| null -> failwith "DB not found"
| _ ->
let def = new DocumentCollection()
def.Id <- "Issues"
let collection = client.CreateDocumentCollectionAsync(db.SelfLink, def).Result.Resource
client.CreateDocumentAsync(collection.DocumentsLink, item).Wait()
Here is what I would like to see.
let item = { Id = "testing"}
let client = new DocumentClient(new Uri(endpoint), authKey, database)
client.Store(item)
Again, I think this would solve the majority case where people don't care about the collection information or don't want to worry about the self-links from the different objects.
To me if they could get it down closer to what I suggested, that would be tremendously helpful, and it would give the brag to my friends about how awesome it is feeling.