Skip to content

Commit

Permalink
Resolved #2563: Create UOW middleware for AspNet Core.
Browse files Browse the repository at this point in the history
  • Loading branch information
hikalkan committed Nov 15, 2017
1 parent 87aee2d commit 4e6bcfc
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private static void ConfigureAspNetCore(IServiceCollection services, IIocResolve

//Add feature providers
var partManager = services.GetSingletonServiceOrNull<ApplicationPartManager>();
partManager.FeatureProviders.Add(new AbpAppServiceControllerFeatureProvider(iocResolver));
partManager?.FeatureProviders.Add(new AbpAppServiceControllerFeatureProvider(iocResolver));

//Configure JSON serializer
services.Configure<MvcJsonOptions>(jsonOptions =>
Expand Down
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>();
}
}
}
39 changes: 39 additions & 0 deletions src/Abp.AspNetCore/AspNetCore/Uow/UnitOfWorkMiddleware.cs
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 src/Abp.AspNetCore/AspNetCore/Uow/UnitOfWorkMiddlewareOptions.cs
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 test/Abp.AspNetCore.Tests/Tests/Uow/UnitOfWorkMiddleware_Tests.cs
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
{

}
}
}

0 comments on commit 4e6bcfc

Please sign in to comment.