creating NHibernate repositories in view models with IoC

Nov 1, 2011 at 4:13 PM

I would like to how to correct build and handle with NHibernate repository and session in view models classes.

For example I use Caliburn Micro framework with Castle Windsor boostraper.

First I created NHibernate repository:

public interface IRepository{//I omitted not needed code for simplification}

public class NHibRepository: IRepository
   private ISession _session;

   public NHibRepository(ISession session)
   // I omitted not needed code for simplification

Second I definied some POCO class and mapping class with Fluent NHibernate.

public class User{}
public class Profile{}
public class Album{}

public class UserMap : ClassMap<User>{}
public class ProfileMap : ClassMap<Profile>{}
public class AlbumMap : ClassMap<Album>{}

Now I need use NHibernate repositories in my view models.

public interface IViewModelA{}

public class ViewModelA : ScreenViewModel, IViewModelA
 public NHibRepository<User> UserRepo{get;set;}
 public NHibRepository<Profile> ProfileRepo{get;set;}


public interface IViewModelB{}

public class ViewModelB : Screen, IViewModelB
 public NHibRepository<Profile> ProfileRepo{get;set;}
 public NHibRepoistory<Album> AlbumRepo{get;set;}

When I am creating repository class for some entity class I need pass session to NHibRepository construtor.

UserRepo= new NHibRepository<User>(NHIBERNATE SESSION);

Until now I used some helper class for creating Nhibernate session and init repository.

Helper class is here:

public class FluentNHibHelper
    private ISessionFactory _sessionFactory;

    public FluentNHibHelper(IPersistenceConfigurer db, Assembly asm)
        InitializeSessionFactory(db, asm);

    private void InitializeSessionFactory(IPersistenceConfigurer db, Assembly asm)
        _sessionFactory = Fluently.Configure()
            .Mappings(m => m.FluentMappings.AddFromAssembly(asm))
            .ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(true, true))

    public ISession OpenSession()
        return _sessionFactory.OpenSession();

with this class I created repository class:

private const string ConnString =

UserRepo = new NHibRepository<User>(
        new FluentNHibHelper(MsSqlConfiguration.MsSql2008.ConnectionString(ConnString),

Now I am confuse I don’t what is the best way for creating repository object in view models.

For example in bootstraper class can I somehow register repository class ?

public class CastleBootstrapper : Bootstrapper<IShellViewModel>
    private IWindsorContainer _windsorContainer;

    protected override void Configure()
        _windsorContainer = new WindsorContainer();
        //register repository class here ???

Can anybody help me? Thank you very much

Nov 1, 2011 at 4:26 PM

This may help:

Nov 1, 2011 at 6:55 PM
dbeattie wrote:

This may help:

Thank you for quick response. I checked this sample. I couldn’t find any boostraper class and code is without commets.

This sample is little complicated for me :(

Any simple solution  without another 3rd part libraries or tutorial.

Nov 2, 2011 at 7:17 AM

Hi Ian,

 Here is the link to a tutorial that covers the windsor and nhibernate

 Is very weird to find this kind of stuff on msdn, I wanted to share this with someone for a wile now.


Hope it helps,