These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Wisdom of accessing "internal" NHibernate structur
PostPosted: Fri Apr 06, 2007 11:53 am 
Newbie

Joined: Wed Mar 21, 2007 4:31 pm
Posts: 6
I'm evaluating NHibernate as a CRUD solution for an application migration to .NET, and so far, it has proven complicated but very capable.

One of my legacy requirements was to be able to determine if an object was changed, and in fact also which parts of the object changed. Sure, I could roll my own implementation, but NHibernate already has the object cached and the loaded values, so why duplicate that?

Anyway, it turns out the what I needed to do this was largely public (although I had to trace into NHibernate during updates to see where the action was):

// Obtain the Session Implementer interface
NHibernate.Engine.ISessionImplementor impl = sess as NHibernate.Engine.ISessionImplementor;

// Get NHibernate's copy of the object and values (Wow!!!!)
NHibernate.Impl.EntityEntry entry = impl.GetEntry(objUser1);

// Make an update
objUser1.Comments = "Update Time -" + DateTime.Now.ToLongTimeString();

// Get array of dirty property indexes...
int[] dirtyProperties = entry.Persister.FindDirty(entry.Persister.GetPropertyValues(objUser1), entry.LoadedState, objUser1, impl);

Of course, I would wrap an abstraction around this in practice, but I was just wondering if this was a reasonable thing to be doing.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 06, 2007 5:50 pm 
Newbie

Joined: Fri Jul 29, 2005 12:03 pm
Posts: 2
Location: Fort Wayne
If you're interested in storing the state of the object into local variables and later doing something with that information, then I guess the best place to cause the object to store its own state information would be by using the Interceptor. You can create an interceptor by implementing the IInterceptor interface defined in NHibernate namespace, and passing that interceptor to the session factory when you construct the session object. For example, it would look something like this...

/* config is your NHibernate configuaration object */
ISessionFactory factory = config.BuildSessionFactory();
ISession session = factory.OpenSession( new MyInterceptor() );
/* do some stuff */
session.Flush();

Use the OnLoad method of the interceptor to store the state of the object in its own variables if you need access to that information. Thats one way to do it, or you could just do it the way you suggest. Anyway, thats my two cents.

_________________
----JonG
Web Developer


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.