Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/Application/Common/Interfaces/IPicklistService.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using CleanArchitecture.Blazor.Application.Features.KeyValues.DTOs;
using CleanArchitecture.Blazor.Application.Features.KeyValues.DTOs;

namespace CleanArchitecture.Blazor.Application.Common.Interfaces;

public interface IPicklistService
{
List<KeyValueDto> DataSource { get; }
event Action? OnChange;
Task InitializeAsync();
void Initialize();
Task Refresh();
void Refresh();
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using CleanArchitecture.Blazor.Application.Features.Identity.DTOs;
using CleanArchitecture.Blazor.Application.Features.Identity.DTOs;

namespace CleanArchitecture.Blazor.Application.Common.Interfaces.Identity;

public interface IUserDataProvider
public interface IUserService
{
List<ApplicationUserDto> DataSource { get; }
event Action? OnChange;
Task InitializeAsync();
void Initialize();
Task Refresh();
void Refresh();
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using CleanArchitecture.Blazor.Application.Features.Tenants.DTOs;
using CleanArchitecture.Blazor.Application.Features.Tenants.DTOs;

namespace CleanArchitecture.Blazor.Application.Common.Interfaces.MultiTenant;

public interface ITenantService
{
List<TenantDto> DataSource { get; }
event Action? OnChange;
Task InitializeAsync();
void Initialize();
Task Refresh();
void Refresh();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace CleanArchitecture.Blazor.Application.Features.KeyValues.EventHandlers;
Expand All @@ -17,9 +17,10 @@ ILogger<KeyValueChangedEventHandler> logger
_logger = logger;
}

public async Task Handle(UpdatedEvent<KeyValue> notification, CancellationToken cancellationToken)
public Task Handle(UpdatedEvent<KeyValue> notification, CancellationToken cancellationToken)
{
_logger.LogInformation("KeyValue Changed {DomainEvent},{@Entity}", nameof(notification), notification.Entity);
await _picklistService.Refresh();
_picklistService.Refresh();
return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.


Expand Down Expand Up @@ -63,7 +63,7 @@ public async Task<Result<string>> Handle(AddEditTenantCommand request, Cancellat
}

await _context.SaveChangesAsync(cancellationToken);
await _tenantsService.Refresh();
_tenantsService.Refresh();
return await Result<string>.SuccessAsync(item.Id);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// 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;
Expand Down Expand Up @@ -46,7 +46,7 @@ public async Task<Result<int>> Handle(DeleteTenantCommand request, CancellationT
foreach (var item in items) _context.Tenants.Remove(item);

var result = await _context.SaveChangesAsync(cancellationToken);
await _tenantsService.Refresh();
_tenantsService.Refresh();
return await Result<int>.SuccessAsync(result);
}
}
12 changes: 6 additions & 6 deletions src/Infrastructure/DependencyInjection.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.IdentityModel.Tokens.Jwt;
Expand Down Expand Up @@ -284,10 +284,10 @@ private static IServiceCollection AddAuthenticationService(this IServiceCollecti
};
});
services.ConfigureApplicationCookie(options => { options.LoginPath = "/pages/authentication/login"; });
services.AddSingleton<UserDataProvider>()
.AddSingleton<IUserDataProvider>(sp =>
services.AddSingleton<UserService>()
.AddSingleton<IUserService>(sp =>
{
var service = sp.GetRequiredService<UserDataProvider>();
var service = sp.GetRequiredService<UserService>();
service.Initialize();
return service;
});
Expand All @@ -301,10 +301,10 @@ private static IServiceCollection AddFusionCacheService(this IServiceCollection
services.AddFusionCache().WithDefaultEntryOptions(new FusionCacheEntryOptions
{
// CACHE DURATION
Duration = TimeSpan.FromMinutes(30),
Duration = TimeSpan.FromMinutes(120),
// FAIL-SAFE OPTIONS
IsFailSafeEnabled = true,
FailSafeMaxDuration = TimeSpan.FromHours(2),
FailSafeMaxDuration = TimeSpan.FromHours(8),
FailSafeThrottleDuration = TimeSpan.FromSeconds(30),
// FACTORY TIMEOUTS
FactorySoftTimeout = TimeSpan.FromMilliseconds(100),
Expand Down
2 changes: 1 addition & 1 deletion src/Infrastructure/Infrastructure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<PackageReference Include="Serilog.Enrichers.ClientInfo" Version="2.0.3" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Blazor.Serilog.Sinks.SQLite" Version="1.0.6" />
<PackageReference Include="ZiggyCreatures.FusionCache" Version="0.24.0" />
<PackageReference Include="ZiggyCreatures.FusionCache" Version="0.25.0" />

</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@
using CleanArchitecture.Blazor.Application.Features.Identity.DTOs;
using CleanArchitecture.Blazor.Domain.Identity;
using LazyCache;
using ZiggyCreatures.Caching.Fusion;

namespace CleanArchitecture.Blazor.Infrastructure.Services.Identity;

public class UserDataProvider : IUserDataProvider
public class UserService : IUserService
{
private const string CACHEKEY = "ALL-ApplicationUserDto";
private readonly IAppCache _cache;
private readonly IFusionCache _fusionCache;
private readonly IMapper _mapper;
private readonly UserManager<ApplicationUser> _userManager;

public UserDataProvider(
IAppCache cache,
public UserService(
IFusionCache fusionCache,
IMapper mapper,
IServiceScopeFactory scopeFactory)
{
_cache = cache;
_fusionCache = fusionCache;
_mapper = mapper;
var scope = scopeFactory.CreateScope();
_userManager = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
Expand All @@ -31,26 +32,22 @@ public UserDataProvider(

public void Initialize()
{
DataSource = _cache.GetOrAdd(CACHEKEY,
() => _userManager.Users.Include(x => x.UserRoles).ThenInclude(x => x.Role)
.ProjectTo<ApplicationUserDto>(_mapper.ConfigurationProvider).OrderBy(x => x.UserName).ToList());
DataSource = _fusionCache.GetOrSet(CACHEKEY,
_ => _userManager.Users.Include(x => x.UserRoles).ThenInclude(x => x.Role)
.ProjectTo<ApplicationUserDto>(_mapper.ConfigurationProvider).OrderBy(x => x.UserName).ToList())
?? new List<ApplicationUserDto>();
OnChange?.Invoke();
}

public async Task InitializeAsync()
{
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 async Task Refresh()
public void 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());
_fusionCache.Remove(CACHEKEY);
DataSource = _fusionCache.GetOrSet(CACHEKEY,
_ => _userManager.Users.Include(x => x.UserRoles).ThenInclude(x => x.Role)
.ProjectTo<ApplicationUserDto>(_mapper.ConfigurationProvider).OrderBy(x => x.UserName).ToList())
?? new List<ApplicationUserDto>();
OnChange?.Invoke();
}
}
40 changes: 17 additions & 23 deletions src/Infrastructure/Services/MultiTenant/TenantService.cs
Original file line number Diff line number Diff line change
@@ -1,58 +1,52 @@
using AutoMapper;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using CleanArchitecture.Blazor.Application.Common.Interfaces.MultiTenant;
using CleanArchitecture.Blazor.Application.Features.Tenants.Caching;
using CleanArchitecture.Blazor.Application.Features.Tenants.DTOs;
using LazyCache;
using ZiggyCreatures.Caching.Fusion;

namespace CleanArchitecture.Blazor.Infrastructure.Services.MultiTenant;

public class TenantService : ITenantService
{
private readonly IAppCache _cache;

private readonly IApplicationDbContext _context;
private readonly IFusionCache _fusionCache;
private readonly IMapper _mapper;

public TenantService(
IAppCache cache,
IFusionCache fusionCache,
IServiceScopeFactory scopeFactory,
IMapper mapper)
{
_cache = cache;

var scope = scopeFactory.CreateScope();
_context = scope.ServiceProvider.GetRequiredService<IApplicationDbContext>();
_fusionCache = fusionCache;
_mapper = mapper;
}

public event Action? OnChange;
public List<TenantDto> DataSource { get; private set; } = new();

public async Task InitializeAsync()
{
DataSource = await _cache.GetOrAddAsync(TenantCacheKey.TenantsCacheKey,
() => _context.Tenants.OrderBy(x => x.Name)
.ProjectTo<TenantDto>(_mapper.ConfigurationProvider)
.ToListAsync(),
TenantCacheKey.MemoryCacheEntryOptions);
}


public void Initialize()
{
DataSource = _cache.GetOrAdd(TenantCacheKey.TenantsCacheKey,
() => _context.Tenants.OrderBy(x => x.Name)
DataSource = _fusionCache.GetOrSet(TenantCacheKey.TenantsCacheKey,
_ => _context.Tenants.OrderBy(x => x.Name)
.ProjectTo<TenantDto>(_mapper.ConfigurationProvider)
.ToList(),
TenantCacheKey.MemoryCacheEntryOptions);
.ToList()) ?? new List<TenantDto>();
}

public async Task Refresh()
public void Refresh()
{
_cache.Remove(TenantCacheKey.TenantsCacheKey);
DataSource = await _cache.GetOrAddAsync(TenantCacheKey.TenantsCacheKey,
() => _context.Tenants.OrderBy(x => x.Name)
.ProjectTo<TenantDto>(_mapper.ConfigurationProvider)
.ToListAsync(),
TenantCacheKey.MemoryCacheEntryOptions);
_fusionCache.Remove(TenantCacheKey.TenantsCacheKey);
DataSource = _fusionCache.GetOrSet(TenantCacheKey.TenantsCacheKey,
_ => _context.Tenants.OrderBy(x => x.Name)
.ProjectTo<TenantDto>(_mapper.ConfigurationProvider)
.ToList()) ?? new List<TenantDto>();
OnChange?.Invoke();
}
}
42 changes: 18 additions & 24 deletions src/Infrastructure/Services/PicklistService.cs
Original file line number Diff line number Diff line change
@@ -1,58 +1,52 @@
using AutoMapper;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using CleanArchitecture.Blazor.Application.Features.KeyValues.Caching;
using CleanArchitecture.Blazor.Application.Features.KeyValues.DTOs;
using LazyCache;
using ZiggyCreatures.Caching.Fusion;

namespace CleanArchitecture.Blazor.Infrastructure.Services;

public class PicklistService : IPicklistService
{
private readonly IAppCache _cache;
private readonly IApplicationDbContext _context;
private readonly IFusionCache _fusionCache;
private readonly IMapper _mapper;

public PicklistService(
IAppCache cache,
IFusionCache fusionCache,
IServiceScopeFactory scopeFactory,
IMapper mapper)
{
_cache = cache;

var scope = scopeFactory.CreateScope();
_context = scope.ServiceProvider.GetRequiredService<IApplicationDbContext>();
_fusionCache = fusionCache;
_mapper = mapper;
}

public event Action? OnChange;
public List<KeyValueDto> DataSource { get; private set; } = new();

public async Task InitializeAsync()
{
DataSource = await _cache.GetOrAddAsync(KeyValueCacheKey.PicklistCacheKey,
() => _context.KeyValues.OrderBy(x => x.Name).ThenBy(x => x.Value)
.ProjectTo<KeyValueDto>(_mapper.ConfigurationProvider)
.ToListAsync(),
KeyValueCacheKey.MemoryCacheEntryOptions);
}


public void Initialize()
{
DataSource = _cache.GetOrAdd(KeyValueCacheKey.PicklistCacheKey,
() => _context.KeyValues.OrderBy(x => x.Name).ThenBy(x => x.Value)
DataSource = _fusionCache.GetOrSet(KeyValueCacheKey.PicklistCacheKey,
_ => _context.KeyValues.OrderBy(x => x.Name).ThenBy(x => x.Value)
.ProjectTo<KeyValueDto>(_mapper.ConfigurationProvider)
.ToList(),
KeyValueCacheKey.MemoryCacheEntryOptions);
.ToList()
)??new List<KeyValueDto>();
}

public async Task Refresh()
public void Refresh()
{
_cache.Remove(KeyValueCacheKey.PicklistCacheKey);
DataSource = await _cache.GetOrAddAsync(KeyValueCacheKey.PicklistCacheKey,
() => _context.KeyValues.OrderBy(x => x.Name).ThenBy(x => x.Value)
.ProjectTo<KeyValueDto>(_mapper.ConfigurationProvider)
.ToListAsync(),
KeyValueCacheKey.MemoryCacheEntryOptions
);
_fusionCache.Remove(KeyValueCacheKey.PicklistCacheKey);
DataSource = _fusionCache.GetOrSet(KeyValueCacheKey.PicklistCacheKey,
_ => _context.KeyValues.OrderBy(x => x.Name).ThenBy(x => x.Value)
.ProjectTo<KeyValueDto>(_mapper.ConfigurationProvider)
.ToList()
) ?? new List<KeyValueDto>();
OnChange?.Invoke();
}
}
Loading