Implementing Polyglot Persistence – Part 5
In this post I will complete the implementation of my MongoDB repository by implementing the Review operations previously defined in my IProductRepository interface. This will also be the post where I visit some of the Create, Update, and Delete API’s of the MongoDB C# Drivers. Everything up to this point has been variations of Read API’s.
However, before I get into the implementation of these methods, I need to revisit my MongoHelper class to support a new collection in MongoDB called “Reviews”, which is the name of the collection where I’ll be storing product reviews.
Update MongoHelper to Support my “Reviews” Collection in MongoDB
In part 3 I first introduced my MongoHelper class and talked about class maps. For the reviews collection, I’ll need a similar class map, but this time only to indicate that the “Id” field in my Review class will be represented as an ObjectId. To achieve this, I added this small piece of code to my RegisterClassMaps method.
While I was in this file, I also added a new read-only property to return a reference to the Reviews collection. The reviewsCollectionName is a string set from an AppSetting in web.config (just like I did for the products collections).
With those few changes in place, I can now proceed to the implementation of the Review operations.
The GetReview Implementation
This method is among the simplest in all the methods I’ll cover in this post and the use case is very straight forward –you just want to retrieve a single review for a product. The way I’ve setup my controller, a review is accessed by including the “Id” of the review in the URL. For example,
Recall that my class map defines the Id property as an ObjectId. So, to retrieve this Id, I just need to look for it in the collection using the FindOneById method.
The GetReviews Implementation
This method returns a list of reviews for a given product Id. Since there could be a large number of reviews, this method also supports some basic pagination. But, to simply return all reviews for a product Product Id, this URL would work.
In this implementation, I’m building a query that will run on the server, searching the Reviews collection for all reviews with a ProdId matching the product Id passed in on the URL. Then, I simply pass that query into the Find method with the necessary pagination (skip, take).
The AddReview Implementation
This is the first method where we actually add something to a collection. The use case here is a user has purchased a product and wants to write a review for it. For this, you need to POST data to the server using this URL and JSON in the Request Body of the message.
You may notice in the JSON that I didn’t specify things like ProdId, Id, and ReviewDate. All of which are properties of the Review class. I chose to set these explicitly in the method. For ProdId, I use the product Id from the URL. For ReviewDate, I just timestamp the document with the current time. And, for Id, I’m generating an new ObjectId before inserting.
The UpdateReview Implementation
To update an existing review, this translates to a PUT verb on my controller with similar JSON in the Request Body for the data I want to update.
In the implementation, I’m applying updates to the properties of the existing review. I’m creating a query to find the review Id passed in. Next, I’m telling MongoDB to update the ReviewDate, Rating, and Comments fields in the review. The Combine method is particularly handy when you need to make updates to multiple fields in the document.
The DeleteReview Implementation
Finally, to remove a review document from the reviews collection, the DELETE verb is set with the URL indicating the id of the review to delete.
Once the reviewId is parsed into an ObjectId, it’s a simple matter of finding the document and calling Remove.
This wraps up my journey through the document database category and MongoDB-as-a-servicefrom Mongolab. The service from Mongolab is very powerful and easy to use. The C# Drivers are a gem and the more I use them the more I appreciate everything they do.
My solution is available here for anyone interested in reviewing the full end-to-end implementation.
In the next part of this series, I’ll start looking at the Key-Value database category.