JOS.Enumeration.SourceGenerator 5.1.7-beta-gde2fcc267f

This is a prerelease version of JOS.Enumeration.SourceGenerator.
dotnet add package JOS.Enumeration.SourceGenerator --version 5.1.7-beta-gde2fcc267f
                    
NuGet\Install-Package JOS.Enumeration.SourceGenerator -Version 5.1.7-beta-gde2fcc267f
                    
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="JOS.Enumeration.SourceGenerator" Version="5.1.7-beta-gde2fcc267f" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="JOS.Enumeration.SourceGenerator" Version="5.1.7-beta-gde2fcc267f" />
                    
Directory.Packages.props
<PackageReference Include="JOS.Enumeration.SourceGenerator" />
                    
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 JOS.Enumeration.SourceGenerator --version 5.1.7-beta-gde2fcc267f
                    
#r "nuget: JOS.Enumeration.SourceGenerator, 5.1.7-beta-gde2fcc267f"
                    
#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 JOS.Enumeration.SourceGenerator@5.1.7-beta-gde2fcc267f
                    
#: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=JOS.Enumeration.SourceGenerator&version=5.1.7-beta-gde2fcc267f&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=JOS.Enumeration.SourceGenerator&version=5.1.7-beta-gde2fcc267f&prerelease
                    
Install as a Cake Tool

JOS.Enumeration

Enumeration implementation with source generation support.

Installation

JOS.Enumeration

Contains the IEnumeration interface and a System.Text.Json JsonConverter. The JOS.Enumeration.SourceGenerator package contains the actual source generator.

Don't forget to install that one as well. 😃

dotnet add package JOS.Enumeration
dotnet add package JOS.Enumeration.SourceGenerator

JOS.Enumeration.Database.Dapper

Contains a custom TypeHandler to use with Dapper.

dotnet add package JOS.Enumeration.Database.Dapper

JOS.Enumeration.Database.EntityFrameworkCore

Contains ConfigureEnumeration extension method to allow usage with EntityFramework Core.

dotnet add package JOS.Enumeration.Database.EntityFrameworkCore

Usage

  • Create a new partial record or class
  • Implement the IEnumeration<T> interface
  • Add your Enumeration items
public partial record Hamburger : IEnumeration<Hamburger>
{
    public static readonly Hamburger Cheeseburger = new (1, "Cheeseburger");
    public static readonly Hamburger BigMac = new(2, "Big Mac");
    public static readonly Hamburger BigTasty = new(3, "Big Tasty");
}

The source generator will implement the following interface:

// Default implementation -> int as Value
public interface IEnumeration<T> : IEnumeration<int, T> where T : IEnumeration<T>
{
}

public interface IEnumeration<TValue, TType> where TValue : IConvertible
{
    TValue Value { get; }
    string Description { get; }
    static abstract IReadOnlySet<TType> GetAll();
    static abstract IEnumerable<TType> GetEnumerable();
    static abstract TType FromValue(TValue value);
    static abstract TType FromDescription(string description);
    static abstract TType FromDescription(ReadOnlySpan<char> description);
    static abstract Type ValueType { get; }
}

The following code will be generated:

[System.Diagnostics.DebuggerDisplay("{Description}")]
[System.CodeDom.Compiler.GeneratedCode("JOS.Enumeration.SourceGenerator", "4.1.11-beta+afeaa87a52")]
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public partial record Hamburger : IComparable<JOS.Enumerations.Hamburger>
{
    private static readonly IReadOnlySet<JOS.Enumerations.Hamburger> AllItems;
    static Hamburger()
    {
        AllItems = new HashSet<JOS.Enumerations.Hamburger>(3)
        {
            Cheeseburger,
            BigMac,
            BigTasty,
        }.ToFrozenSet();
    }

    private Hamburger(int value, string description)
    {
        Value = value;
        Description = description ?? throw new ArgumentNullException(nameof(description));
    }

    public int Value { get; }
    public string Description { get; }

    public static IReadOnlySet<JOS.Enumerations.Hamburger> GetAll()
    {
        return AllItems;
    }

    public static IEnumerable<JOS.Enumerations.Hamburger> GetEnumerable()
    {
        yield return Cheeseburger;
        yield return BigMac;
        yield return BigTasty;
    }

    public static JOS.Enumerations.Hamburger FromValue(int value)
    {
        return value switch
        {
            1 => Cheeseburger,
            2 => BigMac,
            3 => BigTasty,
            _ => throw new InvalidOperationException($"'{value}' is not a valid value in 'JOS.Enumerations.Hamburger'")};
    }

    public static JOS.Enumerations.Hamburger FromDescription(string description)
    {
        return description switch
        {
            "Cheeseburger" => Cheeseburger,
            "Big Mac" => BigMac,
            "Big Tasty" => BigTasty,
            _ => throw new InvalidOperationException($"'{description}' is not a valid description in 'JOS.Enumerations.Hamburger'")};
    }

    public static JOS.Enumerations.Hamburger FromDescription(ReadOnlySpan<char> description)
    {
        return description switch
        {
            "Cheeseburger" => Cheeseburger,
            "Big Mac" => BigMac,
            "Big Tasty" => BigTasty,
            _ => throw new InvalidOperationException($"'{description}' is not a valid description in 'JOS.Enumerations.Hamburger'")};
    }

    public static Type ValueType => typeof(int);

    public int CompareTo(JOS.Enumerations.Hamburger? other) => Value.CompareTo(other!.Value);
    public static implicit operator int (JOS.Enumerations.Hamburger item) => item.Value;
    public static implicit operator JOS.Enumerations.Hamburger(int value) => FromValue(value);
}

Features

  • Generic value
  • Generated IComparable<T> method.
  • Generated implicit operators (convert to/from int).
  • Generated optimized GetAll, FromValue and FromDescription methods.
  • System.Text.Json support
  • Database support (Dapper and EF Core).

Generic value

It's possible to use a generic value instead of the default int value by implementing the IEnumeration<TValue, TEnumeration> interface.

public partial record Car : IEnumeration<string, Car>
{
    public static readonly Car FerrariSpider = new("ferrari-spider", "Ferrari Spider");
    public static readonly Car TeslaModelY = new("tesla-model-y", "Tesla Model Y");
}

TValue has a IConvertible constraint.

The following types has been tested and are guaranteed to work:

  • int (default)
  • bool
  • decimal
  • long
  • string
  • uint
  • ulong

JSON

The package comes with a JsonConverterFactory. Example:

var jsonSerializerOptions = new JsonSerializerOptions
{
    Converters = { new EnumerationJsonConverterFactory() }
};

It supports the following scenarios:

  • Serializing to TValue
  • Deserializing from TValue

If you want any other behaviour, just create your own converter and register it.

Database

public class MyEntity
{
    public MyEntity(Guid id, Hamburger hamburger)
    {
        Id = id;
        Hamburger = hamburger;
    }

    public Guid Id { get; }
    public Hamburger Hamburger { get; }
}
Dapper
  • Register the TypeHandler: SqlMapper.AddTypeHandler(new EnumerationTypeHandler<Hamburger>())
  • Query like this:
var results = (await actConnection.QueryAsync<MyEntity>(
            "SELECT id, hamburger from my_entities WHERE id = @id", new {id = myEntity.Id})).ToList(); 
EF Core
  • Configure your DB Context
public DbSet<MyEntity> MyEntities { get; set; } = null!;

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.ApplyConfigurationsFromAssembly(typeof(JosEnumerationDbContext).Assembly);
} 
public class MyEntityEntityTypeConfiguration : IEntityTypeConfiguration<MyEntity>
{
    public void Configure(EntityTypeBuilder<MyEntity> builder)
    {
        builder.HasKey(x => x.Id);
        builder.Property(x => x.Hamburger).ConfigureEnumeration().IsRequired();
    }
}
  • Query:
var result = await myDbContext.MyEntities.FirstAsync(x => x.Id == myEntity.Id); 

Primitive Collections

Support for primitive collections in net8.0 can be configured like this:

EF Core
public void Configure(EntityTypeBuilder<MyEntity> builder)
{
    builder.ConfigureEnumeration<MyEntity, string, Car>(x => x.Cars);
}
Dapper
SqlMapper.AddTypeHandler(new EnumerationArrayTypeHandler<string, Car>());
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.  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.

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
5.1.7-beta-gde2fcc267f 270 9/15/2025
5.1.6-beta-ga85fcdf050 161 9/3/2025
5.1.5-beta-gfa8f8f193c 158 8/13/2025
5.1.4-beta-gc9c989d1aa 149 8/13/2025
5.1.3-beta-g1c37618027 152 8/12/2025
5.1.2-beta-g79f215376c 123 7/19/2025
4.1.25-beta-g20cbb0bcc9 192 5/26/2025
4.1.24-beta-g018bfdc50b 280 4/17/2025
4.1.23-beta-g5a7b3a5f87 178 3/31/2025
4.1.22-beta-gcef2667efe 153 3/27/2025
4.1.21-beta-g022c850152 152 3/27/2025
4.1.18-beta-g57430ba432 181 3/12/2025
4.1.17-beta-ge7baa0e508 117 12/30/2024
4.1.16-beta-ge5387dc634 107 12/30/2024
4.1.15-beta-g0e49b3ee76 111 12/30/2024
4.1.14-beta-gc2ddf8f752 102 12/2/2024
4.1.13-beta-g0df3582c0d 103 12/2/2024
4.1.12-beta-g97ccd40129 111 12/2/2024
4.1.9-beta-g6ffe24327b 114 12/2/2024
4.1.8-beta-g415eb19bd8 106 12/2/2024
4.1.7-beta-gdb2f2831d6 109 12/2/2024
4.1.6-beta-gef2489413a 223 12/2/2024
4.1.5-beta-g6b481c8235 126 11/28/2024
4.1.3-beta-g6c6fa8e7f6 117 11/28/2024
4.1.2-beta-g94dcadc76c 106 11/28/2024
4.0.2 1,608 11/28/2024
3.1.4-beta-g80cdfd1869 117 11/26/2024
3.1.3-beta-g5539fda0e8 130 11/15/2024
3.1.2-beta-gad970619af 114 11/15/2024
2.0.8-beta-g72852e0cc9 121 11/8/2024
2.0.7-beta-g1c736f7cbd 116 11/7/2024
2.0.6-beta-g33fdc165e1 153 11/7/2024
2.0.5-beta-g4df623a3fd 149 9/17/2024
2.0.4-beta-g41a385195d 134 9/17/2024
2.0.3-beta-g1b3ff0345c 130 9/17/2024
1.3.5-beta-ga3591d0784 187 1/8/2024
1.3.4-beta-g0f657b5945 189 12/15/2023
1.3.3-beta-g86c57c88de 180 12/6/2023
1.3.2-beta-g44904a2c86 156 12/6/2023
1.2.10 318 1/8/2024
1.2.9 235 12/6/2023
1.2.8-beta-g7f4afbc7bc 157 12/6/2023
1.2.7-beta-g8c3c1bea8d 175 12/5/2023
1.2.6-beta-gaceb5947a0 173 12/3/2023
1.2.5-beta-gd88393f87f 172 11/21/2023
1.2.4-beta-gcf63299055 156 11/14/2023
1.2.3-beta-g17d841819f 156 11/14/2023
1.2.2-beta-g48b6b63682 168 11/14/2023
1.1.37 288 11/14/2023
1.1.36-beta-g300cca1b1c 159 11/14/2023
1.1.34-beta-gbd8240dec5 177 11/4/2023
1.1.19-beta-g0616c54bd7 258 7/14/2023
1.1.18-beta-g196c9ec97b 199 7/14/2023
1.1.16-beta-g9ebac3a201 196 7/14/2023
1.1.15-beta-g6aa862b5fa 202 7/14/2023
1.1.14-beta-gd3f3eff596 207 7/14/2023
1.1.13-beta-gaae338e581 207 7/14/2023
1.1.11-beta-gedc10e9f0b 227 7/11/2023
1.1.10-beta-g15d758679f 198 7/11/2023
1.1.9-beta-ga8f5b4d09c 206 7/11/2023
1.1.8-beta-gbe35470ba8 207 7/11/2023
1.1.7-beta-g4edda95a99 217 6/20/2023
1.1.6-beta-gad8ab8816a 182 6/20/2023
1.1.5-beta-g02ae57f212 209 6/19/2023
1.1.4-beta-g2b6e2e274f 213 6/18/2023
1.1.3-beta-gfef378b783 221 6/18/2023
1.1.2-beta-gb3750f4659 207 6/17/2023
1.0.13-beta-g4bac5970df 217 6/17/2023
1.0.12-beta-g5e554e74de 206 6/17/2023
1.0.11-beta-ge0c0ac2086 283 6/17/2023 1.0.11-beta-ge0c0ac2086 is deprecated.
1.0.10-beta-gcbaa073338 284 6/17/2023 1.0.10-beta-gcbaa073338 is deprecated.
1.0.9-beta-g9bac00482d 280 6/17/2023 1.0.9-beta-g9bac00482d is deprecated.
1.0.8-beta-gec072ace6a 296 6/17/2023 1.0.8-beta-gec072ace6a is deprecated.
1.0.6-beta-g3f377c1cfc 280 6/17/2023 1.0.6-beta-g3f377c1cfc is deprecated.
1.0.4-beta-g35471cdafc 283 6/17/2023 1.0.4-beta-g35471cdafc is deprecated.