使用 TimeProvider 类注入时间

This commit is contained in:
administrator committed 2025-03-15 21:15:59 +08:00
1 parent 81c8f608dc
commit c10d9726a2
13 files changed
+28 -61

No files matched your search

@@ -69,6 +69,8 @@ namespace HelloShop.OrderingService.Extensions
builder.Services.AddTransient<IDistributedEventService, DistributedEventService<OrderingServiceDbContext>>();
builder.Services.AddOpenApi();
builder.Services.AddSingleton(TimeProvider.System);
}
public static WebApplication MapApplicationEndpoints(this WebApplication app)
@@ -8,7 +8,7 @@ namespace HelloShop.OrderingService.Validations.Commands
{
public class CreateOrderCommandValidator : AbstractValidator<CreateOrderCommand>
{
public CreateOrderCommandValidator()
public CreateOrderCommandValidator(TimeProvider timeProvider)
{
RuleFor(x => x.UserId).GreaterThan(0);
RuleFor(x => x.UserName).NotNull().NotEmpty().Length(1, 16);
@@ -23,7 +23,7 @@ namespace HelloShop.OrderingService.Validations.Commands
RuleFor(x => x.CardNumber).NotNull().NotEmpty().Length(16).Must(x => x.All(char.IsNumber));
RuleFor(x => x.CardHolderName).NotNull().NotEmpty().Length(1, 8);
RuleFor(x => x.CardSecurityNumber).NotNull().NotEmpty().Length(6).Must(x => x.All(char.IsNumber));
RuleFor(x => x.CardExpiration).Must(x => x.HasValue && x.Value > DateTimeOffset.Now);
RuleFor(x => x.CardExpiration).Must(x => x.HasValue && x.Value > timeProvider.GetUtcNow());
}
public class BasketListItemValidator : AbstractValidator<CreateOrderCommand.CreateOrderCommandItem>
@@ -8,7 +8,7 @@ namespace HelloShop.OrderingService.Validations.Models
{
public class CreateOrderRequestValidator : AbstractValidator<CreateOrderRequest>
{
public CreateOrderRequestValidator()
public CreateOrderRequestValidator(TimeProvider timeProvider)
{
RuleFor(x => x.Items).NotNull().NotEmpty();
RuleForEach(x => x.Items).SetValidator(new BasketListItemValidator());
@@ -21,7 +21,7 @@ namespace HelloShop.OrderingService.Validations.Models
RuleFor(x => x.CardNumber).NotNull().NotEmpty().Length(16).Must(x => x.All(char.IsNumber));
RuleFor(x => x.CardHolderName).NotNull().NotEmpty().Length(1, 8);
RuleFor(x => x.CardSecurityNumber).NotNull().NotEmpty().Length(6).Must(x => x.All(char.IsNumber));
RuleFor(x => x.CardExpiration).Must(x => x.HasValue && x.Value > DateTimeOffset.Now);
RuleFor(x => x.CardExpiration).Must(x => x.HasValue && x.Value > timeProvider.GetLocalNow());
}
public class BasketListItemValidator : AbstractValidator<BasketItem>
@@ -10,7 +10,7 @@ using Microsoft.EntityFrameworkCore;
namespace HelloShop.OrderingService.Workers
{
public class GracePeriodWorker(IServiceScopeFactory serviceScopeFactory, ILogger<GracePeriodWorker> logger) : BackgroundService
public class GracePeriodWorker(IServiceScopeFactory serviceScopeFactory, ILogger<GracePeriodWorker> logger,TimeProvider timeProvider) : BackgroundService
{
protected async override Task ExecuteAsync(CancellationToken stoppingToken)
{
@@ -25,7 +25,7 @@ namespace HelloShop.OrderingService.Workers
var dbContext = scope.ServiceProvider.GetRequiredService<OrderingServiceDbContext>();
var distributedEventBus = scope.ServiceProvider.GetRequiredService<IDistributedEventBus>();
DateTimeOffset dateTimeOffset = DateTimeOffset.UtcNow.AddMinutes(-1);
DateTimeOffset dateTimeOffset = timeProvider.GetUtcNow().AddMinutes(-1);
try
{