我发现我的自我超越Equals()
并GetHashCode()
经常实现具有相同属性值的业务对象相等的语义。这导致代码重复编写且维护脆弱(属性被添加并且一个/两个覆盖没有更新)。
代码最终看起来像这样(欢迎对实现发表评论):
public override bool Equals(object obj)
{
if (object.ReferenceEquals(this, obj)) return true;
MyDerived other = obj as MyDerived;
if (other == null) return false;
bool baseEquals = base.Equals((MyBase)other);
return (baseEquals &&
this.MyIntProp == other.MyIntProp &&
this.MyStringProp == other.MyStringProp &&
this.MyCollectionProp.IsEquivalentTo(other.MyCollectionProp) && // See http://stackoverflow.com/a/9658866/141172
this.MyContainedClass.Equals(other.MyContainedClass));
}
public override int GetHashCode()
{
int hashOfMyCollectionProp = 0;
// http://computinglife.wordpress.com/2008/11/20/why-do-hash-functions-use-prime-numbers/
// BUT... is it worth the extra math given that elem.GetHashCode() should be well-distributed?
int bitSpreader = 31;
foreach (var elem in MyCollectionProp)
{
hashOfMyCollectionProp = spreader * elem.GetHashCode();
bitSpreader *= 31;
}
return base.GetHashCode() ^ // ^ is a good combiner IF the combined values are well distributed
MyIntProp.GetHashCode() ^
(MyStringProp == null ? 0 : MyStringProp.GetHashValue()) ^
(MyContainedClass == null ? 0 : MyContainedClass.GetHashValue()) ^
hashOfMyCollectionProp;
}
我的问题
- 实施模式是否合理?
- 考虑到贡献的组件值分布良好,^ 是否足够?考虑到它们的散列分布良好,在组合集合元素时是否需要乘以 31 到 N?
- 似乎可以将此代码抽象为使用反射来确定公共属性的代码,构建与手动编码的解决方案匹配的表达式树,并根据需要执行表达式树。这种方法看起来合理吗?某处是否有现有的实现?