Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor modules' local user synchronization #17699

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,30 @@
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([NotNull] IObjectMapper objectMapper,
[NotNull] IRepository<BlogUser, Guid> repository) : base(objectMapper, repository)
{
}

public BlogUserSynchronizer(
IBlogUserRepository userRepository,
IBlogUserLookupService userLookupService)
protected override Task<BlogUser> MapToEntityAsync(UserEto eto)
{
UserRepository = userRepository;
UserLookupService = userLookupService;
return Task.FromResult(new BlogUser(eto));
}

public async Task HandleEventAsync(EntityUpdatedEto<UserEto> eventData)
protected override Task MapToEntityAsync(UserEto eto, BlogUser localEntity)
{
var user = await UserRepository.FindAsync(eventData.Entity.Id);
if (user == null)
{
user = await UserLookupService.FindByIdAsync(eventData.Entity.Id);
if (user == null)
{
return;
}
}
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)

Check warning on line 16 in modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Users/CmsUserSynchronizer.cs

View check run for this annotation

Codecov / codecov/patch

modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Users/CmsUserSynchronizer.cs#L16

Added line #L16 was not covered by tests
{
if (!GlobalFeatureManager.Instance.IsEnabled<CmsUserFeature>())
{
return;
IgnoreEntityCreatedEvent = true;
IgnoreEntityUpdatedEvent = true;
IgnoreEntityDeletedEvent = true;

Check warning on line 22 in modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Users/CmsUserSynchronizer.cs

View check run for this annotation

Codecov / codecov/patch

modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Users/CmsUserSynchronizer.cs#L20-L22

Added lines #L20 - L22 were not covered by tests
}
}

Check warning on line 24 in modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Users/CmsUserSynchronizer.cs

View check run for this annotation

Codecov / codecov/patch

modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Users/CmsUserSynchronizer.cs#L24

Added line #L24 was not covered by tests

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

Check warning on line 29 in modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Users/CmsUserSynchronizer.cs

View check run for this annotation

Codecov / codecov/patch

modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Users/CmsUserSynchronizer.cs#L27-L29

Added lines #L27 - L29 were not covered by tests

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

Check warning on line 33 in modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Users/CmsUserSynchronizer.cs

View check run for this annotation

Codecov / codecov/patch

modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Users/CmsUserSynchronizer.cs#L32-L33

Added lines #L32 - L33 were not covered by tests

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