Typical Use Case
Functional
You have a collection of Patients. Before inserting a new Patient, you need to check if there already exists a past record of the same Patient. The Patient is considered already existing if there is a matching Government provided ID (for example SSN, Passport, etc.) Or a combination of First Name, Last Name, and DOB.
Note
The above functional requirement is not an absolute one in the real world. It is just for illustration purposes.
Let's say we have a very simple domain entity class of the Patient. For brevity purpose, I am not decorating the attributes of Required or null coalescing
- public class Patient
- {
- public long PatientID { get; set; }
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public DateTime DOB { get; set; }
- public string GovtID { get; set; }
- }
Now to build our custom comparison for the duplicate check, we would make the <Patient> class inherit from the interface of IEquatable.
- public class Patient : IEquatable<Patient>
This will enforce to implement and override the Equals method. I have added another property for returning the related error message in case there is a duplicate record found. In order to enhance the "equal to" and "not equal to" operators, override them as well. Thus the class will now look like,
- public class Patient : IEquatable<Patient>
- {
- public long PatientID { get; set; }
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public DateTime DOB { get; set; }
- public string GovtID { get; set; }
- //This property will contain value only if there is a duplicate found.
- public string DuplicateEntityFoundMessage { get; set; }
- //The custom EQUALS method is amended to provide the custom comparison
- // First comparison is made on the GovtID and the 2nd one on a combimation of Names and DOB
- // The returning message is different in both cases.
- public bool Equals(Patient other)
- {
- bool response = false;
- other.DuplicateEntityFoundMessage = string.Empty;
- if (other == null)
- response = false;
- if (this.GovtID == other.GovtID)
- {
- response = true;
- other.DuplicateEntityFoundMessage = "Patient already exists with the same GovtID";
- }
- else if (this.FirstName == other.FirstName && this.LastName == other.LastName && this.DOB == other.DOB)
- {
- response = true;
- other.DuplicateEntityFoundMessage = "Patient already exists with the same First Name, Last Name and DOB";
- }
- return response;
- }
- //Overriding the operator ==
- public static bool operator == (Patient person1, Patient person2)
- {
- if (((object)person1) == null || ((object)person2) == null)
- return Object.Equals(person1, person2);
- return person1.Equals(person2);
- }
- //Overriding the operator !=
- public static bool operator != (Patient person1, Patient person2)
- {
- if (((object)person1) == null || ((object)person2) == null)
- return !Object.Equals(person1, person2);
- return !(person1.Equals(person2));
- }
- }

Join the conversation! Your thoughts help the community grow.