30

Does anyone know if it is possible to tell if a specific property on an object is dirty (i.e. the property is different to the one stored on the DB) using NHibernate?

The background to this question is that I will have an object with a (relatively) large number of properties on it. I need to be able to pass a parameter (string) to a function that will determine if that specific property has changed during the lifetime of the page.

If I need to I can create a copy of the object and use reflection at the end of the page lifecycle to check the value, but I am reluctant to do this. It would be great if NHibernate could simply tell me if a property was dirty.

Thanks

3 Answers 3

1

That is not currently available through the nHibernate (or Hibnernate, AFAIK) API. You could write something like this yourself by storing the state of the objects when they're loaded into session, then compare them.

NOTE: I haven't seen this for myself, but, if you haven't locked into an ORM choice, you should look at Subsonic. A lot of the dirty status is tracked within the objects themselves, and you'd be able to determine if a particular property is dirty.

1

the session can tell if the entity is dirty

public static class SessionExtensions
{
    /// <exception cref="NHibernate.PersistentObjectException">object is an uninitialized proxy</exception>
    public static bool IsDirty(this ISession session, object entity)
    {
        var sessionImpl = session.GetSessionImplementation();
        entity = sessionImpl.PersistenceContext.Unproxy(entity);
        var oldEntry = sessionImpl.PersistenceContext.GetEntry(entity);
        var persister = sessionImpl.Factory.GetEntityPersister(oldEntry.EntityName);

        return persister.FindDirty(persister.GetPropertyValues(entity), oldEntry.LoadedState, entity, sessionImpl) != null;
    }

    /// <exception cref="NHibernate.PersistentObjectException">object is an uninitialized proxy</exception>
    public static bool IsDirtyProperty(this ISession session, object entity, string propertyName)
    {
        var sessionImpl = session.GetSessionImplementation();
        entity = sessionImpl.PersistenceContext.Unproxy(entity);
        var oldEntry = sessionImpl.PersistenceContext.GetEntry(entity);
        var persister = sessionImpl.Factory.GetEntityPersister(oldEntry.EntityName);

        return !Equals(persister.GetPropertyValue(entity, propertyName), oldEntry.GetLoadedValue(propertyName));
    }
}
0

Checking whether property is dirty or not may be possible by manually checking it against IPersistenceContext. I never tried it so I cannot provide an exact code. You may refer the GetEntityEntryFromPersistenceContext method in below code sample and write your own code to do that.

You can easily check if entire entity is dirty or not. You can use following utility class to know the various states of entity including the dirty state.

public static class NHibernateUtil
{
    public static bool IsDirtyEntity<TEntity>(ISession session, TEntity instance) where TEntity : class
    {
        if(IsPersistentEntity(session, instance, true) == false)
        {
            throw new NhBoilerplateException("This is not a persistent entity.");
        }

        ISessionImplementor sessionImpl = session.GetSessionImplementation();
        IPersistenceContext persistenceContext = sessionImpl.PersistenceContext;
        EntityEntry oldEntry = sessionImpl.PersistenceContext.GetEntry(instance);

        if((oldEntry == null) && (instance is INHibernateProxy))
        {
            INHibernateProxy proxy = instance as INHibernateProxy;
            object obj = sessionImpl.PersistenceContext.Unproxy(proxy);
            oldEntry = sessionImpl.PersistenceContext.GetEntry(obj);
        }

        string className = NHibernateProxyHelper.GuessClass(instance).FullName;
        IEntityPersister persister = sessionImpl.Factory.GetEntityPersister(className);
        object[] oldState = oldEntry.LoadedState;
        object[] currentState = persister.GetPropertyValues(instance);
        int[] dirtyProps = persister.FindDirty(currentState, oldState, instance, sessionImpl);

        return (dirtyProps != null);
    }

    public static bool IsProxyEntity<TEntity>(TEntity instance) where TEntity : class
    {
        if((instance is INHibernateProxy) == false)
            return false;
        else
            return true;
    }

    public static bool IsPersistentEntity<TEntity>(ISession session, TEntity instance, bool validateProxy) where TEntity : class
    {
        EntityEntry oldEntry = GetEntityEntryFromPersistenceContext(session, instance);

        if(oldEntry != null)
            return true;

        if(validateProxy == false)
            return false;

        if(IsProxyEntity(instance) == true)
            return true;
        else
            return false;
    }

    public static EntityEntry GetEntityEntryFromPersistenceContext<TEntity>(ISession session, TEntity instance) where TEntity : class
    {
        ISessionImplementor sessionImpl = session.GetSessionImplementation();
        IPersistenceContext persistenceContext = sessionImpl.PersistenceContext;
        EntityEntry oldEntry = persistenceContext.GetEntry(instance);
        return oldEntry;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.