Skip to content

Commit 2cf8a26

Browse files
committed
Refactor RoleService to use generic IDataSourceService
Refactored `IRoleService` to inherit from `IDataSourceService<ApplicationRoleDto>`, removing custom methods and properties. Replaced `RoleService` with the new `RoleDataSourceService`, which provides caching and data-loading functionality in a reusable manner. Updated `Roles.razor` and `Users.razor` to use `IDataSourceService<ApplicationRoleDto>` instead of `IRoleService`. Changed `_roles` in `Users.razor` to `IReadOnlyList<ApplicationRoleDto>` to reflect its read-only nature. Removed explicit `IRoleService` registration in `DependencyInjection.cs` in favor of auto-discovery of `IDataSourceService<T>` implementations. These changes simplify the codebase, reduce duplication, and improve maintainability.
1 parent 74cd937 commit 2cf8a26

File tree

6 files changed

+690
-731
lines changed

6 files changed

+690
-731
lines changed

src/Application/Common/Interfaces/Identity/IRoleService.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/Infrastructure/DependencyInjection.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ private static IServiceCollection AddBusinessServices(this IServiceCollection se
163163
{
164164
// Auto-discover and register all IDataSourceService<T> implementations
165165
services.AddDataSourceServices();
166-
services.AddScoped<IRoleService, RoleService>();
167166
services.AddScoped<ITenantSwitchService, TenantSwitchService>();
168167

169168

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using CleanArchitecture.Blazor.Application.Features.Identity.DTOs;
2+
using CleanArchitecture.Blazor.Domain.Identity;
3+
using ZiggyCreatures.Caching.Fusion;
4+
5+
namespace CleanArchitecture.Blazor.Infrastructure.Services.Identity;
6+
7+
public class RoleDataSourceService : DataSourceServiceBase<ApplicationRoleDto>, IDisposable
8+
{
9+
private const string CACHEKEY = "ALL-ApplicationRoleDto";
10+
private readonly IMapper _mapper;
11+
private readonly IServiceScopeFactory _scopeFactory;
12+
13+
public RoleDataSourceService(
14+
IMapper mapper,
15+
IFusionCache fusionCache,
16+
IServiceScopeFactory scopeFactory)
17+
: base(fusionCache, CACHEKEY)
18+
{
19+
_mapper = mapper;
20+
_scopeFactory = scopeFactory;
21+
}
22+
23+
protected override async Task<List<ApplicationRoleDto>?> LoadAsync(CancellationToken cancellationToken)
24+
{
25+
using var scope = _scopeFactory.CreateScope();
26+
var roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<ApplicationRole>>();
27+
return await roleManager.Roles
28+
.ProjectTo<ApplicationRoleDto>(_mapper.ConfigurationProvider)
29+
.OrderBy(x => x.Name)
30+
.ToListAsync(cancellationToken);
31+
}
32+
33+
public void Dispose()
34+
{
35+
GC.SuppressFinalize(this);
36+
}
37+
}

src/Infrastructure/Services/Identity/RoleService.cs

Lines changed: 0 additions & 68 deletions
This file was deleted.

src/Server.UI/Pages/Identity/Roles/Roles.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
@attribute [Authorize(Policy = Permissions.Roles.View)]
1414
@inherits OwningComponentBase
15-
@inject IRoleService _roleService
15+
@inject IDataSourceService<ApplicationRoleDto> _roleService
1616
@inject IFusionCache _fusionCache
1717
@inject IStringLocalizer<Roles> _localizer
1818
@inject IExcelService ExcelService

0 commit comments

Comments
 (0)