Skip to content

Generators

The Deepstaging.Roslyn.Generators namespace provides utilities for building Roslyn incremental source generators with less boilerplate.

Components

Type Description
ForAttribute / AttributeMapper Fluent API for ForAttributeWithMetadataName
Combine Overloads Multi-arity Combine with flat tuples and auto-collect
HintName Consistent hint name generation for output files
OptionalEmit.AddSourceTo Safe source output with diagnostic reporting
Infrastructure DiagnosticInfo, PartialTypeHierarchy, EmbeddedAttribute

The Thin Generator Pattern

Generators should be thin wiring between Projection and Emit layers:

[Generator]
public sealed class StrongIdGenerator : IIncrementalGenerator
{
    public void Initialize(IncrementalGeneratorInitializationContext context)
    {
        var models = context.ForAttribute<StrongIdAttribute>()
            .Map(static (ctx, _) => ctx.TargetSymbol
                .AsValidNamedType()
                .ToStrongIdModel(ctx.SemanticModel));

        context.RegisterSourceOutput(models, static (ctx, model) =>
        {
            model.WriteStrongId()
                .AddSourceTo(ctx, HintName.From(model.Namespace, model.TypeName));
        });
    }
}

See the Generator Guide for patterns, writer classes, and best practices.