As I promised in one of my earlier threads that I’ll try to compile an example of RavenDB and here it is . But first we need to know what an Open database is and why traditional relational databases have started acting as a bottleneck when it comes to cloud computing and horizontal growth .
*NoSQL (Wiki) : In computing, NOSQL (commonly interpreted as “not only SQL”[SUP][1]](NoSQL - Wikipedia)[/SUP]) is a broad class of database management systems identified by non-adherence to the widely used relational database management system model. NoSQL databases are not primarily built on tables, and generally do not use structured query language for data manipulation.*NoSQL database systems are often highly optimized for retrieve and append operations and often offer little functionality beyond record storage (e.g. key-value stores). The reduced run time flexibility compared to full SQL systems is compensated by significant gains in scalability and performance for certain data models.
*In short, NoSQL database management systems are useful when working with a huge quantity of data and the data’s nature does not require a relational model for the data structure. The data could be structured, but it is of minimal importance and what really matters is the ability to store and retrieve great quantities of data, and not the relationships between the elements. For example, to store millions of key-value pairs in one or a few associative arrays or to store millions of data records. This is particularly useful for statistical or real-time analyses for growing list of elements (such as Twitter posts or the Internet server logs from a big group of users).
*Here is the link to RavenDB’s website, you can read more about it here : http://ravendb.net
Let me first tell you how to set it up.
- First you’ll need to setup the RavenDB server. Download whole package from here (Hibernating Rhinos - Build Server).
- Extract it to a folder and run RavenDB-Build-888\Server\Raven.Server.exe
- You’ll see console screen running the server and will be showing the log. By default RavenDB runs on http port 8080, so you can access a web interface for this server at (https://dev.gupshup.org).
Now I’ll dive right in to the code, but before that lets talk about two very important interfaces.
IDocumentStore : This is the interface to main repository and its expensive to initialize it. So do it only once in your application. This is how you initialize it.
IDocumentStore DS = new DocumentStore { Url = “https://dev.gupshup.org” }.Initialize();
IDocumentSession: This creates a session to talk to your database. This is how you can use it
IDocumentSession session = DS.OpenSession()
I have a simple class that I’ll try to save in RavenDB. Here is my class
public class Student
{
public string Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
**Add :
**public void Add(Student student)
{
using (var session = DS.OpenSession())
{
session.Store(student);
session.SaveChanges();
}
}
**Update:
**public void Update(Student student)
{
using (var session = DS.OpenSession())
{
session.Store(student);
session.SaveChanges();
}
}
**Delete:
**public bool Delete(Student student)
{
using (var session = DS.OpenSession())
{
session.Delete(session.Load(student.Id));
session.SaveChanges();
return true;
}
}
**Select/Search:
**public IList Select(string id)
{
using (var session = DS.OpenSession())
{
if (string.IsNullOrEmpty(id))
return session.Query().ToList();
else
return session.Query().Where(o => o.Id == id).ToList();
}
}
LINQ is official language of accessing RavenDB. They also have REST api to communicate using any non-.net consumer framework. This is pretty straightforward, for now, but in future I’ll try to add more complex examples for advance usage.
Please give it a go and come up with questions. It will push me to find answers and we all can learn this new technology that we’ll be using widely in couple of years anyways.