Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7993215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:47:07+00:00 2026-06-04T13:47:07+00:00

I have an MVC app that uses NHibernate for ORM. Each controller takes an

  • 0

I have an MVC app that uses NHibernate for ORM. Each controller takes an ISession construction parameter that is then used to perform CRUD operations on domain model objects. For example,

public class HomeController : Controller
{
    public HomeController(ISession session)
    {
        _session = session;
    }
    public ViewResult Index(DateTime minDate, DateTime maxDate)
    {
        var surveys = _session.CreateCriteria<Survey>()
                              .Add( Expression.Like("Name", "Sm%") )
                              .Add( Expression.Between("EntryDate", minDate, maxDate) )
                              .AddOrder( Order.Desc("EntryDate") )
                              .SetMaxResults(10)
                              .List<Survey>();

        // other logic that I want to unit test that does operations on the surveys variable
        return View(someObject);
    }
    private ISession _session;
}

I would like to unit test this controller in isolation, without actually hitting the database, by mocking the ISession object using Moq or RhinoMocks. However, it is going to be very difficult to mock the ISession interface in the unit test, because it is being used via a fluent interface that chains a number of calls together.

One alternative is to wrap the ISession usage via a repository pattern. I could write a wrapper class something like this:

public interface IRepository
{
   List<Survey> SearchSurveyByDate(DateTime minDate, DateTime maxDate);
}

public class SurveyRepository : IRepository
{
    public SurveyRepository(ISession session)
    {    
        _session = session;
    }
    public List<Survey> SearchSurveyByDate(DateTime minDate, DateTime maxDate)
    {
        return _session.CreateCriteria<Survey>()
                          .Add( Expression.Like("Name", "Sm%") )
                          .Add( Expression.Between("EntryDate", minDate, maxDate) )
                          .AddOrder( Order.Desc("EntryDate") )
                          .SetMaxResults(10)
                          .List<Survey>();
    }
    private ISession _session;
}

I could then re-write my controller to take an IRepository constructor argument, instead of an ISession argument:

public class HomeController : Controller
{
    public HomeController(IRepository repository)
    {
        _repository = repository;
    }

    public ViewResult Index(DateTime minDate, DateTime maxDate)
    {
        var surveys = _repository.SearchSurveyByDate(minDate, maxDate);
         // other logic that I want to unit test that does operations on the surveys variable
        return View(someObject);
    }
    private IRepository _repository;
}

This second approach would be much easier to unit test, because the IRepository interface would be much easier to mock than the ISession interface, since it is just a single method call. However, I really don’t want to go down this route, because:

1) It seems like a really bad idea to create a whole new layer of abstraction and a lot more complexity just to make a unit test easier, and

2) There is a lot of commentary out there that rails against the idea of using a repository pattern with nHibernate, since the ISession interface is already a repository-like interface. (See especially Ayende’s posts here and here) and I tend to agree with this commentary.

So my questions is, is there any way I can unit-test my initial implementation by mocking the ISession object? If not, is my only recourse to wrap the ISession query using the repository pattern, or is there some other way I can solve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T13:47:09+00:00Added an answer on June 4, 2026 at 1:47 pm

    Rather than mocking out ISession have you considered having your tests inherit from a base fixture that makes use of SQLite?

    public class FixtureBase
    {
        protected ISession Session { get; private set; }
        private static ISessionFactory _sessionFactory { get; set; }
        private static Configuration _configuration { get; set; }
    
        [SetUp]
        public void SetUp()
        {
            Session = SessionFactory.OpenSession();
            BuildSchema(Session);
        }
    
        private static ISessionFactory SessionFactory
        {
            get
            {
               if (_sessionFactory == null)
               {
                    var cfg = Fluently.Configure()
                        .Database(FluentNHibernate.Cfg.Db.SQLiteConfiguration.Standard.ShowSql().InMemory())
                        .Mappings(configuration => configuration.FluentMappings.AddFromAssemblyOf<Residential>())
                        .ExposeConfiguration(c => _configuration = c);
    
                    _sessionFactory = cfg.BuildSessionFactory();
               }
    
                return _sessionFactory;
            }
        }
    
        private static void BuildSchema(ISession session)
        {
            var export = new SchemaExport(_configuration);
            export.Execute(true, true, false, session.Connection, null);
        }
    
        [TearDown]
        public void TearDownContext()
        {
            Session.Close();
            Session.Dispose();
        }
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a spring web app that uses Hibernate and Velocity. It's an MVC
I have a MVC app that uses Linq2Sql to access a SQL DB. But
So I have an MVC 2 app that uses the Active Directory Membership Provider.
We have an MVC web app that uses FormsAuthentication and also stores a couple
I have a mvc 3 app that uses EF. In one function I need
I have an ASP.NET MVC 2 app that uses URLS such as: /rsvp/33c4cf68-a2fe-4c0f-9834-08838e0532c3 /rsvp/4f28dad7-b05c-4887-818f-b4ae664b7192
I have a Spring MVC app that uses JSP to render pages. Spring has
I have an MVC app that uses [Authorize] to protect the private bits. When
I´m developing an ASP.NET MVC app that uses EF 4.1 Code First. I have
I have an ASP.NET MVC 3 app that uses Amazon S3 to store movie

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.