I've got a class called Account. After getting data from a webserver and a database, I want to compare the objects from the two sources in specific fields.
To do this, I wrote the following:
public class Account2 : Account
{
public bool Equals(Account acc)
{
if (acc.AccountNumber != this.AccountNumber)
return false;
if (acc.Name != this.Name)
return false;
if (acc.Address1_Line1 != this.Address1_Line1)
return false;
if (acc.Address1_PostalCode != this.Address1_PostalCode)
return false;
if (acc.Address1_City != this.Address1_City)
return false;
if (acc.PrimaryContactId.Id != this.PrimaryContactId.Id)
return false;
if (acc.OwnerId.Id != this.OwnerId.Id)
return false;
if (acc.Address1_Name != this.Address1_Name)
return false;
if (acc.Address1_Country != this.Address1_Country)
return false;
if (acc.Fax != this.Fax)
return false;
if (acc.Telephone1 != this.Telephone1)
return false;
if (acc.Telephone2 != this.Telephone2)
return false;
if (acc.Telephone3 != this.Telephone3)
return false;
if (acc.EMailAddress1 != this.EMailAddress1)
return false;
if (acc.EMailAddress2 != this.EMailAddress2)
return false;
return true;
}
}
I thought about using attributes on the properties and do a loop over all to determine which I should compare. The problem is I dont have access to the base Account-class. The main issue is when adding another class. I have to do this for 5 other ones which really is a lot to type. Additionally its hard to maintain if I add another field.
Is there a better way to do this comparison?