Skip to content

Commit

Permalink
Add Dapper implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ThatRendle committed Jan 7, 2018
1 parent 30d42dd commit 483b0ef
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
<MicrosoftNETPlatformLibrary>Microsoft.NETCore.App</MicrosoftNETPlatformLibrary>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dapper" Version="1.50.4" />
<PackageReference Include="JetBrains.Annotations" Version="11.1.0" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.1.0-*" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="Npgsql" Version="3.2.6" />
</ItemGroup>
<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion samples/Beeline.AspNetCoreBenchmarks/Data/BeelineDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public async Task<int> LoadSingleQueryRow(byte[] buffer, CancellationToken ct =
private static DbCommand CreateSingleQueryCommand(DbConnection db, int id)
{
var cmd = db.CreateCommand();
cmd.CommandText = "SELECT id, randomnumber FROM world WHERE id = @id";
cmd.CommandText = @"SELECT id ""Id"", randomnumber ""RandomNumber"" FROM world WHERE id = @id";
var idParameter = cmd.CreateParameter();
idParameter.ParameterName = "id";
idParameter.DbType = DbType.Int32;
Expand Down
40 changes: 40 additions & 0 deletions samples/Beeline.AspNetCoreBenchmarks/Data/DapperDb.cs
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) });
}
}
}
12 changes: 12 additions & 0 deletions samples/Beeline.AspNetCoreBenchmarks/Data/World.cs
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; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace Beeline.AspNetCoreBenchmarks.Middleware
{
Expand Down Expand Up @@ -48,7 +46,7 @@ public async Task Invoke(HttpContext httpContext)
}
}

public static class SingleQueryDapperMiddlewareExtensions
public static class SingleQueryBeelineMiddlewareExtensions
{
public static IApplicationBuilder UseSingleQueryBeeline(this IApplicationBuilder builder)
{
Expand Down
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>();
}
}
}
2 changes: 2 additions & 0 deletions samples/Beeline.AspNetCoreBenchmarks/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ public void ConfigureServices(IServiceCollection services)
services.AddSingleton<IRandom, DefaultRandom>();
services.AddSingleton<DbProviderFactory>(NpgsqlFactory.Instance);
services.AddSingleton<BeelineDb>();
services.AddSingleton<DapperDb>();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseSingleQueryDapper();
app.UseSingleQueryBeeline();
}
}
Expand Down

0 comments on commit 483b0ef

Please sign in to comment.