Beginner |
|
Joined: Thu May 31, 2007 1:19 pm Posts: 23
|
Looking at the source there doesn't seem to be any support for intercepting persisting of entity collections.
I think something like this will work, but there's probably a more efficient way of doing it - please feel free to comment :)
public void PreFlush(System.Collections.ICollection entities)
{
foreach (object entry in entities)
{
Type objType = entry.GetType();
NHibernate.Metadata.IClassMetadata meta = session.SessionFactory.GetClassMetadata(objType);
for (int i = 0; i < meta.PropertyNames.Length; i++)
{
IType propertyType = meta.PropertyTypes[i];
if (propertyType.IsCollectionType)
{
object propertyValue = meta.GetPropertyValue(entry, meta.PropertyNames[i]);
IPersistentCollection persistentCollection = propertyValue as IPersistentCollection;
if (persistentCollection != null && persistentCollection.IsDirty)
{
ICollection snapshot = persistentCollection.CollectionSnapshot.Snapshot;
if (snapshot != null)
{
// Build list of items common to snapshot and current entries
ArrayList commonItems = new ArrayList();
foreach (object snapshotItem in snapshot)
{
foreach (object item in persistentCollection.Entries())
{
if (snapshotItem.Equals(item))
{
commonItems.Add(item);
}
}
}
// Handle deletes
foreach (object snapshotItem in snapshot)
{
if (!commonItems.Contains(snapshotItem))
{
}
}
// Handle inserts
foreach (object item in persistentCollection.Entries())
{
if (!commonItems.Contains(item))
{
}
}
}
}
}
}
}
}
|
|