emildr revised this gist 3 weeks ago. Go to revision
1 file changed, 14 insertions, 6 deletions
insta_pr_decline.md
| @@ -42,14 +42,22 @@ Correct | |||
| 42 | 42 | ```C# | |
| 43 | 43 | await using var transaction = await context.Database.BeginTransactionAsync(); | |
| 44 | 44 | ||
| 45 | - | var items = await _context.Items.Where(x => itemIds.Contains(x.Id)); | |
| 46 | - | foreach (var item in items) | |
| 45 | + | try | |
| 47 | 46 | { | |
| 48 | - | // Logic | |
| 49 | - | } | |
| 50 | - | await _context.SaveChangesAsync(); | |
| 47 | + | var items = await _context.Items.Where(x => itemIds.Contains(x.Id)); | |
| 48 | + | foreach (var item in items) | |
| 49 | + | { | |
| 50 | + | // Logic | |
| 51 | + | } | |
| 52 | + | await _context.SaveChangesAsync(); | |
| 51 | 53 | ||
| 52 | - | await transaction.CommitAsync(); | |
| 54 | + | await transaction.CommitAsync(); | |
| 55 | + | } | |
| 56 | + | catch (Exception e) | |
| 57 | + | { | |
| 58 | + | await transaction.RollbackAsync(); | |
| 59 | + | // Handle exception or throw; | |
| 60 | + | } | |
| 53 | 61 | ``` | |
| 54 | 62 | ||
| 55 | 63 | ## 3+ Includes without AsSplitQuery() | |
emildr revised this gist 3 weeks ago. Go to revision
1 file changed, 34 insertions, 4 deletions
insta_pr_decline.md
| @@ -1,4 +1,6 @@ | |||
| 1 | - | 1. N+1 Queries | |
| 1 | + | # Instant PR decline reasons | |
| 2 | + | ||
| 3 | + | ## N+1 Queries | |
| 2 | 4 | ||
| 3 | 5 | **Always** query all data you need before executing anything. | |
| 4 | 6 | ||
| @@ -22,7 +24,35 @@ foreach (var item in items) | |||
| 22 | 24 | await _context.SaveChangesAsync(); | |
| 23 | 25 | ``` | |
| 24 | 26 | ||
| 25 | - | 2. 3+ Includes without AsSplitQuery() | |
| 27 | + | ## 2+ Queries updating/inserting queries without a transaction | |
| 28 | + | ||
| 29 | + | If you don't know if you need it, you need it. | |
| 30 | + | ||
| 31 | + | Incorrect | |
| 32 | + | ```C# | |
| 33 | + | var items = await _context.Items.Where(x => itemIds.Contains(x.Id)); | |
| 34 | + | foreach (var item in items) | |
| 35 | + | { | |
| 36 | + | // Logic | |
| 37 | + | } | |
| 38 | + | await _context.SaveChangesAsync(); | |
| 39 | + | ``` | |
| 40 | + | ||
| 41 | + | Correct | |
| 42 | + | ```C# | |
| 43 | + | await using var transaction = await context.Database.BeginTransactionAsync(); | |
| 44 | + | ||
| 45 | + | var items = await _context.Items.Where(x => itemIds.Contains(x.Id)); | |
| 46 | + | foreach (var item in items) | |
| 47 | + | { | |
| 48 | + | // Logic | |
| 49 | + | } | |
| 50 | + | await _context.SaveChangesAsync(); | |
| 51 | + | ||
| 52 | + | await transaction.CommitAsync(); | |
| 53 | + | ``` | |
| 54 | + | ||
| 55 | + | ## 3+ Includes without AsSplitQuery() | |
| 26 | 56 | ||
| 27 | 57 | **Always** consider the query that will actually be executed. | |
| 28 | 58 | ||
| @@ -47,7 +77,7 @@ await _context.Users | |||
| 47 | 77 | .ToListAsync(); | |
| 48 | 78 | ``` | |
| 49 | 79 | ||
| 50 | - | 3. Todos without issues | |
| 80 | + | ## Todos without issues | |
| 51 | 81 | ||
| 52 | 82 | You **will** forget. | |
| 53 | 83 | ||
| @@ -59,6 +89,6 @@ var theThing = getTheThing(); // TODO: Do the right thing! | |||
| 59 | 89 | var theThing = getTheThing(); // TODO: Do the right thing! (#8383) | |
| 60 | 90 | ``` | |
| 61 | 91 | ||
| 62 | - | 4. Big queries without indexes | |
| 92 | + | ## Big/frequent queries without indexes | |
| 63 | 93 | ||
| 64 | 94 | If you are executing a query a lot. It must have an index. | |
emildr revised this gist 3 weeks ago. Go to revision
1 file changed, 5 insertions, 1 deletion
insta_pr_decline.md
| @@ -57,4 +57,8 @@ var theThing = getTheThing(); // TODO: Do the right thing! | |||
| 57 | 57 | ||
| 58 | 58 | ```C# | |
| 59 | 59 | var theThing = getTheThing(); // TODO: Do the right thing! (#8383) | |
| 60 | - | ``` | |
| 60 | + | ``` | |
| 61 | + | ||
| 62 | + | 4. Big queries without indexes | |
| 63 | + | ||
| 64 | + | If you are executing a query a lot. It must have an index. | |
emildr revised this gist 3 weeks ago. Go to revision
1 file changed, 3 insertions
insta_pr_decline.md
| @@ -1,4 +1,5 @@ | |||
| 1 | 1 | 1. N+1 Queries | |
| 2 | + | ||
| 2 | 3 | **Always** query all data you need before executing anything. | |
| 3 | 4 | ||
| 4 | 5 | Incorrect: | |
| @@ -22,6 +23,7 @@ await _context.SaveChangesAsync(); | |||
| 22 | 23 | ``` | |
| 23 | 24 | ||
| 24 | 25 | 2. 3+ Includes without AsSplitQuery() | |
| 26 | + | ||
| 25 | 27 | **Always** consider the query that will actually be executed. | |
| 26 | 28 | ||
| 27 | 29 | Incorrect | |
| @@ -46,6 +48,7 @@ await _context.Users | |||
| 46 | 48 | ``` | |
| 47 | 49 | ||
| 48 | 50 | 3. Todos without issues | |
| 51 | + | ||
| 49 | 52 | You **will** forget. | |
| 50 | 53 | ||
| 51 | 54 | ```C# | |
emildr revised this gist 3 weeks ago. Go to revision
1 file changed, 36 insertions, 3 deletions
insta_pr_decline.md
| @@ -1,24 +1,57 @@ | |||
| 1 | 1 | 1. N+1 Queries | |
| 2 | + | **Always** query all data you need before executing anything. | |
| 3 | + | ||
| 4 | + | Incorrect: | |
| 2 | 5 | ```C# | |
| 3 | - | foreach (var item in items) | |
| 6 | + | foreach (var id in itemIds) | |
| 4 | 7 | { | |
| 5 | - | await _context.Items.FirstOrDefaultAsync(x => x.Id == item.id); | |
| 8 | + | await _context.Items.FirstOrDefaultAsync(x => x.Id == id); | |
| 6 | 9 | // Logic | |
| 7 | 10 | await _context.SaveChangesAsync(); | |
| 8 | 11 | } | |
| 9 | 12 | ``` | |
| 10 | 13 | ||
| 14 | + | Correct: | |
| 15 | + | ```C# | |
| 16 | + | var items = await _context.Items.Where(x => itemIds.Contains(x.Id)); | |
| 17 | + | foreach (var item in items) | |
| 18 | + | { | |
| 19 | + | // Logic | |
| 20 | + | } | |
| 21 | + | await _context.SaveChangesAsync(); | |
| 22 | + | ``` | |
| 23 | + | ||
| 11 | 24 | 2. 3+ Includes without AsSplitQuery() | |
| 25 | + | **Always** consider the query that will actually be executed. | |
| 26 | + | ||
| 27 | + | Incorrect | |
| 28 | + | ```C# | |
| 29 | + | await _context.Users | |
| 30 | + | .Include(u => u.Roles) | |
| 31 | + | .Include(u => u.Events) | |
| 32 | + | .Include(u => u.Installations) | |
| 33 | + | .ThenInclude(i => i.Meters) | |
| 34 | + | .ToListAsync(); | |
| 35 | + | ``` | |
| 36 | + | ||
| 37 | + | Correct: | |
| 12 | 38 | ```C# | |
| 13 | - | _context.Users | |
| 39 | + | await _context.Users | |
| 14 | 40 | .Include(u => u.Roles) | |
| 15 | 41 | .Include(u => u.Events) | |
| 16 | 42 | .Include(u => u.Installations) | |
| 17 | 43 | .ThenInclude(i => i.Meters) | |
| 44 | + | .AsSplitQuery() | |
| 18 | 45 | .ToListAsync(); | |
| 19 | 46 | ``` | |
| 20 | 47 | ||
| 21 | 48 | 3. Todos without issues | |
| 49 | + | You **will** forget. | |
| 50 | + | ||
| 22 | 51 | ```C# | |
| 23 | 52 | var theThing = getTheThing(); // TODO: Do the right thing! | |
| 53 | + | ``` | |
| 54 | + | ||
| 55 | + | ```C# | |
| 56 | + | var theThing = getTheThing(); // TODO: Do the right thing! (#8383) | |
| 24 | 57 | ``` | |
emildr revised this gist 3 weeks ago. Go to revision
No changes
emildr revised this gist 3 weeks ago. Go to revision
1 file changed, 2 insertions, 1 deletion
insta_pr_decline.md
| @@ -5,7 +5,8 @@ foreach (var item in items) | |||
| 5 | 5 | await _context.Items.FirstOrDefaultAsync(x => x.Id == item.id); | |
| 6 | 6 | // Logic | |
| 7 | 7 | await _context.SaveChangesAsync(); | |
| 8 | - | }``` | |
| 8 | + | } | |
| 9 | + | ``` | |
| 9 | 10 | ||
| 10 | 11 | 2. 3+ Includes without AsSplitQuery() | |
| 11 | 12 | ```C# | |
emildr revised this gist 3 weeks ago. Go to revision
1 file changed, 23 insertions
insta_pr_decline.md(file created)
| @@ -0,0 +1,23 @@ | |||
| 1 | + | 1. N+1 Queries | |
| 2 | + | ```C# | |
| 3 | + | foreach (var item in items) | |
| 4 | + | { | |
| 5 | + | await _context.Items.FirstOrDefaultAsync(x => x.Id == item.id); | |
| 6 | + | // Logic | |
| 7 | + | await _context.SaveChangesAsync(); | |
| 8 | + | }``` | |
| 9 | + | ||
| 10 | + | 2. 3+ Includes without AsSplitQuery() | |
| 11 | + | ```C# | |
| 12 | + | _context.Users | |
| 13 | + | .Include(u => u.Roles) | |
| 14 | + | .Include(u => u.Events) | |
| 15 | + | .Include(u => u.Installations) | |
| 16 | + | .ThenInclude(i => i.Meters) | |
| 17 | + | .ToListAsync(); | |
| 18 | + | ``` | |
| 19 | + | ||
| 20 | + | 3. Todos without issues | |
| 21 | + | ```C# | |
| 22 | + | var theThing = getTheThing(); // TODO: Do the right thing! | |
| 23 | + | ``` | |