From 74fdbec1dd5e0735f4906e7cbba5b8fcc38d91be Mon Sep 17 00:00:00 2001 From: "hualin.zhu" Date: Sat, 23 Sep 2023 21:48:01 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20refactoring=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Application/DependencyInjection.cs | 4 +-- .../Commands/AddEdit/AddEditTenantCommand.cs | 5 ++++ .../Commands/Delete/DeleteTenantCommand.cs | 5 ++++ .../TenantChangedEventHandler.cs | 27 ------------------- .../Services/MultiTenant/TenantService.cs | 7 +++-- .../Pages/Identity/Users/Users.razor | 6 +++++ ...thenticationServiceCollectionExtensions.cs | 4 +-- .../Services/Identity/UserDataProvider.cs | 15 ++++++++--- 8 files changed, 36 insertions(+), 37 deletions(-) delete mode 100644 src/Application/Features/Tenants/EventHandlers/TenantChangedEventHandler.cs diff --git a/src/Application/DependencyInjection.cs b/src/Application/DependencyInjection.cs index 2674393ff..469289d24 100644 --- a/src/Application/DependencyInjection.cs +++ b/src/Application/DependencyInjection.cs @@ -41,8 +41,8 @@ public static IServiceCollection AddApplicationServices(this IServiceCollection service.Initialize(); return service; }); - services.AddScoped(); - services.AddScoped(sp => { + services.AddSingleton(); + services.AddSingleton(sp => { var service = sp.GetRequiredService(); service.Initialize(); return service; diff --git a/src/Application/Features/Tenants/Commands/AddEdit/AddEditTenantCommand.cs b/src/Application/Features/Tenants/Commands/AddEdit/AddEditTenantCommand.cs index fed8ace9c..9e91ce93f 100644 --- a/src/Application/Features/Tenants/Commands/AddEdit/AddEditTenantCommand.cs +++ b/src/Application/Features/Tenants/Commands/AddEdit/AddEditTenantCommand.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. +using CleanArchitecture.Blazor.Application.Common.Interfaces.MultiTenant; using CleanArchitecture.Blazor.Application.Features.Tenants.Caching; using CleanArchitecture.Blazor.Application.Features.Tenants.DTOs; @@ -30,16 +31,19 @@ public Mapping() public class AddEditTenantCommandHandler : IRequestHandler> { + private readonly ITenantService _tenantsService; private readonly IApplicationDbContext _context; private readonly IStringLocalizer _localizer; private readonly IMapper _mapper; public AddEditTenantCommandHandler( + ITenantService tenantsService, IApplicationDbContext context, IStringLocalizer localizer, IMapper mapper ) { + _tenantsService = tenantsService; _context = context; _localizer = localizer; _mapper = mapper; @@ -59,6 +63,7 @@ public async Task> Handle(AddEditTenantCommand request, Cancellat item = _mapper.Map(request, item); } await _context.SaveChangesAsync(cancellationToken); + await _tenantsService.Refresh(); return await Result.SuccessAsync(item.Id); } } \ No newline at end of file diff --git a/src/Application/Features/Tenants/Commands/Delete/DeleteTenantCommand.cs b/src/Application/Features/Tenants/Commands/Delete/DeleteTenantCommand.cs index 5c8d9a10f..1c434c6ed 100644 --- a/src/Application/Features/Tenants/Commands/Delete/DeleteTenantCommand.cs +++ b/src/Application/Features/Tenants/Commands/Delete/DeleteTenantCommand.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using CleanArchitecture.Blazor.Application.Common.Interfaces.MultiTenant; using CleanArchitecture.Blazor.Application.Features.Tenants.Caching; namespace CleanArchitecture.Blazor.Application.Features.Tenants.Commands.Delete; @@ -21,16 +22,19 @@ public class DeleteTenantCommandHandler : IRequestHandler> { + private readonly ITenantService _tenantsService; private readonly IApplicationDbContext _context; private readonly IStringLocalizer _localizer; private readonly IMapper _mapper; public DeleteTenantCommandHandler( + ITenantService tenantsService, IApplicationDbContext context, IStringLocalizer localizer, IMapper mapper ) { + _tenantsService = tenantsService; _context = context; _localizer = localizer; _mapper = mapper; @@ -45,6 +49,7 @@ public async Task> Handle(DeleteTenantCommand request, CancellationT } var result = await _context.SaveChangesAsync(cancellationToken); + await _tenantsService.Refresh(); return await Result.SuccessAsync(result); } } \ No newline at end of file diff --git a/src/Application/Features/Tenants/EventHandlers/TenantChangedEventHandler.cs b/src/Application/Features/Tenants/EventHandlers/TenantChangedEventHandler.cs deleted file mode 100644 index d66834263..000000000 --- a/src/Application/Features/Tenants/EventHandlers/TenantChangedEventHandler.cs +++ /dev/null @@ -1,27 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using CleanArchitecture.Blazor.Application.Common.Interfaces.MultiTenant; - -namespace CleanArchitecture.Blazor.Application.Features.Tenants.EventHandlers; - -public class TenantChangedEventHandler : INotificationHandler> -{ - private readonly ILogger _logger; - private readonly ITenantService _tenantsService; - - public TenantChangedEventHandler( - ITenantService tenantsService, - ILogger logger - ) - { - _tenantsService = tenantsService; - _logger = logger; - } - - public async Task Handle(UpdatedEvent notification, CancellationToken cancellationToken) - { - _logger.LogInformation("Tenant Changed {DomainEvent}, {@Entity}", nameof(notification), notification.Entity); - await _tenantsService.Refresh(); - } -} \ No newline at end of file diff --git a/src/Application/Services/MultiTenant/TenantService.cs b/src/Application/Services/MultiTenant/TenantService.cs index 80b6b93f0..0edf545f2 100644 --- a/src/Application/Services/MultiTenant/TenantService.cs +++ b/src/Application/Services/MultiTenant/TenantService.cs @@ -1,6 +1,7 @@ using CleanArchitecture.Blazor.Application.Common.Interfaces.MultiTenant; using CleanArchitecture.Blazor.Application.Features.Tenants.Caching; using CleanArchitecture.Blazor.Application.Features.Tenants.DTOs; +using Microsoft.Extensions.DependencyInjection; namespace CleanArchitecture.Blazor.Application.Services.MultiTenant; @@ -12,10 +13,12 @@ public class TenantService : ITenantService public TenantService( IAppCache cache, - IApplicationDbContext context, IMapper mapper) + IServiceScopeFactory scopeFactory, + IMapper mapper) { _cache = cache; - _context = context; + var scope = scopeFactory.CreateScope(); + _context = scope.ServiceProvider.GetRequiredService(); _mapper = mapper; } diff --git a/src/Blazor.Server.UI/Pages/Identity/Users/Users.razor b/src/Blazor.Server.UI/Pages/Identity/Users/Users.razor index 968886a1e..4f412175f 100644 --- a/src/Blazor.Server.UI/Pages/Identity/Users/Users.razor +++ b/src/Blazor.Server.UI/Pages/Identity/Users/Users.razor @@ -319,6 +319,8 @@ [Inject] private ITenantService TenantsService { get; set; } = null!; [Inject] + private IUserDataProvider UserDataProvider { get; set; } = null!; + [Inject] private IBlazorDownloadFileService BlazorDownloadFileService { get; set; } = null!; [Inject] private IExcelService ExcelService { get; set; } = null!; @@ -453,6 +455,7 @@ await UserManager.AddToRoleAsync(applicationUser, RoleName.Basic); } Snackbar.Add($"{ConstantString.CreateSuccess}", Severity.Info); + await UserDataProvider.Refresh(); await OnRefresh(); } else @@ -502,6 +505,7 @@ } Snackbar.Add($"{ConstantString.UpdateSuccess}", Severity.Info); await OnRefresh(); + await UserDataProvider.Refresh(); } else { @@ -551,6 +555,7 @@ Snackbar.Add($"{ConstantString.DeleteSuccess}", Severity.Info); await OnRefresh(); + await UserDataProvider.Refresh(); } } @@ -592,6 +597,7 @@ } Snackbar.Add($"{ConstantString.DeleteSuccess}", Severity.Info); await OnRefresh(); + await UserDataProvider.Refresh(); } } diff --git a/src/Infrastructure/Extensions/AuthenticationServiceCollectionExtensions.cs b/src/Infrastructure/Extensions/AuthenticationServiceCollectionExtensions.cs index 65b9c2637..e2a51bf42 100644 --- a/src/Infrastructure/Extensions/AuthenticationServiceCollectionExtensions.cs +++ b/src/Infrastructure/Extensions/AuthenticationServiceCollectionExtensions.cs @@ -90,8 +90,8 @@ public static IServiceCollection AddAuthenticationService(this IServiceCollectio }); services.AddScoped(); - services.AddScoped(); - services.AddScoped(sp => + services.AddSingleton(); + services.AddSingleton(sp => { var service = sp.GetRequiredService(); service.Initialize(); diff --git a/src/Infrastructure/Services/Identity/UserDataProvider.cs b/src/Infrastructure/Services/Identity/UserDataProvider.cs index 56618c2e7..ad8a850d4 100644 --- a/src/Infrastructure/Services/Identity/UserDataProvider.cs +++ b/src/Infrastructure/Services/Identity/UserDataProvider.cs @@ -1,10 +1,13 @@ using AutoMapper; using AutoMapper.QueryableExtensions; using CleanArchitecture.Blazor.Application.Features.Identity.Dto; +using LazyCache; namespace CleanArchitecture.Blazor.Infrastructure.Services.Identity; public class UserDataProvider : IUserDataProvider { + private const string CACHEKEY = "ALL-ApplicationUserDto"; + private readonly IAppCache _cache; private readonly IMapper _mapper; private readonly UserManager _userManager; public List DataSource { get; private set; } @@ -12,9 +15,11 @@ public class UserDataProvider : IUserDataProvider public event Action? OnChange; public UserDataProvider( + IAppCache cache, IMapper mapper, IServiceScopeFactory scopeFactory) { + _cache = cache; _mapper = mapper; var scope = scopeFactory.CreateScope(); _userManager = scope.ServiceProvider.GetRequiredService>(); @@ -22,19 +27,21 @@ public UserDataProvider( } public void Initialize() { - DataSource = _userManager.Users.Include(x => x.UserRoles).ThenInclude(x => x.Role).ProjectTo(_mapper.ConfigurationProvider).OrderBy(x=>x.UserName).ToList(); + DataSource = _cache.GetOrAdd(CACHEKEY,()=>_userManager.Users.Include(x => x.UserRoles).ThenInclude(x => x.Role).ProjectTo(_mapper.ConfigurationProvider).OrderBy(x=>x.UserName).ToList()); OnChange?.Invoke(); } public async Task InitializeAsync() { - DataSource =await _userManager.Users.Include(x => x.UserRoles).ThenInclude(x => x.Role).ProjectTo(_mapper.ConfigurationProvider).OrderBy(x => x.UserName).ToListAsync(); + DataSource =await _cache.GetOrAddAsync(CACHEKEY,()=> _userManager.Users.Include(x => x.UserRoles).ThenInclude(x => x.Role).ProjectTo(_mapper.ConfigurationProvider).OrderBy(x => x.UserName).ToListAsync()); OnChange?.Invoke(); } - public Task Refresh() + public async Task Refresh() { + _cache.Remove(CACHEKEY); + DataSource = await _cache.GetOrAddAsync(CACHEKEY, () => _userManager.Users.Include(x => x.UserRoles).ThenInclude(x => x.Role).ProjectTo(_mapper.ConfigurationProvider).OrderBy(x => x.UserName).ToListAsync()); OnChange?.Invoke(); - return Task.CompletedTask; + } }