Browse Source

Recursively check if the `ValueObject` are `ValueEquals`.

Resolve #19312
pull/19313/head
maliming 2 years ago
parent
commit
ec43e96d95
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 12
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Values/ValueObject.cs
  2. 63
      framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Values/AddressWithZipCode.cs
  3. 16
      framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Values/ValueObject_Tests.cs

12
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;
}

63
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<object> 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<object> GetAtomicValues()
{
yield return Code;
}
}

16
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();
}
}

Loading…
Cancel
Save