From 8a15eee94f2336983621b0c91e33b9ecfe540b1c Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 15 Sep 2025 16:23:48 +0800 Subject: [PATCH] Optimize Clock.Now usage in user delegation repositories --- .../EfCoreIdentityUserDelegationRepository.cs | 10 ++++++---- .../MongoDB/MongoIdentityUserDelegationRepository.cs | 8 +++++--- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityUserDelegationRepository.cs b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityUserDelegationRepository.cs index a21e0f8ea0..81953c54f4 100644 --- a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityUserDelegationRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityUserDelegationRepository.cs @@ -31,22 +31,24 @@ public class EfCoreIdentityUserDelegationRepository : EfCoreRepository> GetActiveDelegationsAsync(Guid targetUserId, CancellationToken cancellationToken = default) { + var now = Clock.Now; return await (await GetDbSetAsync()) .AsNoTracking() .Where(x => x.TargetUserId == targetUserId && - x.StartTime <= Clock.Now && - x.EndTime >= Clock.Now) + x.StartTime <= now && + x.EndTime >= now) .ToListAsync(cancellationToken: cancellationToken); } public virtual async Task FindActiveDelegationByIdAsync(Guid id, CancellationToken cancellationToken = default) { + var now = Clock.Now; return await (await GetDbSetAsync()) .AsNoTracking() .FirstOrDefaultAsync(x => x.Id == id && - x.StartTime <= Clock.Now && - x.EndTime >= Clock.Now + x.StartTime <= now && + x.EndTime >= now , cancellationToken: GetCancellationToken(cancellationToken)); } } diff --git a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityUserDelegationRepository.cs b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityUserDelegationRepository.cs index 1855069b4a..bd96ed4164 100644 --- a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityUserDelegationRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityUserDelegationRepository.cs @@ -32,19 +32,21 @@ public class MongoIdentityUserDelegationRepository : MongoDbRepository> GetActiveDelegationsAsync(Guid targetUserId, CancellationToken cancellationToken = default) { + var now = Clock.Now; return await (await GetQueryableAsync(cancellationToken)) .Where(x => x.TargetUserId == targetUserId) - .Where(x => x.StartTime <= Clock.Now && x.EndTime >= Clock.Now) + .Where(x => x.StartTime <= now && x.EndTime >= now) .ToListAsync(cancellationToken: cancellationToken); } public virtual async Task FindActiveDelegationByIdAsync(Guid id, CancellationToken cancellationToken = default) { + var now = Clock.Now; return await (await GetQueryableAsync(cancellationToken)) .FirstOrDefaultAsync(x => x.Id == id && - x.StartTime <= Clock.Now && - x.EndTime >= Clock.Now + x.StartTime <= now && + x.EndTime >= now , cancellationToken: GetCancellationToken(cancellationToken)); } }