NHibernate Forge
The official new home for the NHibernate community

Finding Dirty Properties in NHibernate

Page Details

Published by:
Ricardo Peres
on 10-09-2009
This page has not yet been rated
Article
Comments (0)
History (1)

Finding Dirty Properties in NHibernate

The following extension methods to ISession allow finding if a property or entity has changed, and the original value of a property.

 

public static class SessionExtensions

{

    public static Boolean IsDirtyEntity(this ISession session, Object entity)

    {

        String className = NHibernateProxyHelper.GuessClass(entity).FullName;

        ISessionImplementor sessionImpl = session.GetSessionImplementation();

        IPersistenceContext persistenceContext = sessionImpl.PersistenceContext;

        IEntityPersister persister = sessionImpl.Factory.GetEntityPersister(className);

        EntityEntry oldEntry = sessionImpl.PersistenceContext.GetEntry(entity);

 

        if ((oldEntry == null) && (entity is INHibernateProxy))

        {

            INHibernateProxy proxy = entity as INHibernateProxy;

            Object obj = sessionImpl.PersistenceContext.Unproxy(proxy);

            oldEntry = sessionImpl.PersistenceContext.GetEntry(obj);

        }

 

        Object [] oldState = oldEntry.LoadedState;

        Object [] currentState = persister.GetPropertyValues(entity, sessionImpl.EntityMode);

        Int32 [] dirtyProps = persister.FindDirty(currentState, oldState, entity, sessionImpl);

        return (dirtyProps != null);

    }

 

    public static Boolean IsDirtyProperty(this ISession session, Object entity, String propertyName)

    {

        String className = NHibernateProxyHelper.GuessClass(entity).FullName;

        ISessionImplementor sessionImpl = session.GetSessionImplementation();

        IPersistenceContext persistenceContext = sessionImpl.PersistenceContext;

        IEntityPersister persister = sessionImpl.Factory.GetEntityPersister(className);

        EntityEntry oldEntry = sessionImpl.PersistenceContext.GetEntry(entity);

 

        if ((oldEntry == null) && (entity is INHibernateProxy))

        {

            INHibernateProxy proxy = entity as INHibernateProxy;

            Object obj = sessionImpl.PersistenceContext.Unproxy(proxy);

            oldEntry = sessionImpl.PersistenceContext.GetEntry(obj);

        }

 

        Object [] oldState = oldEntry.LoadedState;

        Object [] currentState = persister.GetPropertyValues(entity, sessionImpl.EntityMode);

        Int32 [] dirtyProps = persister.FindDirty(currentState, oldState, entity, sessionImpl);

        Int32 index = Array.IndexOf(persister.PropertyNames, propertyName);

 

        Boolean isDirty = (dirtyProps != null) ? (Array.IndexOf(dirtyProps, index) != -1) : false;

 

        return (isDirty);

    }

 

    public static Object GetOriginalEntityProperty(this ISession session, Object entity, String propertyName)

    {

        String className = NHibernateProxyHelper.GuessClass(entity).FullName;

        ISessionImplementor sessionImpl = session.GetSessionImplementation();

        IPersistenceContext persistenceContext = sessionImpl.PersistenceContext;

        IEntityPersister persister = sessionImpl.Factory.GetEntityPersister(className);

        EntityEntry oldEntry = sessionImpl.PersistenceContext.GetEntry(entity);

 

        if ((oldEntry == null) && (entity is INHibernateProxy))

        {

            INHibernateProxy proxy = entity as INHibernateProxy;

            Object obj = sessionImpl.PersistenceContext.Unproxy(proxy);

            oldEntry = sessionImpl.PersistenceContext.GetEntry(obj);

        }

 

        Object [] oldState = oldEntry.LoadedState;

        Object [] currentState = persister.GetPropertyValues(entity, sessionImpl.EntityMode);

        Int32 [] dirtyProps = persister.FindDirty(currentState, oldState, entity, sessionImpl);

        Int32 index = Array.IndexOf(persister.PropertyNames, propertyName);

 

        Boolean isDirty = (dirtyProps != null) ? (Array.IndexOf(dirtyProps, index) != -1) : false;

 

        return ((isDirty == true) ? oldState [ index ] : currentState [ index ]);

    }

}

Recent Comments

Leave the first comment for this page.
View All
Powered by Community Server (Commercial Edition), by Telligent Systems