LinkDotNet.EnumValueObject 1.0.6

The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package LinkDotNet.EnumValueObject --version 1.0.6
NuGet\Install-Package LinkDotNet.EnumValueObject -Version 1.0.6
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="LinkDotNet.EnumValueObject" Version="1.0.6" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add LinkDotNet.EnumValueObject --version 1.0.6
#r "nuget: LinkDotNet.EnumValueObject, 1.0.6"
#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 LinkDotNet.EnumValueObject as a Cake Addin
#addin nuget:?package=LinkDotNet.EnumValueObject&version=1.0.6

// Install LinkDotNet.EnumValueObject as a Cake Tool
#tool nuget:?package=LinkDotNet.EnumValueObject&version=1.0.6

EnumValueObject

The EnumValueObject is based on the ValueObject class from here (credits to: vkhorikov). This library lets you create strong typed enums, which can only be in a valid state. Furthermore it allows you to define methods on top of your enum and computed property. See the sample section or the quick start tutorial below.

Installation

Available on Nuget downloads

PM> Install-Package LinkDotNet.EnumValueObject

Extensions

Package Download Description
LinkDotNet.EnumValueObject.Converter.JsonConverter Nuget downloads Use EnumValueObjects with System.Text.Json.JsonSerializer

Type safety from beginning to end

With this EnumValueObject you can only create a valid state of the object itself. Imagine this small EnumValueObject

public class Language : EnumValueObject<Language>
{
    public static readonly Language German = new Language("de");

    public static readonly Language English = new Language("en");

    protected Language(string key) : base(key)
    {
    }
}

To create an EnumValueObject you have to call the static Create method. Define your constructor always protected or private so that the consumer has to take the Create method.

var languageResult = Language.Create("en");

This will return you a Result object. You can check with languageResult.IsFailure if we had an error (yes no exceptions). If not you can just access the EnumValueObject with languageResult.Value.

One downside of the regular enum struct is that you can just give it arbitrary values. The following example will not result in any error:

public enum Language
{
    English = 0,
    German = 1,
}
...
var language = (Language)500;

With the EnumValueObject you will not receive a valid Result.

Comparable to its key

If you have a valid EnumValueObject you can compare it in two ways:

  1. Checking against the EnumValueObject itself
if (language == Language.English)
{
    ...
}
  1. Checking against the key
if (language == "en")
{
    ...
}

The second option can come in handy if you need to test against direct user input.

Extend your Enums

No need for extension methods. Because it is a normal class you can just define functions and also have properties which will make your life easier. Remember the Language example. Lets extend this a bit.

public class Language : EnumValueObject<Language>
{
    public static readonly Language German = new Language("de", "€");

    public static readonly Language English = new Language("en", "£");

    public string Currency { get; }

    protected Language(string key, string currency) : base(key)
    {
        Currency = currency;
    }
}

Once a valid EnumValueObject is created, you can just access those properties and work with it.

var language = Language.Create("de").Result;
var currency = language.Currency; // €

Database friendly

It is enough to store the key to the database. When the EnumValueObject is populated it will automatically set all the dependent properties (like the currency in the last example).

JSON Serializer Support

See the samples how to use EnumValueObjects with the System.Text.Json.JsonSerializer without any problems. With the extension you don't have to provide a public parameterless constructor. Just add the attribute to your property and you are done.

public class MyDto
{
    [JsonConverter(typeof(EnumValueObjectJsonConverter<MyEnumValueObject>))]
    public MyEnumValueObject MyEnumValueObject { get; set; }
}
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.

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