Browse Source

Introduce the IEntityEto<TKey> interface and revise the EntitySynchronizer

pull/15134/head
Halil İbrahim Kalkan 4 years ago
parent
commit
ce23205caf
  1. 5
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Events/Distributed/EntityEto.cs
  2. 24
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Events/Distributed/EntitySynchronizer.cs
  3. 9
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Events/Distributed/IEntityEto.cs
  4. 18
      framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Entities/Events/Distributed/EntitySynchronizers/EntitySynchronizer_Tests.cs
  5. 5
      framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Entities/Events/Distributed/EntitySynchronizers/WithEntityVersion/RemoteBookEto.cs
  6. 6
      framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Entities/Events/Distributed/EntitySynchronizers/WithoutEntityVersion/RemoteAuthorEto.cs

5
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<TKey> : IEntityEto<TKey>
{
public TKey Id { get; set; }
}

24
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<TEntity, TKey, TSourceEntityEto> :
EntitySynchronizer<TEntity, TSourceEntityEto>
where TEntity : class, IEntity<TKey>
where TSourceEntityEto : IEntityEto<TKey>
{
protected new IRepository<TEntity, TKey> Repository { get; }
@ -25,28 +26,7 @@ public abstract class EntitySynchronizer<TEntity, TKey, TSourceEntityEto> :
protected override Task<TEntity> 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);
}
}

9
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<TKey>
{
/// <summary>
/// Unique identifier for this entity.
/// </summary>
TKey Id { get; set; }
}

18
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<EntitySynchronizer_Tes
(await repository.FindAsync(authorId)).ShouldBeNull();
var remoteAuthorEto = new RemoteAuthorEto { KeysAsString = authorId.ToString(), Name = "New" };
var remoteAuthorEto = new RemoteAuthorEto { Id = authorId, Name = "New" };
await authorSynchronizer.HandleEventAsync(new EntityCreatedEto<RemoteAuthorEto>(remoteAuthorEto));
@ -60,7 +60,7 @@ public class EntitySynchronizer_Tests : AbpIntegratedTest<EntitySynchronizer_Tes
author.Id.ShouldBe(authorId);
author.Name.ShouldBe("Old");
var remoteAuthorEto = new RemoteAuthorEto { KeysAsString = authorId.ToString(), Name = "New" };
var remoteAuthorEto = new RemoteAuthorEto { Id = authorId, Name = "New" };
await authorSynchronizer.HandleEventAsync(new EntityUpdatedEto<RemoteAuthorEto>(remoteAuthorEto));
@ -88,7 +88,7 @@ public class EntitySynchronizer_Tests : AbpIntegratedTest<EntitySynchronizer_Tes
author.Id.ShouldBe(authorId);
author.Name.ShouldBe("Old");
var remoteAuthorEto = new RemoteAuthorEto { KeysAsString = authorId.ToString(), Name = "New" };
var remoteAuthorEto = new RemoteAuthorEto { Id = authorId, Name = "New" };
await authorSynchronizer.HandleEventAsync(new EntityDeletedEto<RemoteAuthorEto>(remoteAuthorEto));
@ -111,7 +111,7 @@ public class EntitySynchronizer_Tests : AbpIntegratedTest<EntitySynchronizer_Tes
(await repository.FindAsync(bookId)).ShouldBeNull();
var remoteBookEto = new RemoteBookEto { KeysAsString = bookId.ToString(), EntityVersion = 0, Sold = 1 };
var remoteBookEto = new RemoteBookEto { Id = bookId, EntityVersion = 0, Sold = 1 };
await bookSynchronizer.HandleEventAsync(new EntityCreatedEto<RemoteBookEto>(remoteBookEto));
@ -139,7 +139,7 @@ public class EntitySynchronizer_Tests : AbpIntegratedTest<EntitySynchronizer_Tes
book.Id.ShouldBe(bookId);
book.EntityVersion.ShouldBe(0);
var remoteBookEto = new RemoteBookEto { KeysAsString = bookId.ToString(), EntityVersion = 0, Sold = 10 };
var remoteBookEto = new RemoteBookEto { Id = bookId, EntityVersion = 0, Sold = 10 };
await bookSynchronizer.HandleEventAsync(new EntityUpdatedEto<RemoteBookEto>(remoteBookEto));
@ -188,7 +188,7 @@ public class EntitySynchronizer_Tests : AbpIntegratedTest<EntitySynchronizer_Tes
book.Id.ShouldBe(bookId);
book.EntityVersion.ShouldBe(0);
var remoteBookEto = new RemoteBookEto { KeysAsString = bookId.ToString(), EntityVersion = 0, Sold = 1 };
var remoteBookEto = new RemoteBookEto { Id = bookId, EntityVersion = 0, Sold = 1 };
await bookSynchronizer.HandleEventAsync(new EntityDeletedEto<RemoteBookEto>(remoteBookEto));
@ -245,10 +245,8 @@ public class EntitySynchronizer_Tests : AbpIntegratedTest<EntitySynchronizer_Tes
{
public MyAutoMapperProfile()
{
CreateMap<RemoteBookEto, Book>(MemberList.None)
.ForMember(x => x.Id, options => options.MapFrom(x => Guid.Parse(x.KeysAsString)));
CreateMap<RemoteAuthorEto, Author>(MemberList.None)
.ForMember(x => x.Id, options => options.MapFrom(x => Guid.Parse(x.KeysAsString)));
CreateMap<RemoteBookEto, Book>(MemberList.None);
CreateMap<RemoteAuthorEto, Author>(MemberList.None);
}
}
}

5
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<Guid>, IHasEntityVersion
{
public int EntityVersion { get; set; }

6
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<Guid>
{
public string Name { get; set; }
}
Loading…
Cancel
Save