Skip to content

File and type layout standard

This page sets the rules for how C# types are placed in files and folders, and how members are ordered inside a type. It is written for engineers who add or review C# files. Following it means a type can be found from its name alone and a diff shows one type at a time.

Naming and style rules are in C# style and naming. Files produced by a code generator are exempt from the rules on this page; see Generated code.

Rules

CS-STY-006: Put one top-level type in each file, named after the type

Severity: major · Applies to: service, library, api-client, blazor, template · Enforced by: analyzer SA1402, SA1649 (target: ENF-031) · Status: active

Why

A type is found by opening the file with its name. A second type in the same file is invisible in the solution explorer and in a file search, and its changes appear in the history of an unrelated file. Small companion types are where this goes wrong most often: a request record next to the client that sends it, or a static helper next to the class that uses it.

Do

  • Put each top-level class, record, struct, interface, enum and delegate in its own file.
  • Name the file exactly after the type, for example OrderLineInput.cs. A generic type Result<T> goes in Result{T}.cs or Result.cs.
  • Move a companion record, a small static helper or an enum into its own file, even when it has only a few lines.

Don't

  • Add a second top-level type to an existing file.
  • Group related contracts in one file such as OrderModels.cs.

A private type nested inside its host class is allowed. When a nested type is moved into its own file it becomes internal, so its members then need XML comments (CS-XML-001).

Example

Do                                   Don't
OrderGatewayClient.cs                OrderGatewayClient.cs
OrderGatewayRequest.cs                 class OrderGatewayClient
OrderGatewayResponse.cs                record OrderGatewayRequest
                                       record OrderGatewayResponse

How to check

  • Count the top-level type declarations in each new file; there is one.
  • Compare the file name with the type name.
  • SA1402 and SA1649 report both. Seven .editorconfig files turn SA1649 off (ENF-031).

CS-STY-007: Do not use #region in hand-written code

Severity: nit · Applies to: service, library, api-client, blazor, template · Enforced by: analyzer SA1124 · Status: active

Why

A region hides code instead of organizing it. A type that needs regions to be readable is usually too large and should be split. Hand-written ConnectSoft code almost never uses regions today.

Do

  • Split a large type into smaller types.
  • Rely on member order (CS-STY-009) to group members.

Don't

  • Add #region or #endregion to hand-written code.

Generated files, such as Reqnroll .feature.cs code-behind, are exempt.

How to check

Search the change for #region. SA1124 reports it.

CS-STY-008: Match the namespace to the folder path

Severity: minor · Applies to: service, library, api-client, blazor, template · Enforced by: analyzer IDE0130 · Status: active

Why

When the namespace follows the folder, a type's location can be derived from its full name and the other way round. 94% of ConnectSoft files and 98% of consumer service files already follow this.

Do

  • Use the project's root namespace followed by the folder path, for example ConnectSoft.Orders.DomainModel.Impl.Validators for a file in the Validators folder of ConnectSoft.Orders.DomainModel.Impl.

Don't

  • Leave a file in a folder whose name is not part of its namespace.
  • Move a public type to another namespace only to follow this rule. That is a breaking change in a library.

How to check

IDE0130 reports a namespace that does not match the folder. It is configured at suggestion level today.

CS-STY-009: Order members by kind and access

Severity: nit · Applies to: service, library, api-client, blazor, template · Enforced by: analyzer SA1201, SA1202, SA1204 · Status: active

Why

A fixed order means a reader knows where to look for the fields, the constructor or the public surface of any type.

Do

Order members in this sequence, which is the StyleCop default: 1. constants and fields; 2. constructors; 3. events; 4. properties; 5. methods; 6. nested types.

Within each kind, put public before internal, protected and private, and static before instance members.

Don't

  • Reorder members of existing types only to follow this rule.

How to check

SA1201, SA1202 and SA1204 report members out of order. No ConnectSoft configuration changes their defaults.

CS-STY-010: Use partial classes only to pair hand-written and generated code

Severity: minor · Applies to: service, library, api-client, blazor, template · Enforced by: manual · Status: active

Why

A partial class spread over several hand-written files hides its full shape. partial exists so that generated code and hand-written code can meet in one type.

Do

Use partial for: - Blazor components with a .razor file and a .razor.cs code-behind file; - source-generated logging with [LoggerMessage]; - [OptionsValidator] and other source generators that require it.

Don't

  • Split a hand-written class into several partial files to make it look smaller. Split it into separate types instead.

How to check

For each new partial type, check that one of its parts is generated or is a .razor file.

CS-STY-011: Keep one .slnx solution file and no .sln

Severity: minor · Applies to: service, library, api-client, blazor, template · Enforced by: manual (target: ENF-065) · Status: active

Why

The XML solution format (.slnx) is short, readable in a diff and merges without the GUID conflicts of the old .sln format. Most ConnectSoft repositories already use it. A repository that keeps both formats has two lists of projects that drift apart: a project added to one is missing from builds and IDEs that open the other.

Do

  • Keep exactly one solution file in the repository root, in the .slnx format, named after the repository: ConnectSoft.Extensions.Example.slnx.
  • Create it with dotnet new sln --format slnx, or let the repository template create it (CS-TPL-005).
  • When you change a repository that still has a .sln file, convert it with dotnet sln <name>.sln migrate, check that the .slnx lists the same projects, and delete the .sln in the same change.
  • When a repository has both files, delete the .sln and keep the .slnx.
  • Point build commands, pipeline parameters, Dockerfiles and documentation at the .slnx file.

Don't

  • Add a .sln file, or keep one next to the .slnx "for older tools".
  • Ship a template that generates a .sln file.

Example

<Solution>
  <Folder Name="/src/">
    <Project Path="src/ConnectSoft.Extensions.Example/ConnectSoft.Extensions.Example.csproj" />
  </Folder>
  <Folder Name="/tests/">
    <Project Path="tests/ConnectSoft.Extensions.Example.UnitTests/ConnectSoft.Extensions.Example.UnitTests.csproj" />
  </Folder>
</Solution>

How to check

  • The change adds no .sln file, and a repository that it touches has no .sln file left.
  • dotnet build <name>.slnx succeeds, and pipeline files pass the .slnx file or a **/*.slnx pattern as the solution.

CS-STY-012: Initialise a static field only from fields declared above it

Severity: major · Applies to: service, library, api-client, blazor, template · Enforced by: manual · Status: active

Why

Static field initialisers run in declaration order. An initialiser that reads a static field declared further down reads its default value, and the first use of the class throws TypeInitializationException. Reordering members for style can introduce the failure without any other change.

Do

  • Declare a static field before every static field whose initialiser reads it.
  • Build a table derived from other static data in a static constructor or with Lazy<T>, when the order cannot be kept obvious.

Don't

  • Reorder static fields without checking which initialisers read them.

How to check

A unit test uses the class and reads a value through each derived static table.