Skip to main content

Queries and SQL comments

The documentation strategy's rule for data access: queries stay next to the code that runs them, and say what they do to the data.

  • LINQ first. Almost all data access is EF Core LINQ in the Application services (TaxService, ReportService, ...). The method's XML doc says what it returns. That's the query's documentation, published in the .NET reference.
  • Raw SQL is rare and commented. The only raw SQL is the Postgres advisory lock (Infrastructure/Locking/PostgresAdvisoryLock.cs). Each statement carries a -- comment explaining what it does and why, inside the SQL text, so it also shows up in database logs and pg_stat_activity:
await using var command = new NpgsqlCommand(
"""
-- Scheduled-job leader election: returns false at once if another replica holds the job's lock.
-- Session-level, so it is released on unlock or when this connection drops (pod crash).
SELECT pg_try_advisory_lock(@key)
""", connection);
  • Always parameterize (@key). Never build SQL with string interpolation.
  • Operational queries for support live in the runbook, each with a comment.
  • Indexes are declared next to the mapping in Configurations/*.cs, so the reason for an index sits next to the query that needs it.