Naposledy aktivní 2 weeks ago

Revize fef994f88c644d0e84d8b2c3d030f9cedc410e99

insta_pr_decline.md Raw
  1. N+1 Queries Always query all data you need before executing anything.

Incorrect:

foreach (var id in itemIds)
{
  await _context.Items.FirstOrDefaultAsync(x => x.Id == id);
  // Logic
  await _context.SaveChangesAsync();
}

Correct:

var items = await _context.Items.Where(x => itemIds.Contains(x.Id));
foreach (var item in items)
{
  // Logic
}
await _context.SaveChangesAsync();
  1. 3+ Includes without AsSplitQuery() Always consider the query that will actually be executed.

Incorrect

await _context.Users
  .Include(u => u.Roles)
  .Include(u => u.Events)
  .Include(u => u.Installations)
  .ThenInclude(i => i.Meters)
  .ToListAsync();

Correct:

await _context.Users
  .Include(u => u.Roles)
  .Include(u => u.Events)
  .Include(u => u.Installations)
  .ThenInclude(i => i.Meters)
  .AsSplitQuery()
  .ToListAsync();
  1. Todos without issues You will forget.
var theThing = getTheThing(); // TODO: Do the right thing!
var theThing = getTheThing(); // TODO: Do the right thing! (#8383)