-
-
Save vkhorikov/1e2ce717f2b0581b5486ecbc290e0263 to your computer and use it in GitHub Desktop.
.NET Value Type (struct) as a DDD Value Object
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 struct Email | |
{ | |
public string Value { get; } | |
public bool IsConfirmed { get; } | |
public Email(string value, bool isConfirmed) | |
{ | |
Value = value; | |
IsConfirmed = isConfirmed; | |
} | |
} | |
Email email1 = new Email("my@email.com", true); | |
Email email2 = new Email("my@email.com", true); | |
bool isEqual = email1.Equals(email2); // true |
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 struct Email : IEquatable<Email> | |
{ | |
public string Value { get; } | |
public bool IsConfirmed { get; } | |
public Email(string value, bool isConfirmed) | |
{ | |
Value = value; | |
IsConfirmed = isConfirmed; | |
} | |
public bool Equals(Email other) | |
{ | |
return Value == other.Value && IsConfirmed == other.IsConfirmed; | |
} | |
public override bool Equals(object obj) | |
{ | |
if (ReferenceEquals(null, obj)) | |
return false; | |
return obj is Email && Equals((Email)obj); | |
} | |
public override int GetHashCode() | |
{ | |
unchecked | |
{ | |
return ((Value != null ? Value.GetHashCode() : 0) * 397) ^ IsConfirmed.GetHashCode(); | |
} | |
} | |
public static bool operator ==(Email a, Email b) | |
{ | |
return a.Equals(b); | |
} | |
public static bool operator !=(Email a, Email b) | |
{ | |
return !(a == b); | |
} | |
} |
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
Email email1 = new Email("my@email.com", true); | |
Email email2 = new Email("my@email.com", true); | |
bool isEqual = email1 == email2; |
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
Email email = new Email("my@email.com", true); |
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
Email email = new Email(); |
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
Email[] emails = new Email[10]; |
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
FormatterServices.GetUninitializedObject(typeof(Customer)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment