Meziantou.Framework.StronglyTypedId 1.0.32

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Meziantou.Framework.StronglyTypedId --version 1.0.32
NuGet\Install-Package Meziantou.Framework.StronglyTypedId -Version 1.0.32
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="Meziantou.Framework.StronglyTypedId" Version="1.0.32">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Meziantou.Framework.StronglyTypedId --version 1.0.32
#r "nuget: Meziantou.Framework.StronglyTypedId, 1.0.32"
#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 Meziantou.Framework.StronglyTypedId as a Cake Addin
#addin nuget:?package=Meziantou.Framework.StronglyTypedId&version=1.0.32

// Install Meziantou.Framework.StronglyTypedId as a Cake Tool
#tool nuget:?package=Meziantou.Framework.StronglyTypedId&version=1.0.32

Meziantou.Framework.StronglyTypedId

The source generator generates constructors, properties, equality members, common interfaces, converters for multiple serializers

[StronglyTypedId(typeof(int))]
public partial struct ProjectId { }

StronglyTypedId generates the following code:

[System.ComponentModel.TypeConverterAttribute(typeof(ProjectIdTypeConverter))]
[System.Text.Json.Serialization.JsonConverterAttribute(typeof(ProjectIdJsonConverter))]
[Newtonsoft.Json.JsonConverterAttribute(typeof(ProjectIdNewtonsoftJsonConverter))]
[MongoDB.Bson.Serialization.Attributes.BsonSerializerAttribute(typeof(ProjectIdMongoDBBsonSerializer))]
public partial struct ProjectId :
    System.IEquatable<ProjectId>,
    System.IParsable<ProjectId>,        // .NET 7+
    System.ISpanParsable<ProjectId>,    // .NET 7+
    IStronglyTypedId,                   // When Meziantou.Framework.StronglyTypedId.Interfaces is referenced
    IStronglyTypedId<ProjectId>,        // When Meziantou.Framework.StronglyTypedId.Interfaces is referenced
    IComparable, IComparable<ProjectId> // When at least one of the interface is explicitly defined by the user
{
    public int Value { get; }
    public string ValueAsString { get; } // Value formatted using InvariantCulture

    private ProjectId(int value);

    public static ProjectId FromInt32(int value);
    public static ProjectId Parse(string value);
    public static ProjectId Parse(ReadOnlySpan<char> value);
    public static bool TryParse(string value, out ProjectId result);
    public static bool TryParse(ReadOnlySpan<char> value, out ProjectId result);
    public override int GetHashCode();
    public override bool Equals(object? other);
    public bool Equals(ProjectId other);
    public static bool operator ==(ProjectId a, ProjectId b);
    public static bool operator !=(ProjectId a, ProjectId b);
    public override string ToString();

    private partial class CustomerIdTypeConverter : System.ComponentModel.TypeConverter
    {
        public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType);
        public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value);
        public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type destinationType);
        public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType);
    }

    // Generated only when System.Text.Json is accessible
    private partial class CustomerIdJsonConverter : System.Text.Json.Serialization.JsonConverter<ProjectId>
    {
        public override void Write(System.Text.Json.Utf8JsonWriter writer, ProjectId value, System.Text.Json.JsonSerializerOptions options);
        public override ProjectId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options);
    }

    // Generated only when Newtonsoft.Json is accessible
    private partial class CustomerIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
    {
        public override bool CanRead { get; }
        public override bool CanWrite { get; }
        public override bool CanConvert(System.Type type);
        public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object? value, Newtonsoft.Json.JsonSerializer serializer);
        public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object? existingValue, Newtonsoft.Json.JsonSerializer serializer);
    }

    // Generated only when MongoDB.Bson.Serialization.Serializers.SerializerBase is accessible
    private partial class ProjectIdMongoDBBsonSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase<ProjectId>
    {
        public override ProjectId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args);
        public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, ProjectId value);
    }
}

If the Meziantou.Framework.StronglyTypedId.Interfaces NuGet package is present, the generator will implements IStronglyTypedId and IStronglyTypedId<T>.

Supported types

  • System.Boolean
  • System.Byte
  • System.DateTime
  • System.DateTimeOffset
  • System.Decimal
  • System.Double
  • System.Guid
  • System.Half
  • System.Int16
  • System.Int32
  • System.Int64
  • System.Int128
  • System.Numerics.BigInteger
  • System.SByte
  • System.Single
  • System.String
  • System.Uint16
  • System.Uint32
  • System.Uint64
  • System.Uint128
  • MongoDB.Bson.ObjectId

Configuration

You can configure the code generation using the [StronglyTypedIdAttribute] attribute:

[StronglyTypedId(idType: typeof(long),
                 generateSystemTextJsonConverter: true,
                 generateNewtonsoftJsonConverter: true,
                 generateSystemComponentModelTypeConverter: true,
                 generateMongoDBBsonSerialization: true,
                 addCodeGeneratedAttribute: true
                 )]
public partial struct ProjectId { }

You can generate IComparable, IComparable<T> and comparison operators by adding one interface:

[StronglyTypedId(typeof(int))]
public partial struct ProjectId : IComparable { }

// Generated by the source generator
public partial struct ProjectId : IComparable<ProjectId>
{    
	public int CompareTo(object? other);
	public int CompareTo(ProjectId? other);
	public static bool operator <(ProjectId? left, ProjectId? right);
	public static bool operator <=(IdInt32Comparable? left, IdInt32Comparable? right);
	public static bool operator >(IdInt32Comparable? left, IdInt32Comparable? right);
	public static bool operator >=(IdInt32Comparable? left, IdInt32Comparable? right);
}

Additional resources

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • No dependencies.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Meziantou.Framework.StronglyTypedId:

Package Downloads
Allegro.Extensions.Identifiers.Abstractions The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

Contains strongly typed identifiers abstractions.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on Meziantou.Framework.StronglyTypedId:

Repository Stars
StefH/AnyOf
Use the AnyOf<TFirst, TSecond, ...> type to handle multiple defined types as input parameters or return values for methods.
Version Downloads Last updated
2.2.0 1,227 3/21/2024
2.1.0 2,705 11/26/2023
2.0.0 498 11/19/2023
1.0.35 1,257 11/15/2023
1.0.34 3,844 9/5/2023
1.0.33 608 8/7/2023
1.0.32 5,891 3/26/2023
1.0.31 361 3/21/2023
1.0.30 229 3/20/2023
1.0.29 478 2/18/2023
1.0.28 5,506 1/3/2023
1.0.27 447 1/2/2023
1.0.26 298 1/2/2023
1.0.25 291 1/1/2023
1.0.24 309 12/27/2022
1.0.23 278 12/26/2022
1.0.22 2,627 8/10/2022
1.0.21 2,789 4/29/2022
1.0.20 499 4/23/2022
1.0.19 5,432 2/23/2022
1.0.18 437 2/12/2022
1.0.17 738 11/10/2021
1.0.16 458 11/8/2021
1.0.15 3,199 7/14/2021
1.0.14 357 6/30/2021
1.0.13 330 6/10/2021
1.0.12 824 5/14/2021
1.0.11 320 5/14/2021
1.0.10 372 4/25/2021
1.0.9 323 4/22/2021
1.0.8 1,860 2/17/2021
1.0.7 322 2/16/2021
1.0.6 337 2/16/2021
1.0.5 347 2/15/2021
1.0.4 363 2/8/2021
1.0.3 332 2/4/2021
1.0.2 320 2/4/2021
1.0.1 299 2/3/2021
1.0.0 326 2/1/2021