Skip to content

Allow generating C# classes as records like public sealed record MyRequestMessage : IBufferMessage #25193

@ganesh-tn

Description

@ganesh-tn

Feature request

When using protobuf to generate C# code from .proto files, all messages are generated as mutable classes. Modern C# development favors immutable records for better testability, safety, and cleaner code.

Currently, if I want to use records with protobuf, I must abandon .proto files for a code-first approach.

Describe the solution you'd like

Add an option to generate C# records instead of classes.

Proposed MSBuild property:

<PropertyGroup>
  <GrpcMessagesAsRecords>true</GrpcMessagesAsRecords>
</PropertyGroup>

Or via metadata:

<Protobuf Include="myfile.proto" GrpcOutputOptions="messages_as_records" />

[Theory]
[InlineData("Alice", 25)]
[InlineData("Bob", 30)]
public void ProcessPerson_ShouldValidateAge(string name, int age)
{
    // NOTE: ValidRequests.NewPerson is a a readonly message record
    // Create variation with records using 'with'
    var result = fixture.Client.CreateNewPerson(
        ValidRequests.NewPerson with { Name = name, Age = age }
    );
    Assert.True(result.IsValid);
}

Describe alternatives you've considered

Post-build code transformation - Brittle and breaks on updates

Additional context

C# records were introduced in C# 9.0 (.NET 5.0) and are now widely adopted in the ecosystem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    untriagedauto added to all issues by default when created.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions