11namespace System ;
22
33/// <summary>
4- /// Provides polyfills for GUID generation methods not available in older .NET versions,
5- /// with monotonic counter-based ordering for strict intra-millisecond sequencing.
4+ /// Provides polyfills for GUID generation methods not available in older .NET versions.
65/// </summary>
76internal static class GuidPolyfills
87{
@@ -11,17 +10,24 @@ internal static class GuidPolyfills
1110 private static readonly object s_lock = new ( ) ;
1211
1312 /// <summary>
14- /// Creates a UUID v7 GUID with the specified timestamp.
13+ /// Creates a monotonically increasing UUID v7 GUID with the specified timestamp.
1514 /// Uses a counter for intra-millisecond ordering to ensure strict monotonicity.
1615 /// </summary>
1716 /// <param name="timestamp">The timestamp to embed in the GUID.</param>
18- /// <returns>A new UUID v7 GUID.</returns>
17+ /// <returns>A new monotonically increasing UUID v7 GUID.</returns>
1918 /// <remarks>
20- /// Unlike the built-in <c>Guid.CreateVersion7(DateTimeOffset)</c> in .NET 9+,
21- /// this implementation uses a counter to ensure strict monotonicity within the same millisecond,
22- /// which is required for keyset pagination to work correctly.
19+ /// <para>
20+ /// This method cannot be replaced with <c>Guid.CreateVersion7(DateTimeOffset)</c> because
21+ /// the built-in .NET implementation uses random bits for intra-millisecond uniqueness,
22+ /// which does not guarantee strict monotonicity. For keyset pagination to work correctly,
23+ /// GUIDs created within the same millisecond must be strictly ordered by creation time.
24+ /// </para>
25+ /// <para>
26+ /// This implementation uses RFC 9562's optional counter mechanism to ensure that
27+ /// multiple GUIDs generated within the same millisecond are strictly monotonically increasing.
28+ /// </para>
2329 /// </remarks>
24- public static Guid CreateVersion7 ( DateTimeOffset timestamp )
30+ public static Guid CreateMonotonicUuid ( DateTimeOffset timestamp )
2531 {
2632 // UUID v7 format (RFC 9562):
2733 // - 48 bits: Unix timestamp in milliseconds (big-endian)
@@ -88,4 +94,3 @@ public static Guid CreateVersion7(DateTimeOffset timestamp)
8894 bytes [ 12 ] , bytes [ 13 ] , bytes [ 14 ] , bytes [ 15 ] ) ;
8995 }
9096}
91-
0 commit comments