Can I tell if a property is dirty using nhibernate? - nhibernate

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

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.

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));
}
}

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;
}
}

Related

Persisting a Castle DynamicProxy that's not associated with a NH Session

My session uses an NHInterceptor to add INotifyPropertyChanged support to models.
// I use the session generated here to fetch Data
public class SessionServiceImpl : ISessionService
{
[Inject]
public ISessionFactory SessionFactory { get; set; }
[Inject]
public NhChangeNotificationInterceptorImpl ChangeNotificationInterceptor { get; set; }
public ISession GetSession() // reduced code here
{
return SessionFactory.OpenSession(ChangeNotificationInterceptor);
}
}
// This is the interceptor implementation
public class NhChangeNotificationInterceptorImpl : EmptyInterceptor, IInterceptor
{
[Inject]
public ISessionFactory SessionFactory { get; set; }
[Inject]
public ViewModelProxyFactory ProxyFactory { get; set; }
public override object Instantiate(string entityTypeName, EntityMode entityMode, object id)
{
Type type = Type.GetType(entityTypeName);
if (type == null) { /* Throw Exception*/ }
bool isViewModel = false;
while (type != typeof(object))
{
Type tempType = type.BaseType;
if (tempType == typeof(ViewModelBase))
{
isViewModel = true;
break;
}
}
if (entityMode == EntityMode.Poco && isViewModel)
{
var instance = ProxyFactory.CreateProxy(type);
SessionFactory.GetClassMetadata(entityTypeName).SetIdentifier(instance, id, entityMode);
return instance;
}
return base.Instantiate(entityTypeName, entityMode, id);
}
}
The ProxyFactory uses Castle to create proxies that add change notification functionality. That means all my objects come from the DB as Castle Proxies, which are AFAIK transparent.
Whenever I pass one of those NH Generated MVVM-proxies into Session.Save(), all's fine.
Now, as data driven Applications go, I also need to create new instances and save them. I can create instances of the model type and save them via the session all right. Creating a MVVM proxy instance (using Ninject to ensure that the same SessionFactory and ProxyFactory instances are used all over) and throwing this into Session.Save() results in the following:
"NHibernate.MappingException".
Message=No persister for: Castle.Proxies.FieldDescriptionProxy
Source=NHibernate
StackTrace:
at NHibernate.Impl.SessionFactoryImpl.GetEntityPersister(String entityName)
at NHibernate.Impl.SessionImpl.GetEntityPersister(String entityName, Object obj)
at NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object entity, String entityName, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event)
at NHibernate.Impl.SessionImpl.FireSave(SaveOrUpdateEvent event)
at NHibernate.Impl.SessionImpl.Save(Object obj)
at Interpretation.UI.Service.Impl.Dao.FieldDao.SaveFields(IList`1 fields, ISession session) in C:\...\FieldDao.cs:Zeile 51.
InnerException:
Any ideas what goes wrong here (or what I might have forgotten)?
EDIT : Now got it working, but why do I have to add recognition logic (see code below) to the interceptor for instances created outside, while instances created inside can be persisted as the are?
public override string GetEntityName(object entity)
{
Type type = entity.GetType();
if (type.FullName.StartsWith("Castle.Proxies") &&
type.FullName.EndsWith("Proxy"))
{
return type.BaseType.FullName;
}
return base.GetEntityName(entity);
}
Implementing the GetEntityName method did the trick.
public override string GetEntityName(object entity)
{
Type type = entity.GetType();
if (type.FullName.StartsWith("Castle.Proxies") &&
type.FullName.EndsWith("Proxy"))
{
return type.BaseType.FullName;
}
return base.GetEntityName(entity);
}
article and sample code(Intercepting Entity Creation) demonstrating the use of dynamic proxies to implement (WPF) change notification. As seen there you'll have to use the same proxy generator in NHibernate and outside to implement the recognition of the proxies for NHibernate (see class DataBindingIntercepter method GetEntityName) .
why do I have to add recognition logic (see code below) to the interceptor for instances created outside, while instances created inside can be persisted as the are?
Methods on ISession only add instances to dictionary to search for dirty ones on Flush. Since all instances loaded are automaticly part of this dictionary the session immediatly "knows" the entity and does nothing in SaveOrUpdate. For other instances coming from outside it first has to get the appropriate mapping (using the entitiyname which defaults to the class fullname) to know which properties form the primary key.

NHibernate IInterceptor composition

We have a project that uses an Interceptor object to tell NHibernate to do some general works before saving an entity.This interceptor has a single task to do.Now there's another task that should be added to this interceptor (NHibernate doesn't support multiple interceptors) but I don't want to make this interceptor complicated instead I would like to use composition pattern that will manage all registered interceptors.something like this :
public bool Onload(object entity,object id,object[] state,string propertyNames,IType[] types_
{
var result=false;
foreach(var interceptor in _registeredInterceptors)
result=result || interceptor.OnLoad(entity,id,state,propertyNames,types);
return result;
}
public bool OnFlushDirty(object entity,object id,object[] state,string propertyNames,IType[] types_
{
var result=false;
foreach(var interceptor in _registeredInterceptors)
result=result || interceptor.OnFlushDirty(entity,id,state,propertyNames,types);
return result;
}
by looking at this code I realized that there might be a better way preventing me from repeating myself.
the question is can I make this code more simpler and abstract using Lambda expressions and yield keyword?
One way of doing it could look like this:
public bool Execute(IList<IInterceptor> interceptors, Func<IInterceptor, bool> func)
{
bool result = false;
foreach (IInterceptor interceptor in interceptors)
{
result = result || func(interceptor);
}
return result;
}
And in the parent interceptor:
public bool Onload(object entity, object id, object[] state, string propertyNames, IType[] types_
{
return Execute(_registeredInterceptors, x => x.OnLoad(entity, id, state, propertyNames, types);
}
public bool OnFlushDirty(object entity, object id, object[] state, string propertyNames, IType[] types_
{
return Execute(_registeredInterceptors, x => x.OnFlushDirty(entity, id, state, propertyNames, types);
}
Update
If you want the result type to be generic you could do it like this:
public static T Execute<T>(IList<IInterceptor> interceptors, Func<IInterceptor, T> func)
{
T result = default(T);
foreach (IInterceptor interceptor in interceptors)
{
// your logic based on type T
}
return T;
}
Execution of the generic version would look exactly the same as of the bool one because of Type inference.
Is this what you had in mind?

NHibernate Map class name and resolve it with Reflection

I have a property of a class that is mapped to another class that can't be stored in the database and can't be serialized; it implements the state pattern.
So I have something like this:
public IState MyState { get; set; }
Where I have two different states
public class LockedState : IState ...
public class UnlockedState : IState ...
In the database I need to persist the name of the current state that can be accomplished using, for example:
string name = myState.GetType().Name;
Do I have to write a custom and verbose IUserState or is there anything around?
In order to do that I had to implement a custom IUserType in the following way:
public sealed class StateMapper : IUserType
// get
public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
string objectName = (string)NHibernateUtil.String.NullSafeGet(rs, names[0]);
Type stateType = Type.GetType(objectName, false, true);
if (stateType == null)
{
return null;
}
// StateFacility is used by my code to create a new Type
return StateFacility.CreateState(stateType);
}
// set
public void NullSafeSet(IDbCommand cmd, object value, int index)
{
if (value == null)
{
NHibernateUtil.String.NullSafeSet(cmd, null, index);
return;
}
NHibernateUtil.String.NullSafeSet(cmd, value, index);
}

Problems filtering entities from an nhibernate mapping with an IInterceptor

I have a set of entities that implement an interface:
public interface ILocalised
{
Culture Culture { get; }
}
For lots of complicated reasons I need to filter entities which do not have the correct culture after they are returned back from the DB (i.e. I can't use a Filter). My immediate thought was to create an interceptor that would filter any entities that did not have the correct culture, e.g.
public class LocalisationInterceptor : EmptyInterceptor
{
public override object Instantiate(string clazz, NHibernate.EntityMode entityMode, object id)
{
var entity = base.Instantiate(clazz, entityMode, id); //Returns null already
if ((entity is ILocalised) && false == IsValidCulture((ILocalised)entity))
{
return null;
}
return base.Instantiate(clazz, entityMode, id);
}
public override bool OnLoad(object entity, object id, object[] state, string[] propertyNames, NHibernate.Type.IType[] types)
{
if((entity is ILocalised) && false == IsValidCulture((ILocalised)entity))
{
entity = null;
return false;
}
return base.OnLoad(entity, id, state, propertyNames, types);
}
private bool IsValidCulture(ILocalised localisedEntity)
{
return localisedEntity.Culture == Culture.En;
}
}
However so far, what ever method I try to override it will always return the entity.
Does anyone have any ideas how to prevent certain entities from being loaded in an interceptor or any other solutions?
One way is to wrap up the Session using say a Repository. In the Repository.Search method, when the results are returned, do the filtering. So basically you are doing the filtering in your own code after nHibernate is done.

"Real" Object References in Distributed Cache?

I'm personally committed to .net distributed caching solutions, but I think this question is interesting across all platforms.
Is there a distributed caching solution (or generic strategy) that allows to both store objects in the cache while maintaining the integrity of the references between them?
To exemplify - Suppose I have an object Foo foo that references an object Bar bar and also and object Foo foo2 that references that same Bar bar. If I load foo to the cache, a copy of bar is stored along with it. If I also load foo2 to the cache, a separate copy of bar is stored along with that. If I change foo.bar in the cache, the change does not impact foo2.bar :(
Is there an existing distributed cache solution that will enable me to load foo, foo2 and bar into the cache while maintaining the foo.bar foo2.bar references?
First and foremost
I do not know of any distributed system, and I do not pretend to build one. This post explains how you can simulate this behavior with .NET and C# using the IObjectReference interface with serializable objects.
Now, lets go on with the show
I do not know of such a distributed system, but you can somewhat easily achive this with .NET using the IObjectReference interface. Your implementation of ISerializable.GetObjectData would need to call SerializationInfo.SetType to point out a proxy class that implements IObjectReference, and would be able (with help from data provided by your GetObjectData method) to get a reference to the real object that should be used.
Example code:
[Serializable]
internal sealed class SerializationProxy<TOwner, TKey> : ISerializable, IObjectReference {
private const string KeyName = "Key";
private const string InstantiatorName = "Instantiator";
private static readonly Type thisType = typeof(SerializationProxy<TOwner, TKey>);
private static readonly Type keyType = typeof(TKey);
private static readonly Type instantiatorType = typeof(Func<TKey, TOwner>);
private readonly Func<TKey, TOwner> _instantiator;
private readonly TKey _key;
private SerializationProxy() {
}
private SerializationProxy(SerializationInfo info, StreamingContext context) {
if (info == null) throw new ArgumentNullException("info");
_key = (TKey)info.GetValue(KeyName, keyType);
_instantiator = (Func<TKey, TOwner>)info.GetValue(InstantiatorName, instantiatorType);
}
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) {
throw new NotSupportedException("This type should never be serialized.");
}
object IObjectReference.GetRealObject(StreamingContext context) {
return _instantiator(_key);
}
internal static void PrepareSerialization(SerializationInfo info, TKey key, Func<TKey, TOwner> instantiator) {
if (info == null) throw new ArgumentNullException("info");
if (instantiator == null) throw new ArgumentNullException("instantiator");
info.SetType(thisType);
info.AddValue(KeyName, key, keyType);
info.AddValue(InstantiatorName, instantiator, instantiatorType);
}
}
This code would be called with SerializationProxy.PrepareSerialization(info, myKey, myKey => LoadedInstances.GetById(myKey)) from your GetObjectData method, and your LoadedInstances.GetById should return the instance from a Dictionary<TKey, WeakReference> or load it from cache/database if it isnt already loaded.
EDIT:
I've wrote some example code to show what I mean.
public static class Program {
public static void Main() {
// Create an item and serialize it.
// Pretend that the bytes are stored in some magical
// domain where everyone lives happily ever after.
var item = new Item { Name = "Bleh" };
var bytes = Serialize(item);
{
// Deserialize those bytes back into the cruel world.
var loadedItem1 = Deserialize<Item>(bytes);
var loadedItem2 = Deserialize<Item>(bytes);
// This should work since we've deserialized identical
// data twice.
Debug.Assert(loadedItem1.Id == loadedItem2.Id);
Debug.Assert(loadedItem1.Name == loadedItem2.Name);
// Notice that both variables refer to the same object.
Debug.Assert(ReferenceEquals(loadedItem1, loadedItem2));
loadedItem1.Name = "Bluh";
Debug.Assert(loadedItem1.Name == loadedItem2.Name);
}
{
// Deserialize those bytes back into the cruel world. (Once again.)
var loadedItem1 = Deserialize<Item>(bytes);
// Notice that we got the same item that we messed
// around with earlier.
Debug.Assert(loadedItem1.Name == "Bluh");
// Once again, force the peaceful object to hide its
// identity, and take on a fake name.
loadedItem1.Name = "Blargh";
var loadedItem2 = Deserialize<Item>(bytes);
Debug.Assert(loadedItem1.Name == loadedItem2.Name);
}
}
#region Serialization helpers
private static readonly IFormatter _formatter
= new BinaryFormatter();
public static byte[] Serialize(ISerializable item) {
using (var stream = new MemoryStream()) {
_formatter.Serialize(stream, item);
return stream.ToArray();
}
}
public static T Deserialize<T>(Byte[] bytes) {
using (var stream = new MemoryStream(bytes)) {
return (T)_formatter.Deserialize(stream);
}
}
#endregion
}
// Supercalifragilisticexpialidocious interface.
public interface IDomainObject {
Guid Id { get; }
}
// Holds all loaded instances using weak references, allowing
// the almighty garbage collector to grab our stuff at any time.
// I have no real data to lend on here, but I _presume_ that this
// wont be to overly evil since we use weak references.
public static class LoadedInstances<T>
where T : class, IDomainObject {
private static readonly Dictionary<Guid, WeakReference> _items
= new Dictionary<Guid, WeakReference>();
public static void Set(T item) {
var itemId = item.Id;
if (_items.ContainsKey(itemId))
_items.Remove(itemId);
_items.Add(itemId, new WeakReference(item));
}
public static T Get(Guid id) {
if (_items.ContainsKey(id)) {
var itemRef = _items[id];
return (T)itemRef.Target;
}
return null;
}
}
[DebuggerDisplay("{Id} {Name}")]
[Serializable]
public class Item : IDomainObject, ISerializable {
public Guid Id { get; private set; }
public String Name { get; set; }
// This constructor can be avoided if you have a
// static Create method that creates and saves new items.
public Item() {
Id = Guid.NewGuid();
LoadedInstances<Item>.Set(this);
}
#region ISerializable Members
public void GetObjectData(SerializationInfo info, StreamingContext context) {
// We're calling SerializationProxy to call GetById(this.Id)
// when we should be deserialized. Notice that we have no
// deserialization constructor. Fxcop will hate us for that.
SerializationProxy<Item, Guid>.PrepareSerialization(info, Id, GetById);
}
#endregion
public static Item GetById(Guid id) {
var alreadyLoaded = LoadedInstances<Item>.Get(id);
if (alreadyLoaded != null)
return alreadyLoaded;
// TODO: Load from storage container (database, cache).
// TODO: The item we load should be passed to LoadedInstances<Item>.Set
return null;
}
}