Skip to content

Commit 7ef9ab1

Browse files
committed
Update Guid-backed ID factory to use CreateVersion7 on .NET 9+
The source generator now emits New() for Guid-backed IDs that uses Guid.CreateVersion7() on .NET 9+ and falls back to Guid.NewGuid() on earlier versions. Updated docs and implementation in NewableGuid.cs to match this logic.
1 parent ee43517 commit 7ef9ab1

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/SKILL.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,14 @@ For every struct ID, the source generator emits:
100100
- Implicit/explicit conversion operators to/from `TValue`
101101
- `INewable<TSelf>` / `INewable<TSelf, TValue>` implementation
102102
- `New(TValue value)` static factory method
103-
- `New()` (parameterless) for `Guid`- and `Ulid`-backed IDs, using `Guid.NewGuid()` / `Ulid.NewUlid()`
103+
- `New()` (parameterless) for `Guid`- and `Ulid`-backed IDs, using `Guid.CreateVersion7()` on .NET 9+ or `Guid.NewGuid()` on earlier targets / `Ulid.NewUlid()`
104104

105105
## Factory Methods
106106

107107
```csharp
108108
// Guid-backed: parameterless New() generates a new GUID
109-
var userId = UserId.New(); // new UserId(Guid.NewGuid())
109+
// On .NET 9+, uses Guid.CreateVersion7(); earlier targets use Guid.NewGuid()
110+
var userId = UserId.New(); // new UserId(Guid.CreateVersion7()) on .NET 9+
110111
var userId2 = UserId.New(someGuid); // new UserId(someGuid)
111112
112113
// Ulid-backed: parameterless New() generates a new ULID

src/StructId/Templates/NewableGuid.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@
66
[TStructId]
77
file partial record struct TSelf(Guid Value)
88
{
9-
/// <summary>
10-
/// Creates a new instance of <typeparamref name="TSelf"/> with a <see cref="Guid.NewGuid"/>.
11-
/// </summary>
12-
public static TSelf New() => new(Guid.NewGuid());
9+
/// <summary>Creates a new instance with a newly generated <see cref="Guid"/>.</summary>
10+
public static TSelf New()
11+
{
12+
#if NET9_0_OR_GREATER
13+
return new(Guid.CreateVersion7());
14+
#else
15+
return new(Guid.NewGuid());
16+
#endif
17+
}
1318
}

0 commit comments

Comments
 (0)