Skip to content
Merged
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
8 changes: 3 additions & 5 deletions src/Benchmarks/Middleware/FortunesDapperMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Benchmarks.Configuration;
using Benchmarks.Data;
using Benchmarks.Templates;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using RazorSlices;

namespace Benchmarks.Middleware
{
Expand All @@ -20,13 +19,11 @@ public class FortunesDapperMiddleware

private readonly RequestDelegate _next;
private readonly HtmlEncoder _htmlEncoder;
private readonly SliceFactory<IEnumerable<FortuneUtf16>> _fortunesFactory;

public FortunesDapperMiddleware(RequestDelegate next, HtmlEncoder htmlEncoder)
{
_next = next;
_htmlEncoder = htmlEncoder;
_fortunesFactory = RazorSlice.ResolveSliceFactory<IEnumerable<FortuneUtf16>>("/Templates/FortunesUtf16.cshtml");
}

public async Task Invoke(HttpContext httpContext)
Expand All @@ -35,8 +32,9 @@ public async Task Invoke(HttpContext httpContext)
{
var db = httpContext.RequestServices.GetService<DapperDb>();
var rows = await db.LoadFortunesRows();


await MiddlewareHelpers.RenderFortunesHtml(rows, httpContext, _htmlEncoder, _fortunesFactory);
await MiddlewareHelpers.RenderFortunesHtml(rows, httpContext, _htmlEncoder, FortunesUtf16.Create);

return;
}
Expand Down
6 changes: 1 addition & 5 deletions src/Benchmarks/Middleware/FortunesEfMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Benchmarks.Configuration;
using Benchmarks.Data;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using RazorSlices;

namespace Benchmarks.Middleware
{
Expand All @@ -20,13 +18,11 @@ public class FortunesEfMiddleware

private readonly RequestDelegate _next;
private readonly HtmlEncoder _htmlEncoder;
private readonly SliceFactory<IEnumerable<Fortune>> _fortunesFactory;

public FortunesEfMiddleware(RequestDelegate next, HtmlEncoder htmlEncoder)
{
_next = next;
_htmlEncoder = htmlEncoder;
_fortunesFactory = RazorSlice.ResolveSliceFactory<IEnumerable<Fortune>>("/Templates/Fortunes.cshtml");
}

public async Task Invoke(HttpContext httpContext)
Expand All @@ -36,7 +32,7 @@ public async Task Invoke(HttpContext httpContext)
var db = httpContext.RequestServices.GetService<EfDb>();
var rows = await db.LoadFortunesRows();

await MiddlewareHelpers.RenderFortunesHtml(rows, httpContext, _htmlEncoder, _fortunesFactory);
await MiddlewareHelpers.RenderFortunesHtml(rows, httpContext, _htmlEncoder, Templates.Fortunes.Create);

return;
}
Expand Down
6 changes: 1 addition & 5 deletions src/Benchmarks/Middleware/FortunesRawMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Benchmarks.Configuration;
using Benchmarks.Data;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using RazorSlices;

namespace Benchmarks.Middleware
{
Expand All @@ -20,13 +18,11 @@ public class FortunesRawMiddleware

private readonly RequestDelegate _next;
private readonly HtmlEncoder _htmlEncoder;
private readonly SliceFactory<IEnumerable<Fortune>> _fortunesFactory;

public FortunesRawMiddleware(RequestDelegate next, HtmlEncoder htmlEncoder)
{
_next = next;
_htmlEncoder = htmlEncoder;
_fortunesFactory = RazorSlice.ResolveSliceFactory<IEnumerable<Fortune>>("/Templates/Fortunes.cshtml");
}

public async Task Invoke(HttpContext httpContext)
Expand All @@ -36,7 +32,7 @@ public async Task Invoke(HttpContext httpContext)
var db = httpContext.RequestServices.GetService<RawDb>();
var rows = await db.LoadFortunesRows();

await MiddlewareHelpers.RenderFortunesHtml(rows, httpContext, _htmlEncoder, _fortunesFactory);
await MiddlewareHelpers.RenderFortunesHtml(rows, httpContext, _htmlEncoder, Templates.Fortunes.Create);

return;
}
Expand Down
6 changes: 1 addition & 5 deletions src/Benchmarks/Middleware/FortunesRawSyncMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Benchmarks.Configuration;
using Benchmarks.Data;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using RazorSlices;

namespace Benchmarks.Middleware
{
Expand All @@ -20,13 +18,11 @@ public class FortunesRawSyncMiddleware

private readonly RequestDelegate _next;
private readonly HtmlEncoder _htmlEncoder;
private readonly SliceFactory<IEnumerable<Fortune>> _fortunesFactory;

public FortunesRawSyncMiddleware(RequestDelegate next, HtmlEncoder htmlEncoder)
{
_next = next;
_htmlEncoder = htmlEncoder;
_fortunesFactory = RazorSlice.ResolveSliceFactory<IEnumerable<Fortune>>("/Templates/Fortunes.cshtml");
}

public Task Invoke(HttpContext httpContext)
Expand All @@ -36,7 +32,7 @@ public Task Invoke(HttpContext httpContext)
var db = httpContext.RequestServices.GetService<RawDb>();
var rows = db.LoadFortunesRowsSync();

return MiddlewareHelpers.RenderFortunesHtml(rows, httpContext, _htmlEncoder, _fortunesFactory);
return MiddlewareHelpers.RenderFortunesHtml(rows, httpContext, _htmlEncoder, Templates.Fortunes.Create);
}

return _next(httpContext);
Expand Down
7 changes: 4 additions & 3 deletions src/Benchmarks/Middleware/MiddlewareHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
Expand Down Expand Up @@ -30,13 +31,13 @@ public static int GetMultipleQueriesQueryCount(HttpContext httpContext)
}

public static async Task RenderFortunesHtml<T>(IEnumerable<T> model, HttpContext httpContext,
HtmlEncoder htmlEncoder, SliceFactory<IEnumerable<T>> fortunesFactory)
HtmlEncoder htmlEncoder, Func<IEnumerable<T>, RazorSlice> templateFactory)
{
httpContext.Response.StatusCode = StatusCodes.Status200OK;
httpContext.Response.ContentType = "text/html; charset=UTF-8";

using var template = fortunesFactory(model);
await template.RenderToPipeWriterAsync(httpContext.Response.BodyWriter, htmlEncoder);
using var template = templateFactory(model);
await template.RenderAsync(httpContext.Response.BodyWriter, htmlEncoder);
await httpContext.Response.BodyWriter.FlushAsync();
}
}
Expand Down