diff --git a/src/Volo.Abp.AspNetCore.Mvc/Microsoft/AspNetCore/Builder/AbpAspNetCoreMvcApplicationBuilderExtensions.cs b/src/Volo.Abp.AspNetCore.Mvc/Microsoft/AspNetCore/Builder/AbpAspNetCoreMvcApplicationBuilderExtensions.cs new file mode 100644 index 0000000000..34c669da52 --- /dev/null +++ b/src/Volo.Abp.AspNetCore.Mvc/Microsoft/AspNetCore/Builder/AbpAspNetCoreMvcApplicationBuilderExtensions.cs @@ -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(); + } + } +} diff --git a/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpController.cs b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpController.cs index d3d49950cf..47901a01c3 100644 --- a/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpController.cs +++ b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpController.cs @@ -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 _lazyLogger => new Lazy(() => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance, true); } } diff --git a/src/Volo.Abp/Volo/Abp/AbpServiceBase.cs b/src/Volo.Abp/Volo/Abp/AbpServiceBase.cs index 097e60aea4..7911c7a366 100644 --- a/src/Volo.Abp/Volo/Abp/AbpServiceBase.cs +++ b/src/Volo.Abp/Volo/Abp/AbpServiceBase.cs @@ -15,10 +15,10 @@ namespace Volo.Abp public IGuidGenerator GuidGenerator { get; set; } - protected IUnitOfWork CurrentUnitOfWork => UnitOfWorkManager?.Current; - public ILoggerFactory LoggerFactory { get; set; } + protected IUnitOfWork CurrentUnitOfWork => UnitOfWorkManager?.Current; + protected ILogger Logger => _lazyLogger.Value; private Lazy _lazyLogger => new Lazy(() => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance, true); diff --git a/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/App/AbpAspNetCoreMvcTestModule.cs b/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/App/AbpAspNetCoreMvcTestModule.cs index bec293205e..e95ed9d90d 100644 --- a/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/App/AbpAspNetCoreMvcTestModule.cs +++ b/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/App/AbpAspNetCoreMvcTestModule.cs @@ -34,6 +34,8 @@ namespace Volo.Abp.AspNetCore.App public override void OnApplicationInitialization(ApplicationInitializationContext context) { var app = context.GetApplicationBuilder(); + + app.UseUnitOfWork(); app.UseMvcWithDefaultRoute(); } } diff --git a/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/App/UnitOfWorkTestController.cs b/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/App/UnitOfWorkTestController.cs new file mode 100644 index 0000000000..d83eccd2d8 --- /dev/null +++ b/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/App/UnitOfWorkTestController.cs @@ -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"); + } + } +} diff --git a/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Uow/UnitOfWorkMiddleware_Tests.cs b/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Uow/UnitOfWorkMiddleware_Tests.cs new file mode 100644 index 0000000000..b97c1300ba --- /dev/null +++ b/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Uow/UnitOfWorkMiddleware_Tests.cs @@ -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"); + } + } +}