Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion AuthorDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,20 @@ public class AuthorDTO
public int Id { get; set; }
public int RoleId { get; set; }
public int BooksCount { get; set; }
public List<BookDto> AllBooks { get; set; }
public IEnumerable<BookDto> 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<BookDTO_Optimized> AllBooks { get; set; }
}
}
38 changes: 31 additions & 7 deletions BenchmarkService.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -19,7 +22,7 @@ public BenchmarkService()
/// and all his/her books (Book Name/Title and Publishment Year) published before 1900
/// </summary>
/// <returns></returns>
[Benchmark]
[Benchmark(Baseline = true)]
public List<AuthorDTO> GetAuthors()
{
using var dbContext = new AppDbContext();
Expand Down Expand Up @@ -86,11 +89,32 @@ public List<AuthorDTO> GetAuthors()
}

[Benchmark]
public List<AuthorDTO> GetAuthors_Optimized()
public List<AuthorDTO_Optimized> GetAuthors_Optimized()
{
List<AuthorDTO> authors = new List<AuthorDTO>();
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();
}
}
}
6 changes: 6 additions & 0 deletions BookDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}
7 changes: 5 additions & 2 deletions Context/AppDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using Microsoft.EntityFrameworkCore;
using System;

using Microsoft.EntityFrameworkCore;

using OptimizeMePlease.Entities;

namespace OptimizeMePlease.Context
Expand All @@ -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)
Expand Down
14 changes: 7 additions & 7 deletions OptimizeMePlease.csproj
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.2" />
<PackageReference Include="Bogus" Version="34.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.29" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.29" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.29" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.29">
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.0-rc.1.22426.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.0-rc.1.22426.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0-rc.1.22426.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.0-rc.1.22426.7">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.SqlServer.SqlManagementObjects" Version="161.47027.0" />
<PackageReference Include="Microsoft.SqlServer.SqlManagementObjects" Version="170.7.0-preview" />
</ItemGroup>

</Project>