简体   繁体   English

ID比较与参考比较

[英]ID compare vs. Reference compare

I have some Entity Framework objects which are also identified by their own ID. 我有一些实体框架对象,这些对象也由其自己的ID标识。

I prefer to use reference comparison which is probably more reliable, especially for objects not on the DB yet. 我更喜欢使用引用比较,它可能更可靠,尤其是对于尚未在数据库上的对象。

Which is the gap between Int32 ID comparison and ByRef object comparison in terms of performance? 就性能而言, Int32 ID比较ByRef对象比较之间的差距是什么?

The performance of comparing a reference and comparing an int will be about the same. 比较参考和比较int的性能大致相同。 The ID comparison requires an additional property access call on the stack. ID比较需要在堆栈上进行其他属性访问调用。 However, the performance difference between the two would be negligible. 但是,两者之间的性能差异可以忽略不计。

However, this should not be the determining factor as to how you determine equality. 但是,这不应成为决定平等的决定因素。 Equality should be determined based on what the object represents. 平等应根据对象所代表的内容来确定。 If the object is defined by it's ID property, then the ID property should be used to determine equality. 如果对象是通过其ID属性定义的,则应使用ID属性确定相等性。 If the object is defined by the composition of its values, then you should determine equality by comparing each of its component values. 如果对象是由其值的组成定义的,则应通过比较其每个组成部分的值来确定是否相等。

To better understand what I'm saying, consider the following class as an example: 为了更好地理解我的意思,请考虑以下课程作为示例:

 public class Lady { 
      public Lady(int id, string name, bool isMarried){
          this.ID = id;
          this.Name = name;
      }
      public int ID { get; private set; }
      public string Name { get; set; }

      public override int Equals(object other){  /* What goes here? */ }
      public override int GetHashCode(){  /* What goes here? */ }
 }

Imagine that you're app uses the Lady class like: 假设您的应用程序使用Lady类,例如:

 void Main(){
      var JaneSmith = new Lady(id:12,name:"Jane Smith");
      var JaneSmithJones = new Lady(id:12,name:"Jane Smith-Jones");
 }

In this case, the two objects might actually refer to the same person. 在这种情况下,这两个对象实际上可能是指同一个人。 Perhaps JaneSmith was created and cached before Jane got married and changed her name. 也许JaneSmith是在Jane结婚并更名之前创建和缓存的。 But, now that her name has changed, and now both a value and reference comparison will fail. 但是,现在她的名字已经更改,现在值和引用比较都将失败。 However, if we use ID equality, then this is okay, because we will know they are supposed to refer to the same person (and the same data source). 但是,如果我们使用ID相等,那就可以了,因为我们知道他们应该引用相同的人(和相同的数据源)。 We can then resolve which Lady instance is currently correct by abandoning both instances and reloading the Lady with ID == 12 from the database. 然后,我们可以通过放弃两个实例并从数据库中重新加载ID == 12Lady来确定当前哪个Lady实例正确。 By contrast, if we were using either reference equality or value equality, we would end up saving both objects, possibly overwriting the wrong data. 相比之下,如果我们使用引用相等或值相等,则最终将保存两个对象,可能会覆盖错误的数据。 Also, even if the data in both instances were identical and we only had two instances because we accidentally loaded JaneSmith from the database twice, the reference equality check would return false. 另外,即使两个实例中的数据相同,并且由于意外从数据库中两次加载JaneSmith而我们只有两个实例,引用相等性检查也将返回false。 It doesn't seem quite right that, new Lady(12,"Jane").Equals(new Lady(12,"Jane")) should return false. new Lady(12,"Jane").Equals(new Lady(12,"Jane"))应该返回false,这似乎不太正确。 It also makes caching virtually impossible, because you can never determine if a record has been cached or not . 这也几乎使缓存变得不可能,因为您永远无法确定记录是否已缓存

In general, using reference equality too compare persistent objects is a bad idea. 通常,使用引用相等也比较持久对象是一个坏主意。 However, ID equality is not always appropriate either. 但是, ID相等也不总是合适的。 Neither is value equality. 价值平等也不是。 There are cases for both, and you have to decide what best represents the data you are manipulating. 两者都有情况,您必须决定最能代表您要处理的数据。 For more on this, this article provides a very basic concept of the difference between "Entities" and "Value Objects" (concepts from the Domain Driven Design methodology). 有关更多信息, 本文提供了“实体”和“值对象”之间区别的非常基本的概念(来自域驱动设计方法的概念)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM