Created
September 3, 2012 17:21
-
-
Save davybrion/3611056 to your computer and use it in GitHub Desktop.
code snippets for "Implementing a Value Object" post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Address | |
{ | |
public Address(string street, string city, string region, | |
string postalCode, string country, string phoneNumber) | |
{ | |
_street = StringValueOrEmptyString(street); | |
_city = StringValueOrEmptyString(city); | |
_region = StringValueOrEmptyString(region); | |
_postalCode = StringValueOrEmptyString(postalCode); | |
_country = StringValueOrEmptyString(country); | |
_phoneNumber = StringValueOrEmptyString(phoneNumber); | |
} | |
private string StringValueOrEmptyString(string value) | |
{ | |
return (value != null ? value : string.Empty); | |
} | |
private string _street; | |
public string Street | |
{ | |
get { return _street; } | |
} | |
private string _city; | |
public string City | |
{ | |
get { return _city; } | |
} | |
private string _region; | |
public string Region | |
{ | |
get { return _region; } | |
} | |
private string _postalCode; | |
public string PostalCode | |
{ | |
get { return _postalCode; } | |
} | |
private string _country; | |
public string Country | |
{ | |
get { return _country; } | |
} | |
private string _phoneNumber; | |
public string PhoneNumber | |
{ | |
get { return _phoneNumber; } | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public override bool Equals(object obj) | |
{ | |
Address address = obj as Address; | |
if (address != null) | |
{ | |
return | |
_street.Equals(address.Street) && | |
_city.Equals(address.City) && | |
_region.Equals(address.Region) && | |
_postalCode.Equals(address.PostalCode) && | |
_country.Equals(address.Country) && | |
_phoneNumber.Equals(address.PhoneNumber); | |
} | |
return base.Equals(obj); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public override int GetHashCode() | |
{ | |
return _street.GetHashCode() ^ | |
_city.GetHashCode() ^ | |
_region.GetHashCode() ^ | |
_postalCode.GetHashCode() ^ | |
_country.GetHashCode() ^ | |
_phoneNumber.GetHashCode(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static bool operator ==(Address address1, Address address2) | |
{ | |
if (!object.ReferenceEquals(address1, null) && object.ReferenceEquals(address2, null)) | |
{ | |
return false; | |
} | |
if (object.ReferenceEquals(address1, null) && !object.ReferenceEquals(address2, null)) | |
{ | |
return false; | |
} | |
return address1.Equals(address2); | |
} | |
public static bool operator !=(Address address1, Address address2) | |
{ | |
return !(address1 == address2); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestFixture] | |
public class AddressTests | |
{ | |
private string _defaultStreet = "MyStreet 243"; | |
private string _defaultCity = "MyCity"; | |
private string _defaultRegion = "MyRegion"; | |
private string _defaultPostalCode = "12345"; | |
private string _defaultCountry = "MyCountry"; | |
private string _defaultPhoneNumber = "123-4567-89"; | |
[Test] | |
public void TwoInstancesWithSameDataAreEqual() | |
{ | |
Address address1 = CreateTestAddressWithDefaultData(); | |
Address address2 = CreateTestAddressWithDefaultData(); | |
Assert.AreEqual(address1, address2); | |
Assert.That(address1 == address2); | |
} | |
[Test] | |
public void TwoInstancesWithDifferentDataAreNotEqual() | |
{ | |
Address address1 = CreateTestAddressWithDefaultData(); | |
Address address2 = new Address(_defaultStreet + "2", _defaultCity, _defaultRegion, | |
_defaultPostalCode, _defaultCountry, _defaultPhoneNumber); | |
Assert.AreNotEqual(address1, address2); | |
Assert.That(address1 != address2); | |
} | |
[Test] | |
public void NullAddressReferenceDoesntCrashWhenUsingEqualityOperator() | |
{ | |
Address address1 = null; | |
Address address2 = CreateTestAddressWithDefaultData(); | |
Assert.That(address1 != address2); | |
} | |
[Test] | |
public void TwoInstancesWithSameDateReturnSameHashCode() | |
{ | |
Address address1 = CreateTestAddressWithDefaultData(); | |
Address address2 = CreateTestAddressWithDefaultData(); | |
Assert.AreEqual(address1.GetHashCode(), address2.GetHashCode()); | |
} | |
[Test] | |
public void TwoInstancesWithDifferentDataDoNotReturnSameHashCode() | |
{ | |
Address address1 = CreateTestAddressWithDefaultData(); | |
Address address2 = new Address(_defaultStreet + "2", _defaultCity, _defaultRegion, | |
_defaultPostalCode, _defaultCountry, _defaultPhoneNumber); | |
Assert.AreNotEqual(address1.GetHashCode(), address2.GetHashCode()); | |
} | |
private Address CreateTestAddressWithDefaultData() | |
{ | |
return new Address(_defaultStreet, _defaultCity, _defaultRegion, | |
_defaultPostalCode, _defaultCountry, _defaultPhoneNumber); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment