StrongTypeIdGenerator 1.0.1

dotnet add package StrongTypeIdGenerator --version 1.0.1
                    
NuGet\Install-Package StrongTypeIdGenerator -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="StrongTypeIdGenerator" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="StrongTypeIdGenerator" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="StrongTypeIdGenerator" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add StrongTypeIdGenerator --version 1.0.1
                    
#r "nuget: StrongTypeIdGenerator, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package StrongTypeIdGenerator@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=StrongTypeIdGenerator&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=StrongTypeIdGenerator&version=1.0.1
                    
Install as a Cake Tool

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.

NuGet License: MIT

Why this library

  • Removes boilerplate for strongly typed IDs.
  • Prevents accidental mixing of identifiers across boundaries.
  • Keeps validation close to the ID type via CheckValue hooks.
  • Supports factory-oriented design with optional private constructors.
  • Works with System.ComponentModel.TypeConverter out 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.TypeConverter and has no direct dependency on System.Text.Json, Newtonsoft.Json, or EF Core converters.
  • netstandard2.0-friendly usage. IDs can live in netstandard2.0 libraries without extra serialization dependencies. For System.Text.Json, use the optional StrongTypeIdGenerator.Json package.
  • First-class composite identifiers. CombinedId exists for real-world composite business keys, avoiding ad-hoc wrapper implementations.

Main features

  • StringId and GuidId generation with value semantics.
  • CombinedId generation 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.Json integration 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
  • Unspecified
  • Equals, GetHashCode, comparison operators
  • ToString and 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:

Acknowledgements

Inspired by StronglyTypedId.

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • 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 99 3/16/2026
1.0.0 3,855 8/15/2025
1.0.0-rc9 267 5/24/2025
1.0.0-rc8 196 5/19/2025
1.0.0-rc7 208 5/19/2025
1.0.0-rc6 552 5/5/2025
1.0.0-rc5 463 10/8/2024
1.0.0-rc4 321 7/8/2024
1.0.0-rc3 339 6/22/2024
1.0.0-rc2 323 6/15/2024
1.0.0-rc12 321 6/22/2025
1.0.0-rc11 191 5/31/2025
1.0.0-rc10 123 5/31/2025
1.0.0-rc1 321 6/15/2024
1.0.0-rc0 164 6/15/2024