1+ using AutoMapper ;
2+ using AutoMapper . QueryableExtensions ;
3+ using CleanArchitecture . Blazor . Application . Features . Identity . DTOs ;
4+ using CleanArchitecture . Blazor . Domain . Identity ;
5+ using ZiggyCreatures . Caching . Fusion ;
6+
7+ namespace CleanArchitecture . Blazor . Infrastructure . Services . Identity ;
8+
9+ public class RoleService : IRoleService
10+ {
11+ private const string CACHEKEY = "ALL-ApplicationRoleDto" ;
12+ private readonly IMapper _mapper ;
13+ private readonly IFusionCache _fusionCache ;
14+ private readonly RoleManager < ApplicationRole > _roleManager ;
15+
16+ public RoleService (
17+ IMapper mapper ,
18+ IFusionCache fusionCache ,
19+ IServiceScopeFactory scopeFactory )
20+ {
21+ _mapper = mapper ;
22+ _fusionCache = fusionCache ;
23+ var scope = scopeFactory . CreateScope ( ) ;
24+ _roleManager = scope . ServiceProvider . GetRequiredService < RoleManager < ApplicationRole > > ( ) ;
25+ DataSource = new List < ApplicationRoleDto > ( ) ;
26+ }
27+
28+ public List < ApplicationRoleDto > DataSource { get ; private set ; }
29+
30+ public event Func < Task > ? OnChange ;
31+
32+ public void Initialize ( )
33+ {
34+ DataSource = _fusionCache . GetOrSet ( CACHEKEY ,
35+ _ => _roleManager . Roles
36+ . ProjectTo < ApplicationRoleDto > ( _mapper . ConfigurationProvider ) . OrderBy ( x => x . TenantId ) . ThenBy ( x => x . Name )
37+ . ToList ( ) )
38+ ?? new List < ApplicationRoleDto > ( ) ;
39+ OnChange ? . Invoke ( ) ;
40+ }
41+
42+
43+ public void Refresh ( )
44+ {
45+ _fusionCache . Remove ( CACHEKEY ) ;
46+ DataSource = _fusionCache . GetOrSet ( CACHEKEY ,
47+ _ => _roleManager . Roles
48+ . ProjectTo < ApplicationRoleDto > ( _mapper . ConfigurationProvider ) . OrderBy ( x => x . TenantId ) . ThenBy ( x => x . Name )
49+ . ToList ( ) )
50+ ?? new List < ApplicationRoleDto > ( ) ;
51+ OnChange ? . Invoke ( ) ;
52+ }
53+ }
0 commit comments