StrongTypeIdGenerator.Json
1.0.1
dotnet add package StrongTypeIdGenerator.Json --version 1.0.1
NuGet\Install-Package StrongTypeIdGenerator.Json -Version 1.0.1
<PackageReference Include="StrongTypeIdGenerator.Json" Version="1.0.1" />
<PackageVersion Include="StrongTypeIdGenerator.Json" Version="1.0.1" />
<PackageReference Include="StrongTypeIdGenerator.Json" />
paket add StrongTypeIdGenerator.Json --version 1.0.1
#r "nuget: StrongTypeIdGenerator.Json, 1.0.1"
#:package StrongTypeIdGenerator.Json@1.0.1
#addin nuget:?package=StrongTypeIdGenerator.Json&version=1.0.1
#tool nuget:?package=StrongTypeIdGenerator.Json&version=1.0.1
StrongTypeIdGenerator
StrongTypeIdGenerator is a C# source generator for strongly typed identifiers.
It helps prevent primitive ID mix-ups by generating domain-specific ID types for string, Guid, and combined (composite) keys.
Why this library
- Removes boilerplate for strongly typed IDs.
- Prevents accidental mixing of identifiers across boundaries.
- Keeps validation close to the ID type via
CheckValuehooks. - Supports factory-oriented design with optional private constructors.
- Works with
System.ComponentModel.TypeConverterout of the box.
Design decisions:
- Reference types by design. This project prioritizes invariant safety and controlled construction over minimizing allocations, so invalid IDs are harder to create and propagate.
- Built-in precondition hooks. If an ID class defines
CheckValue(...), the method is called from the generated constructor and can validate or normalize input. - Serializer-agnostic core. The main package only relies on
System.ComponentModel.TypeConverterand has no direct dependency onSystem.Text.Json, Newtonsoft.Json, or EF Core converters. - netstandard2.0-friendly usage. IDs can live in
netstandard2.0libraries without extra serialization dependencies. ForSystem.Text.Json, use the optionalStrongTypeIdGenerator.Jsonpackage. - First-class composite identifiers.
CombinedIdexists for real-world composite business keys, avoiding ad-hoc wrapper implementations.
Main features
StringIdandGuidIdgeneration with value semantics.CombinedIdgeneration for composite identifiers.- Generated equality, comparison, formatting, and operators.
- Optional custom value property name for scalar identifiers.
- Optional constructor privacy (
GenerateConstructorPrivate = true). - Optional
System.Text.Jsonintegration package.
Quick start
Install package:
dotnet add package StrongTypeIdGenerator
Define identifiers:
using StrongTypeIdGenerator;
[StringId]
public sealed partial class OrderId
{
}
[GuidId]
public sealed partial class CustomerId
{
}
[CombinedId(typeof(CustomerId), "CustomerId", typeof(OrderId), "OrderId")]
public sealed partial class CustomerOrderId
{
}
<details> <summary>Generated structure (example for <code>OrderId</code>)</summary>
[TypeConverter(typeof(OrderIdConverter))]
public sealed partial class OrderId : ITypedIdentifier<OrderId, string>
{
public OrderId(string value) { ... }
public static OrderId Unspecified { get; } = ...;
public string Value { get; }
public static implicit operator OrderId?(string? value) { ... }
public static implicit operator string?(OrderId? value) { ... }
public bool Equals(OrderId? other) { ... }
public int CompareTo(OrderId? other) { ... }
public override bool Equals(object? obj) { ... }
public override int GetHashCode() { ... }
public override string ToString() { ... }
public string ToString(string? format, IFormatProvider? provider) { ... }
public static bool operator ==(OrderId left, OrderId right) { ... }
public static bool operator !=(OrderId left, OrderId right) { ... }
public static bool operator <(OrderId left, OrderId right) { ... }
public static bool operator <=(OrderId left, OrderId right) { ... }
public static bool operator >(OrderId left, OrderId right) { ... }
public static bool operator >=(OrderId left, OrderId right) { ... }
private sealed partial class OrderIdConverter : TypeToStringConverter<OrderId>
{
protected override string? InternalConvertToString(OrderId value) { ... }
protected override OrderId? InternalConvertFromString(string value) { ... }
}
}
</details>
The generator creates immutable reference-type identifiers with:
- constructor (public or private based on attribute options)
- typed value/component properties
UnspecifiedEquals,GetHashCode, comparison operatorsToStringand format overloads- implicit conversion operators
- nested
TypeConverter
Optional JSON integration
For System.Text.Json, install:
dotnet add package StrongTypeIdGenerator.Json
Configure serializer:
using StrongTypeIdGenerator.Json;
var options = new JsonSerializerOptions();
options.Converters.Add(new TypeConverterJsonConverterFactory());
Documentation
Detailed docs are in the docs folder:
- Docs Index
- Getting Started
- String IDs
- Guid IDs
- Combined IDs
- Custom Validation (CheckValue)
- Custom Value Property Name
- Private Constructors and Factories
- TypeConverter and System.Text.Json
- Design Decisions
- FAQ
Acknowledgements
Inspired by StronglyTypedId.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- System.Text.Json (>= 6.0.10)
-
net6.0
- No dependencies.
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.1 | 100 | 3/16/2026 |
| 1.0.0 | 2,003 | 8/15/2025 |
| 1.0.0-rc9 | 158 | 5/24/2025 |
| 1.0.0-rc8 | 204 | 5/19/2025 |
| 1.0.0-rc7 | 200 | 5/19/2025 |
| 1.0.0-rc6 | 197 | 5/5/2025 |
| 1.0.0-rc5 | 153 | 10/8/2024 |
| 1.0.0-rc4 | 158 | 7/8/2024 |
| 1.0.0-rc3 | 172 | 6/22/2024 |
| 1.0.0-rc2 | 156 | 6/15/2024 |
| 1.0.0-rc12 | 152 | 6/22/2025 |
| 1.0.0-rc11 | 115 | 5/31/2025 |
| 1.0.0-rc10 | 126 | 5/31/2025 |
| 1.0.0-rc1 | 153 | 6/15/2024 |
| 1.0.0-rc0 | 167 | 6/15/2024 |