The application I’m writing isn’t in a very far stadium yet, but I do have some implementation of repositories using NHibernate. Now I want to switch to RavenDB.
So I downloaded RavenDB (the latest build at this time – build 81) and unzipped it. I wanted to follow the RavenDB Hello World tutorial, but soon found out its built for .NET 4.
I don’t have .NET 4 or Visual Studio 2010 yet, but luckily there’s a client for .NET 3.5. I removed all NHibernate references and added one to Raven.Client-3.5.dll in m Dal project.
You’re sort of screwed if you only have Visual Studio 2008 and .NET 3.5 installed. I wanted to use it as an embedded client, but that’s .NET 4.0 only at the moment.
So I started up the server (check the RavenDB Hello World for more info) and surfed to http://localhost:8080 and all looks well.
On of the first things I did, was change the type of the Id properties of my domain objects from a Guid to a string (which was a good moment to introduce a BaseEntity of which other classes can derive). According to the RavenDB Hello World, this is configurable, but I can’t be bothered right now to find out.
I have a BaseRepository that implements the following interface:
public interface IBaseRepository { T GetOneById(Guid id); IList GetAll(); void SaveOrUpdate(T aggregate); void Delete(T aggregate); }
Other repositories inherit from the BaseRepository, and, through their interface, only expose their relevant methods:
public class PatientRepository : BaseRepository, IPatientRepository { public PatientRepository(string connectionStringName) : base(connectionStringName) { } }
Let’s start with the GetAll method. This is the NHibernate way:
public IList GetAll() { ICriteria criteria = Session.CreateCriteria(typeof (T)); return criteria.List(); }
In the constructor of my BaseRepository, I create an IDocumentStore (an expensive thing to do):
_store = new DocumentStore { Url = @"D:\data" }; _store.Initialise();
Then my method creates a session (a cheap thing to do):
public IList GetAll() { using (var session = _store.OpenSession()) { return session.LuceneQuery().ToList(); } }
I put all my other methods in comment, and everything builds and the application works!
I am using the LuceneQuery because the normal Query() method needs an index, which I don’t have yet. I’m still researching how to do this in the best way.
There’s the DocumentStore.DatabaseCommands.PutIndex method (I found a good example on Rob Ashtons blog), but that would execute everytime my application runs. Or you can check out the RavenDB documentation on indexes, but that seems overly complicated.
Next post should be something on switching from NHibernate to RavenDB, but now it’s time for my couch.