hashset - nhibernate Iesi ISet fails to Remove() -
i have 2 class'es handled nhibernate : assetgroup , asset assetgroup has iset _assets collection. constructor of assetgroup say
_assets = new hashset<asset>();
i have operation add , remove asset in assetgroup
public abstract class entity<tid> { public virtual tid id { get; protected set; } public override bool equals(object obj) { return equals(obj entity<tid>); } public static bool istransient(entity<tid> obj) { return obj != null && equals(obj.id, default(tid)); } private type getunproxiedtype() { return gettype(); } public virtual bool equals(entity<tid> other) { if (other == null) return false; if (referenceequals(this, other)) return true; if (!istransient(this) && !istransient(other) && equals(id, other.id)) { var othertype = other.getunproxiedtype(); var thistype = getunproxiedtype(); return thistype.isassignablefrom(othertype) || othertype.isassignablefrom(thistype); } return false; } public override int gethashcode() { if (equals(id, default(tid))) { return base.gethashcode(); } else { return id.gethashcode(); } } } /////////////////////////////////////// public class assetgroup : entity<int> { public assetgroup() { this._assets = new hashedset<asset>(); } virtual public guid securitykey {get; set;} virtual public string name { get; set; } private iset<asset> _assets; virtual public iset<asset> assets { { return _assets; } protected set { _assets = value; } } virtual public bool addasset(asset asset) { if (asset != null && _assets.add(asset)) { return true; } return false; } virtual public bool removeasset(asset asset) { asset target = null; foreach (var in _assets) { var x = a.gethashcode(); var b = a.equals(asset); if (a.equals(asset)) target = a; } if (target == null) return false; if (asset != null && _assets.remove(target)) { return true; } return false; } } //////////////////////////////////////// public class asset : entity<int> { public asset() { securitykey = guid.newguid(); } public virtual guid securitykey { get; set; } virtual public int assetgroupid { { return (assetgroup != null ? assetgroup.id : 0); } } virtual public string name { get; set; } virtual public assetgroup assetgroup { get; set;} virtual public void setassetgroup(assetgroup assetgroup) { assetgroup prevref = assetgroup; if (prevref == assetgroup) return; assetgroup = assetgroup; if (prevref != null) prevref.assets.remove(this); if (assetgroup != null) assetgroup.assets.add(this); } }
the removeasset fails remove asset . have foreach check if asset exists in _assets . put breakpoints trace thru , foreach loop can find asset (targe) removeasset'ed . strangely enough , when ask _assets remove target. fails remove , return false. if asks _assets.contains(target) .. return false .. if foreach loop in removeasset can find target...
the 2 nhibernate mappings are
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="mysystem.domain" namespace="mysystem.domain" auto-import="true">
can me ?
are sure actual code executing? seems me if override equals , gethashcode in malfunctioning way, find asset , target assigned object _assets, remove method should never fail when called target definitly contained in set. did short test , set behaved expected.
Comments
Post a Comment