Enum.Ext.EFCore
1.1.0
See the version list below for details.
dotnet add package Enum.Ext.EFCore --version 1.1.0
NuGet\Install-Package Enum.Ext.EFCore -Version 1.1.0
<PackageReference Include="Enum.Ext.EFCore" Version="1.1.0" />
paket add Enum.Ext.EFCore --version 1.1.0
#r "nuget: Enum.Ext.EFCore, 1.1.0"
// Install Enum.Ext.EFCore as a Cake Addin #addin nuget:?package=Enum.Ext.EFCore&version=1.1.0 // Install Enum.Ext.EFCore as a Cake Tool #tool nuget:?package=Enum.Ext.EFCore&version=1.1.0
Enum.Ext
Enum.Ext provides a TypeSafeEnum
that has a bunch of advantages compared to the normal .NET Enum
value type.
You can store additional information directly with the enum and later query an enum based on that information, which you stored with it. We also offer various extension packages, that ensure compatibility with other areas of the .NET environment, as well as other known extensions.
Installation
https://www.nuget.org/packages/Enum.Ext/
Enum.Ext
can be installed using the following command via the NuGet package manager console:
PM> Install-Package Enum.Ext
If you are planning to use Enum.Ext
with Entity Framework Core, you should also install Enum.Ext.EFCore
https://www.nuget.org/packages/Enum.Ext.EFCore/
PM> Install-Package Enum.Ext.EFCore
List of all packages that we currently offer:
PM> Install-Package Enum.Ext
PM> Install-Package Enum.Ext.EFCore
PM> Install-Package Enum.Ext.AutoFixture
PM> Install-Package Enum.Ext.NewtonsoftJson
PM> Install-Package Enum.Ext.SystemTextJson
How to use
Simply inherit your class from TypeSafeEnum
or TypeSafeNameEnum
and adjust everything to your needs.
Want a weekday enum with a special string representation for each day?
public sealed class Weekday : TypeSafeNameEnum<Weekday, int>
{
public static readonly Weekday Monday = new Weekday(1, "--Monday--");
public static readonly Weekday Tuesday = new Weekday(2, "--Tuesday--");
public static readonly Weekday Wednesday = new Weekday(3, "--Wednesday--");
....
private Weekday(int id, string name) : base(id, name)
{
}
}
use it just like the native one
var day = Weekday.Monday;
// Assigns Tuesday
day = (Weekday)2;
and access the additional information easily
var day = Weekday.Monday;
// Prints out '--Monday--'
Console.WriteLine(day.Name);
Usage with a switch statment
var day = Weekday.Monday;
switch (day)
{
case var _ when day == Weekday.Monday:
// Do stuff
break;
case var _ when day == Weekday.Tuesday:
// Do Stuff
break;
}
JSON Conversion
We currently support two JSON frameworks:
To use the build-in converters you can either annotate your enum classes directly
// with Newtonsoft.Json
[JsonConverter(typeof(JsonTypeSafeEnumConverter))]
public sealed class Weekday : TypeSafeNameEnum<Weekday, int>
{
...
}
// with System.Text.Json
[JsonConverter(typeof(JsonTypeSafeEnumConverter<Weekday, int>))]
public sealed class Weekday : TypeSafeNameEnum<Weekday, int>
{
...
}
or add them globally (e.g. in ASP.NET Core)
// with Newtonsoft.Json
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers().AddNewtonsoftJson(opt =>
{
opt.SerializerSettings.Converters.Add(new JsonTypeSafeEnumConverter());
});
...
}
// with System.Text.Json
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers().AddJsonOptions(opt =>
{
opt.JsonSerializerOptions.Converters.Add(new JsonTypeSafeEnumConverterFactory());
});
...
}
EF Core configuration
Just add the following line at the end of the OnModelCreating
method in your DbContext
class and you are ready to go
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
...
modelBuilder.ConfigureEnumExt();
}
Usage with AutoFixture
var fixture = new Fixture();
fixture.WithEnumExt();
var weekday = fixture.Create<Weekday>();
Enum.Ext in action
Here you find some examples how you could use the extension.
A fixed price that is valid for a certain time period
public sealed class YearlyPrice : TypeSafeEnum<YearlyPrice, int>
{
public decimal Price { get; private set; }
public DateTime ValidFrom { get; private set; }
public DateTime ValidTo { get; private set; }
public static readonly YearlyPrice Price_2018 =
new YearlyPrice(1, 15.99m, new DateTime(2018, 1, 1), new DateTime(2018, 12, 31));
public static readonly YearlyPrice Price_2019 =
new YearlyPrice(2, 16.99m, new DateTime(2019, 1, 1), new DateTime(2019, 12, 31));
private YearlyPrice(int id, decimal price, DateTime validFrom, DateTime validTo) : base(id)
{
ValidFrom = validFrom;
ValidTo = validTo;
Price = price;
}
public static YearlyPrice GetPriceByDate(DateTime date)
{
// The List property holds all elements declared above
return List.FirstOrDefault(x => x.ValidFrom <= date && date <= x.ValidTo);
}
}
Get the according enum for a given date
DateTime date = new DateTime(2018, 5, 3);
// Returns YearlyPrice.Price_2018
var price = YearlyPrice.GetPriceByDate(date);
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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 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. |
-
net6.0
- Enum.Ext (>= 1.0.7)
- Microsoft.EntityFrameworkCore (>= 6.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.