Use [GuidId] to generate a strongly typed identifier backed by Guid.
using StrongTypeIdGenerator;
[GuidId]
public sealed partial class CustomerId
{
}A generated CustomerId includes:
- Constructor that accepts
Guid Valueproperty (or custom name)Unspecifiedinitialized withGuid.Empty- Equality/comparison members and operators
ToString()and formatting overload- Implicit conversions to and from
Guid - Nested
TypeConverter
[GuidId]
public sealed partial class CustomerId
{
private static Guid CheckValue(Guid value)
{
if (value == Guid.Empty)
throw new ArgumentException("Empty GUID is not allowed", nameof(value));
return value;
}
}[GuidId(ValuePropertyName = "Uuid")]
public sealed partial class ExternalReferenceId
{
}[GuidId(GenerateConstructorPrivate = true)]
public sealed partial class UserId
{
public static UserId CreateNew() => new UserId(Guid.NewGuid());
public static UserId FromString(string value) => new UserId(Guid.Parse(value));
}Next: combined IDs.