Switching from NHibernate to RavenDB

In my previous post, I explained how I went about starting with RavenDB. Now I want to continue my switch from NHibernate to RavenDB. The GetAll method was fairly simple. I also have a GetOneById method in my BaseRepository, but, as I’m not using it right now, I decided to remove it (YAGNI you know).

Then, I changed the my BaseRepository to enforce the T to be a BaseEntity:

public class BaseRepository<T> : IBaseRepository<T> where T : BaseEntity

I thought this would be necessary to check the Id in my SaveOrUpdate method to see if it’s a new object, or one that has already been saved and has to be updated. I found that in Shiju Varghese’s NoSQL with RavenDB and ASP.NET MVC-Part1. Yet it seems unnecessary. Maybe it’s because he’s working with a web project, but I can just call Store() and SaveChanges() and my object is either saved or updated.

public void SaveOrUpdate(T aggregate)
{
    using (var session = _store.OpenSession())
    {
        session.Store(aggregate);
        session.SaveChanges();
    }
}

Keep in mind that the objects aren’t persisted until you call SaveChanges(). Also, only then are the Id’s generated for your objects (if it’s an insert). This method is also transactional.

It should be fairly clear by now. My Delete method:

public void Delete(T aggregate)
{
    using (var session = _store.OpenSession())
    {
        session.Delete(aggregate);
        session.SaveChanges();

    }
}

I also had a PersistAll method in my IBaseRepository, which I removed. Must have copied that from the interface at work :)

So that’s about it. My switch from NHibernate to RavenDB was fairly easy thanks to the BaseRepository I’m using (and it’s a fairly simple application at the moment, I admit).

Next up, is seeing if I can run the RavenDB server when my application starts, and stopping it when it exits. Also, I did a LuceneQuery in the GetAll method (see Starting up RavenDB with .NET 3.5), but I want to check out RavenDB’s indexes.

Now, compare this piece of code, with what you get when you use NHibernate. The BaseRepository for NHibernate might not differ that much, but you need the configuration, the mapping files, the database, … With RavenDB, I just need RavenDB and my BaseRepository. RavenDB takes care of the persistence for you, so you can concentrate on your application. That’s what I’m looking for in a persistence framework.

This is my BaseRepository now:

public class BaseRepository<T> : IBaseRepository<T> where T : BaseEntity
{
    private IDocumentStore _store;

    protected BaseRepository()
    {
        _store = new DocumentStore { Url = @"http://localhost:8080" };
        _store.Initialise();
    }

    public IList<T> GetAll()
    {
        using (var session = _store.OpenSession())
        {
            return session.LuceneQuery<T>().ToList();
        }
    }

    public void SaveOrUpdate(T aggregate)
    {
        using (var session = _store.OpenSession())
        {
            session.Store(aggregate);
            session.SaveChanges();
        }
    }

    public void Delete(T aggregate)
    {
        using (var session = _store.OpenSession())
        {
            session.Delete(aggregate);
            session.SaveChanges();
        }
    }

}

Yes, the url of the documentstore shouldn’t be hard-coded, but that’s on my todo-list.