nhibernate session change tracking

Topics: Customizing Orchard, Writing modules
May 21, 2013 at 7:52 PM
Hi,

I'm implementing a custom module. I need to update some data using a web service. I implemented a record in the Models folder of the module. It's not a content part record, just a plain record.

The service returns all the data, not only updated data. Hence I will loop through the service data, fetch the corresponding record from IRepository<MyRecord> injection, and set its properties.

Now, I've read that nHibernate would track changes to items during the session, and issue a DB update only if the modified properties are different from their original values. Is this the case with Orchard? I assume it is.

I want to set a property of the updated record if any of its properties has been changed. If orchard used Entitiy Framework instead of nhibernate, I would have done something like this:
var now = DateTime.Now;
foreach (var state in new[] { EntityState.Modified, EntityState.Added })
{
    var objectStates = context.ObjectStateManager.GetObjectStateEntries(state);
    foreach (var objectState in objectStates)
    {
        ((MyRecord)objectState.Entity).UpdatedUTC = now;
    }
}
The code above would not work, but you understand what I'm trying to achieve.

Is there a way to access similar information (that a record is modified) through Orchard framework or nHibernate before the changes are flushed, or would you suggest a better way
set a property only if the record is really modified?
Developer
May 22, 2013 at 10:33 AM
Edited May 22, 2013 at 10:33 AM
It will be possible since 1.7, as we're introducing some low level extension points for plugging into NHibernate pipeline.

Take a look at Orchard.Framework/Data/ISessionInterceptor.cs interface in latest 1.x branch. By implementing this interface (there is an existing abstract implementation which is the one you should actually derive from) you'd be able to intercept any event fired during the course of NH session.
May 22, 2013 at 5:06 PM
Sounds good. I guess I won't be able to get dirty entities until the session is flushed and some event is cought by the interceptor, because nhibernate entities are not self tracking.

Thanks for the answer.