From 791a0176fde38027eb782d96951df42103cb11a0 Mon Sep 17 00:00:00 2001 From: MuhammedMajid Date: Wed, 28 Sep 2022 13:44:59 +0400 Subject: [PATCH] Muhammed Majid Optimization --- AuthorDTO_Optimized.cs | 15 +++++++++++++++ BenchmarkService.cs | 32 ++++++++++++++++++++++++++++---- BookDto_Optimized.cs | 8 ++++++++ OptimizeMePlease.csproj | 10 +++++----- Program.cs | 6 +++--- 5 files changed, 59 insertions(+), 12 deletions(-) create mode 100644 AuthorDTO_Optimized.cs create mode 100644 BookDto_Optimized.cs diff --git a/AuthorDTO_Optimized.cs b/AuthorDTO_Optimized.cs new file mode 100644 index 0000000..af0e793 --- /dev/null +++ b/AuthorDTO_Optimized.cs @@ -0,0 +1,15 @@ +using System.Collections.Generic; + +namespace OptimizeMePlease +{ + public class AuthorDTO_Optimized + { + 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/BenchmarkService.cs b/BenchmarkService.cs index d0a958d..0d4374c 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; @@ -85,12 +86,35 @@ public List GetAuthors() return finalAuthors; } - [Benchmark] - public List GetAuthors_Optimized() + [Benchmark] + public List GetAuthors_Optimized() { - List authors = new List(); + var cutOfDate = new DateTime(1900, 1, 1); + + using var dbContext = new AppDbContext(); - return authors; + return dbContext.Authors + .Where(x => x.Country == "Serbia" && x.Age == 27) + .OrderByDescending(x => x.BooksCount) + .Take(2) + .Include(x => x.User) + .Include(x => x.Books.Where(xx => xx.Published < cutOfDate)) + .AsNoTracking() + .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, + PublishedYear = y.Published.Year, + }) + }) + .ToList(); } } } diff --git a/BookDto_Optimized.cs b/BookDto_Optimized.cs new file mode 100644 index 0000000..0da79d7 --- /dev/null +++ b/BookDto_Optimized.cs @@ -0,0 +1,8 @@ +namespace OptimizeMePlease +{ + public class BookDto_Optimized + { + public string Name { get; set; } + public int PublishedYear { get; set; } + } +} diff --git a/OptimizeMePlease.csproj b/OptimizeMePlease.csproj index 7e4fcc9..6da1cf9 100644 --- a/OptimizeMePlease.csproj +++ b/OptimizeMePlease.csproj @@ -2,16 +2,16 @@ Exe - netcoreapp3.1 + net6.0 - - - - + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/Program.cs b/Program.cs index b05ee2d..e31c5ce 100644 --- a/Program.cs +++ b/Program.cs @@ -24,13 +24,13 @@ public class Program static void Main(string[] args) { //Debugging - BenchmarkService benchmarkService = new BenchmarkService(); - benchmarkService.GetAuthors(); + //BenchmarkService benchmarkService = new BenchmarkService(); + //benchmarkService.GetAuthors(); //Comment me after first execution, please. //IWillPopulateData(); - //BenchmarkRunner.Run(); + BenchmarkRunner.Run(); } public static void IWillPopulateData()