Enums
Learn how to define C# enums that are automatically generated as TypeScript enums for your Angular frontend.
Overview
Spiderly lets you define enums once in C# and automatically generates matching TypeScript enums for your Angular frontend. This keeps backend and frontend enum definitions in sync — no manual duplication or copy-paste errors.
How It Works
NgEnumsGenerator is a Roslyn source generator that runs at build time (when you build the .Business project). It scans for two kinds of types in namespaces ending with .Enums:
enumdeclarations → generated as numeric TypeScript enums (explicit values preserved)static classdeclarations → generated as string TypeScript enums (property names become string values)
All output is consolidated into a single file:
Frontend\src\app\business\enums\enums.generated.tsEnums are sorted alphabetically by name in the generated file.
Do not edit enums.generated.ts — your changes will be overwritten on the next build. To add
custom TypeScript enums, create a separate file alongside the generated one.
See Architecture for the full list of generators, and spiderly.json Configuration for disabling the generator.
Defining an Enum
- Create a file in
Backend\{YourAppName}.Business\Enums\ - Use a block namespace ending with
.Enums - Define a standard C# enum
C# input:
// File: Backend\YourAppName.Business\Enums\OrderStatusCodes.cs
namespace YourAppName.Business.Enums
{
public enum OrderStatusCodes
{
PendingPayment = 1,
Pending = 2,
Processing = 3,
Shipped = 4,
Delivered = 5,
Cancelled = 6
}
}Generated TypeScript output:
// File: Frontend\src\app\business\enums\enums.generated.ts (auto-generated)
export enum OrderStatusCodes {
PendingPayment = 1,
Pending = 2,
Processing = 3,
Shipped = 4,
Delivered = 5,
Cancelled = 6,
}You must use block namespace syntax (namespace X.Y.Enums {'{'} ... {'}'}), not file-scoped
(namespace X.Y.Enums;). The source generator relies on the syntax tree structure of block
namespaces to discover enums.
Naming Convention
Spiderly follows the ...Codes naming convention for enums (e.g., OrderStatusCodes, PaymentMethodCodes, CartStatusCodes). This convention makes it clear that the type represents a fixed set of coded values.
Class-Based Enums
Static classes in the .Enums namespace are also picked up by the generator and emitted as string enums. This is used internally for PermissionCodes — a partial class where Spiderly auto-generates CRUD permission codes for every entity.
C# input:
// File: Backend\YourAppName.Business\Enums\PermissionCodes.cs
namespace YourAppName.Business.Enums
{
public static partial class PermissionCodes
{
}
}The class itself is empty — Spiderly auto-generates Read/Update/Insert/Delete permission codes for every entity in your project. You can add custom permission codes by adding public static string properties to the partial class.
Generated TypeScript output:
// File: Frontend\src\app\business\enums\enums.generated.ts (auto-generated)
export enum PermissionCodes {
ReadBanner = "ReadBanner",
UpdateBanner = "UpdateBanner",
InsertBanner = "InsertBanner",
DeleteBanner = "DeleteBanner",
// ... Read/Update/Insert/Delete for every entity
}See Backend Customization for how to extend PermissionCodes with custom permissions.