Nhibernate强制您使用Iesi Set,而不是net 4 ISet接口。在下面的代码片段中,我检查一个iesi集是否包含一个项目:
public virtual void Remove(Substance substance)
{
var test = _substances.First() == substance;
if (!_substances.Contains(substance)) return;
_substances.Remove(substance);
substance.SubstanceGroup = null;
}
变量_substances引用HashedSet。我添加了测试var只是为了检查代码作为临时措施。 我像这样覆盖了Equals方法:
public override int GetHashCode()
{
return Equals(Id, default(TId)) ? base.GetHashCode() : Id.GetHashCode();
}
这会导致项目将Id(Guid)作为哈希返回。 如果我签入调试器,我会得到以下结果:
test
true
_substances.Contains(substance)
false
_substances.First().GetHashCode()
-2974953
substance.GetHashCode()
-2974953
如何使用该集合的contains方法在集合中找不到完全相同的对象?我甚至可以在调试器中执行此操作:
_substances.Contains(_substances.First())
false
显然,_substances.Remove(物质)也不起作用。经过一些额外的研究后,我发现NH用它自己的Persistent Generic集取代了该集合。使用此套件时会出现问题。如果我从该集合中检索一个项目并且我在同一个集合上调用Contains,它总是返回false。我已经覆盖了GetHashCode和Equals,甚至在Equals方法中返回true。
答案 0 :(得分:3)
您的Equals和GetHashCode实现有问题,因为我向您保证Iesi ISet集合正常运行。它被PersistentGenericSet取代的原因是ISet只是一个接口,集合必须由具体类型替换。如果没有更多的代码,很难看出问题出在哪里,所以我在下面粘贴了一个更好的平等实现。我可以在你的问题中看到的一个问题是,在分配Id之后哈希码会改变,我的版本通过缓存哈希码来处理它。
public class Substance
{
private int? _cachedHashCode;
public Substance()
{
Id = Guid.Empty;
}
public Substance(Guid id)
{
Id = id;
}
public Guid Id { get; set; }
public bool IsTransient
{
get { return Id == Guid.Empty; }
}
public bool Equals(Substance other)
{
if (IsTransient ^ other.IsTransient)
{
return false;
}
if (IsTransient && other.IsTransient)
{
return ReferenceEquals(this, other);
}
return other.Id.Equals(Id);
}
public override bool Equals(object obj)
{
if (obj == null || obj.GetType() != GetType())
{
return false;
}
var other = (Substance)obj;
return Equals(other);
}
public override int GetHashCode()
{
if (!_cachedHashCode.HasValue)
{
_cachedHashCode = IsTransient ? base.GetHashCode() : Id.GetHashCode();
}
return _cachedHashCode.Value;
}
}
public class Mixture
{
public Mixture()
{
Substances = new HashedSet<Substance>();
}
public ISet<Substance> Substances { get; set; }
}
public class Tests
{
[Test]
public void set_contains_transient_substance()
{
var mixture = new Mixture();
var s1 = new Substance();
mixture.Substances.Add(s1);
Assert.IsTrue(mixture.Substances.Contains(s1));
}
[Test]
public void set_contains_persistent_substance()
{
var id = Guid.NewGuid();
var mixture = new Mixture();
var s1 = new Substance(id);
mixture.Substances.Add(s1);
var s2 = new Substance(id);
// these were created with the same id so hash code is not cached
// and id equality is used
Assert.IsTrue(mixture.Substances.Contains(s2));
}
[Test]
public void remove_substance()
{
var id = Guid.NewGuid();
var mixture = new Mixture();
var s1 = new Substance(id);
mixture.Substances.Add(s1);
var s2 = new Substance(id);
mixture.Substances.Remove(s2);
Assert.IsTrue(mixture.Substances.Count() == 0);
}
[Test]
public void hash_code_is_cached()
{
var s1 = new Substance(Guid.NewGuid());
var s2 = new Substance(Guid.NewGuid());
var mixture = new Mixture();
mixture.Substances.Add(s1);
Assert.IsFalse(mixture.Substances.Contains(s2));
// assign s1 id to s2, s2 hashcode is cached so they are not equal
s2.Id = s1.Id;
Assert.IsFalse(mixture.Substances.Contains(s2));
}
}