From 4075d2cf95b75636dbd79a205b8955e05a20b661 Mon Sep 17 00:00:00 2001 From: Panagiotis Efthymiou Date: Thu, 29 Sep 2022 20:53:13 +0300 Subject: [PATCH 1/2] pefthymiou optimizations --- AuthorDTO.cs | 13 ++++++++++++- BenchmarkService.cs | 42 ++++++++++++++++++++++++++++++++++------- BookDto.cs | 6 ++++++ Context/AppDbContext.cs | 7 +++++-- OptimizeMePlease.csproj | 14 +++++++------- 5 files changed, 65 insertions(+), 17 deletions(-) diff --git a/AuthorDTO.cs b/AuthorDTO.cs index dc6416c..a393d7c 100644 --- a/AuthorDTO.cs +++ b/AuthorDTO.cs @@ -17,9 +17,20 @@ public class AuthorDTO public int Id { get; set; } public int RoleId { get; set; } public int BooksCount { get; set; } - public List AllBooks { get; set; } + public IEnumerable AllBooks { get; set; } public int AuthorAge { get; set; } public string AuthorCountry { get; set; } public string AuthorNickName { get; set; } } + + public sealed class AuthorDTO_Optimized + { + public string UserFirstName { get; set; } + public string UserLastName { get; set; } + public string UserEmail { get; set; } + public string UserName { get; set; } + public int AuthorAge { get; set; } + public string AuthorCountry { get; set; } + public IEnumerable AllBooks { get; set; } + } } diff --git a/BenchmarkService.cs b/BenchmarkService.cs index d0a958d..6bbb47b 100644 --- a/BenchmarkService.cs +++ b/BenchmarkService.cs @@ -1,14 +1,20 @@ -using BenchmarkDotNet.Attributes; -using Microsoft.EntityFrameworkCore; -using OptimizeMePlease.Context; +using System; using System.Collections.Generic; using System.Linq; +using BenchmarkDotNet.Attributes; + +using Microsoft.EntityFrameworkCore; + +using OptimizeMePlease.Context; + namespace OptimizeMePlease { [MemoryDiagnoser] public class BenchmarkService { + private readonly DateTime _publishedDate = new(1900, 1, 1); + public BenchmarkService() { } @@ -19,7 +25,7 @@ public BenchmarkService() /// and all his/her books (Book Name/Title and Publishment Year) published before 1900 /// /// - [Benchmark] + [Benchmark(Baseline = true)] public List GetAuthors() { using var dbContext = new AppDbContext(); @@ -86,11 +92,33 @@ public List GetAuthors() } [Benchmark] - public List GetAuthors_Optimized() + public List GetAuthors_Optimized() { - List authors = new List(); + using var dbContext = new AppDbContext(); + + var authors = dbContext.Authors + .Include(x => x.Books.Where(b => b.Published < _publishedDate)) + .AsNoTracking() + .Where(x => x.Country == "Serbia" && x.Age == 27) + .OrderByDescending(x => x.BooksCount) + .Take(2) + .Select(x => new AuthorDTO_Optimized + { + UserFirstName = x.User.FirstName, + UserLastName = x.User.LastName, + UserName = x.User.UserName, + UserEmail = x.User.Email, + AuthorAge = x.Age, + AuthorCountry = x.Country, + AllBooks = x.Books + .Select(y => new BookDTO_Optimized + { + Name = y.Name, + Published = y.Published + }) + }); - return authors; + return authors.ToList(); } } } diff --git a/BookDto.cs b/BookDto.cs index 7438367..89608df 100644 --- a/BookDto.cs +++ b/BookDto.cs @@ -11,4 +11,10 @@ public class BookDto public string PublisherName { get; set; } public string ISBN { get; set; } } + + public sealed class BookDTO_Optimized + { + public string Name { get; set; } + public DateTime Published { get; set; } + } } diff --git a/Context/AppDbContext.cs b/Context/AppDbContext.cs index a4c4b8b..7cfdf01 100644 --- a/Context/AppDbContext.cs +++ b/Context/AppDbContext.cs @@ -1,4 +1,7 @@ -using Microsoft.EntityFrameworkCore; +using System; + +using Microsoft.EntityFrameworkCore; + using OptimizeMePlease.Entities; namespace OptimizeMePlease.Context @@ -7,7 +10,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(@"Server=(localdb)\MSSQLLocalDB;Database=OptimizeMePlease;Trusted_Connection=True;Integrated Security=true;MultipleActiveResultSets=true"); } protected override void OnModelCreating(ModelBuilder modelBuilder) diff --git a/OptimizeMePlease.csproj b/OptimizeMePlease.csproj index 7e4fcc9..98738c8 100644 --- a/OptimizeMePlease.csproj +++ b/OptimizeMePlease.csproj @@ -1,21 +1,21 @@ - + Exe - netcoreapp3.1 + net7.0 - - - - + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - + From 2ecb615f2f156fdf587777fe7dabe12ac90a8fa3 Mon Sep 17 00:00:00 2001 From: Panagiotis Efthymiou Date: Tue, 4 Oct 2022 23:13:28 +0300 Subject: [PATCH 2/2] Fix filtering --- BenchmarkService.cs | 10 +++------- BookDto.cs | 2 +- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/BenchmarkService.cs b/BenchmarkService.cs index 6bbb47b..eaee420 100644 --- a/BenchmarkService.cs +++ b/BenchmarkService.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using BenchmarkDotNet.Attributes; @@ -13,8 +12,6 @@ namespace OptimizeMePlease [MemoryDiagnoser] public class BenchmarkService { - private readonly DateTime _publishedDate = new(1900, 1, 1); - public BenchmarkService() { } @@ -97,8 +94,6 @@ public List GetAuthors_Optimized() using var dbContext = new AppDbContext(); var authors = dbContext.Authors - .Include(x => x.Books.Where(b => b.Published < _publishedDate)) - .AsNoTracking() .Where(x => x.Country == "Serbia" && x.Age == 27) .OrderByDescending(x => x.BooksCount) .Take(2) @@ -111,10 +106,11 @@ public List GetAuthors_Optimized() AuthorAge = x.Age, AuthorCountry = x.Country, AllBooks = x.Books + .Where(b => b.Published.Year < 1900) .Select(y => new BookDTO_Optimized { Name = y.Name, - Published = y.Published + Published = y.Published.Year }) }); diff --git a/BookDto.cs b/BookDto.cs index 89608df..9ee40b1 100644 --- a/BookDto.cs +++ b/BookDto.cs @@ -15,6 +15,6 @@ public class BookDto public sealed class BookDTO_Optimized { public string Name { get; set; } - public DateTime Published { get; set; } + public int Published { get; set; } } }