Created
December 5, 2014 10:19
-
-
Save KumoKairo/9a958325506f8d1a5a8a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using NHibernate; | |
using NHibernate.Engine; | |
using NHibernate.Persister.Entity; | |
using NHibernate.Proxy; | |
namespace Bars.Energo | |
{ | |
public static class SessionExtensions | |
{ | |
public static IEnumerable<T> Local<T>(this ISession session) | |
{ | |
ISessionImplementor impl = session.GetSessionImplementation(); | |
IPersistenceContext pc = impl.PersistenceContext; | |
return pc.EntityEntries.Keys.OfType<T>().Select(key => (key)).ToArray(); | |
} | |
public static Boolean IsDirtyEntity(this ISession session, Object entity) | |
{ | |
ISessionImplementor sessionImpl = session.GetSessionImplementation(); | |
IPersistenceContext persistenceContext = sessionImpl.PersistenceContext; | |
EntityEntry oldEntry = persistenceContext.GetEntry(entity); | |
String className = oldEntry.EntityName; | |
IEntityPersister persister = sessionImpl.Factory.GetEntityPersister(className); | |
// Решарпер говорит что это выражение always null, но это совершенно не обязательно, | |
// не поддавайтесь на его уловки | |
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); | |
// в NHibernate WIKI использовался этот способ, | |
// но он не ловит фактически изменившиеся поля у сущности, | |
// которая возвращает true при вызове IsDirtyEntity | |
Int32[] dirtyProps1 = persister.FindDirty(currentState, oldState, entity, sessionImpl); | |
Int32[] dirtyProps = oldState.Select((o, i) => (oldState[i] == currentState[i]) ? -1 : i) | |
.Where(x => x >= 0) | |
.ToArray(); | |
return (dirtyProps != null); | |
} | |
public static Boolean IsDirtyProperty(this ISession session, Object entity, String propertyName) | |
{ | |
ISessionImplementor sessionImpl = session.GetSessionImplementation(); | |
IPersistenceContext persistenceContext = sessionImpl.PersistenceContext; | |
EntityEntry oldEntry = persistenceContext.GetEntry(entity); | |
String className = oldEntry.EntityName; | |
IEntityPersister persister = sessionImpl.Factory.GetEntityPersister(className); | |
// Решарпер говорит что это выражение always null, но это совершенно не обязательно, | |
// не поддавайтесь на его уловки | |
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 = oldState.Select((o, i) => (oldState[i] == currentState[i]) ? -1 : i) | |
.Where(x => x >= 0) | |
.ToArray(); | |
//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) | |
{ | |
ISessionImplementor sessionImpl = session.GetSessionImplementation(); | |
IPersistenceContext persistenceContext = sessionImpl.PersistenceContext; | |
EntityEntry oldEntry = persistenceContext.GetEntry(entity); | |
String className = oldEntry.EntityName; | |
IEntityPersister persister = sessionImpl.Factory.GetEntityPersister(className); | |
// Решарпер говорит что это выражение always null, но это совершенно не обязательно, | |
// не поддавайтесь на его уловки | |
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]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment