Skip to main content

Coding Standards & Patterns

This page focuses on the technical standards and recurring patterns for .NET 10 development in InventoryAlert.

For a high-level overview of how to test the system, see the Test Structure & App Quality. For technical details on the three integration tiers, see Integration Testing Tiers.


C# Coding Standardsโ€‹

RuleDescription
Primary ConstructorsUse C# 12 primary constructors across all layers
CancellationToken ctMust be the last parameter in every async method
No async without awaitRemove async keyword; return Task.FromResult(...) instead (fixes CS1998)
string.EmptyUse over "" for empty string defaults on entity properties
null! suppressorForbidden unless accompanied by a comment explaining why
Private fields_camelCase. Everything else: PascalCase
ILogger<T>Use for all logging. Console.WriteLine is banned
AsNoTracking()Required on all read-only EF Core queries
FluentValidationApplied at Web layer only; controllers must not contain inline if validation

Common Patternsโ€‹

Transaction Capture Patternโ€‹

Ensure results are captured inside the transaction block to avoid returning stale or blank entities.

// โœ… GOOD โ€” result assigned inside the lambda
PortfolioPositionResponse result = null!;
await _unitOfWork.ExecuteTransactionAsync(async () => {
var updated = await _repo.UpdateAsync(entity);
result = MapToResponse(updated);
}, ct);
return result;

Async Polling Helper (Tests)โ€‹

Used in E2E tests to wait for background worker side-effects.

await WaitHelper.WaitForConditionAsync(async () => {
var res = await Client.GetAsync("/api/v1/notifications");
return res.IsSuccessStatusCode;
}, timeoutSeconds: 15);

Git Commit Conventionโ€‹

Follow the standard type(scope): message format.

TypeDescription
featNew feature
fixBug fix
testAdding or updating tests
refactorCode change that neither fixes a bug nor adds a feature
docsDocumentation only changes
choreMaintenance tasks

Scopes: domain, infra, api, worker, ui, tests.

Example: feat(api): add position bulk import