Look at Raven DB Integration [NoSQL] with ASP.NET MVC 4 Application

In this article, we will look into NoSQL databases, How Raven DB implements it and how to integrate Raven DB with MVC 4 application. NoSQL [Not Only SQL] database is an alternative to traditional RDBMS systems like SQL Server, Oracle and it is having simple, lightweight mechanism for storage and retrieval of data that provides higher scalability and availability than traditional relational databases. NoSQL databases are highly used in Big Data systems, where the data's nature does not require a relational model and most of the data is semi-structured or unstructured. Raven DB, Mongo DB are few of them providing NoSQL Databases.

      RavenDB is a transactional, open-source Document Database [NoSQL] written in .NET offering a flexible data model designed to address requirements coming from real-world systems. RavenDB allows you to build high-performance, low-latency applications quickly and efficiently. RavenDB stores data schema-less as JSON documents within the database, and can be queried using LINQ queries or using RESTful API.

Let’s take an example of storing an employee record in RDBMS vs NoSQL. In RDBMS, our database design looks like as shown below:

When it comes to NoSQL specifically Raven DB, structure looks like as shown below:

public class Employee { public int EmployeeId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public DateTime DateJoined { get; set; } public Position Position { get; set; } public Department Department { get; set; } } public class Position { public string Name { get; set; } public string Description { get; set; } } public class Department { public string Name { get; set; } public string Description { get; set; } } // Create an instance of Employee for stroing in Raven DB 

 Employee objEmp = new Employee
 {
 FirstName = "AAA",
 LastName = "BBB",
 EmployeeId = 30,
 DateJoined = DateTime.Now.AddYears(-1),
 Department = new Department{ Description = "Software services", Name = "IT" },
 Position = new Position{ Name = "Project Lead", Description = "Project Mgmt Role" }
 };

Above structure doesn’t have any relationships as in RDBMS. This structure is simple, flexible and no need of Joins to get the Employee’s data.

Let's create a sample MVC 4 application to understand it in a better way. Create a new MVC 4 Internet application and name it as RavenDBSample. Go to References under Solution Explorer, click on "Manage NuGet Packages" and install Raven DB Client [API] & Server [to host Database] as shown below:

We can host database on Raven DB in below ways:

1) As a stand-alone process 

2) As a Windows Service

3) Embedded within the.NET  application

4) On IIS

In this example, we host the database as stand-alone process. Now, run Raven.Server.exe under ~\RavenDBSample\packages\RavenDB.Server.2.5.2700\tools as shown below:

From above console, we know that our database is listening on https://machinename:8080/. Let's insert and retrieve our Employee object from Raven DB database. Modify Global.asax.cs to create our DocumentStore:

 public class MvcApplication : System.Web.HttpApplication
 {
 public static DocumentStore MyStore; 
 protected void Application_Start()
 {
 AreaRegistration.RegisterAllAreas();
 
 WebApiConfig.Register(GlobalConfiguration.Configuration);
 FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
 RouteConfig.RegisterRoutes(RouteTable.Routes);
 BundleConfig.RegisterBundles(BundleTable.Bundles);
 AuthConfig.RegisterAuth();
 MyStore = new DocumentStore { Url="https://localhost:8080"}; 
 MyStore.Initialize(); 
 }
 }

Creating DocumentStore object is time consuming process, so it needs to be created only once per application.Add above class definitions of Employee, Position, Department to AccountsModel.cs and add below code to Index view under Home folder:

 @model RavenDBSample.Models.Employee
 @{
 ViewBag.Title = "Employee Details";
 }
 <fieldset>
 <legend>Employee</legend>
 <table>
 <tbody>
 <tr>
 <td>
 First Name: @Model.FirstName
 </td>
 </tr>
 <tr>
 <td>
 Last Name: @Model.LastName
 </td>
 </tr>
 <tr>
 <td>
 Date of Join: @Model.DateJoined
 </td>
 </tr>
 <tr>
 <td>
 Position: @Model.Position.Name
 </td>
 </tr>
 <tr>
 <td>
 Department: @Model.Department.Name
 </td>
 </tr>
 </tbody>
 </table>
 </fieldset>
 

Now, add below code to Index action in HomeController to save and retrieve Employee data from Database:

 public ActionResult Index() { using (var session = MvcApplication.MyStore.OpenSession()) { Employee objEmp = new Employee { FirstName = "AAA", LastName = "BBB", EmployeeId = 30, DateJoined = DateTime.Now.AddYears(-1), Department = new Department{ Description = "Software services", Name = "IT" }, Position = new Position{ Name = "Project Lead", Description = "Project Mgmt Role" } }; session.Store(objEmp); session.SaveChanges();//To Commit our changes to DB. //Get Employee back from DB Employee myEmp = session.Query<Employee>().Where(a => a.FirstName == "AAA").First(); return View(myEmp); } }

Run the application, it will show the Employee details. We haven't created any tables in Raven DB and those are created on the fly to store Employee object. I attached source code of this application. We will look into more details of Raven DB with MVC 4 and Web API in coming articles. I hope this article will be helpful for all.

 

RavenDBSample.rar