-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1584e30
commit cb498f9
Showing
3 changed files
with
53 additions
and
3 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Product.Domain.ValueObjects; | ||
|
||
public class IPAddress | ||
{ | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
namespace Product.Domain.ValueObjects; | ||
|
||
public abstract class ValueObject : IEquatable<ValueObject> | ||
{ | ||
public static bool operator ==(ValueObject one, ValueObject two) | ||
=> EqualOperator(one, two); | ||
|
||
|
||
public static bool operator !=(ValueObject one, ValueObject two) | ||
=> !EqualOperator(one, two); | ||
|
||
|
||
public bool Equals(ValueObject? other) | ||
=> other is not null && ValuesAreEqual(other); | ||
|
||
|
||
public override bool Equals(object? obj) | ||
{ | ||
if (obj is null || obj.GetType() != GetType()) | ||
return false; | ||
|
||
return obj is ValueObject other && ValuesAreEqual(other); | ||
} | ||
|
||
public override int GetHashCode() | ||
=> GetEqualityComponents() | ||
.Select(x => x is not null ? x.GetHashCode() : 0) | ||
.Aggregate((x, y) => x ^ y); | ||
|
||
|
||
protected static bool EqualOperator(ValueObject? left, ValueObject? right) | ||
{ | ||
if (left is null && right is null) | ||
return true; | ||
|
||
if (left is null || right is null) | ||
return false; | ||
|
||
return ReferenceEquals(left, right) || left.Equals(right); | ||
} | ||
|
||
protected abstract IEnumerable<object> GetEqualityComponents(); | ||
|
||
private bool ValuesAreEqual(ValueObject? other) | ||
=> other is not null && GetEqualityComponents().SequenceEqual(other.GetEqualityComponents()); | ||
|
||
} |