Repository navigation
Expand file tree
/
Copy pathBenchmarkService.cs
More file actions
738 lines (637 loc) · 34.5 KB
/
Copy pathBenchmarkService.cs
File metadata and controls
738 lines (637 loc) · 34.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using CompiledModels;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using OptimizeMePlease.Context;
using OptimizeMePlease.Entities;
namespace OptimizeMePlease
{
[MemoryDiagnoser(false)]
//[InProcess]
public class BenchmarkService
{
/// <summary>
/// Get top 2 Authors (FirstName, LastName, UserName, Email, Age, Country)
/// from country Serbia aged 27, with the highest BooksCount
/// and all his/her books (Book Name/Title and Publishment Year) published before 1900
/// </summary>
/// <returns></returns>
[Benchmark(Baseline = true)]
public List<AuthorDTO> GetAuthors()
{
using var dbContext = new AppDbContext();
var authors = dbContext.Authors
.Include(x => x.User)
.ThenInclude(x => x.UserRoles)
.ThenInclude(x => x.Role)
.Include(x => x.Books)
.ThenInclude(x => x.Publisher)
.ToList()
.Select(x => new AuthorDTO
{
UserCreated = x.User.Created,
UserEmailConfirmed = x.User.EmailConfirmed,
UserFirstName = x.User.FirstName,
UserLastActivity = x.User.LastActivity,
UserLastName = x.User.LastName,
UserEmail = x.User.Email,
UserName = x.User.UserName,
UserId = x.User.Id,
RoleId = x.User.UserRoles.FirstOrDefault(y => y.UserId == x.UserId).RoleId,
BooksCount = x.BooksCount,
AllBooks = x.Books.Select(y => new BookDto
{
Id = y.Id,
Name = y.Name,
Published = y.Published,
ISBN = y.ISBN,
PublisherName = y.Publisher.Name
})
.ToList(),
AuthorAge = x.Age,
AuthorCountry = x.Country,
AuthorNickName = x.NickName,
Id = x.Id
})
.ToList()
.Where(x => (x.AuthorCountry == "Serbia") && (x.AuthorAge == 27))
.ToList();
var orderedAuthors = authors.OrderByDescending(x => x.BooksCount).ToList().Take(2).ToList();
var finalAuthors = new List<AuthorDTO>();
foreach (var author in orderedAuthors)
{
var books = new List<BookDto>();
var allBooks = author.AllBooks;
foreach (var book in allBooks)
{
if (book.Published.Year < 1900)
{
book.PublishedYear = book.Published.Year;
books.Add(book);
}
}
author.AllBooks = books;
finalAuthors.Add(author);
}
return finalAuthors;
}
[Benchmark]
public IList<AuthorDtoOptimized> GetAuthors_Optimized()
{
using var db = ContextFactory.CreateDbContext();
return Get(db).ToList();
}
[Benchmark]
public IList<AuthorDtoOptimized> GetAuthors_OptimizedInline()
{
using var db = ContextFactory.CreateDbContext();
return db.Set<Author>()
.Where(author => (author.Country == Serbia) && (author.Age == Age))
.OrderByDescending(x => x.BooksCount)
.Take(2)
.Select(author => new AuthorDtoOptimized
{
UserFirstName = author.User.FirstName,
UserLastName = author.User.LastName,
UserEmail = author.User.Email,
UserName = author.User.UserName,
BooksCount = author.BooksCount,
Books = author.Books.AsQueryable()
.Where(book => book.Published < EF.Functions.DateFromParts(Year, 1, 1))
.Select(y => new BookDtoOptimized
{
Name = y.Name,
PublishedYear = y.Published.Year
})
.ToList(),
Age = author.Age,
Country = author.Country
})
.ToList();
}
[Benchmark]
public IList<AuthorDtoOptimized> GetAuthors_OptimizedInlineWithParameters()
{
using var db = ContextFactory.CreateDbContext();
string country = Serbia;
int age = Age;
int year = Year;
return db.Set<Author>()
.Where(author => (author.Country == country) && (author.Age == age))
.OrderByDescending(x => x.BooksCount)
.Take(2)
.Select(author => new AuthorDtoOptimized
{
UserFirstName = author.User.FirstName,
UserLastName = author.User.LastName,
UserEmail = author.User.Email,
UserName = author.User.UserName,
BooksCount = author.BooksCount,
Books = author.Books.AsQueryable()
.Where(book => book.Published < EF.Functions.DateFromParts(year, 1, 1))
.Select(y => new BookDtoOptimized
{
Name = y.Name,
PublishedYear = y.Published.Year
})
.ToList(),
Age = author.Age,
Country = author.Country
})
.ToList();
}
[Benchmark]
public IList<AuthorDtoOptimized> GetAuthors_OptimizedWithoutExpressions()
{
using var db = ContextFactory.CreateDbContext();
return GetWithoutExpressions(db).ToList();
}
[Benchmark]
public async Task<AuthorDtoOptimized[]> GetAuthors_OptimizedAsync()
{
await using var db = await ContextFactory.CreateDbContextAsync();
var authors = new AuthorDtoOptimized[2];
int counter = 0;
await foreach (var author in GetAsync(db))
{
authors[counter] = author;
counter++;
}
return authors;
}
[Benchmark]
public async IAsyncEnumerable<AuthorDtoOptimized> GetAuthors_OptimizedAsyncPassThrough()
{
await using var db = await ContextFactory.CreateDbContextAsync();
await foreach (var author in GetAsync(db))
{
yield return author;
}
}
[Benchmark]
public IList<AuthorDtoOptimized> GetAuthors_OptimizedParameterized()
{
using var db = ContextFactory.CreateDbContext();
return GetParameterized(db, Serbia, Age).ToList();
}
[Benchmark]
public Span<AuthorRecordDto> GetAuthors_OptimizedRecordSpan()
{
using var db = ContextFactory.CreateDbContext();
var array = ArrayPool.Rent(2);
int counter = 0;
foreach (var dto in GetRecord(db))
{
array[counter] = dto;
counter++;
}
var result = array.AsSpan();
ArrayPool.Return(array);
return result;
}
[Benchmark]
public IList<AuthorRecordDto> GetAuthors_OptimizedRecordList()
{
using var db = ContextFactory.CreateDbContext();
return GetRecord(db).ToList();
}
[Benchmark]
public IList<AuthorRecordDto> GetAuthors_OptimizedRecordListInline()
{
using var db = ContextFactory.CreateDbContext();
return db.Set<Author>()
.Where(author => (author.Country == Serbia) && (author.Age == Age))
.OrderByDescending(x => x.BooksCount)
.Take(2)
.Select(author =>
new AuthorRecordDto(author.Age,
author.Country,
author.BooksCount,
author.User.UserName,
author.User.FirstName,
author.User.LastName,
author.User.Email,
author.Books.AsQueryable()
.Where(book => book.Published < EF.Functions.DateFromParts(Year, 1, 1))
.Select(y => new BookRecordDto(y.Name, y.Published.Year))
.ToList()))
.ToList();
}
[Benchmark]
public IList<AuthorRecordDto> GetAuthors_OptimizedRecordListInlineWithParameters()
{
using var db = ContextFactory.CreateDbContext();
string country = Serbia;
int age = Age;
int year = Year;
return db.Set<Author>()
.Where(author => (author.Country == country) && (author.Age == age))
.OrderByDescending(x => x.BooksCount)
.Take(2)
.Select(author =>
new AuthorRecordDto(author.Age,
author.Country,
author.BooksCount,
author.User.UserName,
author.User.FirstName,
author.User.LastName,
author.User.Email,
author.Books.AsQueryable()
.Where(book => book.Published < EF.Functions.DateFromParts(year, 1, 1))
.Select(y => new BookRecordDto(y.Name, y.Published.Year))
.ToList()))
.ToList();
}
[Benchmark]
public IList<AuthorRecordDto> GetAuthors_OptimizedRecordListWithoutExpressions()
{
using var db = ContextFactory.CreateDbContext();
return GetRecordWithoutExpressions(db).ToList();
}
[Benchmark]
public async Task<AuthorRecordDto[]> GetAuthors_OptimizedRecordAsync()
{
await using var db = await ContextFactory.CreateDbContextAsync();
var authors = new AuthorRecordDto[2];
int counter = 0;
await foreach (var author in GetRecordAsync(db))
{
authors[counter] = author;
counter++;
}
return authors;
}
[Benchmark]
public async IAsyncEnumerable<AuthorRecordDto> GetAuthors_OptimizedRecordAsyncPassThrough()
{
await using var db = await ContextFactory.CreateDbContextAsync();
await foreach (var author in GetRecordAsync(db))
{
yield return author;
}
}
[Benchmark]
public IList<AuthorRecordDto> GetAuthors_OptimizedRecordListParameterized()
{
using var db = ContextFactory.CreateDbContext();
return GetRecordParameterized(db, Serbia, Age).ToList();
}
[Benchmark]
public AuthorRecordDto[] GetAuthors_RawSqlArray()
{
using var db = ContextFactory.CreateDbContext();
using var command = db.Database.GetDbConnection().CreateCommand();
command.CommandText = CommandText;
var countryParameter = new SqlParameter("@country", Serbia)
{
SqlDbType = SqlDbType.NVarChar
};
command.Parameters.Add(countryParameter);
command.Parameters.Add(new SqlParameter("@take", 2));
command.Parameters.Add(new SqlParameter("@age", Age));
command.Parameters.Add(new SqlParameter("@year", 1900));
return ProcessRawSqlResult(db, command);
}
[Benchmark]
public AuthorRecordDto[] GetAuthors_RawSqlWithoutParametersArray()
{
using var db = ContextFactory.CreateDbContext();
using var command = db.Database.GetDbConnection().CreateCommand();
command.CommandText = CommandTextWithoutParameters;
return ProcessRawSqlResult(db, command);
}
private static AuthorRecordDto[] ProcessRawSqlResult(DbContext db, DbCommand command)
{
db.Database.OpenConnection();
using var reader = command.ExecuteReader();
var authors = new List<AuthorRecordDto>();
while (reader.Read())
{
//SELECT [u].[FirstName], [u].[LastName], [u].[Email], [u].[UserName], [t].[BooksCount], [t].[Id], [u].[Id], [t0].[Name], [t0].[PublishedYear], [t0].[Id], [t].[Age], [t].[Country]
//(int Age, string Country, int BooksCount, string UserName, string FirstName, string LastName, string Email, ICollection<BookStructDto> Books);
authors.Add(new AuthorRecordDto(reader.GetInt32(10), reader.GetString(11), reader.GetInt32(4), reader.GetString(3), reader.GetString(0), reader.GetString(1), reader.GetString(2),
new[] { new BookRecordDto(reader.GetString(7), reader.GetInt32(8)) }));
}
return authors.GroupBy(x => x.UserName)
.Select(x => x.First() with
{
UserName = x.Key,
Books = x.SelectMany(y => y.Books).ToList()
})
.ToArray();
}
[Benchmark]
public AuthorRecordDto[] GetAuthors_OptimizedMultipleQueries()
{
using var db = ContextFactory.CreateDbContext();
var authors = db.Set<Author>()
.Where(AuthorWhereFilterExpression)
.Select(x => new
{
x.Id,
x.BooksCount,
x.UserId
})
.OrderByDescending(x => x.BooksCount)
.Take(2)
.ToList();
var books = db.Set<Book>()
.Where(BookWhereFilterExpression)
.Where(x => authors.Select(y => y.Id).Contains(x.AuthorId))
.Select(x => new
{
x.AuthorId,
x.Name,
x.Published.Year
})
.ToList();
var users = db.Set<User>()
.Where(x => authors.Select(y => y.UserId).Contains(x.Id))
.Select(x => new
{
x.Id,
x.UserName,
x.Email,
x.FirstName,
x.LastName
})
.ToList();
return authors.Select(x =>
{
var user = users.First(y => y.Id == x.UserId);
return new AuthorRecordDto(Age, Serbia, x.BooksCount, user.UserName, user.FirstName, user.LastName, user.Email,
books.Where(y => y.AuthorId == x.Id).Select(y => new BookRecordDto(y.Name, y.Year)).ToArray());
})
.ToArray();
}
[Benchmark]
public AuthorRecordDto[] GetAuthors_MultipleQuery()
{
using var db = ContextFactory.CreateDbContext();
var authors = GetAuthorsSimplified(db).ToList();
var books = GetBooksSimplified(db, authors.Select(x => x.Id)).ToList();
var users = GetUsersSimplified(db, authors.Select(x => x.UserId)).ToList();
return ProcessMultipleQueryResult(authors, users, books);
}
[Benchmark]
public AuthorRecordDto[] GetAuthors_MultipleQueryWithoutSelectorExpressions()
{
using var db = ContextFactory.CreateDbContext();
var authors = GetAuthorsSimplifiedWithoutSelectorExpression(db).ToList();
var books = GetBooksSimplifiedWithoutSelectorExpression(db, authors.Select(x => x.Id)).ToList();
var users = GetUsersSimplifiedWithoutSelectorExpression(db, authors.Select(x => x.UserId)).ToList();
return ProcessMultipleQueryResult(authors, users, books);
}
private static AuthorRecordDto[] ProcessMultipleQueryResult(IEnumerable<AuthorSimplified> authors, IReadOnlyCollection<UserSimplified> users, IReadOnlyCollection<BookSimplified> books)
{
return authors.Select(x =>
{
var user = users.First(y => y.Id == x.UserId);
return new AuthorRecordDto(Age, Serbia, x.BookCount, user.UserName, user.FirstName, user.LastName, user.Email,
books.Where(y => y.AuthorId == x.Id).Select(y => new BookRecordDto(y.Name, y.Year)).ToArray());
})
.ToArray();
}
private static readonly Func<DbContext, IEnumerable<AuthorSimplified>> GetAuthorsSimplifiedWithoutSelectorExpression =
EF.CompileQuery((DbContext db) =>
db.Set<Author>()
.Where(AuthorWhereFilterExpression)
.OrderByDescending(x => x.BooksCount)
.Take(2)
.Select(author => new AuthorSimplified
{
Id = author.Id,
BookCount = author.BooksCount,
UserId = author.UserId
}));
private static readonly Func<DbContext, IEnumerable<int>, IEnumerable<BookSimplified>> GetBooksSimplifiedWithoutSelectorExpression =
EF.CompileQuery((DbContext db, IEnumerable<int> authorIds) =>
db.Set<Book>()
.Where(BookWhereFilterExpression)
.Where(x => authorIds.Contains(x.AuthorId))
.Select(book => new BookSimplified
{
AuthorId = book.AuthorId,
Name = book.Name,
Year = book.Published.Year
}));
private static readonly Func<DbContext, IEnumerable<int>, IEnumerable<UserSimplified>> GetUsersSimplifiedWithoutSelectorExpression =
EF.CompileQuery((DbContext db, IEnumerable<int> userIds) =>
db.Set<User>()
.Where(x => userIds.Contains(x.Id))
.Select(user => new UserSimplified
{
Id = user.Id,
UserName = user.UserName,
Email = user.Email,
FirstName = user.FirstName,
LastName = user.LastName
}));
private static readonly Expression<Func<Author, AuthorSimplified>> AuthorSimplifiedSelectorExpression = author => new AuthorSimplified
{
Id = author.Id,
BookCount = author.BooksCount,
UserId = author.UserId
};
private static readonly Func<DbContext, IEnumerable<AuthorSimplified>> GetAuthorsSimplified =
EF.CompileQuery((DbContext db) =>
db.Set<Author>()
.Where(AuthorWhereFilterExpression)
.OrderByDescending(x => x.BooksCount)
.Take(2)
.Select(AuthorSimplifiedSelectorExpression));
private static readonly Expression<Func<Book, BookSimplified>> BookSimplifiedSelectorExpression = book => new BookSimplified
{
AuthorId = book.AuthorId,
Name = book.Name,
Year = book.Published.Year
};
private static readonly Func<DbContext, IEnumerable<int>, IEnumerable<BookSimplified>> GetBooksSimplified =
EF.CompileQuery((DbContext db, IEnumerable<int> authorIds) =>
db.Set<Book>()
.Where(BookWhereFilterExpression)
.Where(x => authorIds.Contains(x.AuthorId))
.Select(BookSimplifiedSelectorExpression));
private static readonly Expression<Func<User, UserSimplified>> UserSimplifiedSelectorExpression = user => new UserSimplified
{
Id = user.Id,
UserName = user.UserName,
Email = user.Email,
FirstName = user.FirstName,
LastName = user.LastName
};
private static readonly Func<DbContext, IEnumerable<int>, IEnumerable<UserSimplified>> GetUsersSimplified =
EF.CompileQuery((DbContext db, IEnumerable<int> userIds) =>
db.Set<User>()
.Where(x => userIds.Contains(x.Id))
.Select(UserSimplifiedSelectorExpression));
private const string CommandText =
@"SELECT [u].[FirstName], [u].[LastName], [u].[Email], [u].[UserName], [t].[BooksCount], [t].[Id], [u].[Id], [t0].[Name], [t0].[PublishedYear], [t0].[Id], [t].[Age], [t].[Country]
FROM (
SELECT TOP(@take) [a].[Id], [a].[Age], [a].[BooksCount], [a].[Country], [a].[UserId]
FROM [Authors] AS [a]
WHERE ([a].[Country] = @country) AND ([a].[Age] = @age)
ORDER BY [a].[BooksCount] DESC
) AS [t]
INNER JOIN [Users] AS [u] ON [t].[UserId] = [u].[Id]
LEFT JOIN (
SELECT [b].[Name], DATEPART(year, [b].[Published]) AS [PublishedYear], [b].[Id], [b].[AuthorId]
FROM [Books] AS [b]
WHERE [b].[Published] < DATEFROMPARTS(@year, 1, 1)
) AS [t0] ON [t].[Id] = [t0].[AuthorId]
ORDER BY [t].[BooksCount] DESC, [t].[Id], [u].[Id]";
private const string CommandTextWithoutParameters =
@"SELECT [u].[FirstName], [u].[LastName], [u].[Email], [u].[UserName], [t].[BooksCount], [t].[Id], [u].[Id], [t0].[Name], [t0].[PublishedYear], [t0].[Id], [t].[Age], [t].[Country]
FROM (
SELECT TOP(2) [a].[Id], [a].[Age], [a].[BooksCount], [a].[Country], [a].[UserId]
FROM [Authors] AS [a]
WHERE ([a].[Country] = N'Serbia') AND ([a].[Age] = 27)
ORDER BY [a].[BooksCount] DESC
) AS [t]
INNER JOIN [Users] AS [u] ON [t].[UserId] = [u].[Id]
LEFT JOIN (
SELECT [b].[Name], DATEPART(year, [b].[Published]) AS [PublishedYear], [b].[Id], [b].[AuthorId]
FROM [Books] AS [b]
WHERE [b].[Published] < DATEFROMPARTS(1900, 1, 1)
) AS [t0] ON [t].[Id] = [t0].[AuthorId]
ORDER BY [t].[BooksCount] DESC, [t].[Id], [u].[Id]";
private static readonly ArrayPool<AuthorRecordDto> ArrayPool = ArrayPool<AuthorRecordDto>.Shared;
private const string Serbia = nameof(Serbia);
private const int Age = 27;
private const int Year = 1900;
public const string ConnectionString =
"Server=c0711p\\sqlstafl;Database=OptimizeMePlease;Trusted_Connection=True;Integrated Security=true;MultipleActiveResultSets=true;TrustServerCertificate=true;";
private static readonly PooledDbContextFactory<DbContext> ContextFactory = new(new DbContextOptionsBuilder<DbContext>().UseModel(AppDbContextModel.Instance)
.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking)
.EnableThreadSafetyChecks(false)
.UseSqlServer(ConnectionString)
.Options);
private static readonly Expression<Func<Author, AuthorDtoOptimized>> AuthorDtoSelectorExpression = author => new AuthorDtoOptimized
{
UserFirstName = author.User.FirstName,
UserLastName = author.User.LastName,
UserEmail = author.User.Email,
UserName = author.User.UserName,
BooksCount = author.BooksCount,
Books = author.Books.AsQueryable()
.Where(BookWhereFilterExpression)
.Select(BookSelectorExpression)
.ToList(),
Age = author.Age,
Country = author.Country
};
private static readonly Expression<Func<Book, BookDtoOptimized>> BookSelectorExpression = book => new BookDtoOptimized
{
Name = book.Name,
PublishedYear = book.Published.Year
};
private static readonly Expression<Func<Author, AuthorRecordDto>> AuthorRecordDtoSelectorExpression = author =>
new AuthorRecordDto(author.Age,
author.Country,
author.BooksCount,
author.User.UserName,
author.User.FirstName,
author.User.LastName,
author.User.Email,
author.Books.AsQueryable()
.Where(BookWhereFilterExpression)
.Select(BookRecordSelectorExpression)
.ToList());
private static readonly Expression<Func<Book, BookRecordDto>> BookRecordSelectorExpression = book => new BookRecordDto(book.Name, book.Published.Year);
private static readonly Expression<Func<Author, bool>> AuthorWhereFilterExpression = author => (author.Country == Serbia) && (author.Age == Age);
private static readonly Expression<Func<Book, bool>> BookWhereFilterExpression = book => book.Published < EF.Functions.DateFromParts(Year, 1, 1);
private static readonly Func<DbContext, IEnumerable<AuthorDtoOptimized>> Get =
EF.CompileQuery((DbContext db) =>
db.Set<Author>()
.Where(AuthorWhereFilterExpression)
.OrderByDescending(x => x.BooksCount)
.Take(2)
.Select(AuthorDtoSelectorExpression));
private static readonly Func<DbContext, IEnumerable<AuthorDtoOptimized>> GetWithoutExpressions =
EF.CompileQuery((DbContext db) =>
db.Set<Author>()
.Where(author => (author.Country == Serbia) && (author.Age == Age))
.OrderByDescending(x => x.BooksCount)
.Take(2)
.Select(author => new AuthorDtoOptimized
{
UserFirstName = author.User.FirstName,
UserLastName = author.User.LastName,
UserEmail = author.User.Email,
UserName = author.User.UserName,
BooksCount = author.BooksCount,
Books = author.Books.AsQueryable()
.Where(book => book.Published < EF.Functions.DateFromParts(Year, 1, 1))
.Select(y => new BookDtoOptimized
{
Name = y.Name,
PublishedYear = y.Published.Year
})
.ToList(),
Age = author.Age,
Country = author.Country
}));
private static readonly Func<DbContext, string, int, IEnumerable<AuthorDtoOptimized>> GetParameterized =
EF.CompileQuery((DbContext db, string country, int age) =>
db.Set<Author>()
.Where(x => (x.Country == country) && (x.Age == age))
.OrderByDescending(x => x.BooksCount)
.Take(2)
.Select(AuthorDtoSelectorExpression));
private static readonly Func<DbContext, IAsyncEnumerable<AuthorDtoOptimized>> GetAsync =
EF.CompileAsyncQuery((DbContext db) =>
db.Set<Author>()
.Where(AuthorWhereFilterExpression)
.OrderByDescending(x => x.BooksCount)
.Take(2)
.Select(AuthorDtoSelectorExpression));
private static readonly Func<DbContext, IEnumerable<AuthorRecordDto>> GetRecord =
EF.CompileQuery((DbContext db) =>
db.Set<Author>()
.Where(AuthorWhereFilterExpression)
.OrderByDescending(x => x.BooksCount)
.Take(2)
.Select(AuthorRecordDtoSelectorExpression));
private static readonly Func<DbContext, IEnumerable<AuthorRecordDto>> GetRecordWithoutExpressions =
EF.CompileQuery((DbContext db) =>
db.Set<Author>()
.Where(author => (author.Country == Serbia) && (author.Age == Age))
.OrderByDescending(x => x.BooksCount)
.Take(2)
.Select(author =>
new AuthorRecordDto(author.Age,
author.Country,
author.BooksCount,
author.User.UserName,
author.User.FirstName,
author.User.LastName,
author.User.Email,
author.Books.AsQueryable()
.Where(book => book.Published < EF.Functions.DateFromParts(Year, 1, 1))
.Select(y => new BookRecordDto(y.Name, y.Published.Year))
.ToList())));
private static readonly Func<DbContext, IAsyncEnumerable<AuthorRecordDto>> GetRecordAsync =
EF.CompileAsyncQuery((DbContext db) =>
db.Set<Author>()
.Where(AuthorWhereFilterExpression)
.OrderByDescending(x => x.BooksCount)
.Take(2)
.Select(AuthorRecordDtoSelectorExpression));
private static readonly Func<DbContext, string, int, IEnumerable<AuthorRecordDto>> GetRecordParameterized =
EF.CompileQuery((DbContext db, string country, int age) =>
db.Set<Author>()
.Where(x => (x.Country == country) && (x.Age == age))
.OrderByDescending(x => x.BooksCount)
.Take(2)
.Select(AuthorRecordDtoSelectorExpression));
}
}