AI

Reviewing AI-generated C# before it reaches production

DEV Community: #dotnet Ankitkumar Singh
AI

AI assistants now produce much of the C# in our pull requests, and it compiles and passes tests before failing in production. This guide lists the recurring mistakes and the review checks that catch them.

Key points

  • String interpolation inside FromSqlRaw is not parameterized; FromSql (EF Core 7+) turns each FormattableString value into a real @p0 parameter.
  • Returning records from FindAsync(id) without an owner filter is BOLA; in multi-tenant apps a HasQueryFilter makes the safe path the default.
  • Calling .Result or .Wait() blocks a thread-pool thread, and an exception thrown inside async void can take down the process.
  • A new HttpClient() per call burns sockets; register a typed client with IHttpClientFactory, an explicit timeout and a resilience handler.
  • ToList() before Where loads the whole table, and awaiting inside a foreach turns one page into thousands of round trips.
  • A singleton that holds a DbContext breaks the scoped lifetime; enable ValidateScopes and ValidateOnBuild in every environment, not just development.

Why it matters

None of these failures appear in a compiler warning or a unit test; they hide inside code that works on the happy path. As AI drafts more of the pull request, review attention shifts from naming and style to query shape, authorization boundaries and service lifetimes. A grep for raw SQL, unfiltered FindAsync, .Result and new HttpClient catches most of them.

The durable fix makes the safe path the default: global query filters, DTOs instead of entities, one exception handler, scope validation everywhere. AI-generated code then inherits the guardrails instead of relying on a reviewer's memory. It is cheap and mostly demands changing habits the team already has.

Real-world use

  • Report filters, search screens and export-to-Excel endpoints that accept free text from users
  • A multi-tenant SaaS API where changing an invoice id in the URL exposes another tenant's billing data
  • A dashboard that totals positions per client and slows down as the client list grows
  • A backend service that calls a market-data or payment HTTP dependency under heavy load

This summary was generated by the agent from the original post on DEV Community: #dotnet. The full text and all rights belong to its author and publisher. Go to original

Report this story

Is something wrong with the summary, or do you own this content? Your message comes straight to my inbox.

Type of message

Related stories

Feedback

Found a bug or have an idea? Your message lands straight in my inbox.

Type of message