-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
30d42dd
commit 483b0ef
Showing
7 changed files
with
114 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System.Data.Common; | ||
using System.Threading.Tasks; | ||
using Beeline.AspNetCoreBenchmarks.Configuration; | ||
using Dapper; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Beeline.AspNetCoreBenchmarks.Data | ||
{ | ||
public class DapperDb | ||
{ | ||
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; | ||
} | ||
|
||
public async Task<World> LoadSingleQueryRow() | ||
{ | ||
using (var db = _dbProviderFactory.CreateConnection()) | ||
{ | ||
db.ConnectionString = _connectionString; | ||
|
||
// Note: Don't need to open connection if only doing one thing; let dapper do it | ||
return await ReadSingleRow(db); | ||
} | ||
} | ||
|
||
async Task<World> ReadSingleRow(DbConnection db) | ||
{ | ||
return await db.QueryFirstOrDefaultAsync<World>( | ||
"SELECT id, randomnumber FROM world WHERE id = @Id", | ||
new { Id = _random.Next(1, 10001) }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using JetBrains.Annotations; | ||
|
||
namespace Beeline.AspNetCoreBenchmarks.Data | ||
{ | ||
[UsedImplicitly] | ||
public class World | ||
{ | ||
public int Id { get; set; } | ||
|
||
public int RandomNumber { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
samples/Beeline.AspNetCoreBenchmarks/Middleware/SingleQueryDapperMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Beeline.AspNetCoreBenchmarks.Data; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Serialization; | ||
|
||
namespace Beeline.AspNetCoreBenchmarks.Middleware | ||
{ | ||
public class SingleQueryDapperMiddleware | ||
{ | ||
private static readonly PathString _path = new PathString("/db/dapper"); | ||
private static readonly JsonSerializerSettings _jsonSettings = new JsonSerializerSettings | ||
{ | ||
ContractResolver = new CamelCasePropertyNamesContractResolver() | ||
}; | ||
|
||
private readonly RequestDelegate _next; | ||
|
||
public SingleQueryDapperMiddleware(RequestDelegate next) | ||
{ | ||
_next = next; | ||
} | ||
|
||
public async Task Invoke(HttpContext httpContext) | ||
{ | ||
if (httpContext.Request.Path.StartsWithSegments(_path, StringComparison.Ordinal)) | ||
{ | ||
var db = httpContext.RequestServices.GetService<DapperDb>(); | ||
var row = await db.LoadSingleQueryRow(); | ||
|
||
var result = JsonConvert.SerializeObject(row, _jsonSettings); | ||
|
||
httpContext.Response.StatusCode = StatusCodes.Status200OK; | ||
httpContext.Response.ContentType = "application/json"; | ||
httpContext.Response.ContentLength = result.Length; | ||
|
||
await httpContext.Response.WriteAsync(result); | ||
|
||
return; | ||
} | ||
|
||
await _next(httpContext); | ||
} | ||
} | ||
|
||
public static class SingleQueryDapperMiddlewareExtensions | ||
{ | ||
public static IApplicationBuilder UseSingleQueryDapper(this IApplicationBuilder builder) | ||
{ | ||
return builder.UseMiddleware<SingleQueryDapperMiddleware>(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters