views:

57

answers:

3

I guess what I'm after here is a quick example of how to do this. I've got fluent nhibernate working fine. I can create new records with no problem, and read in existing records no problem. The issue I'm having is with the asp.net user session and how I handle the fact that I want to keep that around between page loads, and I want to write it to the database occasionally too.

The flow I'm trying to get working is something like this:

  • User logs in
  • NHibernate fetches user record
  • user record is stuffed in asp.net session
  • NHibernate session (I think) ends
  • app displays a link to "add address" page
  • user goes to "add address" page
  • user enters address details
  • user clicks "submit"
  • address is added to user object
  • user object is written to database

When I attempt to write the user object (complete with new address) back to the database, I get an error telling me that user object is already associated with an existing session. Below is the code I'm using to get the user out of the asp.net session, add the address and write it back to the database. It's only at the point where it hits .SaveOrUpdate that things go wrong, the address is added to the object perfectly.

  void AddAddress_Click(object sender, EventArgs e)
  {
   var session = Global.SessionFactory.GetCurrentSession();
   User user=(User)HttpContext.Current.Session["User"];

   user.AddAddress(txtDescription.Text, txtLine1.Text, 
     txtLine2.Text, txtTown.Text, txtCounty.Text, 
     txtPostCode.Text, txtCountry.Text);

   session.SaveOrUpdate(user);
   session.Flush();
  }

Please ignore the 101 ways in which this is probably wrong for now, I'm well aware of it. I just want to get something working to prove to myself that NHibernate is the way forward for this project, so it will be a little quick and dirty at the moment.

+1  A: 

A common practice with NHibernate is to use single ISession per request. This approach is otherwise known as "Open Session In View" or "OSIV". See this and this.

Anton Gogolev
A: 

When using NHibernate in asp.net applications, I've found the following two rules really help in keeping me out of various pitfalls.

1) Use exactly one NHibernate session per request. Never share session between requests, and avoid opening more than one in a single request.

2) Don't store any NHibernate entities in the http session context. Although NHibernate supports "detatched" entities, doing so adds complexity and confusion to the code. Keep things simple by storing only primitive data, the ids and property values.

Felix Ungman
A: 

Thanks to Felix, I think I've got this now. Sorry the original question was difficult to understand, but it looks like Felix got what I meant. For some strange reason I've been unable to edit the question since I posted it, but I'll have another go again tonight. As you can possibly tell, I asked the question without creating an account first, and now that I have created an account there's some sort of mismatch going on. I'm going to try to resolve that tonight, and give credit where it's due.

Adam Short