Guest User

Untitled

a guest
Jun 23rd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.06 KB | None | 0 0
  1. using System;
  2.  
  3. namespace EqualityWithBiDirectionalAssociation
  4. {
  5.  
  6. public class Person : IEquatable<Person>
  7. {
  8. private string _firstName;
  9. private string _lastName;
  10. private Address _address;
  11.  
  12. public Person(string firstName, string lastName, Address address)
  13. {
  14. FirstName = firstName;
  15. LastName = lastName;
  16. Address = address;
  17. }
  18.  
  19. public virtual Address Address
  20. {
  21. get { return _address; }
  22. set { _address = value; }
  23. }
  24.  
  25. public virtual string FirstName
  26. {
  27. get { return _firstName; }
  28. set { _firstName = value; }
  29. }
  30.  
  31. public virtual string LastName
  32. {
  33. get { return _lastName; }
  34. set { _lastName = value; }
  35. }
  36.  
  37. public override bool Equals(object obj)
  38. {
  39. // Use 'as' rather than a cast to get a null rather an exception
  40. // if the object isn't convertible
  41. Person person = obj as Person;
  42. return this.Equals(person);
  43. }
  44.  
  45. public override int GetHashCode()
  46. {
  47. string composite = FirstName + LastName;
  48. return composite.GetHashCode();
  49. }
  50.  
  51.  
  52. #region IEquatable<Person> Members
  53.  
  54. public virtual bool Equals(Person other)
  55. {
  56. // Per MSDN documentation, x.Equals(null) should return false
  57. if ((object)other == null)
  58. {
  59. return false;
  60. }
  61.  
  62. return (this.Address.Equals(other.Address)
  63. && this.FirstName.Equals(other.FirstName)
  64. && this.LastName.Equals(other.LastName));
  65. }
  66.  
  67. #endregion
  68.  
  69. }
  70.  
  71. public class Address : IEquatable<Address>
  72. {
  73. private string _streetName;
  74. private string _city;
  75. private string _state;
  76. private Person _resident;
  77.  
  78. public Address(string city, string state, string streetName)
  79. {
  80. City = city;
  81. State = state;
  82. StreetName = streetName;
  83. _resident = null;
  84. }
  85.  
  86. public virtual string City
  87. {
  88. get { return _city; }
  89. set { _city = value; }
  90. }
  91.  
  92. public virtual Person Resident
  93. {
  94. get { return _resident; }
  95. set { _resident = value; }
  96. }
  97.  
  98. public virtual string State
  99. {
  100. get { return _state; }
  101. set { _state = value; }
  102. }
  103.  
  104. public virtual string StreetName
  105. {
  106. get { return _streetName; }
  107. set { _streetName = value; }
  108. }
  109.  
  110. public override bool Equals(object obj)
  111. {
  112. // Use 'as' rather than a cast to get a null rather an exception
  113. // if the object isn't convertible
  114. Address address = obj as Address;
  115. return this.Equals(address);
  116. }
  117.  
  118. public override int GetHashCode()
  119. {
  120. string composite = StreetName + City + State;
  121. return composite.GetHashCode();
  122. }
  123.  
  124.  
  125. #region IEquatable<Address> Members
  126.  
  127. public virtual bool Equals(Address other)
  128. {
  129. // Per MSDN documentation, x.Equals(null) should return false
  130. if ((object)other == null)
  131. {
  132. return false;
  133. }
  134.  
  135. return (this.City.Equals(other.City)
  136. && this.State.Equals(other.State)
  137. && this.StreetName.Equals(other.StreetName)
  138. && this.Resident.Equals(other.Resident));
  139. }
  140.  
  141. #endregion
  142. }
  143.  
  144. public class Program
  145. {
  146. static void Main(string[] args)
  147. {
  148. Address address1 = new Address("seattle", "washington", "Awesome St");
  149. Address address2 = new Address("seattle", "washington", "Awesome St");
  150.  
  151. Person person1 = new Person("John", "Doe", address1);
  152.  
  153. address1.Resident = person1;
  154. address2.Resident = person1;
  155.  
  156. if (address1.Equals(address2)) // <-- Generates a stack overflow!
  157. {
  158. Console.WriteLine("The two addresses are equal");
  159. }
  160.  
  161. Person person2 = new Person("John", "Doe", address2);
  162. address2.Resident = person2;
  163.  
  164. if (address1.Equals(address2)) // <-- Generates a stack overflow!
  165. {
  166. Console.WriteLine("The two addresses are equal");
  167. }
  168.  
  169. Console.Read();
  170. }
  171. }
  172. }
  173.  
  174. using System;
  175.  
  176. namespace EqualityWithBiDirectionalAssociation
  177. {
  178.  
  179. public class Person : IEquatable<Person>
  180. {
  181. private string _firstName;
  182. private string _lastName;
  183. private Address _address;
  184.  
  185. public Person(string firstName, string lastName, Address address)
  186. {
  187. FirstName = firstName;
  188. LastName = lastName;
  189. Address = address;
  190. }
  191.  
  192. public virtual Address Address
  193. {
  194. get { return _address; }
  195. set { _address = value; }
  196. }
  197.  
  198. public virtual string FirstName
  199. {
  200. get { return _firstName; }
  201. set { _firstName = value; }
  202. }
  203.  
  204. public virtual string LastName
  205. {
  206. get { return _lastName; }
  207. set { _lastName = value; }
  208. }
  209.  
  210. public override bool Equals(object obj)
  211. {
  212. // Use 'as' rather than a cast to get a null rather an exception
  213. // if the object isn't convertible
  214. Person person = obj as Person;
  215. return this.Equals(person);
  216. }
  217.  
  218. public override int GetHashCode()
  219. {
  220. string composite = FirstName + LastName;
  221. return composite.GetHashCode();
  222. }
  223.  
  224. internal virtual bool EqualsIgnoringAddress(Person other)
  225. {
  226. // Per MSDN documentation, x.Equals(null) should return false
  227. if ((object)other == null)
  228. {
  229. return false;
  230. }
  231.  
  232. return ( this.FirstName.Equals(other.FirstName)
  233. && this.LastName.Equals(other.LastName));
  234. }
  235.  
  236. #region IEquatable<Person> Members
  237.  
  238. public virtual bool Equals(Person other)
  239. {
  240. // Per MSDN documentation, x.Equals(null) should return false
  241. if ((object)other == null)
  242. {
  243. return false;
  244. }
  245.  
  246. return (this.Address.EqualsIgnoringPerson(other.Address) // Don't have Address check it's person
  247. && this.FirstName.Equals(other.FirstName)
  248. && this.LastName.Equals(other.LastName));
  249. }
  250.  
  251. #endregion
  252.  
  253. }
  254.  
  255. public class Address : IEquatable<Address>
  256. {
  257. private string _streetName;
  258. private string _city;
  259. private string _state;
  260. private Person _resident;
  261.  
  262. public Address(string city, string state, string streetName)
  263. {
  264. City = city;
  265. State = state;
  266. StreetName = streetName;
  267. _resident = null;
  268. }
  269.  
  270. public virtual string City
  271. {
  272. get { return _city; }
  273. set { _city = value; }
  274. }
  275.  
  276. public virtual Person Resident
  277. {
  278. get { return _resident; }
  279. set { _resident = value; }
  280. }
  281.  
  282. public virtual string State
  283. {
  284. get { return _state; }
  285. set { _state = value; }
  286. }
  287.  
  288. public virtual string StreetName
  289. {
  290. get { return _streetName; }
  291. set { _streetName = value; }
  292. }
  293.  
  294. public override bool Equals(object obj)
  295. {
  296. // Use 'as' rather than a cast to get a null rather an exception
  297. // if the object isn't convertible
  298. Address address = obj as Address;
  299. return this.Equals(address);
  300. }
  301.  
  302. public override int GetHashCode()
  303. {
  304. string composite = StreetName + City + State;
  305. return composite.GetHashCode();
  306. }
  307.  
  308.  
  309.  
  310. internal virtual bool EqualsIgnoringPerson(Address other)
  311. {
  312. // Per MSDN documentation, x.Equals(null) should return false
  313. if ((object)other == null)
  314. {
  315. return false;
  316. }
  317.  
  318. return (this.City.Equals(other.City)
  319. && this.State.Equals(other.State)
  320. && this.StreetName.Equals(other.StreetName));
  321. }
  322.  
  323. #region IEquatable<Address> Members
  324.  
  325. public virtual bool Equals(Address other)
  326. {
  327. // Per MSDN documentation, x.Equals(null) should return false
  328. if ((object)other == null)
  329. {
  330. return false;
  331. }
  332.  
  333. return (this.City.Equals(other.City)
  334. && this.State.Equals(other.State)
  335. && this.StreetName.Equals(other.StreetName)
  336. && this.Resident.EqualsIgnoringAddress(other.Resident));
  337. }
  338.  
  339. #endregion
  340. }
  341.  
  342. public class Program
  343. {
  344. static void Main(string[] args)
  345. {
  346. Address address1 = new Address("seattle", "washington", "Awesome St");
  347. Address address2 = new Address("seattle", "washington", "Awesome St");
  348.  
  349. Person person1 = new Person("John", "Doe", address1);
  350.  
  351. address1.Resident = person1;
  352. address2.Resident = person1;
  353.  
  354. if (address1.Equals(address2)) // <-- No stack overflow!
  355. {
  356. Console.WriteLine("The two addresses are equal");
  357. }
  358.  
  359. Person person2 = new Person("John", "Doe", address2);
  360. address2.Resident = person2;
  361.  
  362. if (address1.Equals(address2)) // <-- No a stack overflow!
  363. {
  364. Console.WriteLine("The two addresses are equal");
  365. }
  366.  
  367. Console.Read();
  368. }
  369. }
  370. }
  371.  
  372. public override bool Equals(object obj){
  373. // Use 'as' rather than a cast to get a null rather an exception
  374. // if the object isn't convertible .
  375. Person person = obj as Person;
  376. return this.Equals(person); // wrong
  377. this.FirstName.Equals(person.FirstName)
  378. this.LastName.Equals(person.LastName)
  379. // and so on
  380. }
Add Comment
Please, Sign In to add comment