Monday, September 11, 2017

Use case IEquatable and Object.Equal

Introduction

One can use IEquatable for comparing two object values. This is useful when we have to check any duplicate enteries. Let me know if you have any better solution to implement the same use case scenario. I have address string in Address object.

String Comparisons

  • Handle whitespace between two words , letters.
  • Handle trailing and leading spaces
  • Handle casing of strings

//Private Member
private List<Address> _addresses;

//Method for update Address and duplicate checks
public string UpdateAddress(Address personAddress)
{
           
string requestCorrelationId = _client.GetCorrelationRequest();
           
var userContext = _sessionService.Get<UserContext>(SessionKeys.UserContext);
           
try
           {
               
if (IsDuplicateAddress(personAddress))
               {
return "Duplicate some Message"
}
}
}

//Private Method
private bool IsDuplicateAddress(Address personAddress)
{
    
return _addresses().Any(p => p.Equals(personAddress));
}

//Implement IEquatable
public partial class Address : IEquatable<Address>
  {
  
       
public string AddressLine1 { getset; }

       
public string AddressLine2 { getset; }

       
public string AddressLine3 { getset; }

       
public string Suburb { getset; }

       
public string State { getset; }

       
public string PostCode { getset; }

       
public string CountryCode { getset; }

       
public long? Id { getset; }


      public bool Equals(Address newAddress)
      {     
          
if (newAddress == nullreturn false;
          
return 
string.Equals(Regex.Replace(newAddress.AddressLine1 ?? 
string.Empty, @"\s"string.Empty), 
Regex.Replace(AddressLine1, @"\s"string.Empty), 
StringComparison.OrdinalIgnoreCase)
              && 
string.Equals(Regex.Replace(newAddress.AddressLine2 ?? 
string.Empty, @"\s"string.Empty), 
Regex.Replace(AddressLine2, @"\s"string.Empty), 
StringComparison.OrdinalIgnoreCase)
              && 
string.Equals(Regex.Replace(newAddress.AddressLine3 ?? 
string.Empty, @"\s"string.Empty), 
Regex.Replace(AddressLine3, @"\s"string.Empty), 
StringComparison.OrdinalIgnoreCase)
              && 
string.Equals(newAddress.CountryCode ?? 
string.Empty,CountryCode, 
StringComparison.OrdinalIgnoreCase)
              && 
string.Equals(newAddress.PostCode ?? 
string.Empty,PostCode, 
StringComparison.OrdinalIgnoreCase)
              && 
string.Equals(newAddress.State ?? 
string.Empty, State, 
StringComparison.OrdinalIgnoreCase)
              && 
string.Equals(Regex.Replace(newAddress.Suburb ?? 
string.Empty, @"\s"string.Empty), 
Regex.Replace(Suburb, @"\s"string.Empty), 
StringComparison.OrdinalIgnoreCase);
      }

      
public override bool Equals(object obj)
      {
          
if (ReferenceEquals(null, obj)) return false;
          
if (ReferenceEquals(this, obj)) return true;
          
if (obj.GetType() != GetType()) return false;
          
return Equals(obj as Address);
      }

      
public override int GetHashCode()
      {
          
unchecked
          {
              
var hashCode = AddressLine1?.GetHashCode() ?? 0;
              hashCode = (hashCode
*397) ^ (AddressLine2?.GetHashCode() ?? 0);
              hashCode = (hashCode
*397) ^ (AddressLine3?.GetHashCode() ?? 0);
              hashCode = (hashCode
*397) ^ (Suburb?.GetHashCode() ?? 0);
              hashCode = (hashCode
*397) ^ (State?.GetHashCode() ?? 0);
              hashCode = (hashCode
*397) ^ (PostCode?.GetHashCode() ?? 0);
              hashCode = (hashCode
*397) ^ (CountryCode?.GetHashCode() ?? 0);
              hashCode = (hashCode
*397) ^ Id.GetHashCode();
              
return hashCode;
          }
      }
  }

No comments :