The Enumeration class in the previous article came from a short enum declaration. There was no hand-written class, lookup collection, or conversion switch to maintain beside it. One and done.
The approach uses an attribute to tell a code generator which Enumeration class to create. Here is how to use it and what to expect from the generated conversion methods.
Define the specification
Here is the same payment-status declaration:
using Ifx.Generators.Attributes;
namespace Example;
[GenerateEnumeration("PaymentStatus", nameof(PaymentStatusSpecification.UNKNOWN))]
public enum PaymentStatusSpecification
{
UNKNOWN = 0,
PENDING = 10,
AUTHORIZED = 15,
SETTLED = 20,
FAILED = 30
}
Usage
GenerateEnumeration is an Ifx attribute. The consuming project needs three things: the generator available to the compiler, the attribute available to the source, and the Enumeration base type available to the generated class. The generator implementation lives in Ifx.Roslyn.Generators; the attribute's namespace is Ifx.Generators.Attributes.
The first argument names the output class. The second selects the fallback member. The output namespace defaults to the enum's namespace, and the base type defaults to Ifx.Primitives.Enumeration. The attribute also permits explicit Namespace, EnumerationNamespace, and EnumerationTypeName settings.
The Specification suffix separates the source enum from the generated class. Components sharing the same object definition can still use the enum as a common vocabulary. Enumeration adds the richer representation and generated conversion around those choices.
Inspect the contract once
The generated class includes instances equivalent to these:
public static readonly PaymentStatus PENDING = new(10, nameof(PENDING));
public static readonly PaymentStatus SETTLED = new(20, nameof(SETTLED));
It also supplies a private constructor, an All collection, FromValue(int), and FromName(string?). Its equality implementation compares identifiers within the generated concrete type, with a matching hash code. The base class exposes Id, Name, and a ToString() implementation that returns Name.
The generated class is sealed and is not partial, so another declaration cannot add members to it. Additional behavior needs a separate policy, a wrapper, or a change to the generator.
For a known value, the conversion requires only a call:
var status = PaymentStatus.FromName("SETTLED");
var databaseValue = status.Name; // "SETTLED"
The generator removes the repeated implementation. The application still owns the decision to accept that value.
Choose the fallback deliberately
The current implementation matches names exactly and returns the configured default when lookup fails:
PaymentStatus.FromValue(20); // SETTLED
PaymentStatus.FromValue(999); // UNKNOWN
PaymentStatus.FromName("SETTLED"); // SETTLED
PaymentStatus.FromName("settled"); // UNKNOWN
PaymentStatus.FromName(null); // UNKNOWN
UNKNOWN gives callers a result they can check instead of silently receiving a valid payment state. In this example, the database excludes UNKNOWN as a normal state. Retain the original input when reporting a failure; the fallback alone cannot explain which value was rejected.
Without an explicit default, the generator uses the first member. An invalid configured default also falls back to the first member. In either case, reordering the specification can change failed-conversion behavior even when its numeric identifiers stay fixed. A valid, explicitly selected fallback avoids that dependence on order.
I prefer an explicit fallback value such as UNKNOWN, checked at the database boundary. A build diagnostic for an invalid configured default would make configuration mistakes easier to catch. That is a proposed improvement; this version does not report it.
Keep generation separate from persistence policy
My preference remains explicit string values in database rows. This generator derives those strings from member names, so the names become part of the persistence contract when an application stores Name. A member rename must be reviewed as a data compatibility change.
It does not provide a separate string-code setting or a generated TryFromCode method. Independent display labels belong outside the persistent name, and a business need for separately versioned storage codes would require an additional mapping or an extension to the generator.
The generator reports diagnostic GEN002 when a numeric member cannot fit in System.Int32. Duplicate identifiers, fallback selection, and compatibility with historical data still deserve review. A source generator can make these choices consistent without making every choice correct for every application.
The useful checks compile the output and exercise known values, unknown values, case differences, null input, equality, and default selection. For persisted names, include data written by an earlier version and check that a declaration change preserves its meaning.
The practical result is a short specification instead of repeated conversion code. That makes the implementation easier to maintain while keeping the decisions that affect stored meaning visible for review.