Browse Source

#23: UOW Action filter should be able to work without middleware too.

pull/112/head
Halil İbrahim Kalkan 9 years ago
parent
commit
1117073201
  1. 2
      src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUnitOfWorkMiddleware.cs
  2. 24
      src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUowActionFilter.cs
  3. 5
      src/Volo.Abp/Volo/Abp/Uow/IUnitOfWorkManager.cs
  4. 14
      src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkManager.cs
  5. 3
      test/Volo.Abp.Tests/Volo/Abp/Uow/UnitOfWork_Ambient_Scope_Tests.cs

2
src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUnitOfWorkMiddleware.cs

@ -18,7 +18,7 @@ namespace Volo.Abp.AspNetCore.Mvc.Uow
using (var uow = unitOfWorkManager.Reserve(AbpUowActionFilter.UnitOfWorkReservationName)) using (var uow = unitOfWorkManager.Reserve(AbpUowActionFilter.UnitOfWorkReservationName))
{ {
await _next(httpContext); await _next(httpContext);
await uow.CompleteAsync(); await uow.CompleteAsync(httpContext.RequestAborted);
} }
} }
} }

24
src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUowActionFilter.cs

@ -18,8 +18,28 @@ namespace Volo.Abp.AspNetCore.Mvc.Uow
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{ {
_unitOfWorkManager.BeginReserved(UnitOfWorkReservationName); if (!context.ActionDescriptor.IsControllerAction())
await next(); {
await next();
return;
}
if (_unitOfWorkManager.TryBeginReserved(UnitOfWorkReservationName))
{
await next();
return;
}
//TODO: Check if disabled. Get and apply attributes to control UOW.
using (var uow = _unitOfWorkManager.Begin())
{
var result = await next();
if (result.Exception == null || result.ExceptionHandled)
{
await uow.CompleteAsync(context.HttpContext.RequestAborted);
}
}
} }
} }
} }

5
src/Volo.Abp/Volo/Abp/Uow/IUnitOfWorkManager.cs

@ -10,7 +10,8 @@ namespace Volo.Abp.Uow
[NotNull] [NotNull]
IBasicUnitOfWork Begin([NotNull] UnitOfWorkStartOptions options); IBasicUnitOfWork Begin([NotNull] UnitOfWorkStartOptions options);
[NotNull] void BeginReserved([NotNull] string reservationName);
IBasicUnitOfWork BeginReserved([NotNull] string reservationName);
bool TryBeginReserved([NotNull] string reservationName);
} }
} }

14
src/Volo.Abp/Volo/Abp/Uow/UnitOfWorkManager.cs

@ -35,7 +35,15 @@ namespace Volo.Abp.Uow
return CreateUnitOfWork(options); return CreateUnitOfWork(options);
} }
public IBasicUnitOfWork BeginReserved(string reservationName) public void BeginReserved(string reservationName)
{
if (!TryBeginReserved(reservationName))
{
throw new AbpException($"Could not find a reserved unit of work with reservation name: {reservationName}");
}
}
public bool TryBeginReserved(string reservationName)
{ {
Check.NotNull(reservationName, nameof(reservationName)); Check.NotNull(reservationName, nameof(reservationName));
@ -49,11 +57,11 @@ namespace Volo.Abp.Uow
if (uow == null) if (uow == null)
{ {
throw new AbpException($"Could not find a reserved unit of work with reservation name: {reservationName}"); return false;
} }
uow.IsReserved = false; uow.IsReserved = false;
return uow; return true;
} }
private IUnitOfWork GetCurrentUnitOfWork() private IUnitOfWork GetCurrentUnitOfWork()

3
test/Volo.Abp.Tests/Volo/Abp/Uow/UnitOfWork_Ambient_Scope_Tests.cs

@ -62,10 +62,9 @@ namespace Volo.Abp.Uow
_unitOfWorkManager.Current.ShouldBeNull(); _unitOfWorkManager.Current.ShouldBeNull();
var reserverUow = _unitOfWorkManager.BeginReserved("Reservation1"); _unitOfWorkManager.BeginReserved("Reservation1");
_unitOfWorkManager.Current.ShouldNotBeNull(); _unitOfWorkManager.Current.ShouldNotBeNull();
_unitOfWorkManager.Current.Id.ShouldBe(reserverUow.Id);
_unitOfWorkManager.Current.Id.ShouldBe(uow1.Id); _unitOfWorkManager.Current.Id.ShouldBe(uow1.Id);
await uow1.CompleteAsync(); await uow1.CompleteAsync();

Loading…
Cancel
Save