From ce23205caf41b59317ba822790ccca61e7f91ad9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Thu, 15 Dec 2022 17:58:35 +0300 Subject: [PATCH] Introduce the IEntityEto interface and revise the EntitySynchronizer --- .../Entities/Events/Distributed/EntityEto.cs | 5 ++++ .../Events/Distributed/EntitySynchronizer.cs | 24 ++----------------- .../Entities/Events/Distributed/IEntityEto.cs | 9 +++++++ .../EntitySynchronizer_Tests.cs | 18 +++++++------- .../WithEntityVersion/RemoteBookEto.cs | 5 ++-- .../WithoutEntityVersion/RemoteAuthorEto.cs | 6 +++-- 6 files changed, 31 insertions(+), 36 deletions(-) create mode 100644 framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Events/Distributed/IEntityEto.cs diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Events/Distributed/EntityEto.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Events/Distributed/EntityEto.cs index 4c1c068a81..6e30e9afab 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Events/Distributed/EntityEto.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Events/Distributed/EntityEto.cs @@ -20,3 +20,8 @@ public class EntityEto : EtoBase KeysAsString = keysAsString; } } + +public class EntityEto : IEntityEto +{ + public TKey Id { get; set; } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Events/Distributed/EntitySynchronizer.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Events/Distributed/EntitySynchronizer.cs index 9f03c45f51..c5f3f58542 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Events/Distributed/EntitySynchronizer.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Events/Distributed/EntitySynchronizer.cs @@ -14,6 +14,7 @@ namespace Volo.Abp.Domain.Entities.Events.Distributed; public abstract class EntitySynchronizer : EntitySynchronizer where TEntity : class, IEntity + where TSourceEntityEto : IEntityEto { protected new IRepository Repository { get; } @@ -25,28 +26,7 @@ public abstract class EntitySynchronizer : protected override Task FindLocalEntityAsync(TSourceEntityEto eto) { - return Repository.FindAsync(GetExternalEntityId(eto)); - } - - protected virtual TKey GetExternalEntityId(TSourceEntityEto eto) - { - var keyType = typeof(TKey); - - if (eto is not EntityEto entityEto) - { - throw new AbpException( - $"The given ETO is not an EntityEto! Its type is {typeof(TSourceEntityEto).FullName}. " + - $"In this case, you should override the {nameof(GetExternalEntityId)} method and return the entity's Id, or override the {nameof(FindLocalEntityAsync)} method and return the entity."); - } - - var keyValue = Check.NotNullOrEmpty(entityEto.KeysAsString, nameof(entityEto.KeysAsString)); - - if (keyType == typeof(Guid)) - { - return (TKey)TypeDescriptor.GetConverter(keyType).ConvertFromInvariantString(keyValue); - } - - return (TKey)Convert.ChangeType(keyValue, keyType, CultureInfo.InvariantCulture); + return Repository.FindAsync(eto.Id); } } diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Events/Distributed/IEntityEto.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Events/Distributed/IEntityEto.cs new file mode 100644 index 0000000000..289f5354fd --- /dev/null +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Events/Distributed/IEntityEto.cs @@ -0,0 +1,9 @@ +namespace Volo.Abp.Domain.Entities.Events.Distributed; + +public interface IEntityEto +{ + /// + /// Unique identifier for this entity. + /// + TKey Id { get; set; } +} \ No newline at end of file diff --git a/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Entities/Events/Distributed/EntitySynchronizers/EntitySynchronizer_Tests.cs b/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Entities/Events/Distributed/EntitySynchronizers/EntitySynchronizer_Tests.cs index 77c50d2aca..3ad8778913 100644 --- a/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Entities/Events/Distributed/EntitySynchronizers/EntitySynchronizer_Tests.cs +++ b/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Entities/Events/Distributed/EntitySynchronizers/EntitySynchronizer_Tests.cs @@ -33,7 +33,7 @@ public class EntitySynchronizer_Tests : AbpIntegratedTest(remoteAuthorEto)); @@ -60,7 +60,7 @@ public class EntitySynchronizer_Tests : AbpIntegratedTest(remoteAuthorEto)); @@ -88,7 +88,7 @@ public class EntitySynchronizer_Tests : AbpIntegratedTest(remoteAuthorEto)); @@ -111,7 +111,7 @@ public class EntitySynchronizer_Tests : AbpIntegratedTest(remoteBookEto)); @@ -139,7 +139,7 @@ public class EntitySynchronizer_Tests : AbpIntegratedTest(remoteBookEto)); @@ -188,7 +188,7 @@ public class EntitySynchronizer_Tests : AbpIntegratedTest(remoteBookEto)); @@ -245,10 +245,8 @@ public class EntitySynchronizer_Tests : AbpIntegratedTest(MemberList.None) - .ForMember(x => x.Id, options => options.MapFrom(x => Guid.Parse(x.KeysAsString))); - CreateMap(MemberList.None) - .ForMember(x => x.Id, options => options.MapFrom(x => Guid.Parse(x.KeysAsString))); + CreateMap(MemberList.None); + CreateMap(MemberList.None); } } } \ No newline at end of file diff --git a/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Entities/Events/Distributed/EntitySynchronizers/WithEntityVersion/RemoteBookEto.cs b/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Entities/Events/Distributed/EntitySynchronizers/WithEntityVersion/RemoteBookEto.cs index 48e532222d..3b5aea031d 100644 --- a/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Entities/Events/Distributed/EntitySynchronizers/WithEntityVersion/RemoteBookEto.cs +++ b/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Entities/Events/Distributed/EntitySynchronizers/WithEntityVersion/RemoteBookEto.cs @@ -1,8 +1,9 @@ -using Volo.Abp.Auditing; +using System; +using Volo.Abp.Auditing; namespace Volo.Abp.Domain.Entities.Events.Distributed.EntitySynchronizers.WithEntityVersion; -public class RemoteBookEto : EntityEto, IHasEntityVersion +public class RemoteBookEto : EntityEto, IHasEntityVersion { public int EntityVersion { get; set; } diff --git a/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Entities/Events/Distributed/EntitySynchronizers/WithoutEntityVersion/RemoteAuthorEto.cs b/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Entities/Events/Distributed/EntitySynchronizers/WithoutEntityVersion/RemoteAuthorEto.cs index 3923b2eeff..2cf44f8691 100644 --- a/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Entities/Events/Distributed/EntitySynchronizers/WithoutEntityVersion/RemoteAuthorEto.cs +++ b/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Entities/Events/Distributed/EntitySynchronizers/WithoutEntityVersion/RemoteAuthorEto.cs @@ -1,6 +1,8 @@ -namespace Volo.Abp.Domain.Entities.Events.Distributed.EntitySynchronizers.WithoutEntityVersion; +using System; -public class RemoteAuthorEto : EntityEto +namespace Volo.Abp.Domain.Entities.Events.Distributed.EntitySynchronizers.WithoutEntityVersion; + +public class RemoteAuthorEto : EntityEto { public string Name { get; set; } } \ No newline at end of file