diff --git a/BenchmarkService.cs b/BenchmarkService.cs index d0a958d..1ec3bcd 100644 --- a/BenchmarkService.cs +++ b/BenchmarkService.cs @@ -1,6 +1,7 @@ using BenchmarkDotNet.Attributes; using Microsoft.EntityFrameworkCore; using OptimizeMePlease.Context; +using System; using System.Collections.Generic; using System.Linq; @@ -86,11 +87,36 @@ public List GetAuthors() } [Benchmark] - public List GetAuthors_Optimized() + public List GetAuthors_Optimized() { - List authors = new List(); + using var context = new AppDbContext(); + var date = new DateTime(1900, 1, 1); + + var authors = context.Authors + .Include(b => b.Books.Where(by => by.Published < date)) + .AsNoTracking() + .Where(a => a.Country == "Serbia" && a.Age == 27) + .OrderByDescending(a => a.BooksCount) + .Select(author => new MyAuthorDTO + { + + UserFirstName = author.User.FirstName, + UserLastName = author.User.LastName, + UserEmail = author.User.Email, + UserName = author.User.UserName, + AuthorAge = author.Age, + AuthorCountry = author.Country, + AllBooks = author.Books.Select(y => new MyBookDTO + { + Name = y.Name, + PublishedYear = y.Published.Year + + }) + }).Take(2).ToList(); + return authors; } } } + diff --git a/Context/AppDbContext.cs b/Context/AppDbContext.cs index a4c4b8b..739ce89 100644 --- a/Context/AppDbContext.cs +++ b/Context/AppDbContext.cs @@ -7,7 +7,7 @@ public class AppDbContext : DbContext { protected override void OnConfiguring(DbContextOptionsBuilder options) { - options.UseSqlServer("Server=localhost;Database=OptimizeMePlease;Trusted_Connection=True;Integrated Security=true;MultipleActiveResultSets=true"); + options.UseSqlServer(@"Data Source=(localdb)\mssqllocaldb;Initial Catalog=OptimizeMePlease;Integrated Security=True"); } protected override void OnModelCreating(ModelBuilder modelBuilder) diff --git a/MyAuthorDTO.cs b/MyAuthorDTO.cs new file mode 100644 index 0000000..f7f250b --- /dev/null +++ b/MyAuthorDTO.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OptimizeMePlease +{ + public class MyAuthorDTO + { + public string UserFirstName { get; set; } + public string UserLastName { get; set; } + public string UserName { get; set; } + public string UserEmail { get; set; } + public int AuthorAge { get; set; } + public string AuthorCountry { get; set; } + public IEnumerable AllBooks { get; set; } + } +} diff --git a/MyBookDTO.cs b/MyBookDTO.cs new file mode 100644 index 0000000..369173c --- /dev/null +++ b/MyBookDTO.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OptimizeMePlease +{ + public class MyBookDTO + { + public string Name { get; set; } + public int PublishedYear { get; set; } + } +} diff --git a/OptimizeMePlease.csproj b/OptimizeMePlease.csproj index 7e4fcc9..77db97e 100644 --- a/OptimizeMePlease.csproj +++ b/OptimizeMePlease.csproj @@ -2,16 +2,16 @@ Exe - netcoreapp3.1 + net6 - - - - - - + + + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/Program.cs b/Program.cs index b05ee2d..6460c92 100644 --- a/Program.cs +++ b/Program.cs @@ -25,17 +25,17 @@ static void Main(string[] args) { //Debugging BenchmarkService benchmarkService = new BenchmarkService(); - benchmarkService.GetAuthors(); + //benchmarkService.GetAuthors(); //Comment me after first execution, please. //IWillPopulateData(); - //BenchmarkRunner.Run(); + BenchmarkRunner.Run(); } public static void IWillPopulateData() { - string sqlConnectionString = @"Server=localhost;Database=OptimizeMePlease;Trusted_Connection=True;Integrated Security=true;MultipleActiveResultSets=true"; + string sqlConnectionString = @"Data Source=(localdb)\mssqllocaldb;Initial Catalog=OptimizeMePlease;Integrated Security=True"; string workingDirectory = Environment.CurrentDirectory; string path = Path.Combine(Directory.GetParent(workingDirectory).Parent.Parent.FullName, @"script.sql");