请解释此代码中用于测试对象相等性和同一性的技术

发布于 2024-12-13 19:30:17 字数 1456 浏览 0 评论 0原文

请解释此代码中用于测试对象相等性和同一性的技术。

如果您能给我提供任何网络链接/书籍参考以进行详细讨论,那就更好了。

[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如梦亦如幻 2024-12-20 19:30:17
  • 它首先检查 other 是否是与当前对象类型相同的实例。如果不相等,则它们不相等
  • ,然后执行引用相等来检查 other 和当前对象是否是同一实例。如果是,显然它们是相等的。
  • 如果 other 和当前对象都是瞬态的(即尚未持久化),则它们没有 ID,因此无法通过 ID 进行比较。相反,它们是通过参考进行比较的。 (正如 Marc Gravell 在评论中指出的,检查对象是否瞬态的测试已被破坏;将 int 与 default(T) 进行比较是没有意义的)
  • 最终,它们的 ID 是比较的;如果对象具有相同的 ID,则认为它们相等
  • it first checks if other is an instance of the same type as the current object. If not, they're not equal
  • it then performs a reference equality to check if other and the current object are the same instance. If they are, obviously they are equal
  • If both other 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))
  • Eventually, their IDs are compared; the objects are considered equal if they have the same ID
谈情不如逗狗 2024-12-20 19:30:17

认为代码试图做的是说“它有一个ID吗”,即“瞬态”对象可能(如果我正确地阅读了代码的意图)是一个尚未保存的对象到数据库。那么相等性的定义是:

  • 如果有ID,那么它匹配吗? (即使对于同一类型的不同实例)
  • 如果它没有 ID,它是否是相同的对象实例(引用)

不幸的是,实现看起来完全损坏,如 Equals(ID, default(T))T 是完全不同的东西(BusinessObject)时,code> 毫无意义 - 因此 default(T) 将始终是 nullID决不null(不可为空)。因此,没有任何东西会被报告为瞬态。

此外,此代码

if (null == other || !GetType().IsInstanceOfType(other))

非常效率低下。我怀疑涉及 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:

  • if it has an ID, does it match? (even for different instances of the same type)
  • if it doesn't have an ID, is it the same object instance (reference)

unfortunately the implementation looks completely broken, as Equals(ID, default(T)) is meaningless when T is something completely different (a BusinessObject<T>) - hence default(T) will always be null and ID will never be null (it is not nullable). So nothing will ever report as transient.

Additionally, this code:

if (null == other || !GetType().IsInstanceOfType(other))

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文