Geeks With Blogs
Patrice Calve Life's short, have fun

RavenDB is an Open Source, document DB (NoSql)

Links:

- CodePlex: http://ravendb.net/

- WikiPedia on NoSql http://en.wikipedia.org/wiki/NoSQL

RavenDB can run as a server (standalone) or embedded in your application.

Here’s a very brief example of embedding RavenDb in an ASP.Net MVC application, it takes about 5-10 minutes in all.

 

Basic Setup Steps

  • Create an empty Asp.Net MVC Web Application
  • Install NuGet (if not already done)
  • Install RavenDb Embedded (will also install RavenDb Client)
  • Modify Global.Asax (this is where you will start the RavenDb embedded server)
  • That's it, press F5 to build and run....

Note: You “start” RavenDb (server) in the Global.asax by convenience.  Start and Stop of the server is process intensive and you want to do it only once.


Detailed Setup Steps

=> Modify Global.Asax (this is where you will start the RavenDb server)

1) Create a Property

public static DocumentStore Store { get; set; }

 

2)

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            Store = new EmbeddableDocumentStore { DataDirectory = "~/Store", UseEmbeddedHttpServer = true };
            Store.Initialize();

        }

 

 

Validation

There are two ways to verify the installation:

  1. through RavenDb's integrated Management Studio (simply navigate your browser to http://localhost:8080)
  2. or by code…

In this silly example, I'll modify the "HomeController" and the Index page.  Note, to fake some initial data to work with, I'll simply override the HomeController's constructor and save a dummy object to RavenDb if none exist... 


- Create a New RavenPerson Class (this will be my model)
- Override the constructor and store a sample record (just for the sake of testing)
- Override the Index() method to fetch the sample record and pass it to the view
- Override index.cshtml
- F5
 

 

Validation Details

 


=> Create a New RavenPerson Class (this will be my model)

namespace MvcApplicationForRavenDb.Controllers
{
    public class RavenPerson
    {
        public string Id { get; set; }
        public string Name { get; set; }
    }
    public class HomeController : Controller
    { …
=> Override the constructor and store a sample record (just for the sake of testing)

public HomeController()
        {
            using (var session = MvcApplication.Store.OpenSession())
            {
                var rp = session.Load<RavenPerson>("/Person/1");
 
                if (rp == null)
                {
                    rp = new RavenPerson();
                    rp.Id = "/Person/1";
                    rp.Name = "Pat";
                   
                    session.Store(rp,rp.Id);
                    session.SaveChanges();
                }
 
            }
 
        }           


=> Override the Index() method to fetch the sample record and pass it to the view

        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";
 
            using (var session = MvcApplication.Store.OpenSession())
            {
                var rp = session.Load<RavenPerson>("/Person/1");
 
                return View(rp);
            }
        }

 

=> Override index.cshtml

@model MvcApplicationForRavenDb.Controllers.RavenPerson
          
@{
    ViewBag.Title = "Home Page";
}
 
<h2>@ViewBag.Message</h2>
<p>
    To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
</p>
 
<p>The Raven Person should be [@Model.Name]</p>


- F5

 

Welcome to ASP.NET MVC!

To learn more about ASP.NET MVC visit http://asp.net/mvc.

The Raven Person should be [Pat]


Posted on Thursday, August 16, 2012 9:43 PM | Back to top


Comments on this post: RavenDb NoSql Sample Asp.Net MVC Embedded

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Patrice Calvé | Powered by: GeeksWithBlogs.net