请解释此代码中用于测试对象相等性和同一性的技术
请解释此代码中用于测试对象相等性和同一性的技术。
如果您能给我提供任何网络链接/书籍参考以进行详细讨论,那就更好了。
[Serializable]
public abstract class BusinessObject<T> : IBusinessObject where T : BusinessObject<T>
{
private int? requestedHashCode;
public virtual int ID { get; set; }
public virtual bool Equals(IBusinessObject other)
{
if (null == other || !GetType().IsInstanceOfType(other))
{
return false;
}
if (ReferenceEquals(this, other))
{
return true;
}
bool otherIsTransient = Equals(other.ID, default(T));
bool thisIsTransient = IsTransient();
if (otherIsTransient && thisIsTransient)
{
return ReferenceEquals(other, this);
}
return other.ID.Equals(ID);
}
protected bool IsTransient()
{
return Equals(ID, default(T));
}
public override bool Equals(object obj)
{
var that = obj as IBusinessObject;
return Equals(that);
}
public override int GetHashCode()
{
if (!requestedHashCode.HasValue)
{
requestedHashCode = IsTransient() ? base.GetHashCode() : ID.GetHashCode();
}
return requestedHashCode.Value;
}
}
什么是瞬态对象?
Please explain the technique used in this code to test Object Equality and Identity.
Better, if you can supply me any web-link/book-reference for detailed discussion.
[Serializable]
public abstract class BusinessObject<T> : IBusinessObject where T : BusinessObject<T>
{
private int? requestedHashCode;
public virtual int ID { get; set; }
public virtual bool Equals(IBusinessObject other)
{
if (null == other || !GetType().IsInstanceOfType(other))
{
return false;
}
if (ReferenceEquals(this, other))
{
return true;
}
bool otherIsTransient = Equals(other.ID, default(T));
bool thisIsTransient = IsTransient();
if (otherIsTransient && thisIsTransient)
{
return ReferenceEquals(other, this);
}
return other.ID.Equals(ID);
}
protected bool IsTransient()
{
return Equals(ID, default(T));
}
public override bool Equals(object obj)
{
var that = obj as IBusinessObject;
return Equals(that);
}
public override int GetHashCode()
{
if (!requestedHashCode.HasValue)
{
requestedHashCode = IsTransient() ? base.GetHashCode() : ID.GetHashCode();
}
return requestedHashCode.Value;
}
}
What is a transient object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
other
是否是与当前对象类型相同的实例。如果不相等,则它们不相等other
和当前对象是否是同一实例。如果是,显然它们是相等的。other
和当前对象都是瞬态的(即尚未持久化),则它们没有 ID,因此无法通过 ID 进行比较。相反,它们是通过参考进行比较的。 (正如 Marc Gravell 在评论中指出的,检查对象是否瞬态的测试已被破坏;将 int 与 default(T) 进行比较是没有意义的)other
is an instance of the same type as the current object. If not, they're not equalother
and the current object are the same instance. If they are, obviously they are equalother
and the current object are transient (i.e. not yet persisted), they don't have an ID, so they can't be compared by ID. Instead, they are compared by reference. (as noted by Marc Gravell in the comments, the test to check if the object is transient is broken; it doesn't make sense to compare an int to default(T))我认为代码试图做的是说“它有一个ID吗”,即“瞬态”对象可能(如果我正确地阅读了代码的意图)是一个尚未保存的对象到数据库。那么相等性的定义是:
不幸的是,实现看起来完全损坏,如
Equals(ID, default(T))T
是完全不同的东西(BusinessObject
)时,code> 毫无意义 - 因此default(T)
将始终是null
和ID
将决不为null
(不可为空)。因此,没有任何东西会被报告为瞬态。此外,此代码
非常效率低下。我怀疑涉及
as
的东西会更好,但同样:代码看起来如此......折磨......我讨厌事后猜测其意图。I think what the code is trying to do is say "has it got an ID yet", i.e. a "transient" object might (if I read the code's intent correctly) be one that is not yet saved to the DB. Then equality is defined as:
unfortunately the implementation looks completely broken, as
Equals(ID, default(T))
is meaningless whenT
is something completely different (aBusinessObject<T>
) - hencedefault(T)
will always benull
andID
will never benull
(it is not nullable). So nothing will ever report as transient.Additionally, this code:
is hugely inefficient. I suspect something involving
as
would be far preferable, but again: the code looks so... tortured... that I'm loathe to second-guess the intent.