-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolved #2563: Create UOW middleware for AspNet Core.
- Loading branch information
Showing
5 changed files
with
129 additions
and
1 deletion.
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
18 changes: 18 additions & 0 deletions
18
src/Abp.AspNetCore/AspNetCore/Uow/AbpUnitOfWorkMiddlewareExtensions.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,18 @@ | ||
using System; | ||
using Abp.AspNetCore.Uow; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
|
||
// ReSharper disable once CheckNamespace | ||
namespace Microsoft.AspNetCore.Builder | ||
{ | ||
public static class AbpUnitOfWorkMiddlewareExtensions | ||
{ | ||
public static IApplicationBuilder UseUnitOfWork(this IApplicationBuilder app, Action<UnitOfWorkMiddlewareOptions> optionsAction = null) | ||
{ | ||
var options = app.ApplicationServices.GetRequiredService<IOptions<UnitOfWorkMiddlewareOptions>>().Value; | ||
optionsAction?.Invoke(options); | ||
return app.UseMiddleware<AbpUnitOfWorkMiddleware>(); | ||
} | ||
} | ||
} |
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,39 @@ | ||
using System.Threading.Tasks; | ||
using Abp.Domain.Uow; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Abp.AspNetCore.Uow | ||
{ | ||
public class AbpUnitOfWorkMiddleware | ||
{ | ||
private readonly RequestDelegate _next; | ||
private readonly IUnitOfWorkManager _unitOfWorkManager; | ||
private readonly UnitOfWorkMiddlewareOptions _options; | ||
|
||
public AbpUnitOfWorkMiddleware( | ||
RequestDelegate next, | ||
IUnitOfWorkManager unitOfWorkManager, | ||
IOptions<UnitOfWorkMiddlewareOptions> options) | ||
{ | ||
_next = next; | ||
_unitOfWorkManager = unitOfWorkManager; | ||
_options = options.Value; | ||
} | ||
|
||
public async Task Invoke(HttpContext httpContext) | ||
{ | ||
if (!_options.Filter(httpContext)) | ||
{ | ||
await _next(httpContext); | ||
return; | ||
} | ||
|
||
using (var uow = _unitOfWorkManager.Begin(_options.OptionsFactory(httpContext))) | ||
{ | ||
await _next(httpContext); | ||
await uow.CompleteAsync(); | ||
} | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Abp.AspNetCore/AspNetCore/Uow/UnitOfWorkMiddlewareOptions.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,13 @@ | ||
using System; | ||
using Abp.Domain.Uow; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Abp.AspNetCore.Uow | ||
{ | ||
public class UnitOfWorkMiddlewareOptions | ||
{ | ||
public Func<HttpContext, bool> Filter { get; set; } = context => true; | ||
|
||
public Func<HttpContext, UnitOfWorkOptions> OptionsFactory { get; set; } = context => new UnitOfWorkOptions(); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
test/Abp.AspNetCore.Tests/Tests/Uow/UnitOfWorkMiddleware_Tests.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,58 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Abp.AspNetCore.TestBase; | ||
using Abp.Domain.Uow; | ||
using Abp.Modules; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace Abp.AspNetCore.Tests.Uow | ||
{ | ||
public class UnitOfWorkMiddleware_Tests : AbpAspNetCoreIntegratedTestBase<UnitOfWorkMiddleware_Tests.Startup> | ||
{ | ||
[Fact] | ||
public async Task Current_UnitOfWork_Should_Be_Available_After_UnitOfWork_Middleware() | ||
{ | ||
var response = await Client.GetAsync("/"); | ||
var str = await response.Content.ReadAsStringAsync(); | ||
str.ShouldBe("not-null"); | ||
} | ||
|
||
public class Startup | ||
{ | ||
public IServiceProvider ConfigureServices(IServiceCollection services) | ||
{ | ||
return services.AddAbp<StartupModule>(options => | ||
{ | ||
options.SetupTest(); | ||
}); | ||
} | ||
|
||
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) | ||
{ | ||
app.UseAbp(); | ||
|
||
app.UseUnitOfWork(); | ||
|
||
app.Use(async (context, func) => | ||
{ | ||
await context.Response.WriteAsync( | ||
context.RequestServices.GetRequiredService<IUnitOfWorkManager>().Current == null | ||
? "null" | ||
: "not-null" | ||
); | ||
}); | ||
} | ||
} | ||
|
||
public class StartupModule : AbpModule | ||
{ | ||
|
||
} | ||
} | ||
} |