From 00018acc6e3f9bc1150a86526d529cc8e8878843 Mon Sep 17 00:00:00 2001 From: "Daniel Mackay [SSW]" <2636640+danielmackay@users.noreply.github.com> Date: Thu, 24 Oct 2024 10:11:43 +1000 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Switch=20to=20using=20Guid?= =?UTF-8?q?.CreateVersion7()=20instead=20of=20Guid.NewGuid().=20(#397)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Switch to using Guid.CreateVersion7() instead of Guid.NewGuid(). --- src/Domain/Heroes/Hero.cs | 8 ++++++-- src/Domain/Teams/Mission.cs | 2 +- src/Domain/Teams/Team.cs | 2 +- tests/Domain.UnitTests/Heroes/HeroTests.cs | 2 +- .../Common/Fixtures/DatabaseContainer.cs | 2 +- .../Endpoints/Heroes/Commands/UpdateHeroCommandTests.cs | 2 +- 6 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/Domain/Heroes/Hero.cs b/src/Domain/Heroes/Hero.cs index c955afb3..e97b77e3 100644 --- a/src/Domain/Heroes/Hero.cs +++ b/src/Domain/Heroes/Hero.cs @@ -4,7 +4,10 @@ namespace SSW.CleanArchitecture.Domain.Heroes; // For strongly typed IDs, check out the rule: https://www.ssw.com.au/rules/do-you-use-strongly-typed-ids/ -public readonly record struct HeroId(Guid Value); +public readonly record struct HeroId(Guid Value) +{ + public HeroId() : this(Guid.CreateVersion7()) { } +} public class Hero : AggregateRoot { @@ -20,7 +23,8 @@ private Hero() { } public static Hero Create(string name, string alias) { - var hero = new Hero { Id = new HeroId(Guid.NewGuid()) }; + Guid.CreateVersion7(); + var hero = new Hero { Id = new HeroId(Guid.CreateVersion7()) }; hero.UpdateName(name); hero.UpdateAlias(alias); diff --git a/src/Domain/Teams/Mission.cs b/src/Domain/Teams/Mission.cs index e3487910..2cf574e0 100644 --- a/src/Domain/Teams/Mission.cs +++ b/src/Domain/Teams/Mission.cs @@ -20,7 +20,7 @@ internal static Mission Create(string description) ThrowIfNullOrWhiteSpace(description); return new Mission { - Id = new MissionId(Guid.NewGuid()), + Id = new MissionId(Guid.CreateVersion7()), Description = description, Status = MissionStatus.InProgress }; diff --git a/src/Domain/Teams/Team.cs b/src/Domain/Teams/Team.cs index c0d24550..cca467aa 100644 --- a/src/Domain/Teams/Team.cs +++ b/src/Domain/Teams/Team.cs @@ -26,7 +26,7 @@ public static Team Create(string name) { ThrowIfNullOrWhiteSpace(name); - var team = new Team { Id = new TeamId(Guid.NewGuid()), Name = name, Status = TeamStatus.Available }; + var team = new Team { Id = new TeamId(Guid.CreateVersion7()), Name = name, Status = TeamStatus.Available }; return team; } diff --git a/tests/Domain.UnitTests/Heroes/HeroTests.cs b/tests/Domain.UnitTests/Heroes/HeroTests.cs index 598d1075..a6c0288c 100644 --- a/tests/Domain.UnitTests/Heroes/HeroTests.cs +++ b/tests/Domain.UnitTests/Heroes/HeroTests.cs @@ -100,7 +100,7 @@ public void AddPower_ShouldRaisePowerLevelUpdatedEvent() { // Act var hero = Hero.Create("name", "alias"); - hero.Id = new HeroId(Guid.NewGuid()); + hero.Id = new HeroId(); hero.UpdatePowers([new Power("Super-strength", 10)]); // Assert diff --git a/tests/WebApi.IntegrationTests/Common/Fixtures/DatabaseContainer.cs b/tests/WebApi.IntegrationTests/Common/Fixtures/DatabaseContainer.cs index edaa62de..2db2d3c4 100644 --- a/tests/WebApi.IntegrationTests/Common/Fixtures/DatabaseContainer.cs +++ b/tests/WebApi.IntegrationTests/Common/Fixtures/DatabaseContainer.cs @@ -10,7 +10,7 @@ public class DatabaseContainer : IAsyncDisposable { private readonly MsSqlContainer _container = new MsSqlBuilder() .WithImage("mcr.microsoft.com/mssql/server:2022-CU14-ubuntu-22.04") - .WithName($"CleanArchitecture-IntegrationTests-{Guid.NewGuid()}") + .WithName($"CleanArchitecture-IntegrationTests-{Guid.CreateVersion7()}") .WithPassword("Password123") .WithPortBinding(1433, true) .WithAutoRemove(true) diff --git a/tests/WebApi.IntegrationTests/Endpoints/Heroes/Commands/UpdateHeroCommandTests.cs b/tests/WebApi.IntegrationTests/Endpoints/Heroes/Commands/UpdateHeroCommandTests.cs index 90600342..082e5221 100644 --- a/tests/WebApi.IntegrationTests/Endpoints/Heroes/Commands/UpdateHeroCommandTests.cs +++ b/tests/WebApi.IntegrationTests/Endpoints/Heroes/Commands/UpdateHeroCommandTests.cs @@ -53,7 +53,7 @@ public async Task Command_ShouldUpdateHero() public async Task Command_WhenHeroDoesNotExist_ShouldReturnNotFound() { // Arrange - var heroId = new HeroId(Guid.NewGuid()); + var heroId = new HeroId(); var cmd = new UpdateHeroCommand( "foo", "bar",