The No-SQL thing I met   Leave a comment


seteveDennisI didn’t post on my blog in this month, I just got the time. This month is the one we lost two of the special* people in the tech industry. I know the term special is not enough to talk about them; Steve Jobs and Dennis Ritche. I am not going to talk about them. As SJ said, death is something universal and we are the ones to follow them. I just want to say Rest in peace. The last weeks, I had the chance to work on two things that made me feel good. The first one is the class I am taking online, Database class, at Stanford University. I like it. I just finished watching the first unit of the course. The final part is about Relational database design techniques. It has been almost six years since read my first book on database design; all the normalization and others. Since then, I was working all in a relation database. Microsoft SQL server, Oracle, MySQL and others. My mind is set in a relational database. Whenever one tell me about a problem domain, I start thinking … ‘Oh this will be a one to many relation, this goes to this table and will have this primary key and … ‘. The other part of the challenge continues, how to map the relational model to the objects in the code; Entity framework, NHibernate, LINQ to SQL, and so on.

The other thing that I was working on the past weeks is RavenDb. I have heard the term NoSQL long time ago though I didn’t get the chance to work on any one of them. If you have never tried one, I guess it is time for you to get your hands ready. If you have already experienced one of the many document databases or NoSQL data stores, this is not for you. There are lots of document databases like MongoDb, CouchDb and others. RavenDb caught my attention because it is designed for the dot net platform. Raven is an open source (with commercial option) document database. So, I will just go to the sample application I did for learning to work with it, which you can find it here. It is a simple order management system, where a user comes and orders some product and the system takes the order information, stores it in raven and send an email to the customer. You can get the latest build of Raven from here.

If I was going to do the same application using a relation database, I have to have at least five tables; one for the product, one for the customer, one for the customer category, one for the order items and one for the order itself. The Customer will have a reference to a category and then the the order item will have a reference to the product and the order will have reference to the order items. However, this is how it looks when stored in Raven:

{
“OrderNumber”: “HR-00015”,
“Customer”: {
“FullName”: “Name of Customer 14”,
“Email”: “customer@gmail.com”,
“Category”: {
“Description”: “Category 14”,
“DiscountPercent”: 14.0
}
},
“OrderItems”: [
{
“Product”: {
“ProductName”: “Product Name 14”,
“ProductCode”: “Code”,
“UnitPrice”: 14.0
},
“Quantity”: 14.0,
“Confirmed”: true,
“Acknowledged”: false
},
{
“Product”: {
“ProductName”: “Product Name 14”,
“ProductCode”: “Code”,
“UnitPrice”: 14.0
},
“Quantity”: 14.0,
“Confirmed”: true,
“Acknowledged”: true
},
{
“Product”: {
“ProductName”: “Product Name 14”,
“ProductCode”: “Code”,
“UnitPrice”: 14.0
},
“Quantity”: 14.0,
“Confirmed”: false,
“Acknowledged”: false
}
],
“DateOrdered”: “2011-10-04T01:07:39.8246527+03:00”
}

As you can see, there is no pre defined schema that is used to store the Order object. It is a complete object with all it’s references in it; the order items along with the products and customers in it. The order management system is a simple MVC application. Assuming that there is another view which is used to fill the order information, validating all the things, the Order object will be forwarded to this order controller. This is not a recommended way of doing it, but I put the code to store the object in to Raven inside the controller. I didn’t finish the view that will post the order information.

using (var session = DocumentStoreHolder.DocumentStore.OpenSession())

            {

                session.Store(order);

                session.SaveChanges();

                foreach (var orderItem in order.OrderItems)

                {

                    orderItem.Acknowledged = true;

                }

                session.SaveChanges();

                ViewData["Message"] =

                    "Your licences will be sent to the email address provided. We thank you.";

                CommandExecuter.ExcuteLater(new SendEmailCommand(order));

            }

 It creates a session variable just like entity framework context object and it’s this session object that does all the magic. In the above code the Store method takes the object and stores it in memory, when the SaveChanges method is called it will be written to the database. One of the selling points of No SQL databases is the fast read and write capability. The session object is also used to Load a single record with id, to update or delete. The Load method is used in the ‘Confirmation’ Action of the Order controller. The other part of the sample application is just a fancy way of sending the email, so that the user enjoys while we process the sendingof the email in another task. You can see the full sample of working a long running task in another process on a sample application, similar to stack overflow site, Raven Overflow and Racoon Blog which are both on git hub. I will have another post on this issue soon, with details and may be a better sample application. If you want to try the sample application, you will need to get the Client API for for dot net framework 4.0 either using NuGet or from the Client folder of the latest build from github.
Advertisements

Posted October 19, 2011 by Behailu S. in Dot net

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: