Java Mailing List Archive

http://www.gg3721.com/

Home » the NHibernate development list »

[NHibernate-development] Small refactoring for
 AbstractPersistentCollection.IdentityRemoveAll()

Gennadii Donchyts

2008-05-09

Replies: Find Java Web Hosting

Author LoginPost Reply
Dear NHibernaters,
   
Currently signature of the AbstractPersistentCollection.IdentityRemoveAll() method looks like this:   

        public static void IdentityRemoveAll(IList list, ICollection collection, string entityName, ISessionImplementor session)

My suggestion is to change ICollection to IEnumerable:

        public static void IdentityRemoveAll(IList list, IEnumerable enumerable, string entityName, ISessionImplementor session)

Why:

The method is implemented in the following way, note that there is no need for ICollection here:

        public static void IdentityRemoveAll(IList list, ICollection collection, string entityName, ISessionImplementor session)
        {
            IEnumerator enumer = collection.GetEnumerator();
            while (enumer.MoveNext())
            {
                if (enumer.Current==null)
                    continue;
                IdentityRemove(list, enumer.Current, entityName, session);
            }
        }

and it is called by the classes like PersistentGenericList, Bag, etc. in the following way:

        public override ICollection GetOrphans(object snapshot, string entityName)
        {
            IList<T> sn = (IList<T>) snapshot;
            List<T> result = new List<T>(sn.Count);
            result.AddRange(sn);
            IdentityRemoveAll(result, (ICollection) list, entityName, this.Session); // what if my list is not ICollection but ICollection<T>?
            return result;
        }


I'm trying to define my custom collection class and as result getting the following error:

FailedSystem.InvalidCastException: Unable to cast object of type 'MyCustomGenericTypeDerivedFromPersistentGenericList' to type 'System.Collections.ICollection'.
at NHibernate.Collection.Generic.PersistentGenericList`1.GetOrphans(Object snapshot, String entityName) in PersistentGenericList.cs: line 352
at NHibernate.Engine.CollectionEntry.GetOrphans(String entityName, IPersistentCollection collection) in CollectionEntry.cs: line 386

Changing the above method to:

        public static void IdentityRemoveAll(IList list, IEnumerable enumerable, string entityName, ISessionImplementor session)
        {
            IEnumerator enumer = enumerable.GetEnumerator();
            while (enumer.MoveNext())
            {
                if (enumer.Current==null)
                    continue;
                IdentityRemove(list, enumer.Current, entityName, session);
            }
        }

and using in the PersistentGenericList as:

        public override ICollection GetOrphans(object snapshot, string entityName)
        {
            IList<T> sn = (IList<T>) snapshot;
            List<T> result = new List<T>(sn.Count);
            result.AddRange(sn);
            IdentityRemoveAll(result, list, entityName, this.Session); // << NO CASTING OF IList<T> to ICollection!
            return result;
        }

solves the problem.

Regards,
Gena


-- 

Delft Hydraulics, GeoDelft, the Subsurface and Groundwater unit of TNO and parts of Rijkswaterstaat have joined forces in a new independent institute for delta technology, Deltares. Deltares combines knowledge and experience in the field of water, soil and the subsurface. We provide innovative solutions to make living in deltas, coastal areas and river basins safe, clean and sustainable.

DISCLAIMER: This message is intended exclusively for the addressee(s) and may contain confidential and privileged information. If you are not the intended recipient please notify the sender immediately and destroy this message. Unauthorized use, disclosure or copying of this message is strictly prohibited. The foundation 'Stichting Deltares', which has its seat at Delft, The Netherlands, Commercial Registration Number 41146461, is not liable in any way whatsoever for consequences and/or damages resulting from the improper, incomplete and untimely dispatch, receipt and/or content of this e-mail.


Attachment: gennadii_donchyts.vcf (zipped)
-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Nhibernate-development mailing list
Nhibernate-development@(protected)
https://lists.sourceforge.net/lists/listinfo/nhibernate-development
©2008 gg3721.com - Jax Systems, LLC, U.S.A.