Skip to content

Latest commit

 

History

History
64 lines (49 loc) · 1.23 KB

File metadata and controls

64 lines (49 loc) · 1.23 KB

Guid IDs

Use [GuidId] to generate a strongly typed identifier backed by Guid.

Basic example

using StrongTypeIdGenerator;

[GuidId]
public sealed partial class CustomerId
{
}

Generated behavior

A generated CustomerId includes:

  • Constructor that accepts Guid
  • Value property (or custom name)
  • Unspecified initialized with Guid.Empty
  • Equality/comparison members and operators
  • ToString() and formatting overload
  • Implicit conversions to and from Guid
  • Nested TypeConverter

Validation example

[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;
    }
}

Custom property name

[GuidId(ValuePropertyName = "Uuid")]
public sealed partial class ExternalReferenceId
{
}

Private constructor mode

[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.