NoSQL Zone is brought to you in partnership with:

I'm an independent consultant working in Italy with more than 8 years of experience in designing and developing application for .NET framework, both in windows and Web environments. I'm particularly involved in Continuous Integration strategies, and designing infrastructure of applications, since I'm a lover of pattern and Application Lifecycle Management. I'm a great fan of Communities and I'm a co-founder of DotNetMarche, an Italian community focused on .NET development and I love blogging about technology. Ricci is a DZone MVB and is not an employee of DZone and has posted 42 posts at DZone. View Full User Profile

NoSQL: A Life Without Schema

02.06.2012
| 5495 views |
  • submit to reddit

NoSql is not a replacemente for SQL databases, but it is a valid alternative for a lot of situations where standard SQL is not the best approach to store your data. Since we were taught that whenever you need to store data on a “data store” and you need to query that data for retrieval, SQL is the best solution, you have only to decide what Sql Engine to use and the game is done.

In 2012 this sentence has proven wrong, what I mean is that is not possible to assume anymore that SQL is the “only way to go” to store data; but you should be aware that other alternatives exists and it is called NO SQL. Under this term we have various storage engines that are not based on SQL and in .NET we have an exceptional product called RavenDB (you can find a really good introduction to RavenDb in Mauro’s Blog).

The first big difference with standard Sql is being Schemaless. One of the most annoying restriction of Sql Server is the need to specify exactly the format of the data you want to store inside your storage. This is needed for a lot of good reason, but there are situation when you really does not care about it, especially if your software is heavily based on OOP concepts. Suppose you have this object

   1: class Player

   2: {

   3:     public String Name { get; set; }

   4:  

   5:     public DateTime RegistrationDate { get; set; }

   6:  

   7:     public Int32 Age { get; set; }

   8: }

 

For a moment do not care about the fact that this object is not well encapsulated (it has public getter and setter) but focus only on the need to “store” this object somewhere. If you use a standard Sql storage, first of all you need to create a table, then define columns, decide maximum length for the Name column and finally decide an ORM to use or build a dedicate data layer and finally you can save the object.

If you work with raven, this is the only code you need

   1: var store = new DocumentStore { Url = "http://localhost:8080" }; 

   2: store.Initialize();

   3: using (var session = store.OpenSession())

   4: {

   5:     var player = new Player

   6:     {

   7:         Age = 30,

   8:         RegistrationDate = DateTime.Now,

   9:         Name = "Alkampfer",

  10:     };

  11:     session.Store(player);

  12:     session.SaveChanges();

  13: }

I simply created a DocumentStore based on a local server, opened a session and saved an object, I did not defined anything on the server, I did not need to have an ORM, the server simply takes the object and save it, period!

I liked very much this approach because

I needed to save an object to a data storage and everything I need is just a two function all, Store to tell the storage the object I want to save and SaveChanges that actually do the save.

What I got with this simple snippet of code? Just browse with a standard browser to the address of the server and you should see the content of the database.

image

Figure 1: Content of the database after insertion of a simple object

From Figure 1 you can see content of the raven database, it contains a player and the little 1 beside the object is the Id that Raven uses internally to uniquely identify that object. The other object called Sys Doc Hilo/players takes care of id generation for Players object with an Hilo algorithm.

That’s all folks, no need to define schema, no need to have special Id property or any other requirement to make the object compatible with the store, just call Store method on whatever .NET object and your object is inside the database, Period!.

This is only a scratch of the many functionalities of RavenDb :) , more to come in my blog and in Mauro’s one.

Gian Maria.

Source:  http://www.codewrecks.com/blog/index.php/2012/02/04/nosql-and-a-life-without-schema/

Published at DZone with permission of Ricci Gian Maria, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)