I have just read this blog post from Jimmy Bogard and was drawn to this comment.

Where this falls down is when a component doesn’t support a given layering/architecture. But even with NHibernate, I just use the ISession directly in the controller action these days. Why make things complicated?

I then commented on the post and ask this question:-

my question to jimmy

My question here is what options would you have testing the controller logic IF you do not mock out the NHibernate ISession.

I am curious what options we have if we utilise the ISession directly on the controller?

  • I think my main gripe with your entire question is that the term "controller logic" here seems to infer that your controllers are full of business logic that, ideally, shouldn't be there. Can you provide an example of when you might use an ISession from within a controller? Sep 14, 2012 at 13:17
  • Ah good point my question does infer this, however one problem is where does the business logic sit is it also in the controller? I understand it isn't ideal to have any BL in controller's but sometimes it creeps in, if you look at the blog post Jimmy is calling the database (albeit a RavenDB) straight from the controller. He goes on to mention that he uses NHibernate in same way so my main question is more about testing the ISession with unit tests.
    – Rippo
    Sep 14, 2012 at 13:34
  • freshbrewedcode.com/derekgreer/2012/04/01/…
    – m0sa
    Feb 11, 2013 at 12:52

1 Answer 1


Use a light weight in-memory database that's NHibernate supported.

Or you can use seams on the controller, extract code that uses ISession to a separate method, make it virtual. Override it in a class on which you run your tests.

If you do these methods you may as well put them on a repository interface though.

  • +1. That's actually easier than mocking everything out of session. Not to mention that you can't directly mock the NHibernate.Linq Query<T> as you can (try) with QueryOver<T>. Swapping to an in-memory SQLite database is easy enough with NHibernate. Jun 11, 2013 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.