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
31 changes: 5 additions & 26 deletions source/EasyWay/Clock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,12 @@

namespace EasyWay
{
public static class Clock
public sealed class Clock
{
[ThreadStatic] private static TimeSpan? _differenceBetweenMoments;
public TimeProvider TimeProvider => InternalClock.TimeProvider;

public static DateTime UtcNow
{
get
{
if (_differenceBetweenMoments.HasValue)
{
return DateTime.UtcNow.Add(_differenceBetweenMoments.Value);
}

return DateTime.UtcNow;
}
}

internal static void Set(DateTime customDateTime)
{
if (customDateTime.Kind != DateTimeKind.Utc)
{
throw new CustomDateTimeMustBeUtcException();
}

_differenceBetweenMoments = customDateTime - DateTime.UtcNow;
}

internal static void Reset() => _differenceBetweenMoments = null;
public DateTime UtcNow => InternalClock.UtcNow;

internal Clock() { }
}
}
5 changes: 3 additions & 2 deletions source/EasyWay/DomainEvent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using EasyWay.Internals.GuidGenerators;
using EasyWay.Internals.Clocks;
using EasyWay.Internals.GuidGenerators;

namespace EasyWay
{
Expand All @@ -14,7 +15,7 @@ public abstract class DomainEvent
protected DomainEvent()
{
EventId = GuidGenerator.New;
OccurrenceOn = Clock.UtcNow;
OccurrenceOn = InternalClock.UtcNow;
}
}
}

This file was deleted.

17 changes: 17 additions & 0 deletions source/EasyWay/Internals/Clocks/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.Extensions.DependencyInjection;

namespace EasyWay.Internals.Clocks
{
internal static class Extensions
{
internal static IServiceCollection AddClocks(this IServiceCollection services)
{
var clock = new Clock();

services.AddSingleton(clock);
services.AddSingleton(clock.TimeProvider);

return services;
}
}
}
15 changes: 15 additions & 0 deletions source/EasyWay/Internals/Clocks/InternalClock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace EasyWay.Internals.Clocks
{
internal static class InternalClock
{
[ThreadStatic] private static TimeProvider? _testTimeProvider;

private static TimeProvider _timeProvider = TimeProvider.System;

internal static TimeProvider TimeProvider => _testTimeProvider is null ? _timeProvider : _testTimeProvider;

internal static DateTime UtcNow => TimeProvider.GetUtcNow().UtcDateTime;

internal static void Test(TimeProvider timeProvider) => _testTimeProvider = timeProvider;
}
}
2 changes: 2 additions & 0 deletions source/EasyWay/Internals/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using EasyWay.Internals.AggregateRoots;
using EasyWay.Internals.Clocks;
using EasyWay.Internals.Commands;
using EasyWay.Internals.Contexts;
using EasyWay.Internals.DomainEvents;
Expand All @@ -21,6 +22,7 @@ internal static void AddEasyWay(
IEnumerable<Assembly> assemblies)
{
services
.AddClocks()
.AddContexts()
.AddAggregateRoots()
.AddCommands(moduleType, assemblies)
Expand Down
6 changes: 4 additions & 2 deletions source/EasyWay/Internals/GuidGenerators/GuidGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace EasyWay.Internals.GuidGenerators
using EasyWay.Internals.Clocks;

namespace EasyWay.Internals.GuidGenerators
{
internal static class GuidGenerator
{
Expand All @@ -10,6 +12,6 @@ internal static class GuidGenerator

internal static void Reset() => _customId = null;

private static Guid Create() => Guid.CreateVersion7(Clock.UtcNow);
private static Guid Create() => Guid.CreateVersion7(InternalClock.UtcNow);
}
}
1 change: 1 addition & 0 deletions tests/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<CentralPackageVersionOverrideEnabled>false</CentralPackageVersionOverrideEnabled>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Microsoft.Extensions.TimeProvider.Testing" Version="9.2.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageVersion Include="Moq" Version="4.20.72" />
<PackageVersion Include="xunit" Version="2.9.2" />
Expand Down
74 changes: 0 additions & 74 deletions tests/EasyWay.Tests/Clocks/ClockTests.cs

This file was deleted.

7 changes: 5 additions & 2 deletions tests/EasyWay.Tests/DomainEvents/DomainEventTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace EasyWay.Tests.DomainEvents
using EasyWay.Internals.Clocks;
using Microsoft.Extensions.Time.Testing;

namespace EasyWay.Tests.DomainEvents
{
public sealed class DomainEventTests
{
Expand All @@ -11,7 +14,7 @@ public void Test()
var expectedDateTime = DateTime.UtcNow.AddMonths(-6);
var precision = TimeSpan.FromMilliseconds(50);

Clock.Set(expectedDateTime);
InternalClock.Test(new FakeTimeProvider(expectedDateTime));

// Act
var domainEvent = new TestDomainEvent();
Expand Down
1 change: 1 addition & 0 deletions tests/EasyWay.Tests/EasyWay.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.TimeProvider.Testing" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Moq" />
<PackageReference Include="xunit" />
Expand Down
Loading