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.
|