“Last time, on Bill DeLude dot com…”
Hmm, it kind of loses its effect without a good narrator voice. Does someone have Morgan Freeman’s number? Because he could say that last time I talked about creating a data repository class using DocumentDB as the data store and covered the setup of the class and the read methods. This time, I’m going to go over the write methods.
But first a disclaimer before we get started. Not everything in this class is complete. As with normal programming, I like to take a highly iterative approach. First, I’ll make it work. Then I’ll improve some part of it. And then I’ll improve some other part, and so on. There’s two things conspicuously missing from today’s code. First, the write methods, as they are offered up, are synchronous. Second, it doesn’t allow for the Equipment type to change. I’ll address both of these in a later post.
First, I’m going to work on adding Equipment to the collection. Here’s the implementation of the AddEquipment method:
public Equipment AddEquipment(Equipment equipment)
{
dynamic doc = Client.CreateDocumentAsync(CollectionLink, equipment)
.Result.Resource;
Equipment result = doc;
return result;
}
Adding a document to a collection in DocumentDB is pretty straight-forward. All you really have to do is call CreateDocumentAsync with the link to the collection and the document you’re adding.
I made it a little more complicated because I like to return the added document back to the caller, so they can have any changes such as the id or a timestamp. To accomplish that, I need to get the Resource of the CreateDocumentAsync result as a dynamic type. Then I put that into an Equipment type and return the equipment.
Now that we can add documents, let’s make sure we can update them. Updating in DocumentDB is a two step process. You first have to get a document link by reading the collection, and then you replace the document with your changed version. Gone are the days of “UPDATE Equipment SET …” because DocumentDB, as with most NoSQL databases, just doesn’t work that way. Here’s the code for updating:
public Equipment UpdateEquipment(string id, Equipment equipment)
{
var doc = Client.CreateDocumentQuery(CollectionLink)
.AsEnumerable().First(d => d.Id == id);
dynamic updatedDoc = Client.ReplaceDocumentAsync(doc.SelfLink,
equipment).Result.Resource;
Equipment result = updatedDoc;
return result;
}
Notice that when I get the document this time, I’m not specifying a type to return. By omitting the return type, I get it as a Document and not as an Equipment. I want to do that because Equipment doesn’t have the self link I need to do the replace on the next line. Once you have the document, replacing it is pretty simple. Just call ReplaceDocumentAsync with the document’s self link and the new document. Returning the updated equipment requires some extra work, but it’s just like the Create.
Finally, we need to delete documents from the collection. This works a lot like updating: get a self link, then call a delete method. It looks like this:
public void DeleteEquipment(string id)
{
var doc = Client.CreateDocumentQuery(CollectionLink)
.AsEnumerable().First(d => d.Id == id);
Client.DeleteDocumentAsync(doc.SelfLink);
}
And that’s it. Now you have a full blown Equipment repository using DocumentDB as the data store.
The code that was used at the time of this writing is in my github at this commit.