Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement primary constructor #2003

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
9 changes: 2 additions & 7 deletions src/Benchmarks/Configuration/ConsoleArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@

namespace Benchmarks.Configuration
{
public class ConsoleArgs
public class ConsoleArgs(string[] args)
{
public ConsoleArgs(string[] args)
{
Args = args;
}

public string[] Args { get; }
public string[] Args { get; } = args;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,9 @@

namespace Benchmarks.Configuration
{
public class ConsoleHostScenariosConfiguration : IScenariosConfiguration
public class ConsoleHostScenariosConfiguration(ConsoleArgs args) : IScenariosConfiguration
{
private readonly string[] _args;

public ConsoleHostScenariosConfiguration(ConsoleArgs args)
{
_args = args.Args;
}
private readonly string[] _args = args.Args;

public void ConfigureScenarios(Scenarios scenarios)
{
Expand Down
12 changes: 3 additions & 9 deletions src/Benchmarks/Configuration/EnabledScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,10 @@

namespace Benchmarks.Configuration
{
public class EnabledScenario
public class EnabledScenario(string name, IEnumerable<string> paths)
{
public EnabledScenario(string name, IEnumerable<string> paths)
{
Name = name;
Paths = paths;
}
public string Name { get; } = name;

public string Name { get; }

public IEnumerable<string> Paths { get; }
public IEnumerable<string> Paths { get; } = paths;
}
}
5 changes: 1 addition & 4 deletions src/Benchmarks/Configuration/Scenarios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ namespace Benchmarks.Configuration
{
public class Scenarios
{
public Scenarios(IScenariosConfiguration scenariosConfiguration)
{
scenariosConfiguration.ConfigureScenarios(this);
}
public Scenarios(IScenariosConfiguration scenariosConfiguration) => scenariosConfiguration.ConfigureScenarios(this);

[ScenarioPath("/plaintext")]
public bool Plaintext { get; set; }
Expand Down
15 changes: 3 additions & 12 deletions src/Benchmarks/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,11 @@ public class HomeController : Controller
}).ToList();

[HttpGet("plaintext")]
public IActionResult Plaintext()
{
return new PlainTextActionResult();
}
public IActionResult Plaintext() => new PlainTextActionResult();

[HttpGet("json")]
[Produces("application/json")]
public object Json()
{
return new { message = "Hello, World!" };
}
public object Json() => new { message = "Hello, World!" };

// Note that this produces 4kb data. We're leaving the misnamed scenario as is to avoid loosing historical context
[HttpGet("json2k")]
Expand All @@ -76,10 +70,7 @@ public object Json()
public ActionResult JsonInput([FromBody] List<Entry> entry) => Ok();

[HttpGet("view")]
public ViewResult Index()
{
return View();
}
public ViewResult Index() => View();

private class PlainTextActionResult : IActionResult
{
Expand Down
15 changes: 3 additions & 12 deletions src/Benchmarks/Controllers/MultipleQueriesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,15 @@ public class MultipleQueriesController : Controller
{
[HttpGet("raw")]
[Produces("application/json")]
public Task<World[]> Raw(int queries = 1)
{
return ExecuteQuery<RawDb>(queries);
}
public Task<World[]> Raw(int queries = 1) => ExecuteQuery<RawDb>(queries);

[HttpGet("dapper")]
[Produces("application/json")]
public Task<World[]> Dapper(int queries = 1)
{
return ExecuteQuery<DapperDb>(queries);
}
public Task<World[]> Dapper(int queries = 1) => ExecuteQuery<DapperDb>(queries);

[HttpGet("ef")]
[Produces("application/json")]
public Task<World[]> Ef(int queries = 1)
{
return ExecuteQuery<EfDb>(queries);
}
public Task<World[]> Ef(int queries = 1) => ExecuteQuery<EfDb>(queries);

private Task<World[]> ExecuteQuery<T>(int queries) where T : IDb
{
Expand Down
15 changes: 3 additions & 12 deletions src/Benchmarks/Controllers/MultipleUpdatesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,15 @@ public class MultipleUpdatesController : Controller
{
[HttpGet("raw")]
[Produces("application/json")]
public Task<World[]> Raw(int queries = 1)
{
return ExecuteQuery<RawDb>(queries);
}
public Task<World[]> Raw(int queries = 1) => ExecuteQuery<RawDb>(queries);

[HttpGet("dapper")]
[Produces("application/json")]
public Task<World[]> Dapper(int queries = 1)
{
return ExecuteQuery<DapperDb>(queries);
}
public Task<World[]> Dapper(int queries = 1) => ExecuteQuery<DapperDb>(queries);

[HttpGet("ef")]
[Produces("application/json")]
public Task<World[]> Ef(int queries = 1)
{
return ExecuteQuery<EfDb>(queries);
}
public Task<World[]> Ef(int queries = 1) => ExecuteQuery<EfDb>(queries);

private Task<World[]> ExecuteQuery<T>(int queries) where T : IDb
{
Expand Down
15 changes: 3 additions & 12 deletions src/Benchmarks/Controllers/SingleQueryController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,15 @@ public class SingleQueryController : Controller
{
[HttpGet("raw")]
[Produces("application/json")]
public Task<World> Raw()
{
return ExecuteQuery<RawDb>();
}
public Task<World> Raw() => ExecuteQuery<RawDb>();

[HttpGet("dapper")]
[Produces("application/json")]
public Task<World> Dapper()
{
return ExecuteQuery<DapperDb>();
}
public Task<World> Dapper() => ExecuteQuery<DapperDb>();

[HttpGet("ef")]
[Produces("application/json")]
public Task<World> Ef()
{
return ExecuteQuery<EfDb>();
}
public Task<World> Ef() => ExecuteQuery<EfDb>();

private Task<World> ExecuteQuery<T>() where T : IDb
{
Expand Down
21 changes: 6 additions & 15 deletions src/Benchmarks/Data/DapperDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,13 @@

namespace Benchmarks.Data
{
public class DapperDb : IDb
public class DapperDb(IRandom random, DbProviderFactory dbProviderFactory, IOptions<AppSettings> appSettings) : IDb
{
private static readonly Comparison<World> WorldSortComparison = (a, b) => a.Id.CompareTo(b.Id);

private readonly IRandom _random;
private readonly DbProviderFactory _dbProviderFactory;
private readonly string _connectionString;

public DapperDb(IRandom random, DbProviderFactory dbProviderFactory, IOptions<AppSettings> appSettings)
{
_random = random;
_dbProviderFactory = dbProviderFactory;
_connectionString = appSettings.Value.ConnectionString;
}
private readonly IRandom _random = random;
private readonly DbProviderFactory _dbProviderFactory = dbProviderFactory;
private readonly string _connectionString = appSettings.Value.ConnectionString;

public async Task<World> LoadSingleQueryRow()
{
Expand All @@ -38,12 +31,10 @@ public async Task<World> LoadSingleQueryRow()
}
}

Task<World> ReadSingleRow(DbConnection db)
{
return db.QueryFirstOrDefaultAsync<World>(
Task<World> ReadSingleRow(DbConnection db) =>
db.QueryFirstOrDefaultAsync<World>(
"SELECT id, randomnumber FROM world WHERE id = @Id",
new { Id = _random.Next(1, 10001) });
}

public async Task<World[]> LoadMultipleQueriesRows(int count)
{
Expand Down
13 changes: 3 additions & 10 deletions src/Benchmarks/Data/EfDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,10 @@

namespace Benchmarks.Data
{
public class EfDb : IDb
public class EfDb(IRandom random, ApplicationDbContext dbContext, IOptions<AppSettings> appSettings) : IDb
{
private readonly IRandom _random;
private readonly ApplicationDbContext _dbContext;

public EfDb(IRandom random, ApplicationDbContext dbContext, IOptions<AppSettings> appSettings)
{
_random = random;
_dbContext = dbContext;
}

private readonly IRandom _random = random;
private readonly ApplicationDbContext _dbContext = dbContext;
private static readonly Func<ApplicationDbContext, int, Task<World>> _firstWorldQuery
= EF.CompileAsyncQuery((ApplicationDbContext context, int id)
=> context.World.First(w => w.Id == id));
Expand Down
12 changes: 3 additions & 9 deletions src/Benchmarks/Data/Fortune.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,9 @@ public class Fortune : IComparable<Fortune>, IComparable
[IgnoreDataMember]
[Required]
public string Message { get; set; }

public int CompareTo(object obj)
{
return CompareTo((Fortune)obj);
}

public int CompareTo(Fortune other)
{
return String.CompareOrdinal(Message, other.Message);
}
public int CompareTo(object obj) => CompareTo((Fortune)obj);

public int CompareTo(Fortune other) => String.CompareOrdinal(Message, other.Message);
}
}
5 changes: 1 addition & 4 deletions src/Benchmarks/Data/Random.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ private static Random CreateRandom()
return _random;
}

public int Next(int minValue, int maxValue)
{
return Random.Next(minValue, maxValue);
}
public int Next(int minValue, int maxValue) => Random.Next(minValue, maxValue);
}
}
15 changes: 4 additions & 11 deletions src/Benchmarks/Data/RawDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,13 @@

namespace Benchmarks.Data
{
public class RawDb : IDb
public class RawDb(IRandom random, DbProviderFactory dbProviderFactory, IOptions<AppSettings> appSettings) : IDb
{
private static readonly Comparison<World> WorldSortComparison = (a, b) => a.Id.CompareTo(b.Id);

private readonly IRandom _random;
private readonly DbProviderFactory _dbProviderFactory;
private readonly string _connectionString;

public RawDb(IRandom random, DbProviderFactory dbProviderFactory, IOptions<AppSettings> appSettings)
{
_random = random;
_dbProviderFactory = dbProviderFactory;
_connectionString = appSettings.Value.ConnectionString;
}
private readonly IRandom _random = random;
private readonly DbProviderFactory _dbProviderFactory = dbProviderFactory;
private readonly string _connectionString = appSettings.Value.ConnectionString;

public async Task<World> LoadSingleQueryRow()
{
Expand Down
15 changes: 4 additions & 11 deletions src/Benchmarks/Middleware/AutoShutdownMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,12 @@

namespace Benchmarks.Middleware
{
public class AutoShutdownMiddleware
public class AutoShutdownMiddleware(RequestDelegate next)
{
private static readonly PathString _path = new PathString("/shutdown");

private readonly RequestDelegate _next;
private readonly RequestDelegate _next = next;

public AutoShutdownMiddleware(RequestDelegate next)
{
_next = next;
}

public async Task Invoke(HttpContext httpContext)
{
if (httpContext.Request.Path.StartsWithSegments(_path, StringComparison.Ordinal))
Expand All @@ -42,9 +37,7 @@ public async Task Invoke(HttpContext httpContext)

public static class AutoShutdownMiddlewareExtensions
{
public static IApplicationBuilder UseAutoShutdown(this IApplicationBuilder builder)
{
return builder.UseMiddleware<AutoShutdownMiddleware>();
}
public static IApplicationBuilder UseAutoShutdown(this IApplicationBuilder builder) =>
builder.UseMiddleware<AutoShutdownMiddleware>();
}
}
15 changes: 4 additions & 11 deletions src/Benchmarks/Middleware/CopyToAsyncMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,11 @@

namespace Benchmarks.Middleware
{
public class CopyToAsyncMiddleware
public class CopyToAsyncMiddleware(RequestDelegate next)
{
private static readonly PathString _path = new PathString(Scenarios.GetPath(s => s.CopyToAsync));

private readonly RequestDelegate _next;

public CopyToAsyncMiddleware(RequestDelegate next)
{
_next = next;
}
private readonly RequestDelegate _next = next;

public async Task Invoke(HttpContext httpContext)
{
Expand Down Expand Up @@ -47,9 +42,7 @@ public async Task Invoke(HttpContext httpContext)

public static class CopyToAsyncMiddlewareMiddlewareExtensions
{
public static IApplicationBuilder UseCopyToAsync(this IApplicationBuilder builder)
{
return builder.UseMiddleware<CopyToAsyncMiddleware>();
}
public static IApplicationBuilder UseCopyToAsync(this IApplicationBuilder builder) =>
builder.UseMiddleware<CopyToAsyncMiddleware>();
}
}
18 changes: 5 additions & 13 deletions src/Benchmarks/Middleware/FortunesDapperMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,12 @@

namespace Benchmarks.Middleware
{
public class FortunesDapperMiddleware
public class FortunesDapperMiddleware(RequestDelegate next, HtmlEncoder htmlEncoder)
{
private static readonly PathString _path = new PathString(Scenarios.GetPath(s => s.DbFortunesDapper));

private readonly RequestDelegate _next;
private readonly HtmlEncoder _htmlEncoder;

public FortunesDapperMiddleware(RequestDelegate next, HtmlEncoder htmlEncoder)
{
_next = next;
_htmlEncoder = htmlEncoder;
}
private readonly RequestDelegate _next = next;
private readonly HtmlEncoder _htmlEncoder = htmlEncoder;

public async Task Invoke(HttpContext httpContext)
{
Expand All @@ -43,9 +37,7 @@ public async Task Invoke(HttpContext httpContext)

public static class FortunesDapperMiddlewareExtensions
{
public static IApplicationBuilder UseFortunesDapper(this IApplicationBuilder builder)
{
return builder.UseMiddleware<FortunesDapperMiddleware>();
}
public static IApplicationBuilder UseFortunesDapper(this IApplicationBuilder builder) =>
builder.UseMiddleware<FortunesDapperMiddleware>();
}
}
Loading