Skip to content

Commit 673e683

Browse files
committed
refactor: replace custom SystemClock with native TimeProvider
The custom written SystemClock and its fake counterpart for integration tests have been removed. They have been replaced with the TimeProvider from the Microsoft.Extensions.Time.Testing package. This change affects several places across the codebase, where the system time was primarily used.
1 parent 0dc605c commit 673e683

File tree

10 files changed

+16
-37
lines changed

10 files changed

+16
-37
lines changed

Chapter-2-modules-separation/Src/Common/Fitnet.Common.Core/SystemClock/ISystemClock.cs

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

Chapter-2-modules-separation/Src/Common/Fitnet.Common.Core/SystemClock/SystemClock.cs

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

Chapter-2-modules-separation/Src/Common/Fitnet.Common.Core/SystemClock/SystemClockModule.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ public static class SystemClockModule
66
{
77
public static IServiceCollection AddSystemClock(this IServiceCollection services)
88
{
9-
services.AddSingleton<ISystemClock, SystemClock>();
9+
services.AddSingleton(TimeProvider.System);
1010

1111
return services;
1212
}
13-
}
13+
}

Chapter-2-modules-separation/Src/Common/Fitnet.Common.IntegrationTests/Fitnet.Common.IntegrationTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<ItemGroup>
44
<PackageReference Include="Bogus" Version="34.0.2" />
55
<PackageReference Include="FluentAssertions" Version="6.10.0" />
6+
<PackageReference Include="Microsoft.Extensions.TimeProvider.Testing" Version="8.0.0" />
67
<PackageReference Include="Verify.Xunit" Version="19.11.2" />
78
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
89
<PrivateAssets>all</PrivateAssets>

Chapter-2-modules-separation/Src/Common/Fitnet.Common.IntegrationTests/TestEngine/Configuration/ConfigurationExtensions.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
namespace EvolutionaryArchitecture.Fitnet.Common.IntegrationTests.TestEngine.Configuration;
22

3-
using Core.SystemClock;
43
using Database;
54
using Infrastructure.Events.EventBus;
65
using Infrastructure.Mediator;
76
using Microsoft.AspNetCore.TestHost;
87
using Microsoft.Extensions.DependencyInjection;
9-
using SystemClock;
8+
using Microsoft.Extensions.Time.Testing;
109

1110
public static class ConfigurationExtensions
1211
{
@@ -32,7 +31,7 @@ public static WebApplicationFactory<T> SetFakeSystemClock<T>(
3231
where T : class =>
3332
webApplicationFactory.WithWebHostBuilder(webHostBuilder =>
3433
webHostBuilder.ConfigureTestServices(services =>
35-
services.AddSingleton<ISystemClock>(new FakeSystemClock(fakeDateTimeOffset))));
34+
services.AddSingleton<TimeProvider>(new FakeTimeProvider(fakeDateTimeOffset))));
3635

3736
public static WebApplicationFactory<T> WithFakeEventBus<T>(
3837
this WebApplicationFactory<T> webApplicationFactory,

Chapter-2-modules-separation/Src/Common/Fitnet.Common.IntegrationTests/TestEngine/SystemClock/FakeSystemClock.cs

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

Chapter-2-modules-separation/Src/Contracts/Fitnet.Contracts.Application/Sign/SignContractCommandHandler.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
namespace EvolutionaryArchitecture.Fitnet.Contracts.Application.Sign;
22

33
using Common.Api.ErrorHandling;
4-
using Common.Core.SystemClock;
54
using Common.Infrastructure.Events.EventBus;
65
using Core;
76
using IntegrationEvents;
87

98
[UsedImplicitly]
109
internal sealed class SignContractCommandHandler(
1110
IContractsRepository contractsRepository,
12-
ISystemClock systemClock,
11+
TimeProvider timeProvider,
1312
IEventBus eventBus) : IRequestHandler<SignContractCommand>
1413
{
1514
public async Task Handle(SignContractCommand command, CancellationToken cancellationToken)
1615
{
1716
var contract = await contractsRepository.GetByIdAsync(command.Id, cancellationToken) ??
1817
throw new ResourceNotFoundException(command.Id);
1918

20-
contract.Sign(command.SignedAt, systemClock.Now);
19+
var nowDate = timeProvider.GetUtcNow();
20+
contract.Sign(command.SignedAt, nowDate);
2121
await contractsRepository.CommitAsync(cancellationToken);
2222
var @event = ContractSignedEvent.Create(contract.Id,
2323
contract.CustomerId,

Chapter-2-modules-separation/Src/Offers/Fitnet.Offers.Api/Prepare/PassExpiredEventHandler.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
namespace EvolutionaryArchitecture.Fitnet.Offers.Api.Prepare;
22

3-
using Common.Core.SystemClock;
43
using Common.Infrastructure.Events;
54
using Common.Infrastructure.Events.EventBus;
65
using DataAccess;
@@ -10,11 +9,12 @@ namespace EvolutionaryArchitecture.Fitnet.Offers.Api.Prepare;
109
internal sealed class PassExpiredEventHandler(
1110
IEventBus eventBus,
1211
OffersPersistence persistence,
13-
ISystemClock systemClock) : IIntegrationEventHandler<PassExpiredEvent>
12+
TimeProvider timeProvider) : IIntegrationEventHandler<PassExpiredEvent>
1413
{
1514
public async Task Handle(PassExpiredEvent @event, CancellationToken cancellationToken)
1615
{
17-
var offer = Offer.PrepareStandardPassExtension(@event.CustomerId, systemClock.Now);
16+
var nowDate = timeProvider.GetUtcNow();
17+
var offer = Offer.PrepareStandardPassExtension(@event.CustomerId, nowDate);
1818
persistence.Offers.Add(offer);
1919
await persistence.SaveChangesAsync(cancellationToken);
2020

Chapter-2-modules-separation/Src/Passes/Fitnet.Passes.Api/MarkPassAsExpired/MarkPassAsExpiredEndpoint.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
namespace EvolutionaryArchitecture.Fitnet.Passes.Api.MarkPassAsExpired;
22

3-
using Common.Core.SystemClock;
43
using IntegrationEvents;
54
using Common.Infrastructure.Events.EventBus;
65
using DataAccess.Database;
@@ -15,7 +14,7 @@ internal static void MapMarkPassAsExpired(this IEndpointRouteBuilder app) => app
1514
async (
1615
Guid id,
1716
PassesPersistence persistence,
18-
ISystemClock systemClock,
17+
TimeProvider systemClock,
1918
IEventBus eventBus,
2019
CancellationToken cancellationToken) =>
2120
{
@@ -26,7 +25,8 @@ internal static void MapMarkPassAsExpired(this IEndpointRouteBuilder app) => app
2625
return Results.NotFound();
2726
}
2827

29-
pass.MarkAsExpired(systemClock.Now);
28+
var nowDate = systemClock.GetUtcNow();
29+
pass.MarkAsExpired(nowDate);
3030
await persistence.SaveChangesAsync(cancellationToken);
3131

3232
var passExpiredEvent = PassExpiredEvent.Create(pass.Id, pass.CustomerId);

Chapter-2-modules-separation/Src/Reports/Fitnet.Reports/GenerateNewPassesRegistrationsPerMonthReport/DataRetriever/NewPassesRegistrationPerMonthReportDataRetriever.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
namespace EvolutionaryArchitecture.Fitnet.Reports.GenerateNewPassesRegistrationsPerMonthReport.DataRetriever;
22

3-
using Common.Core.SystemClock;
43
using Dapper;
54
using Dtos;
65
using DataAccess;
76

87
internal sealed class NewPassesRegistrationPerMonthReportDataRetriever(
98
IDatabaseConnectionFactory databaseConnectionFactory,
10-
ISystemClock clock) : INewPassesRegistrationPerMonthReportDataRetriever
9+
TimeProvider timeProvider) : INewPassesRegistrationPerMonthReportDataRetriever
1110
{
1211
public async Task<IReadOnlyCollection<NewPassesRegistrationsPerMonthDto>> GetReportDataAsync(
1312
CancellationToken cancellationToken = default)
@@ -18,7 +17,7 @@ SELECT EXTRACT(MONTH FROM ""Passes"".""From"")::INTEGER AS ""{nameof(NewPassesRe
1817
to_char(""Passes"".""From"", 'Month') AS ""{nameof(NewPassesRegistrationsPerMonthDto.MonthName)}"",
1918
COUNT(*) AS ""{nameof(NewPassesRegistrationsPerMonthDto.RegisteredPasses)}""
2019
FROM ""Passes"".""Passes""
21-
WHERE EXTRACT(YEAR FROM ""Passes"".""From"") = '{clock.Now.Year}'
20+
WHERE EXTRACT(YEAR FROM ""Passes"".""From"") = '{timeProvider.GetUtcNow().Year}'
2221
GROUP BY ""{nameof(NewPassesRegistrationsPerMonthDto.MonthName)}"", ""{nameof(NewPassesRegistrationsPerMonthDto.MonthOrder)}""
2322
ORDER BY ""{nameof(NewPassesRegistrationsPerMonthDto.MonthOrder)}""";
2423

0 commit comments

Comments
 (0)