-
Notifications
You must be signed in to change notification settings - Fork 16k
Open
Labels
untriagedauto added to all issues by default when created.auto added to all issues by default when created.
Description
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
Labels
untriagedauto added to all issues by default when created.auto added to all issues by default when created.