mirror of https://github.com/abpframework/abp.git
6 changed files with 67 additions and 3 deletions
@ -0,0 +1,12 @@ |
|||
using Volo.Abp.AspNetCore.Mvc.Uow; |
|||
|
|||
namespace Microsoft.AspNetCore.Builder |
|||
{ |
|||
public static class AbpAspNetCoreMvcApplicationBuilderExtensions |
|||
{ |
|||
public static IApplicationBuilder UseUnitOfWork(this IApplicationBuilder app) |
|||
{ |
|||
return app.UseMiddleware<AbpUnitOfWorkMiddleware>(); |
|||
} |
|||
} |
|||
} |
|||
@ -1,10 +1,27 @@ |
|||
using Microsoft.AspNetCore.Mvc; |
|||
using System; |
|||
using Microsoft.AspNetCore.Mvc; |
|||
using Microsoft.Extensions.Logging; |
|||
using Microsoft.Extensions.Logging.Abstractions; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Guids; |
|||
using Volo.Abp.ObjectMapping; |
|||
using Volo.Abp.Uow; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc |
|||
{ |
|||
public abstract class AbpController : Controller, ITransientDependency |
|||
{ |
|||
public IUnitOfWorkManager UnitOfWorkManager { get; set; } |
|||
|
|||
public IObjectMapper ObjectMapper { get; set; } |
|||
|
|||
public IGuidGenerator GuidGenerator { get; set; } |
|||
|
|||
public ILoggerFactory LoggerFactory { get; set; } |
|||
|
|||
protected IUnitOfWork CurrentUnitOfWork => UnitOfWorkManager?.Current; |
|||
|
|||
protected ILogger Logger => _lazyLogger.Value; |
|||
private Lazy<ILogger> _lazyLogger => new Lazy<ILogger>(() => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance, true); |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,19 @@ |
|||
using Microsoft.AspNetCore.Mvc; |
|||
using Shouldly; |
|||
using Volo.Abp.AspNetCore.Mvc; |
|||
|
|||
namespace Volo.Abp.AspNetCore.App |
|||
{ |
|||
[Route("api/unitofwork-test")] |
|||
public class UnitOfWorkTestController : AbpController |
|||
{ |
|||
[HttpGet] |
|||
[Route("ActionRequiresUow")] |
|||
public ActionResult ActionRequiresUow() |
|||
{ |
|||
CurrentUnitOfWork.ShouldNotBeNull(); |
|||
|
|||
return Content("OK"); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,14 @@ |
|||
using System.Threading.Tasks; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.Uow |
|||
{ |
|||
public class UnitOfWorkMiddleware_Tests : AspNetCoreMvcTestBase |
|||
{ |
|||
[Fact] |
|||
public async Task ActionRequiresUow() |
|||
{ |
|||
await GetResponseAsStringAsync("/api/unitofwork-test/ActionRequiresUow"); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue