From d7f3414a0cb682dd594110f6f148120cb0bed4c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Thu, 15 Dec 2022 17:27:30 +0300 Subject: [PATCH] Remove EntityEto inheritance requirement --- .../Events/Distributed/EntitySynchronizer.cs | 91 +++++++++++-------- 1 file changed, 53 insertions(+), 38 deletions(-) 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 91ec77583f..9f03c45f51 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 @@ -11,28 +11,35 @@ using Volo.Abp.Uow; namespace Volo.Abp.Domain.Entities.Events.Distributed; -public abstract class EntitySynchronizer : - EntitySynchronizer +public abstract class EntitySynchronizer : + EntitySynchronizer where TEntity : class, IEntity - where TExternalEntityEto : EntityEto { - private readonly IRepository _repository; + protected new IRepository Repository { get; } protected EntitySynchronizer(IObjectMapper objectMapper, IRepository repository) : base(objectMapper, repository) { - _repository = repository; + Repository = repository; } - protected override Task FindLocalEntityAsync(TExternalEntityEto eto) + protected override Task FindLocalEntityAsync(TSourceEntityEto eto) { - return _repository.FindAsync(GetExternalEntityId(eto)); + return Repository.FindAsync(GetExternalEntityId(eto)); } - protected virtual TKey GetExternalEntityId(TExternalEntityEto eto) + protected virtual TKey GetExternalEntityId(TSourceEntityEto eto) { var keyType = typeof(TKey); - var keyValue = Check.NotNullOrEmpty(eto.KeysAsString, nameof(eto.KeysAsString)); + + 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)) { @@ -43,30 +50,28 @@ public abstract class EntitySynchronizer : } } -public abstract class EntitySynchronizer : - IDistributedEventHandler>, - IDistributedEventHandler>, - IDistributedEventHandler>, - IUnitOfWorkEnabled +public abstract class EntitySynchronizer : + IDistributedEventHandler>, + IDistributedEventHandler>, + IDistributedEventHandler> where TEntity : class, IEntity - where TExternalEntityEto : EntityEto { protected IObjectMapper ObjectMapper { get; } - private readonly IRepository _repository; + protected IRepository Repository { get; } - protected virtual bool IgnoreEntityCreatedEvent { get; set; } - protected virtual bool IgnoreEntityUpdatedEvent { get; set; } - protected virtual bool IgnoreEntityDeletedEvent { get; set; } + protected bool IgnoreEntityCreatedEvent { get; set; } + protected bool IgnoreEntityUpdatedEvent { get; set; } + protected bool IgnoreEntityDeletedEvent { get; set; } public EntitySynchronizer( IObjectMapper objectMapper, IRepository repository) { ObjectMapper = objectMapper; - _repository = repository; + Repository = repository; } - public virtual async Task HandleEventAsync(EntityCreatedEto eventData) + public virtual async Task HandleEventAsync(EntityCreatedEto eventData) { if (IgnoreEntityCreatedEvent) { @@ -76,7 +81,7 @@ public abstract class EntitySynchronizer : await TryCreateOrUpdateEntityAsync(eventData.Entity); } - public virtual async Task HandleEventAsync(EntityUpdatedEto eventData) + public virtual async Task HandleEventAsync(EntityUpdatedEto eventData) { if (IgnoreEntityUpdatedEvent) { @@ -86,7 +91,7 @@ public abstract class EntitySynchronizer : await TryCreateOrUpdateEntityAsync(eventData.Entity); } - public virtual async Task HandleEventAsync(EntityDeletedEto eventData) + public virtual async Task HandleEventAsync(EntityDeletedEto eventData) { if (IgnoreEntityDeletedEvent) { @@ -96,7 +101,8 @@ public abstract class EntitySynchronizer : await TryDeleteEntityAsync(eventData.Entity); } - protected virtual async Task TryCreateOrUpdateEntityAsync(TExternalEntityEto eto) + [UnitOfWork] + protected virtual async Task TryCreateOrUpdateEntityAsync(TSourceEntityEto eto) { var localEntity = await FindLocalEntityAsync(eto); @@ -111,11 +117,14 @@ public abstract class EntitySynchronizer : if (localEntity is IHasEntityVersion versionedLocalEntity && eto is IHasEntityVersion versionedEto) { - ObjectHelper.TrySetProperty(versionedLocalEntity, x => x.EntityVersion, - () => versionedEto.EntityVersion); + ObjectHelper.TrySetProperty( + versionedLocalEntity, + x => x.EntityVersion, + () => versionedEto.EntityVersion + ); } - await _repository.InsertAsync(localEntity, true); + await Repository.InsertAsync(localEntity); } else { @@ -123,30 +132,36 @@ public abstract class EntitySynchronizer : if (localEntity is IHasEntityVersion versionedLocalEntity && eto is IHasEntityVersion versionedEto) { - // The version will auto-increment by one when the repository updates the entity. + /* The version will auto-increment by one when the repository updates the entity. + * So, we are decreasing it as a workaround here. + */ var entityVersion = versionedEto.EntityVersion - 1; - - ObjectHelper.TrySetProperty(versionedLocalEntity, x => x.EntityVersion, () => entityVersion); + ObjectHelper.TrySetProperty( + versionedLocalEntity, + x => x.EntityVersion, + () => entityVersion + ); } - await _repository.UpdateAsync(localEntity, true); + await Repository.UpdateAsync(localEntity); } return true; } - protected virtual Task MapToEntityAsync(TExternalEntityEto eto) + protected virtual Task MapToEntityAsync(TSourceEntityEto eto) { - return Task.FromResult(ObjectMapper.Map(eto)); + return Task.FromResult(ObjectMapper.Map(eto)); } - protected virtual Task MapToEntityAsync(TExternalEntityEto eto, TEntity localEntity) + protected virtual Task MapToEntityAsync(TSourceEntityEto eto, TEntity localEntity) { ObjectMapper.Map(eto, localEntity); return Task.CompletedTask; } - protected virtual async Task TryDeleteEntityAsync(TExternalEntityEto eto) + [UnitOfWork] + protected virtual async Task TryDeleteEntityAsync(TSourceEntityEto eto) { var localEntity = await FindLocalEntityAsync(eto); @@ -155,15 +170,15 @@ public abstract class EntitySynchronizer : return false; } - await _repository.DeleteAsync(localEntity, true); + await Repository.DeleteAsync(localEntity, true); return true; } [ItemCanBeNull] - protected abstract Task FindLocalEntityAsync(TExternalEntityEto eto); + protected abstract Task FindLocalEntityAsync(TSourceEntityEto eto); - protected virtual Task IsEtoNewerAsync(TExternalEntityEto eto, [CanBeNull] TEntity localEntity) + protected virtual Task IsEtoNewerAsync(TSourceEntityEto eto, [CanBeNull] TEntity localEntity) { if (localEntity is IHasEntityVersion versionedLocalEntity && eto is IHasEntityVersion versionedEto) {