The NHibernate 3 Music Store – Part 1
28 August 2010
Ok so I’ve got the code up on Git, got the solution running in VS2008 again (I really need to get VS2010 some time…).
First thing I’m going to do is delete the Models/StoreDB.edmx file which houses the ADO.NET EF4 Entities and object contexts. Obviously this means that just about none of the code will compile, but that’s ok for now :)
This list will obviously get longer once I’ve created the POCOs for these entities and I have to start implementing the properties.
Entities
There were three entities in the StoreDB.edmx:
- Artist
- Cart
- Genre
First up I need to build those out using POCOs. My first objective is to get this to compile again, so I’m just going to create empty classes for now, the rest will come later.
I will also need to build out the Album, Order and AccountModels entities later as at the moment they’re just being used to save metadata for EF4.
Replacing the ObjectContext
NHibernate is based on the ISession, rather than EF4’s concrete ObjectContexts. Where the MVC Music Store uses MusicStoreEntities, I’m going to change that out with an interface – IMusicStoreContext.
namespace MvcMusicStore.Models
{
public interface IMusicStoreContext
{
ISession Session { get; }
IQueryable<Album> Albums { get; }
IQueryable<Artist> Artists { get; }
IQueryable<Cart> Carts { get; }
IQueryable<Genre> Genres { get; }
IQueryable<Order> Orders { get; }
}
}
I don’t believe in locking the ORM behind layers of abstraction, when I build out a concrete implementation of the IMusicStoreContext I want to be able to address my NHibernate ISession directly if needed. In most scenarios however, just exposing the IQueryable that’s now available in NH3 should suffice.