Skip to content

Source Generators ​

ALPHA - FLOW ACTION GENERATOR ONLY

Switchboard is currently in preview (v0.1.0-preview.62). Currently, only the FlowActionGenerator is implemented. Other generators listed in older documentation (DynamoDB, Lambda) do not yet exist. See the roadmap for planned features.

Switchboard uses C# source generators to automatically create flow implementations from your attribute-decorated classes. This eliminates boilerplate code and ensures consistency across your contact center infrastructure.

What Are Source Generators? ​

Source generators are compile-time code generators that analyze your code and produce additional C# source files. They run as part of the compiler pipeline, so generated code is available immediately with full IntelliSense support.

Your Code (Attributes)
        ↓
Roslyn Compiler
        ↓
Source Generator Analysis
        ↓
Generated Code (automatic)
        ↓
Final Compilation

Current: FlowActionGenerator ​

What It Does ​

The FlowActionGenerator analyzes [FlowAction]-decorated classes and generates extension methods on IFlowBuilder:

Input: Your Flow Action

csharp
using Switchboard.SourceGenerators.Attributes;

[FlowAction("PlayMessage")]
public class PlayMessageAction : FlowAction
{
    [Parameter]
    public string Text { get; set; }

    [Parameter]
    public string Voice { get; set; } = "Joanna";
}

Output: Generated Extension Method

csharp
// Generated by FlowActionGenerator
public static IFlowBuilder PlayMessage(
    this IFlowBuilder builder,
    string text,
    string voice = "Joanna")
{
    var action = new PlayMessageAction
    {
        Identifier = $"play-message-{Guid.NewGuid().ToString()[..8]}",
        Text = text,
        Voice = voice
    };
    return builder.AddFlowAction(action);
}

Usage:

csharp
var flow = new FlowBuilder()
    .SetName("WelcomeFlow")
    .SetType(FlowType.ContactFlow)
    .PlayMessage("Welcome to our support center", "Matthew")
    .TransferToQueue("GeneralSupport")
    .Build();

Available Attributes ​

[FlowAction] ​

Marks a class for extension method generation.

csharp
[FlowAction("PlayMessage")]
public class PlayMessageAction : FlowAction { }

[Parameter] ​

Marks a property as a method parameter. Only marked properties appear in the generated method signature.

csharp
[FlowAction("PlayMessage")]
public class PlayMessageAction : FlowAction
{
    [Parameter]
    public string Text { get; set; }  // In signature

    public string InternalId { get; set; }  // Not in signature
}

[Transition] ​

Marks a property for transition routing information.

csharp
[FlowAction("Router")]
public class RouterAction : FlowAction
{
    [Transition]
    public Dictionary<string, string> Routes { get; set; }
}

Best Practices ​

  1. Inherit from FlowAction — All action classes must extend FlowAction
  2. Use [Parameter] selectively — Only mark properties you want in the generated method
  3. Provide defaults — Use default values for optional parameters
  4. Use clear names — Name your action classes descriptively

Future: More Generators (Post-v1.0) ​

Planned generators for future releases:

  • FlowDefinitionGenerator - For [ContactFlow] attribute-based flows
  • DynamoDbSchemaGenerator - For [DynamoDbTable] configuration models
  • LambdaHandlerGenerator - For [LambdaHandler] interface-based handlers

For now, use the fluent API for all flow and resource definitions.


See Also ​

Preview release - Licensing terms TBD before 1.0