|
5 | 5 | namespace NodaTime; |
6 | 6 |
|
7 | 7 | /// <summary> |
8 | | -/// Helper functions that utilize NodaTime's Instant struct rather than DateTime |
| 8 | +/// Provides extension methods for working with NodaTime's <see cref="Instant"/> and sequential GUID generators. |
9 | 9 | /// </summary> |
| 10 | +/// <remarks> |
| 11 | +/// This static class contains helper methods to facilitate the creation of sequential GUIDs and |
| 12 | +/// conversions between <see cref="Instant"/> and GUID-related types. |
| 13 | +/// </remarks> |
10 | 14 | public static class ExtensionMethods |
11 | 15 | { |
12 | 16 | /// <summary> |
13 | | - /// Takes an instance of NodaTime's Instant struct and returns back a sequential guid |
| 17 | + /// Generates a new sequential <see cref="Guid"/> based on the specified <see cref="Instant"/> timestamp. |
14 | 18 | /// </summary> |
15 | | - /// <param name="generator">Extension parameter the singleton instance of the generator</param> |
16 | | - /// <param name="timestamp">Time value in UTC between the Unix epoch and now</param> |
17 | | - /// <returns>sequential guid</returns> |
| 19 | + /// <param name="generator"> |
| 20 | + /// The <see cref="SequentialGuidGenerator"/> instance used to generate the sequential <see cref="Guid"/>. |
| 21 | + /// </param> |
| 22 | + /// <param name="timestamp"> |
| 23 | + /// The <see cref="Instant"/> representing the point in time to base the GUID generation on. |
| 24 | + /// </param> |
| 25 | + /// <returns> |
| 26 | + /// A new sequential <see cref="Guid"/> generated using the provided <paramref name="timestamp"/>. |
| 27 | + /// </returns> |
| 28 | + /// <remarks> |
| 29 | + /// This method converts the provided <see cref="Instant"/> to a UTC <see cref="DateTime"/> and uses it |
| 30 | + /// to generate a sequential <see cref="Guid"/>. Sequential GUIDs are particularly useful for database |
| 31 | + /// indexing and reducing fragmentation. |
| 32 | + /// </remarks> |
| 33 | + /// <exception cref="ArgumentException"> |
| 34 | + /// Thrown if the <paramref name="timestamp"/> is outside the valid range for GUID generation. |
| 35 | + /// </exception> |
18 | 36 | public static Guid NewGuid(this SequentialGuidGenerator generator, Instant timestamp) => |
19 | 37 | generator.NewGuid(timestamp.ToDateTimeUtc()); |
20 | | - |
| 38 | + |
21 | 39 | /// <summary> |
22 | | - /// Takes an instance of NodaTime's Instant struct and returns back a sequential guid sorted for SQL Server |
| 40 | + /// Generates a new sequential <see cref="Guid"/> based on the provided <see cref="Instant"/> timestamp. |
23 | 41 | /// </summary> |
24 | | - /// <param name="generator">Extension parameter the singleton instance of the generator</param> |
25 | | - /// <param name="timestamp">Time value in UTC between the Unix epoch and now</param> |
26 | | - /// <returns>sequential guid for SQL Server</returns> |
| 42 | + /// <param name="generator"> |
| 43 | + /// The <see cref="SequentialSqlGuidGenerator"/> instance used to generate the sequential GUID. |
| 44 | + /// </param> |
| 45 | + /// <param name="timestamp"> |
| 46 | + /// The <see cref="Instant"/> representing the timestamp to base the GUID generation on. |
| 47 | + /// </param> |
| 48 | + /// <returns> |
| 49 | + /// A new sequential <see cref="Guid"/> generated using the specified timestamp. |
| 50 | + /// </returns> |
| 51 | + /// <remarks> |
| 52 | + /// This method converts the provided <see cref="Instant"/> to a UTC <see cref="DateTime"/> and uses it |
| 53 | + /// to generate a sequential GUID. Sequential GUIDs are particularly useful in scenarios where maintaining |
| 54 | + /// index performance in databases is critical. |
| 55 | + /// </remarks> |
| 56 | + /// <exception cref="ArgumentException"> |
| 57 | + /// Thrown if the <paramref name="timestamp"/> is outside the valid range for GUID generation. |
| 58 | + /// </exception> |
27 | 59 | public static Guid NewGuid(this SequentialSqlGuidGenerator generator, Instant timestamp) => |
28 | 60 | generator.NewGuid(timestamp.ToDateTimeUtc()); |
29 | | - |
| 61 | + |
30 | 62 | /// <summary> |
31 | | - /// Takes an instance of NodaTime's Instant struct and returns back a sequential SQL guid |
| 63 | + /// Generates a new sequential <see cref="SqlGuid"/> based on the specified <see cref="Instant"/> timestamp. |
32 | 64 | /// </summary> |
33 | | - /// <param name="generator">Extension parameter the singleton instance of the generator</param> |
34 | | - /// <param name="timestamp">Time value in UTC between the Unix epoch and now</param> |
35 | | - /// <returns>sequential SQL guid</returns> |
| 65 | + /// <param name="generator"> |
| 66 | + /// The <see cref="SequentialSqlGuidGenerator"/> instance used to create the sequential <see cref="SqlGuid"/>. |
| 67 | + /// </param> |
| 68 | + /// <param name="timestamp"> |
| 69 | + /// The <see cref="Instant"/> representing the point in time to base the sequential <see cref="SqlGuid"/> on. |
| 70 | + /// </param> |
| 71 | + /// <returns> |
| 72 | + /// A new sequential <see cref="SqlGuid"/> corresponding to the provided <see cref="Instant"/>. |
| 73 | + /// </returns> |
| 74 | + /// <remarks> |
| 75 | + /// This method facilitates the creation of sequential <see cref="SqlGuid"/> values, which are particularly |
| 76 | + /// useful in database scenarios where maintaining index performance is critical. The <paramref name="timestamp"/> |
| 77 | + /// is converted to a <see cref="DateTime"/> in UTC before generating the <see cref="SqlGuid"/>. |
| 78 | + /// </remarks> |
36 | 79 | public static SqlGuid NewSqlGuid(this SequentialSqlGuidGenerator generator, Instant timestamp) => |
37 | 80 | generator.NewSqlGuid(timestamp.ToDateTimeUtc()); |
38 | 81 |
|
39 | 82 | /// <summary> |
40 | | - /// Will return the value of SystemClock.Instance.GetCurrentInstant() at the time of the generation of the Guid will |
41 | | - /// keep you from needing to store separate audit fields |
| 83 | + /// Converts a <see cref="Guid"/> to a <see cref="NodaTime.Instant"/> if the <see cref="Guid"/> contains a valid timestamp. |
42 | 84 | /// </summary> |
43 | | - /// <param name="id">A sequential Guid with the first 8 bytes containing the system ticks at time of generation</param> |
44 | | - /// <returns>Instant?</returns> |
| 85 | + /// <param name="id">The <see cref="Guid"/> to extract the timestamp from.</param> |
| 86 | + /// <returns> |
| 87 | + /// An <see cref="NodaTime.Instant"/> representing the timestamp embedded in the <see cref="Guid"/>, |
| 88 | + /// or <c>null</c> if the <see cref="Guid"/> does not contain a valid timestamp. |
| 89 | + /// </returns> |
45 | 90 | public static Instant? ToInstant(this Guid id) => |
46 | 91 | id.ToDateTime().ToInstant(); |
47 | 92 |
|
48 | 93 | /// <summary> |
49 | | - /// Will return the value of SystemClock.Instance.GetCurrentInstant() at the time of the generation of the Guid will |
50 | | - /// keep you from needing to store separate audit fields |
| 94 | + /// Converts a <see cref="SqlGuid"/> to a nullable <see cref="NodaTime.Instant"/>. |
51 | 95 | /// </summary> |
52 | | - /// <param name="sqlGuid"> |
53 | | - /// A sequential SqlGuid with the first sorted 8 bytes containing the system ticks at time of |
54 | | - /// generation |
55 | | - /// </param> |
56 | | - /// <returns>Instant?</returns> |
| 96 | + /// <param name="sqlGuid">The <see cref="SqlGuid"/> to convert.</param> |
| 97 | + /// <returns> |
| 98 | + /// A nullable <see cref="NodaTime.Instant"/> representing the timestamp embedded in the <see cref="SqlGuid"/>, |
| 99 | + /// or <c>null</c> if the <see cref="SqlGuid"/> does not contain a valid timestamp. |
| 100 | + /// </returns> |
| 101 | + /// <remarks> |
| 102 | + /// This method extracts the timestamp from the provided <see cref="SqlGuid"/> and converts it to a |
| 103 | + /// <see cref="NodaTime.Instant"/>. If the <see cref="SqlGuid"/> does not contain a valid timestamp, |
| 104 | + /// the method returns <c>null</c>. |
| 105 | + /// </remarks> |
57 | 106 | public static Instant? ToInstant(this SqlGuid sqlGuid) => |
58 | 107 | sqlGuid.ToDateTime().ToInstant(); |
59 | 108 |
|
60 | 109 | // Helper function to prevent code duplication all it does is conditionally convert a nullable datetime to a nullable instant |
61 | 110 | private static Instant? ToInstant(this DateTime? value) => |
62 | | - value.HasValue ? Instant.FromDateTimeUtc(value.Value) : default(Instant?); |
| 111 | + value.HasValue ? Instant.FromDateTimeUtc(value.Value) : null; |
63 | 112 | } |
0 commit comments