Source Generators
ALPHA RELEASE
Switchboard is currently in preview (v0.1.0-preview.17). APIs may change between releases.
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 CompilationHow Switchboard Uses Generators
Input: Your Attributed Class
[ContactFlow("WelcomeFlow")]
public partial class WelcomeFlow : FlowDefinitionBase
{
[Message("Welcome to our contact center")]
public partial void Welcome();
[TransferToQueue("GeneralSupport")]
public partial void Transfer();
}Output: Generated Implementation
The source generator automatically creates:
// WelcomeFlow.g.cs (generated)
public partial class WelcomeFlow
{
private readonly List<IAction> _actions = new();
public partial void Welcome()
{
_actions.Add(new MessageAction
{
Identifier = "Welcome-001",
Text = "Welcome to our contact center",
Transitions = new Transitions { NextAction = "Transfer-001" }
});
}
public partial void Transfer()
{
_actions.Add(new TransferToQueueAction
{
Identifier = "Transfer-001",
QueueName = "GeneralSupport"
});
}
public override CfnContactFlow BuildCdkConstruct(Construct scope)
{
// Execute methods to populate actions
Welcome();
Transfer();
return new CfnContactFlow(scope, "WelcomeFlow", new CfnContactFlowProps
{
Name = "WelcomeFlow",
Type = "CONTACT_FLOW",
Content = SerializeActions(_actions),
InstanceArn = GetInstanceArn(scope)
});
}
}Built-In Generators
Switchboard includes several source generators:
1. FlowDefinitionGenerator
Generates flow implementations from [ContactFlow] attributes.
See: Reference: Source Generators for complete details.
2. DynamoDbSchemaGenerator
Generates DynamoDB table definitions from configuration models.
3. LambdaHandlerGenerator
Creates Lambda function handlers for dynamic configuration.
Viewing Generated Code
Visual Studio
- Expand project in Solution Explorer
- Navigate to Dependencies → Analyzers → NickSoftware.Switchboard.SourceGenerators
- View generated
.g.csfiles
Rider
- Right-click project → Navigate To → Generated Files
- Browse generated source files
Command Line
Generated files are in:
obj/Debug/net10.0/generated/NickSoftware.Switchboard.SourceGenerators/Benefits
- ✅ Less boilerplate - Write declarations, get implementations
- ✅ Compile-time generation - No runtime overhead
- ✅ Type safety - Generated code is strongly typed
- ✅ IntelliSense support - Full IDE integration
- ✅ Consistency - Same patterns across all flows
Next Steps
- Reference: Source Generators - Complete generator documentation
- Roslyn Analyzers - Compile-time validation
- Attribute-Based Flows - Using attributes
- Architecture - How generators fit in
Related Resources
- Attributes - Declarative configuration
- Flow Basics - Building flows