views:

23

answers:

2

I was wondering if there is any sort of functionality in NHibernate to check if changes have been made to an object sense it has been loaded that would require a write to the DB to save.

I have some other pieces like currently selected elements and so forth that may change but if they do it makes no difference in terms of actual data.

Failing that what is a good way in WPF/C# to check if there are changes that need to be saved so I can prompt the user before they exit.

Thanks

A: 

Short answer: You'll need to implement your own "IsDirty" methods.


NHibernate was designed to work without imposing any requirements on your entity classes. It does its own internal tracking in the session for whether an entity being tracked by the session is dirty. While it would be nice if it exposed some method on ISession to check if an entity is dirty, that doesn't address changes made to entities while they are detached from a session.

_

Your class could implement ICloneable and IEquatable, then you could make a clone of your object before you allow the user to make changes, then afterwards compare that clone to the current object to see if they are no longer the same.

If the collections within your class also implement ICloneable and IEquatable, this method could also let you know if something changed somewhere down the hierarchy.

Rafael Belliard
A: 

You can query Session.IsDirty, which will return true if there are changes to any object.

A more involved approach involves getting the SessionImplementor, Persister, PersistenceContext and current values, then using FindDirty there.

I do not recommend it, but I can give you the code if needed. It's better to track that at the ViewModel level.

Diego Mijelshon