ZaNetDev.MassTransit.MessageHelpers.CodeEnumeration 1.0.0

dotnet add package ZaNetDev.MassTransit.MessageHelpers.CodeEnumeration --version 1.0.0
NuGet\Install-Package ZaNetDev.MassTransit.MessageHelpers.CodeEnumeration -Version 1.0.0
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="ZaNetDev.MassTransit.MessageHelpers.CodeEnumeration" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ZaNetDev.MassTransit.MessageHelpers.CodeEnumeration --version 1.0.0
#r "nuget: ZaNetDev.MassTransit.MessageHelpers.CodeEnumeration, 1.0.0"
#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 ZaNetDev.MassTransit.MessageHelpers.CodeEnumeration as a Cake Addin
#addin nuget:?package=ZaNetDev.MassTransit.MessageHelpers.CodeEnumeration&version=1.0.0

// Install ZaNetDev.MassTransit.MessageHelpers.CodeEnumeration as a Cake Tool
#tool nuget:?package=ZaNetDev.MassTransit.MessageHelpers.CodeEnumeration&version=1.0.0

CodeEnumeration

This simple utility base class is intended for use with MassTransit. Its purpose is to allow the addition of a field to a message contract (interface) that will be constrained to a set of predefined values.

One could use a regular enum however it is serialised as an integer. This is not always desired, and there is also the danger of having values shift if the order is changed.

The "enumeration class" is an idea I first encountered many years ago when I first began following the work of Steve Smith (Ardalis), and the code included here is largely based off of that initial idea.

Documentation

By way of example, below is a possible interface that could be published or sent as a message from MassTransit

public interface SubmitOrder
{
    Guid OrderId { get; }
    DateTimeOffset OrderDateTime { get; }
    MyStatusEnum Status { get; }
}

In the interface above you will note the last field (Status) is of type MyStatusEnum. This is defined below and is the Message Enumeration Class. As can be seen values are specified by declaring static fields with the same return type as the enclosing one. Note also the JsonConverter attribute being applied to the class. This is what allows the class to be serialised as its own code value, instead of as an object.

[JsonConverter(typeof(CodeEnumerationJsonConverter<MyStatusEnum>))]
public class MyStatusEnum : CodeEnumeration<MyStatusEnum>
{
    public static MyStatusEnum Started = new MyStatusEnum("st");
    public static MyStatusEnum Finished = new MyStatusEnum("fn");

    public MyStatusEnum(string code) : base(code: code)
    {
    }
}

If you publish or send a message for the "SubmitOrder" interface:

await bus.Send<SubmitOrder>(
    new
    {
        OrderId = orderId,
        OrderDateTime = DateTimeOffset.Now,
        Status = MyStatusEnum.Started
    }
    );

This would be the resultant json message payload

{
  "message": {
    "orderId": "7fb80000-5dc3-0015-0c2d-08d9334eacb9",
    "orderDateTime": "2021-06-19T20:18:44.7920189+02:00",
    "status": "st"
  }
}

Take note that the "status" field is simply a string "st" which is the code declared in the MyStatusEnum class.

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
1.0.0 318 7/2/2021