Skip to content

Commit

Permalink
Refactor modules' local user synchronization
Browse files Browse the repository at this point in the history
  • Loading branch information
gdlcf88 committed Sep 21, 2023
1 parent 56e6148 commit 031525f
Show file tree
Hide file tree
Showing 29 changed files with 188 additions and 176 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Threading.Tasks;
using JetBrains.Annotations;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Auditing;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
Expand All @@ -12,7 +12,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>
where TSourceEntityEto : IEntityEto<TKey>
{
protected new IRepository<TEntity, TKey> Repository { get; }

Expand All @@ -32,6 +32,7 @@ public abstract class EntitySynchronizer<TEntity, TSourceEntityEto> :
IDistributedEventHandler<EntityCreatedEto<TSourceEntityEto>>,
IDistributedEventHandler<EntityUpdatedEto<TSourceEntityEto>>,
IDistributedEventHandler<EntityDeletedEto<TSourceEntityEto>>,
IEntitySynchronizer<TSourceEntityEto>,
ITransientDependency
where TEntity : class, IEntity
{
Expand Down Expand Up @@ -77,11 +78,13 @@ public virtual async Task HandleEventAsync(EntityDeletedEto<TSourceEntityEto> ev
return;
}

// todo: if it fails, create the entity and try to delete it again.
await TryDeleteEntityAsync(eventData.Entity);
}

[UnitOfWork]
protected virtual async Task<bool> TryCreateOrUpdateEntityAsync(TSourceEntityEto eto)
public virtual async Task<bool> TryCreateOrUpdateEntityAsync(TSourceEntityEto eto,
CancellationToken cancellationToken = default)
{
var localEntity = await FindLocalEntityAsync(eto);

Expand All @@ -103,7 +106,7 @@ protected virtual async Task<bool> TryCreateOrUpdateEntityAsync(TSourceEntityEto
);
}

await Repository.InsertAsync(localEntity);
await Repository.InsertAsync(localEntity, cancellationToken: cancellationToken);
}
else
{
Expand All @@ -122,7 +125,7 @@ protected virtual async Task<bool> TryCreateOrUpdateEntityAsync(TSourceEntityEto
);
}

await Repository.UpdateAsync(localEntity);
await Repository.UpdateAsync(localEntity, cancellationToken: cancellationToken);
}

return true;
Expand All @@ -140,7 +143,8 @@ protected virtual Task MapToEntityAsync(TSourceEntityEto eto, TEntity localEntit
}

[UnitOfWork]
protected virtual async Task<bool> TryDeleteEntityAsync(TSourceEntityEto eto)
public virtual async Task<bool> TryDeleteEntityAsync(TSourceEntityEto eto,
CancellationToken cancellationToken = default)
{
var localEntity = await FindLocalEntityAsync(eto);

Expand All @@ -149,7 +153,7 @@ protected virtual async Task<bool> TryDeleteEntityAsync(TSourceEntityEto eto)
return false;
}

await Repository.DeleteAsync(localEntity, true);
await Repository.DeleteAsync(localEntity, true, cancellationToken);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Threading;
using System.Threading.Tasks;

namespace Volo.Abp.Domain.Entities.Events.Distributed;

public interface IEntitySynchronizer<TSourceEntityEto>
{
Task<bool> TryCreateOrUpdateEntityAsync(TSourceEntityEto eto, CancellationToken cancellationToken = default);

Task<bool> TryDeleteEntityAsync(TSourceEntityEto eto, CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ public class BlogUser : AggregateRoot<Guid>, IUser, IUpdateUserData
public virtual string PhoneNumber { get; protected set; }

public virtual bool PhoneNumberConfirmed { get; protected set; }


public virtual int EntityVersion { get; protected set; }

[CanBeNull]
public virtual string WebSite { get; set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
using Volo.Abp.Uow;
using Volo.Abp.Users;
using Volo.Abp.Users;

namespace Volo.Blogging.Users
{
public class BlogUserLookupService : UserLookupService<BlogUser, IBlogUserRepository>, IBlogUserLookupService
{
public BlogUserLookupService(
IBlogUserRepository userRepository,
IUnitOfWorkManager unitOfWorkManager)
: base(
userRepository,
unitOfWorkManager)
public BlogUserLookupService()
{

}

protected override BlogUser CreateUser(IUserData externalUser)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,33 @@
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using System;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Volo.Abp.Domain.Entities.Events.Distributed;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;
using Volo.Abp.Users;

namespace Volo.Blogging.Users
{
public class BlogUserSynchronizer :
IDistributedEventHandler<EntityUpdatedEto<UserEto>>,
ITransientDependency
public class BlogUserSynchronizer : EntitySynchronizer<BlogUser, Guid, UserEto>
{
protected IBlogUserRepository UserRepository { get; }
protected IBlogUserLookupService UserLookupService { get; }

public BlogUserSynchronizer(
IBlogUserRepository userRepository,
IBlogUserLookupService userLookupService)
public BlogUserSynchronizer([NotNull] IObjectMapper objectMapper,
[NotNull] IRepository<BlogUser, Guid> repository) : base(objectMapper, repository)
{
UserRepository = userRepository;
UserLookupService = userLookupService;
}

public async Task HandleEventAsync(EntityUpdatedEto<UserEto> eventData)
protected override Task<BlogUser> MapToEntityAsync(UserEto eto)
{
var user = await UserRepository.FindAsync(eventData.Entity.Id);
if (user == null)
{
user = await UserLookupService.FindByIdAsync(eventData.Entity.Id);
if (user == null)
{
return;
}
}
return Task.FromResult(new BlogUser(eto));
}

protected override Task MapToEntityAsync(UserEto eto, BlogUser localEntity)
{
localEntity.Update(eto);

if (user.Update(eventData.Entity))
{
await UserRepository.UpdateAsync(user);
}
return Task.CompletedTask;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class CmsUser : AggregateRoot<Guid>, IUser, IUpdateUserData

public virtual bool PhoneNumberConfirmed { get; protected set; }

public virtual int EntityVersion { get; protected set; }

protected CmsUser()
{

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
using Volo.Abp.Uow;
using Volo.Abp.Users;
using Volo.Abp.Users;

namespace Volo.CmsKit.Users;

public class CmsUserLookupService : UserLookupService<CmsUser, ICmsUserRepository>, ICmsUserLookupService
{
public CmsUserLookupService(
ICmsUserRepository userRepository,
IUnitOfWorkManager unitOfWorkManager)
: base(
userRepository,
unitOfWorkManager)
public CmsUserLookupService()
{

}

protected override CmsUser CreateUser(IUserData externalUser)
{
return new CmsUser(externalUser);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,50 +1,37 @@
using System.Linq;
using System;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using JetBrains.Annotations;
using Volo.Abp.Domain.Entities.Events.Distributed;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.GlobalFeatures;
using Volo.Abp.ObjectMapping;
using Volo.Abp.Users;
using Volo.CmsKit.GlobalFeatures;

namespace Volo.CmsKit.Users;

public class CmsUserSynchronizer :
IDistributedEventHandler<EntityUpdatedEto<UserEto>>,
ITransientDependency
public class CmsUserSynchronizer : EntitySynchronizer<CmsUser, Guid, UserEto>
{
protected ICmsUserRepository UserRepository { get; }

protected ICmsUserLookupService UserLookupService { get; }

public CmsUserSynchronizer(
ICmsUserRepository userRepository,
ICmsUserLookupService userLookupService)
{
UserRepository = userRepository;
UserLookupService = userLookupService;
}

public virtual async Task HandleEventAsync(EntityUpdatedEto<UserEto> eventData)
public CmsUserSynchronizer([NotNull] IObjectMapper objectMapper, [NotNull] IRepository<CmsUser, Guid> repository) :
base(objectMapper, repository)
{
if (!GlobalFeatureManager.Instance.IsEnabled<CmsUserFeature>())
{
return;
IgnoreEntityCreatedEvent = true;
IgnoreEntityUpdatedEvent = true;
IgnoreEntityDeletedEvent = true;
}
}

var user = await UserRepository.FindAsync(eventData.Entity.Id);
if (user == null)
{
user = await UserLookupService.FindByIdAsync(eventData.Entity.Id);
if (user == null)
{
return;
}
}
protected override Task<CmsUser> MapToEntityAsync(UserEto eto)
{
return Task.FromResult(new CmsUser(eto));
}

if (user.Update(eventData.Entity))
{
await UserRepository.UpdateAsync(user);
}
protected override Task MapToEntityAsync(UserEto eto, CmsUser localEntity)
{
localEntity.Update(eto);

return Task.CompletedTask;
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace Volo.Abp.Identity;

public class IdentityUser : FullAuditedAggregateRoot<Guid>, IUser, IHasEntityVersion
public class IdentityUser : FullAuditedAggregateRoot<Guid>, IUser
{
public virtual Guid? TenantId { get; protected set; }

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.Auditing.Contracts\Volo.Abp.Auditing.Contracts.csproj" />
<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.Ddd.Domain.Shared\Volo.Abp.Ddd.Domain.Shared.csproj" />
<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.EventBus\Volo.Abp.EventBus.csproj" />
<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.MultiTenancy\Volo.Abp.MultiTenancy.csproj" />
</ItemGroup>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Volo.Abp.EventBus;
using Volo.Abp.Auditing;
using Volo.Abp.Domain;
using Volo.Abp.EventBus;
using Volo.Abp.Modularity;
using Volo.Abp.MultiTenancy;

Expand All @@ -8,8 +10,10 @@ namespace Volo.Abp.Users;

[DependsOn(
typeof(AbpMultiTenancyModule),
typeof(AbpEventBusModule)
)]
typeof(AbpEventBusModule),
typeof(AbpDddDomainSharedModule),
typeof(AbpAuditingContractsModule)
)]
public class AbpUsersAbstractionModule : AbpModule
{

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System;
using JetBrains.Annotations;
using Volo.Abp.Auditing;
using Volo.Abp.Data;

namespace Volo.Abp.Users;

public interface IUserData : IHasExtraProperties
public interface IUserData : IHasExtraProperties, IHasEntityVersion
{
Guid Id { get; }

Expand Down

0 comments on commit 031525f

Please sign in to comment.