StrongTypeIdGenerator.SourceGenerator 1.0.0-rc3

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

// Install StrongTypeIdGenerator.SourceGenerator as a Cake Tool
#tool nuget:?package=StrongTypeIdGenerator.SourceGenerator&version=1.0.0-rc3&prerelease                

StrongTypeIdGenerator

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

Define this:

[StringId]
partial class FooId
{
}

and get this generated:

[System.ComponentModel.TypeConverter(typeof(FooIdConverter))]
partial class FooId : IEquatable<FooId>, IComparable<FooId>, IFormattable
{
    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; }

    public static implicit operator FooId(string 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 exist.

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.

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.

Ability to define custom id precondition checks.

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

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

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.

Installation

Add nuget package https://www.nuget.org/packages/StrongTypeIdGenerator. Make sure to specify PrivateAssets="all":

<PackageReference Include="StrongTypeIdGenerator" Version="1.0.0" PrivateAssets="all" />

Usage

Just define your Id type like that

[StringId]
public sealed partial class FooId
{
}

[GuidId]
public sealed partial class BarId
{
}

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 was computed.  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 was computed.  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. 
.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.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on StrongTypeIdGenerator.SourceGenerator:

Package Downloads
StrongTypeIdGenerator

StrongTypeIdGenerator is a source generator that helps you create strongly-typed identifiers in your C# projects. It supports Guid and string-based identifiers. This is a meta-package that includes all necessary components to use StrongTypeIdGenerator in your project.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0-rc4 52 7/8/2024
1.0.0-rc3 58 6/22/2024
1.0.0-rc2 55 6/15/2024
1.0.0-rc1 52 6/15/2024