Since the GetHashCode operation is critical to the success of any
nHibernate project how do people generally implement that function?
For tables without a timestamp I envision this:

      public override int GetHashCode()
        {
            System.Text.StringBuilder sb = new
System.Text.StringBuilder();

            sb.Append(this.GetType().FullName);
            sb.Append(_userName);
            sb.Append(_authorizationLevel);

            return sb.ToString().GetHashCode();
        }

And have all objects be derived from DomainObject as described here:
http://devlicio.us/blogs/billy_mccafferty/archive/2007/04/25/using-equals-gethashcode-effectively.aspx

Obviously for large tables with many fields it may not be very good to
make the hashcode out of all the columns.  Therefore if there was a
timestamp column, wouldn't that be sufficient for the GetHashCode
function and look something like this:

      public override int GetHashCode()
        {
            System.Text.StringBuilder sb = new
System.Text.StringBuilder();
            byte[] byt =
System.Text.Encoding.UTF8.GetBytes(_timestampColumn);

            sb.Append(this.GetType().FullName);
            sb.Append(Convert.ToBase64String(byt));

            return sb.ToString().GetHashCode();
        }

Or would the overhead of converting to a base64 string be too much?
How do people typically do this with say a table with 60 columns.
Surely they don't add all the column data to one massive string do
they if they have a simpler timestamp column available to them?

On Jul 23, 8:18 am, John Davidson <[email protected]> wrote:
> Hold just the version key in session state as you describe should work fine
> in this scenartio.
>
> John Davidson
>
>
>
> On Fri, Jul 23, 2010 at 9:07 AM, MattO <[email protected]> wrote:
> > Okay, so it appears that I should be using the version property on an
> > object (wich I assume uses that GetHashCode stuff people recommend
> > using with nHibernate correct??).
>
> > Specifically it appears that I could just retain the version value in
> > session state and load the new database object from the database again
> > upon postback and then if the version key is the same then proceed
> > with the update.
>
> > But according to this example it almost looks like they are retaining
> > the entire "foo" instance in session state, which obviously would
> > consume much more memory then if you just retained the version value
> > in session state.
>
> > So how do people normally do it?
>
> > On Jul 23, 6:53 am, Roger Kratz <[email protected]> wrote:
> > > Have you read the manual about opt concurrency control?
> >http://nhforge.org/doc/nh/en/index.html#transactions-optimistic
>
> > > ________________________________________
> > > Från: [email protected] [[email protected]] f&#246;r MattO
> > [[email protected]]
> > > Skickat: den 23 juli 2010 01:29
> > > Till: nhusers
> > > Ämne: [nhusers] ASP .NET, Concurrency, and nHibernate Questions
>
> > > NHibernate users,
>
> > > I'm pretty new to nHibernate and I've been watching the summer of
> > > nhibernate series (very good btw!).  I've also read this:
> >http://www.codeproject.com/KB/architecture/NHibernateBestPractices.aspx
> > > and this:
> >http://nhforge.org/blogs/nhibernate/archive/2010/07/11/nhibernate-boo...
>
> > > All of those methods seem to suggest that Session Per Request is the
> > > way to go with nHibernate.  Though this may work for multiple hits per
> > > session and speed things up, how do people do this for ASP .NET
> > > applications?
>
> > > My issue is that when I load the data via nHibernate the session goes
> > > away, and then when I want to save the data back to the database the
> > > data in the database may have already changed and I need to throw an
> > > exception to the user and give them the latest data.
>
> > > Can anyone provide a working code example on how they have handled
> > > this with the follow types of data:
>
> > > 1.  Tables with timestamp columns
> > > 2.  Tables without timestamp columns or any versioning columns to
> > > begin with, therefore I'm looking for the techniques in nHibernate
> > > that just pass in a huge WHERE SQL clause that compares all columns to
> > > make sure they are still the same and if not throws a concurrency
> > > violation.  We have a lot of these due to legacy databases.
>
> > > Right now I have the session working on a per request basis in the
> > > ASP .NET demo application, but am looking for guidance on the
> > > concurrency issue (with code examples, and the appropriate HBM XML
> > > mapping examples).
>
> > > As I see it right now I need to have nHibernate go and retrieve the
> > > data from the database again, compare the timestamp value and if it is
> > > different then throw a concurrency violation to the user and give them
> > > the latest data to try update again.
>
> > > How have people done this in the past with highly volatile data using
> > > a stateless application such as ASP .NET?  Any code examples you can
> > > point me to?
>
> > > --
> > > You received this message because you are subscribed to the Google Groups
> > "nhusers" group.
> > > To post to this group, send email to [email protected].
> > > To unsubscribe from this group, send email to
> > [email protected]<nhusers%[email protected]­>
> > .
> > > For more options, visit this group athttp://
> > groups.google.com/group/nhusers?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "nhusers" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> > [email protected]<nhusers%[email protected]­>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/nhusers?hl=en.- Hide quoted text -
>
> - Show quoted text -

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to