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 8599927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:30:13+00:00 2026-06-12T01:30:13+00:00

I am having issues editing and deleting objects and i think its because i

  • 0

I am having issues editing and deleting objects and i think its because i am not sharing the same session objects between my repository classes and my unitofwork class. I am trying to find some document on the best way to wire this up so I share the same session object.

I am using ninject as my IOC container in the mvc website.

  • 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-12T01:30:14+00:00Added an answer on June 12, 2026 at 1:30 am

    I usually set the session as a dependency of the repository, so Ninject can resolve the dependency (ISession = NHibernate.ISession):

    public UserRepository(ISession session)
    {
        ...
    }
    

    This is how I set the binding:

    kernel.Bind<ISession>().ToMethod(x => GetRequestSession()).InRequestScope();
    

    So when a session is required Ninject will call GetRequestSession() to retrieve the session. The function is implemented as follows:

    private static ISession GetRequestSession()
            {
                IDictionary httpContextItems = HttpContext.Current.Items;
    
                ISession session;
                if (!httpContextItems.Contains(MvcApplication.SESSION_KEY))
                {
                    // Create an NHibernate session for this request
                    session = MvcApplication.SessionFactory.OpenSession();
                    httpContextItems.Add(MvcApplication.SESSION_KEY, session);
                }
                else
                {
                    // Re-use the NHibernate session for this request
                    session = (ISession)httpContextItems[MvcApplication.SESSION_KEY];
                }
                return session;
            }
    

    The NHibernate session is stored in the HttpContext items. This is a key-value collection which can be used to store and share data during the handlng of one request.

    The session is created only once per request, and is re-used during the request.

    MvcApplication.SESSION_KEY is just a constant string I defined in Global.asax to be able to store and retrieve the session from the HttpContext. Also the session factory is located in global.asax and is created at start-up.

    Your unit of work class could also set the ISession as a dependency, so Ninject will resolve this dependency as well and therefore use the same session. On the other hand, you might not need a unit of work class, because NHibernate’s implementation of ISession in itself is already a unit of work class.

    I’m not sure if this is a best practice, but it works perfectly for me.

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

Sidebar

Related Questions

Im having issues getting this to work, maybe its not even possible? I have
I've been having some issues with the propertygrid. Namely: The behavior is not the
Having issues with Twitter Bootstrap responsive navigation. The best way to explain this is
Having issues referencing $(this) from within a the nested ajax 'success' function... I know
Am having issues with printing different sized PDF's using GhostScript (V9.05). The PDF in
Still having issues with this problem. Please help if you can. So I am
Im having issues vertically positioning text inside of a text input field in Firefox.
I am having issues with networking being very slow.. For testing purposes, I tried
I am having issues passing a dynamic parameter to a JavaScript function using innerHTML.
I am having issues creating a simple 2D scene with Three.js. I would like

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.