diff --git a/BenchmarkService.cs b/BenchmarkService.cs index d0a958d..ad63ad2 100644 --- a/BenchmarkService.cs +++ b/BenchmarkService.cs @@ -19,7 +19,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(); @@ -88,9 +88,40 @@ public List GetAuthors() [Benchmark] public List GetAuthors_Optimized() { - List authors = new List(); - - return authors; + using var dbContext = new AppDbContext(); + return dbContext.Authors + .Where(x => x.Country == "Serbia" && x.Age == 27) + .OrderByDescending(x => x.BooksCount) + .Take(2) + .Include(x => x.User) + .ThenInclude(x => x.UserRoles) + .ThenInclude(x => x.Role) + .Include(x => x.Books) + .ThenInclude(x => x.Publisher) + .Select(x => new AuthorDTO { + UserCreated = x.User.Created, + UserEmailConfirmed = x.User.EmailConfirmed, + UserFirstName = x.User.FirstName, + UserLastActivity = x.User.LastActivity, + UserLastName = x.User.LastName, + UserEmail = x.User.Email, + UserName = x.User.UserName, + UserId = x.User.Id, + RoleId = x.User.UserRoles.FirstOrDefault(y => y.UserId == x.UserId).RoleId, + BooksCount = x.BooksCount, + AllBooks = x.Books.Where(y => y.Published.Year < 1900).Select(y => new BookDto { + Id = y.Id, + Name = y.Name, + Published = y.Published, + ISBN = y.ISBN, + PublisherName = y.Publisher.Name + }).ToList(), + AuthorAge = x.Age, + AuthorCountry = x.Country, + AuthorNickName = x.NickName, + Id = x.Id + }) + .ToList(); } } } diff --git a/Context/AppDbContext.cs b/Context/AppDbContext.cs index a4c4b8b..815f574 100644 --- a/Context/AppDbContext.cs +++ b/Context/AppDbContext.cs @@ -5,9 +5,10 @@ namespace OptimizeMePlease.Context { public class AppDbContext : DbContext { + public static readonly string sqlConnectionString = @"Server=.;Database=OptimizeMePlease;Trusted_Connection=True;Integrated Security=true;MultipleActiveResultSets=true"; protected override void OnConfiguring(DbContextOptionsBuilder options) { - options.UseSqlServer("Server=localhost;Database=OptimizeMePlease;Trusted_Connection=True;Integrated Security=true;MultipleActiveResultSets=true"); + options.UseSqlServer(sqlConnectionString); } protected override void OnModelCreating(ModelBuilder modelBuilder) diff --git a/OptimizeMePlease.csproj b/OptimizeMePlease.csproj index 7e4fcc9..16e057e 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..ff9d1d0 100644 --- a/Program.cs +++ b/Program.cs @@ -4,6 +4,7 @@ using Microsoft.SqlServer.Management.Smo; using System; using System.IO; +using OptimizeMePlease.Context; namespace OptimizeMePlease { @@ -24,24 +25,22 @@ 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() { - string sqlConnectionString = @"Server=localhost;Database=OptimizeMePlease;Trusted_Connection=True;Integrated Security=true;MultipleActiveResultSets=true"; - string workingDirectory = Environment.CurrentDirectory; string path = Path.Combine(Directory.GetParent(workingDirectory).Parent.Parent.FullName, @"script.sql"); string script = File.ReadAllText(path); - SqlConnection conn = new SqlConnection(sqlConnectionString); + SqlConnection conn = new SqlConnection(AppDbContext.sqlConnectionString); Server server = new Server(new ServerConnection(conn));