I originally asked a variant of this question here:
http://groups.google.com/group/nhusers/browse_thread/thread/3381a892f7478561

Some users have asked me to post my full solution, so I am posting my
code below.  The code attempts to obtain the snapshot of the
collection in the same data structure as the original collection.
However, I'm wondering if there is an easier way to do this?  It seems
like something that people would commonly want to do, for auditing
purposes, etc.

private static object GetCollectionSnapshot(IPersistentCollection
collection)
{
        // the collection is not dirty, then the snapshot is the collection
        if (!collection.IsDirty)
        {
                return collection;
        }

        // it is dirty, so we need to get the snapshot
        ICollection snapshot = collection.CollectionSnapshot.Snapshot;

        // sometimes it seems there is no snapshot - in this case we just
return null
        // to indicate that the snapshot is empty
        if (snapshot == null)
                return null;

        // unfortunately, the snapshot is not always stored in the same data
structure as the collection itself
        if(collection is ISet)
        {
                // "set"
                return new HybridSet(((IDictionary)snapshot).Values);
        }
        else if(collection is IList && snapshot is IDictionary)
        {
                // "idbag"
                return new ArrayList(((IDictionary) snapshot).Values);
        }
        else if(collection is IList && snapshot is IList)
        {
                // "list" or "bag"
                // in this case, the NH snapshot is the same type as the 
original
collection
                return snapshot;
        }
        else if(collection is IDictionary)
        {
                // in this case, the NH snapshot is the same type as the 
original
collection
                return snapshot;
        }
        else
        {
                // TODO: implement this for other types of collection
                throw new NotImplementedException("Snapshot is not implemented 
for
this collection type.");
        }
}

Thanks,
jr
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to