diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Values/ValueObject.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Values/ValueObject.cs index 4c9dab3487..acc0dcd59a 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Values/ValueObject.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Values/ValueObject.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; namespace Volo.Abp.Domain.Values; @@ -30,7 +31,14 @@ public abstract class ValueObject return false; } - if (thisValues.Current != null && !thisValues.Current.Equals(otherValues.Current)) + if (thisValues.Current is ValueObject currentValueObject && otherValues.Current is ValueObject otherValueObject) + { + if (!currentValueObject.ValueEquals(otherValueObject)) + { + return false; + } + } + else if (thisValues.Current != null && !thisValues.Current.Equals(otherValues.Current)) { return false; } diff --git a/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Values/AddressWithZipCode.cs b/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Values/AddressWithZipCode.cs new file mode 100644 index 0000000000..84f65d4128 --- /dev/null +++ b/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Values/AddressWithZipCode.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; + +namespace Volo.Abp.Domain.Values; + +public class AddressWithZipCode : ValueObject +{ + public Guid CityId { get; } + + public string Street { get; } + + public int Number { get; } + + public ZipCode ZipCode { get; } + + public string[] Tags { get; } + + private AddressWithZipCode() + { + } + + public AddressWithZipCode( + Guid cityId, + string street, + int number, + ZipCode zipCode, + params string[] tags) + { + CityId = cityId; + Street = street; + Number = number; + ZipCode = zipCode; + Tags = tags; + } + + //Requires to implement this method to return properties. + protected override IEnumerable GetAtomicValues() + { + yield return Street; + yield return CityId; + yield return Number; + yield return ZipCode; + foreach (var tag in Tags) + { + yield return tag; + } + } +} + +public class ZipCode : ValueObject +{ + public string Code { get; } + + public ZipCode(string code) + { + Code = code; + } + + protected override IEnumerable GetAtomicValues() + { + yield return Code; + } +} diff --git a/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Values/ValueObject_Tests.cs b/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Values/ValueObject_Tests.cs index 11b2581437..97145c17a9 100644 --- a/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Values/ValueObject_Tests.cs +++ b/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Values/ValueObject_Tests.cs @@ -37,4 +37,20 @@ public class ValueObject_Tests emailAddress1.ValueEquals(emailAddress2).ShouldBeFalse(); } + + [Fact] + public void ValueObjects_Recursively_ValueEquals() + { + var cityId = Guid.NewGuid(); + + var address1 = new AddressWithZipCode(cityId, "Baris Manco", 42, new ZipCode("0000001"), "home", "office"); + var address2 = new AddressWithZipCode(cityId, "Baris Manco", 42, new ZipCode("0000001"), "home", "office"); + + address1.ValueEquals(address2).ShouldBeTrue(); + + address1 = new AddressWithZipCode(cityId, "Baris Manco", 42, new ZipCode("0000001"), "home", "office"); + address2 = new AddressWithZipCode(cityId, "Baris Manco", 42, new ZipCode("0000002"), "home", "office"); + + address1.ValueEquals(address2).ShouldBeFalse(); + } }