Session is Closed! NHibernate shouldn't be trying to grab data

Posted by Jeremy Holovacs on Stack Overflow See other posts from Stack Overflow or by Jeremy Holovacs
Published on 2012-08-30T01:48:45Z Indexed on 2012/08/30 15:38 UTC
Read the original article Hit count: 274

I have a UnitOfWork/Service pattern where I populate my model using NHibernate before sending it to the view. For some reason I still get the YSOD, and I don't understand why the object collection is not already populated.

My controller method looks like this:

public ActionResult PendingRegistrations()
{
    var model = new PendingRegistrationsModel();
    using (var u = GetUnitOfWork())
    {
        model.Registrations = u.UserRegistrations.GetRegistrationsPendingAdminApproval();
    }
    return View(model);
}

The service/unit of work looks like this:

public partial class NHUserRegistrationRepository : IUserRegistrationRepository
{
    public IEnumerable<UserRegistration> GetRegistrationsPendingAdminApproval()
    {
        var r =
            from UserRegistration ur in _Session.Query<UserRegistration>()
            where ur.Status == AccountRegistrationStatus.PendingAdminReview
            select ur;
        NHibernateUtil.Initialize(r);
        return r;
    }
}

What am I doing wrong?

© Stack Overflow or respective owner

Related posts about c#

Related posts about asp.net-mvc