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..eaee420 100644 --- a/BenchmarkService.cs +++ b/BenchmarkService.cs @@ -1,8 +1,11 @@ -using BenchmarkDotNet.Attributes; +using System.Collections.Generic; +using System.Linq; + +using BenchmarkDotNet.Attributes; + using Microsoft.EntityFrameworkCore; + using OptimizeMePlease.Context; -using System.Collections.Generic; -using System.Linq; namespace OptimizeMePlease { @@ -19,7 +22,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 +89,32 @@ 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 + .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 + .Where(b => b.Published.Year < 1900) + .Select(y => new BookDTO_Optimized + { + Name = y.Name, + Published = y.Published.Year + }) + }); - return authors; + return authors.ToList(); } } } diff --git a/BookDto.cs b/BookDto.cs index 7438367..9ee40b1 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 int 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 - +