Skip to content

Commit

Permalink
Merge pull request #516 from neozhu/ImproveMappingConfiguration
Browse files Browse the repository at this point in the history
✨ refactoring code
  • Loading branch information
neozhu authored Sep 23, 2023
2 parents bf5c1d1 + 74fdbec commit eaab42e
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/Application/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public static IServiceCollection AddApplicationServices(this IServiceCollection
service.Initialize();
return service;
});
services.AddScoped<TenantService>();
services.AddScoped<ITenantService>(sp => {
services.AddSingleton<TenantService>();
services.AddSingleton<ITenantService>(sp => {
var service = sp.GetRequiredService<TenantService>();
service.Initialize();
return service;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -30,16 +31,19 @@ public Mapping()

public class AddEditTenantCommandHandler : IRequestHandler<AddEditTenantCommand, Result<string>>
{
private readonly ITenantService _tenantsService;
private readonly IApplicationDbContext _context;
private readonly IStringLocalizer<AddEditTenantCommandHandler> _localizer;
private readonly IMapper _mapper;

public AddEditTenantCommandHandler(
ITenantService tenantsService,
IApplicationDbContext context,
IStringLocalizer<AddEditTenantCommandHandler> localizer,
IMapper mapper
)
{
_tenantsService = tenantsService;
_context = context;
_localizer = localizer;
_mapper = mapper;
Expand All @@ -59,6 +63,7 @@ public async Task<Result<string>> Handle(AddEditTenantCommand request, Cancellat
item = _mapper.Map(request, item);
}
await _context.SaveChangesAsync(cancellationToken);
await _tenantsService.Refresh();
return await Result<string>.SuccessAsync(item.Id);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,16 +22,19 @@ public class DeleteTenantCommandHandler :
IRequestHandler<DeleteTenantCommand, Result<int>>

{
private readonly ITenantService _tenantsService;
private readonly IApplicationDbContext _context;
private readonly IStringLocalizer<DeleteTenantCommandHandler> _localizer;
private readonly IMapper _mapper;

public DeleteTenantCommandHandler(
ITenantService tenantsService,
IApplicationDbContext context,
IStringLocalizer<DeleteTenantCommandHandler> localizer,
IMapper mapper
)
{
_tenantsService = tenantsService;
_context = context;
_localizer = localizer;
_mapper = mapper;
Expand All @@ -45,6 +49,7 @@ public async Task<Result<int>> Handle(DeleteTenantCommand request, CancellationT
}

var result = await _context.SaveChangesAsync(cancellationToken);
await _tenantsService.Refresh();
return await Result<int>.SuccessAsync(result);
}
}

This file was deleted.

7 changes: 5 additions & 2 deletions src/Application/Services/MultiTenant/TenantService.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<IApplicationDbContext>();
_mapper = mapper;
}

Expand Down
6 changes: 6 additions & 0 deletions src/Blazor.Server.UI/Pages/Identity/Users/Users.razor
Original file line number Diff line number Diff line change
Expand Up @@ -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!;
Expand Down Expand Up @@ -453,6 +455,7 @@
await UserManager.AddToRoleAsync(applicationUser, RoleName.Basic);
}
Snackbar.Add($"{ConstantString.CreateSuccess}", Severity.Info);
await UserDataProvider.Refresh();
await OnRefresh();
}
else
Expand Down Expand Up @@ -502,6 +505,7 @@
}
Snackbar.Add($"{ConstantString.UpdateSuccess}", Severity.Info);
await OnRefresh();
await UserDataProvider.Refresh();
}
else
{
Expand Down Expand Up @@ -551,6 +555,7 @@

Snackbar.Add($"{ConstantString.DeleteSuccess}", Severity.Info);
await OnRefresh();
await UserDataProvider.Refresh();
}
}

Expand Down Expand Up @@ -592,6 +597,7 @@
}
Snackbar.Add($"{ConstantString.DeleteSuccess}", Severity.Info);
await OnRefresh();
await UserDataProvider.Refresh();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ public static IServiceCollection AddAuthenticationService(this IServiceCollectio
});

services.AddScoped<AccessTokenProvider>();
services.AddScoped<UserDataProvider>();
services.AddScoped<IUserDataProvider>(sp =>
services.AddSingleton<UserDataProvider>();
services.AddSingleton<IUserDataProvider>(sp =>
{
var service = sp.GetRequiredService<UserDataProvider>();
service.Initialize();
Expand Down
15 changes: 11 additions & 4 deletions src/Infrastructure/Services/Identity/UserDataProvider.cs
Original file line number Diff line number Diff line change
@@ -1,40 +1,47 @@
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<ApplicationUser> _userManager;
public List<ApplicationUserDto> DataSource { get; private set; }

public event Action? OnChange;

public UserDataProvider(
IAppCache cache,
IMapper mapper,
IServiceScopeFactory scopeFactory)
{
_cache = cache;
_mapper = mapper;
var scope = scopeFactory.CreateScope();
_userManager = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
DataSource = new List<ApplicationUserDto>();
}
public void Initialize()
{
DataSource = _userManager.Users.Include(x => x.UserRoles).ThenInclude(x => x.Role).ProjectTo<ApplicationUserDto>(_mapper.ConfigurationProvider).OrderBy(x=>x.UserName).ToList();
DataSource = _cache.GetOrAdd(CACHEKEY,()=>_userManager.Users.Include(x => x.UserRoles).ThenInclude(x => x.Role).ProjectTo<ApplicationUserDto>(_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<ApplicationUserDto>(_mapper.ConfigurationProvider).OrderBy(x => x.UserName).ToListAsync();
DataSource =await _cache.GetOrAddAsync(CACHEKEY,()=> _userManager.Users.Include(x => x.UserRoles).ThenInclude(x => x.Role).ProjectTo<ApplicationUserDto>(_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<ApplicationUserDto>(_mapper.ConfigurationProvider).OrderBy(x => x.UserName).ToListAsync());
OnChange?.Invoke();
return Task.CompletedTask;

}
}

0 comments on commit eaab42e

Please sign in to comment.