StrongTypeIdGenerator 1.0.0-rc12

This is a prerelease version of StrongTypeIdGenerator.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package StrongTypeIdGenerator --version 1.0.0-rc12
                    
NuGet\Install-Package StrongTypeIdGenerator -Version 1.0.0-rc12
                    
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.0-rc12" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="StrongTypeIdGenerator" Version="1.0.0-rc12" />
                    
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.0-rc12
                    
#r "nuget: StrongTypeIdGenerator, 1.0.0-rc12"
                    
#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.0-rc12
                    
#: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.0-rc12&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=StrongTypeIdGenerator&version=1.0.0-rc12&prerelease
                    
Install as a Cake Tool

StrongTypeIdGenerator

StrongTypeIdGenerator is a source generator that helps you create strongly-typed identifiers in your C# projects. It supports Guid, string-based identifiers, and combined.

Installation

Add nuget package https://www.nuget.org/packages/StrongTypeIdGenerator.

<PackageReference Include="StrongTypeIdGenerator" />

Getting Started

Define your ID type:

[StringId]
partial class FooId
{
}

The generator will produce:

[System.ComponentModel.TypeConverter(typeof(FooIdConverter))]
partial class FooId : ITypedIdentifier<FooId, string>
{
    public FooId(string value)
    {
        if (value is null)
        {
            throw new ArgumentNullException(nameof(value));
        }

        Value = value;
    }

    public static FooId Unspecified { get; } = new FooId(string.Empty);

    public string Value { get; }

    [return: NotNullIfNotNull(nameof(value))]
    public static implicit operator FooId?(string? value) { ... }

    [return: NotNullIfNotNull(nameof(value))]
    public static implicit operator string?(FooId? value) { ... }

    public bool Equals(FooId? other) { ... }

    public int CompareTo(FooId? other) { ... }

    public override bool Equals(object? obj) { ... }

    public override int GetHashCode() => Value.GetHashCode();

    public override string ToString() => Value;

    public string ToString(string? format, IFormatProvider? formatProvider) => Value;

    public static bool operator ==(FooId left, FooId right) { ... }

    public static bool operator !=(FooId left, FooId right) { ... }

    public static bool operator <(FooId left, FooId right) { ... }

    public static bool operator <=(FooId left, FooId right) { ... }

    public static bool operator >(FooId left, FooId right) { ... }

    public static bool operator >=(FooId left, FooId right) { ... }

    private sealed partial class FooIdConverter : TypeToStringConverter<FooId>
    {
        protected override string? InternalConvertToString(FooId value)
        {
            return value.Value;
        }

        protected override FooId? InternalConvertFromString(string value)
        {
            return new FooId(value);
        }
    }
}

Design decisions

There are a few opinionated principles regarding what strong type identifiers should and should not do, which may be different from similar libraries and are reasons this project existence.

Idenifier type should be a reference type, not a value type.

Being able to protect invariants and not allow instance of id with invalid value to exist, is chosen over avoiding additional object allocation.

Ability to define custom id precondition checks.

If Id class defines method static string CheckValue(string value), that method would be called from generated constructor.

[StringId]
partial class FooId
{
    private static string CheckValue(string value)
    {
        if (value.Length > 10)
        {
            throw new ArgumentException("Value is too long", nameof(value));
        }

        return value;
    }
}

No dependency on serialization libraries.

StrongTypeIdGenerator only defines System.ComponentModel.TypeConverter that can convert to and from string. No System.Text.Json or Newtonsoft.Json or EF Core converters defined.

This way Id types can be defined in netstandard2.0 libraries with no additional dependencies.

The proposed way to use generated Id classes in serialization e.g. with System.Text.Json is to provide custom JsonConverterFactory to serializer, that would utilize generated TypeConverter.

Usage

Define ID types easily:

[StringId]
public sealed partial class FooId
{
}

[GuidId]
public sealed partial class BarId
{
}

Or use [CombinedId] to create a composite identifier:

[CombinedId(typeof(BarId), "BarId", typeof(string), "StringId", typeof(Guid), "GuidId", typeof(int), "IntId")]
public partial class FooBarCombinedId
{
}

Combined indentifier supports other StrongTypeId generated types and primitives e.g. string, int, Guid.

Custom Validation

You can add custom validation logic to your ID types by defining a CheckValue method. The method will be called from the constructor and can validate (throw exceptions) or modify the input value.

String and Guid IDs

For StringId and GuidId, define a method with this signature:

private static string CheckValue(string value)
private static Guid CheckValue(Guid value)
{
    // Validation logic here
    return value;
}
Combined IDs

For CombinedId, the CheckValue method should accept individual parameters matching the constructor and return a tuple with the validated values:

private static (BarId, string, Guid, int) CheckValue(BarId barId, string stringId, Guid guidId, int intId)
{
    // Validation logic here
    return (barId, stringId, guidId, intId);
}

The CheckValue method is called from the constructor and its result is used to set the properties of the ID class.

Custom Value Property Name

You can customize the name of the property that holds the identifier's value by setting the ValuePropertyName property on the attribute:

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

And generated class fill get 'Uuid' property instead of 'Value':

public sealed partial class BarId
{
  ...
  public Guid Uuid { get; }
  ...
}

When using a custom property name, the generated class will still implement the ITypedIdentifier<T> interface by providing an explicit implementation for the Value property that forwards to your custom property.

Acknowledgements

Inspired by a great library https://github.com/andrewlock/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.0-rc9 116 5/24/2025
1.0.0-rc8 121 5/19/2025
1.0.0-rc7 120 5/19/2025
1.0.0-rc6 472 5/5/2025
1.0.0-rc5 388 10/8/2024
1.0.0-rc4 82 7/8/2024
1.0.0-rc3 84 6/22/2024
1.0.0-rc2 77 6/15/2024
1.0.0-rc12 207 6/22/2025
1.0.0-rc11 106 5/31/2025
1.0.0-rc10 45 5/31/2025
1.0.0-rc1 79 6/15/2024
1.0.0-rc0 78 6/15/2024