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

  • Home
  • SEARCH
  • 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 156905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:24:33+00:00 2026-05-11T10:24:33+00:00

I have a repository class that defines some basic Get/Save/Delete methods. Inside these, I

  • 0

I have a repository class that defines some basic Get/Save/Delete methods. Inside these, I use NHibernate to do the work on my business entities. For example:

Public Class SecurityRepositoryNHibImpl : Implements ISecurityRepository     Public Function GetUser(ByVal UUID As System.Guid) As Entities.User Implements ISecurityRepository.GetUser         Dim eUser As Entities.User         Using session As ISession = NHibernateHelper.OpenSession()             eUser = session.Get(Of Entities.User)(UUID)         End Using         Return eUser     End Function End Class 

However, on my User class I have some properties and collections of other objects that ideally I’d like to be lazy loaded. But of course, the ISession is created and disposed within the repository, which I guess is why, outside of that, when I try to access those properties I get a ‘Could not initialize proxy – no Session’ error.

Is my only option then to disable lazy loading when using repositories? Or is it possible (or just foolish) to somehow get the session into scope in the business layer?

I do like the repository model, and NHibernate is growing on me (after lots of initial frustration trying to make it work), so what’s the best way you gurus have found of using them together?

I’m pretty new to NHibernate and repository models in general (at work we still mainly use VB6!), so forgive what may be a silly question. Thanks.


@mookid: Thanks dude, that’s really helpful but I might leave it open a little longer. It’s the back-end to a WCF web service, and all the functions will be per-call context, so a per-call session-life will be good. Just not sure how to get such a thing working in the business layer, ideally I don’t want the business objects to have to interface directly with any NHibernate classes. I guess some kind of wrapper for the NHibernate session at least abstracts it away… Hmm, you’ve put me on the right track at least.


Looks like the key word here is ‘Unit of Work’, for which a ton of resources exist on the web in relation to NHibernate. In particular look for Ayende’s implementation in Rhino Commons, and his App Architecture web cast (no. 9 at Hibernating Rhinos), very informative. I was initially confused because I though putting a ‘Unit of Work’ in the business layer was mixing concerns, but I was soon corrected.

  • 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. 2026-05-11T10:24:33+00:00Added an answer on May 11, 2026 at 10:24 am

    Creating your ISession and disposing it explicitly inside your repositories like that is ok for small and very simple projects – but, as you have found out, it comes short when you start wanting to use some of the cool features that NHibernate supports.

    You should probably let the lifestyle of the session be controlled by something outside of your repositories – e.g. if you are doing ASP.NET web applications, you might want to save the session in the current request (HttpContext.Current.Items if I recall correctly) for the duration of the web request, and then commit and dispose at the end of the request (or rollback if an exception occurs).

    I don’t know the best way to control the lifestyle of your session in ASP.NET applications, but there must be some way to have code called at the beginning and at the end of each request. In ASP.NET MVC it can be easily accomplished by deriving all your controllers from a base controller that has overriden its OnActionExecuting and OnActionExecuted methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 101k
  • Answers 101k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer No, you can't do that. order_by is applied at the… May 11, 2026 at 8:04 pm
  • Editorial Team
    Editorial Team added an answer If I remember correctly, that is their Distinguished Name, which… May 11, 2026 at 8:04 pm
  • Editorial Team
    Editorial Team added an answer Looks like you're rendering your text directly to the recycle… May 11, 2026 at 8:04 pm

Related Questions

I have a problem with reflection. I need to find the type that instantiates
I have an interface named IAuthorizationRepository with the following interface: public interface IAuthorizationRepository {
I am writing an application where we will need to extend a basic entity
The title to this question isn't written very well. Sorry 'bout that. I'm curious

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.