Use [StringId] to generate a strongly typed identifier backed by string.
using StrongTypeIdGenerator;
[StringId]
public sealed partial class OrderId
{
}A generated OrderId includes:
- Constructor that accepts
string Valueproperty (or custom name)Unspecifiedinitialized withstring.Empty- Equality/comparison members and operators
ToString()andToString(string?, IFormatProvider?)- Implicit conversions to and from
string - Nested
TypeConverter
[StringId]
public sealed partial class OrderId
{
private static string CheckValue(string value)
{
if (string.IsNullOrWhiteSpace(value))
throw new ArgumentException("Order ID cannot be empty", nameof(value));
return value.Trim().ToUpperInvariant();
}
}CheckValue is called from the generated constructor.
[StringId(ValuePropertyName = "Code")]
public sealed partial class ProductCode
{
}The generated property name becomes Code instead of Value.
[StringId(GenerateConstructorPrivate = true)]
public sealed partial class SessionToken
{
public static SessionToken Create(string raw) => new SessionToken(raw);
}Continue with custom validation and private constructors.