Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace SystemClock with TimeProvider #55

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
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ namespace EvolutionaryArchitecture.Fitnet.IntegrationTests.Common.TestEngine.Con
using System.Reflection;
using EvolutionaryArchitecture.Fitnet.Common.Events.EventBus;
using EvolutionaryArchitecture.Fitnet.Common.Events.EventBus.InMemory;
using EvolutionaryArchitecture.Fitnet.Common.SystemClock;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.TestHost;
using SystemClock;
using Microsoft.Extensions.Time.Testing;

internal static class ConfigurationExtensions
{
Expand Down Expand Up @@ -37,7 +36,7 @@ private static WebApplicationFactory<T> UseSettings<T>(this WebApplicationFactor
internal static WebApplicationFactory<T> SetFakeSystemClock<T>(this WebApplicationFactory<T> webApplicationFactory, DateTimeOffset fakeDateTimeOffset)
where T : class =>
webApplicationFactory.WithWebHostBuilder(webHostBuilder => webHostBuilder.ConfigureTestServices(services =>
services.AddSingleton<ISystemClock>(new FakeSystemClock(fakeDateTimeOffset))));
services.AddSingleton<TimeProvider>(new FakeTimeProvider(fakeDateTimeOffset))));

internal static WebApplicationFactory<T> WithFakeEventBus<T>(this WebApplicationFactory<T> webApplicationFactory, IEventBus eventBusMock)
where T : class =>
Expand All @@ -48,5 +47,4 @@ internal static WebApplicationFactory<T> WithFakeConsumers<T>(this WebApplicatio
where T : class =>
webApplicationFactory.WithWebHostBuilder(webHostBuilder => webHostBuilder.ConfigureTestServices(services =>
services.AddInMemoryEventBus(Assembly.GetExecutingAssembly())));

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<ItemGroup>
<PackageReference Include="Bogus" Version="34.0.2" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.Extensions.TimeProvider.Testing" Version="8.0.0" />
<PackageReference Include="NSubstitute" Version="5.1.0" />
<PackageReference Include="Verify.Xunit" Version="22.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace EvolutionaryArchitecture.Fitnet.Common.Clock;

internal static class ClockModule
{
internal static IServiceCollection AddClock(this IServiceCollection services) =>
services.AddSingleton(TimeProvider.System);
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ namespace EvolutionaryArchitecture.Fitnet.Contracts.SignContract;
using Data.Database;
using Events;
using EvolutionaryArchitecture.Fitnet.Common.Events.EventBus;
using Common.SystemClock;
using Common.Validation.Requests;

internal static class SignContractEndpoint
{
internal static void MapSignContract(this IEndpointRouteBuilder app) => app.MapPatch(ContractsApiPaths.Sign,
async (Guid id, SignContractRequest request,
ContractsPersistence persistence, IEventBus bus, ISystemClock systemClock,
ContractsPersistence persistence, IEventBus bus,
TimeProvider timeProvider,
CancellationToken cancellationToken) =>
{
var contract =
Expand All @@ -21,7 +21,8 @@ internal static void MapSignContract(this IEndpointRouteBuilder app) => app.MapP
return Results.NotFound();
}

contract.Sign(request.SignedAt, systemClock.Now);
var dateNow = timeProvider.GetUtcNow();
contract.Sign(request.SignedAt, dateNow);
await persistence.SaveChangesAsync(cancellationToken);

var @event = ContractSignedEvent.Create(contract.Id, contract.CustomerId, contract.SignedAt!.Value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ namespace EvolutionaryArchitecture.Fitnet.Offers.Prepare;
using Passes.MarkPassAsExpired.Events;
using Common.Events;
using Common.Events.EventBus;
using Common.SystemClock;

internal sealed class PassExpiredEventHandler(
IEventBus eventBus,
OffersPersistence persistence,
ISystemClock systemClock) : IIntegrationEventHandler<PassExpiredEvent>
TimeProvider timeProvider) : IIntegrationEventHandler<PassExpiredEvent>
{
public async Task Handle(PassExpiredEvent @event, CancellationToken cancellationToken)
{
var offer = Offer.PrepareStandardPassExtension(@event.CustomerId, systemClock.Now);
var nowDate = timeProvider.GetUtcNow();
var offer = Offer.PrepareStandardPassExtension(@event.CustomerId, nowDate);
persistence.Offers.Add(offer);
await persistence.SaveChangesAsync(cancellationToken);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ namespace EvolutionaryArchitecture.Fitnet.Passes.MarkPassAsExpired;
using Data.Database;
using Events;
using EvolutionaryArchitecture.Fitnet.Common.Events.EventBus;
using Common.SystemClock;

internal static class MarkPassAsExpiredEndpoint
{
Expand All @@ -12,7 +11,7 @@ internal static void MapMarkPassAsExpired(this IEndpointRouteBuilder app) => app
async (
Guid id,
PassesPersistence persistence,
ISystemClock systemClock,
TimeProvider timeProvider,
IEventBus eventBus,
CancellationToken cancellationToken) =>
{
Expand All @@ -22,7 +21,8 @@ internal static void MapMarkPassAsExpired(this IEndpointRouteBuilder app) => app
return Results.NotFound();
}

pass.MarkAsExpired(systemClock.Now);
var nowDate = timeProvider.GetUtcNow();
pass.MarkAsExpired(nowDate);
await persistence.SaveChangesAsync(cancellationToken);
await eventBus.PublishAsync(PassExpiredEvent.Create(pass.Id, pass.CustomerId), cancellationToken);

Expand Down
8 changes: 3 additions & 5 deletions Chapter-1-initial-architecture/Src/Fitnet/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using EvolutionaryArchitecture.Fitnet.Common.Clock;
using EvolutionaryArchitecture.Fitnet.Common.ErrorHandling;
using EvolutionaryArchitecture.Fitnet.Common.Events.EventBus;
using EvolutionaryArchitecture.Fitnet.Common.SystemClock;
using EvolutionaryArchitecture.Fitnet.Common.Validation.Requests;
using EvolutionaryArchitecture.Fitnet.Contracts;
using EvolutionaryArchitecture.Fitnet.Offers;
Expand All @@ -12,9 +12,9 @@
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddSystemClock();
builder.Services.AddEventBus();
builder.Services.AddRequestsValidations();
builder.Services.AddClock();

builder.Services.AddPasses(builder.Configuration);
builder.Services.AddContracts(builder.Configuration);
Expand Down Expand Up @@ -52,7 +52,5 @@
namespace EvolutionaryArchitecture.Fitnet
{
[UsedImplicitly]
public sealed class Program
{
}
public sealed class Program;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ namespace EvolutionaryArchitecture.Fitnet.Reports.GenerateNewPassesRegistrations
using Dapper;
using Dtos;
using DataAccess;
using Common.SystemClock;

internal sealed class NewPassesRegistrationPerMonthReportDataRetriever(IDatabaseConnectionFactory databaseConnectionFactory, ISystemClock clock) : INewPassesRegistrationPerMonthReportDataRetriever
internal sealed class NewPassesRegistrationPerMonthReportDataRetriever(IDatabaseConnectionFactory databaseConnectionFactory, TimeProvider timeProvider) : INewPassesRegistrationPerMonthReportDataRetriever
{
public async Task<IReadOnlyCollection<NewPassesRegistrationsPerMonthDto>> GetReportDataAsync(CancellationToken cancellationToken = default)
{
Expand All @@ -15,7 +14,7 @@ SELECT EXTRACT(MONTH FROM ""Passes"".""From"")::INTEGER AS ""{nameof(NewPassesRe
to_char(""Passes"".""From"", 'Month') AS ""{nameof(NewPassesRegistrationsPerMonthDto.MonthName)}"",
COUNT(*) AS ""{nameof(NewPassesRegistrationsPerMonthDto.RegisteredPasses)}""
FROM ""Passes"".""Passes""
WHERE EXTRACT(YEAR FROM ""Passes"".""From"") = '{clock.Now.Year}'
WHERE EXTRACT(YEAR FROM ""Passes"".""From"") = '{timeProvider.GetUtcNow().Year}'
GROUP BY ""{nameof(NewPassesRegistrationsPerMonthDto.MonthName)}"", ""{nameof(NewPassesRegistrationsPerMonthDto.MonthOrder)}""
ORDER BY ""{nameof(NewPassesRegistrationsPerMonthDto.MonthOrder)}""";

Expand Down