Using RavenDb with Mvc WebApi

 

I’m starting this post with a disclaimer. I’m not a RavenDb expect, actually far from it, in fact I’ve only started working with it this evening!

I’m working on a pet project and getting the initial building blocks in place. I read about RavenDB in a recent edition of CODE magazine. The same magazine dealt with some other NoSql databases, e.g. Mongo Db, I’ve had a passing interest in NoSql in the last while so I wanted to get me a piece of the pie, and RavenDB jumped out at me for a few reasons.

  • Written in .NET
  • Scalability over RDBMS
  • RestAPI (although i won’t be using it, my app will have its own REST API using RavenDb managed api underneath)
  • .NET API with Linq querying
  • Automatic indexing
  • Scalability

    Create project

    Fire up visual studio and create an new MVC4 project

    Add the RavenDb package,

    Global.asax.cs

    Do the very same that is indicated on the Raven website, We need a single Document Store in our application.

     protected void Application_Start()
            {
                AreaRegistration.RegisterAllAreas();
                            
                RegisterGlobalFilters(GlobalFilters.Filters);
                RegisterRoutes(RouteTable.Routes);
    
                BundleTable.Bundles.RegisterTemplateBundles();
    
                InitRaven();
            }
    
            private void InitRaven()
            {
                Store = new DocumentStore { ConnectionStringName = "RavenDB" };
                Store.Initialize();
    
                IndexCreation.CreateIndexes(Assembly.GetCallingAssembly(), Store);
    
            }
    
            public static DocumentStore Store;

    Create RavenController

    public abstract class RavenController : ApiController
        {
            public RavenController()
            {
                this.AutoSave = true;
                RavenSession = TickTockBaby.Mvc.WebApiApplication.Store.OpenSession();
            }
    
            public bool AutoSave { get; set; }
    
            public IDocumentSession RavenSession { get; protected set; }
    
            protected override void Dispose(bool disposing)
            {
                if (disposing)
                {
                    if (RavenSession != null)
                    {
                        using (RavenSession)
                        {
                            if (this.AutoSave)
                                RavenSession.SaveChanges();
                            RavenSession.Dispose();
                            RavenSession = null;
                        }
                    }
                }
    
                base.Dispose(disposing);
            } 
        }

    Let me explain what I’ve done here…

    I’ve initialized the session in the constructor, and cleaned it up in dispose, I also SaveChanges by default unless it gets switched off in a derived class.

     

    Derive from RavenController

    public class UsersController : RavenController
        {       
            // GET /api/values
            public IEnumerable<string> Get()
            {
                this.AutoSave = false;
                return RavenSession.Query<User>().Select(u => u.Name);            
            }        
        }

    That’s my initial attempt, hope it helps someone out a little.

    I’ll post my final solution possibly using Ninject DI after I’ve used RavenDb for a while and get a better feel for it.

    Raven Studio


    Check results in browser

    Comments (1) -

    Brian Keating
    5/1/2012 4:00:09 AM #

    I guess it's because of my disclaimer :-). I'm new to Raven :-)

    Where would you suggest I use it?
    Would I get a race condition in my Get (as it exists now).
    If the same thread is used in constructor and then the get then i guess not..
    But I'd have to check this out in the source, also I wonder about the ExecuteAsync on controllers, would i then again be worries about thread syncronization?

    So thanks, you've given me something to look into, if you have the answer already for me it would be appreciated save me digging about in the WebStack source tonight.

    cheers
    Brian.

    Comments are closed

    Recent Tweets

    Note: For Customization and Configuration, CheckOut Recent Tweets Documentation

    Calendar

    <<  October 2017  >>
    MoTuWeThFrSaSu
    2526272829301
    2345678
    9101112131415
    16171819202122
    23242526272829
    303112345

    View posts in large calendar

    Month List