From f68f72e4160f94f2912494e9b178d38bc35e9051 Mon Sep 17 00:00:00 2001 From: Piotr Szmyd Date: Fri, 5 Apr 2013 01:30:55 +0200 Subject: [PATCH] Implemented IWorkContextEvents extension point. Allows plugging into work context lifetime events. --HG-- branch : 1.x --- .../DefaultWorkContextAccessorTests.cs | 5 ++++ .../DefaultShellContextFactoryTests.cs | 7 ++++- .../State/DefaultProcessingEngineTests.cs | 5 ++++ .../Mvc/Routes/ShellRouteTests.cs | 6 +++- src/Orchard/Environment/IWorkContextEvents.cs | 16 +++++++++++ .../Environment/WorkContextAccessor.cs | 28 +++++++++++++++++-- src/Orchard/Orchard.Framework.csproj | 1 + 7 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 src/Orchard/Environment/IWorkContextEvents.cs diff --git a/src/Orchard.Tests/Environment/DefaultWorkContextAccessorTests.cs b/src/Orchard.Tests/Environment/DefaultWorkContextAccessorTests.cs index e70ac0c2e..3a58a5df5 100644 --- a/src/Orchard.Tests/Environment/DefaultWorkContextAccessorTests.cs +++ b/src/Orchard.Tests/Environment/DefaultWorkContextAccessorTests.cs @@ -2,7 +2,9 @@ using Autofac; using NUnit.Framework; using Orchard.Environment; +using Orchard.Logging; using Orchard.Mvc; +using Orchard.Tests.Logging; using Orchard.Tests.Stubs; using Orchard.Tests.Utility; @@ -20,6 +22,9 @@ namespace Orchard.Tests.Environment { protected override void Register(ContainerBuilder builder) { builder.RegisterModule(new WorkContextModule()); builder.RegisterType().As(); + builder.RegisterModule(new LoggingModule()); + var stubFactory = new LoggingModuleTests.StubFactory(); + builder.RegisterInstance(stubFactory).As(); builder.RegisterAutoMocking(); } diff --git a/src/Orchard.Tests/Environment/ShellBuilders/DefaultShellContextFactoryTests.cs b/src/Orchard.Tests/Environment/ShellBuilders/DefaultShellContextFactoryTests.cs index b89e545ea..421bf0e37 100644 --- a/src/Orchard.Tests/Environment/ShellBuilders/DefaultShellContextFactoryTests.cs +++ b/src/Orchard.Tests/Environment/ShellBuilders/DefaultShellContextFactoryTests.cs @@ -8,7 +8,9 @@ using Orchard.Environment.ShellBuilders; using Orchard.Environment.Descriptor; using Orchard.Environment.Descriptor.Models; using Orchard.Environment.ShellBuilders.Models; +using Orchard.Logging; using Orchard.Mvc; +using Orchard.Tests.Logging; using Orchard.Tests.Utility; namespace Orchard.Tests.Environment.ShellBuilders { @@ -22,7 +24,10 @@ namespace Orchard.Tests.Environment.ShellBuilders { builder.RegisterType().As(); builder.RegisterModule(new WorkContextModule()); builder.RegisterType().As(); - builder.RegisterAutoMocking(Moq.MockBehavior.Strict); + builder.RegisterModule(new LoggingModule()); + var stubFactory = new LoggingModuleTests.StubFactory(); + builder.RegisterInstance(stubFactory).As(); + builder.RegisterAutoMocking(MockBehavior.Strict); _container = builder.Build(); } diff --git a/src/Orchard.Tests/Environment/State/DefaultProcessingEngineTests.cs b/src/Orchard.Tests/Environment/State/DefaultProcessingEngineTests.cs index b3b1a009b..60dabf2eb 100644 --- a/src/Orchard.Tests/Environment/State/DefaultProcessingEngineTests.cs +++ b/src/Orchard.Tests/Environment/State/DefaultProcessingEngineTests.cs @@ -10,7 +10,9 @@ using Orchard.Environment.ShellBuilders; using Orchard.Environment.State; using Orchard.Environment.Descriptor.Models; using Orchard.Events; +using Orchard.Logging; using Orchard.Mvc; +using Orchard.Tests.Logging; using Orchard.Tests.Utility; namespace Orchard.Tests.Environment.State { @@ -25,6 +27,9 @@ namespace Orchard.Tests.Environment.State { builder.RegisterType().As(); builder.RegisterModule(new WorkContextModule()); builder.RegisterType().As(); + builder.RegisterModule(new LoggingModule()); + var stubFactory = new LoggingModuleTests.StubFactory(); + builder.RegisterInstance(stubFactory).As(); builder.RegisterAutoMocking(); _container = builder.Build(); diff --git a/src/Orchard.Tests/Mvc/Routes/ShellRouteTests.cs b/src/Orchard.Tests/Mvc/Routes/ShellRouteTests.cs index 75fec6f72..67feb27d2 100644 --- a/src/Orchard.Tests/Mvc/Routes/ShellRouteTests.cs +++ b/src/Orchard.Tests/Mvc/Routes/ShellRouteTests.cs @@ -10,8 +10,10 @@ using Orchard.Caching; using Orchard.Environment; using Orchard.Environment.Configuration; using Orchard.Environment.Extensions; +using Orchard.Logging; using Orchard.Mvc; using Orchard.Mvc.Routes; +using Orchard.Tests.Logging; using Orchard.Tests.Stubs; using Orchard.Tests.Utility; @@ -42,7 +44,9 @@ namespace Orchard.Tests.Mvc.Routes { rootBuilder.RegisterType().As(); rootBuilder.RegisterType().As(); rootBuilder.RegisterType().As(); - + rootBuilder.RegisterModule(new LoggingModule()); + var stubFactory = new LoggingModuleTests.StubFactory(); + rootBuilder.RegisterInstance(stubFactory).As(); _rootContainer = rootBuilder.Build(); _containerA = _rootContainer.BeginLifetimeScope( diff --git a/src/Orchard/Environment/IWorkContextEvents.cs b/src/Orchard/Environment/IWorkContextEvents.cs new file mode 100644 index 000000000..295cd8c22 --- /dev/null +++ b/src/Orchard/Environment/IWorkContextEvents.cs @@ -0,0 +1,16 @@ +namespace Orchard.Environment { + /// + /// Describes events fired during work context lifetime. + /// + public interface IWorkContextEvents : IUnitOfWorkDependency { + /// + /// Fired when the work context is started. + /// + void Started(); + + /// + /// Fired when the work context is finished. + /// + void Finished(); + } +} \ No newline at end of file diff --git a/src/Orchard/Environment/WorkContextAccessor.cs b/src/Orchard/Environment/WorkContextAccessor.cs index 307ed2dd2..6f5e5b17f 100644 --- a/src/Orchard/Environment/WorkContextAccessor.cs +++ b/src/Orchard/Environment/WorkContextAccessor.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Concurrent; +using System.Collections.Generic; using System.Web; using Autofac; +using Orchard.Logging; using Orchard.Mvc; namespace Orchard.Environment { @@ -40,7 +42,12 @@ namespace Orchard.Environment { var workLifetime = _lifetimeScope.BeginLifetimeScope("work"); workLifetime.Resolve>().Value = httpContext; + var events = workLifetime.Resolve>(); + var logger = workLifetime.Resolve(new TypedParameter(typeof(Type), typeof(HttpContextScopeImplementation))); + events.Invoke(e => e.Started(), logger); + return new HttpContextScopeImplementation( + logger, workLifetime, httpContext, _workContextKey); @@ -52,8 +59,15 @@ namespace Orchard.Environment { if (httpContext != null) return CreateWorkContextScope(httpContext); + var workLifetime = _lifetimeScope.BeginLifetimeScope("work"); + + var events = workLifetime.Resolve>(); + var logger = workLifetime.Resolve(new TypedParameter(typeof(Type), typeof(ThreadStaticScopeImplementation))); + events.Invoke(e => e.Started(), logger); + return new ThreadStaticScopeImplementation( - _lifetimeScope.BeginLifetimeScope("work"), + logger, + workLifetime, EnsureThreadStaticContexts(), _workContextKey); } @@ -67,10 +81,14 @@ namespace Orchard.Environment { readonly WorkContext _workContext; readonly Action _disposer; - public HttpContextScopeImplementation(ILifetimeScope lifetimeScope, HttpContextBase httpContext, object workContextKey) { + public HttpContextScopeImplementation(ILogger logger, ILifetimeScope lifetimeScope, HttpContextBase httpContext, object workContextKey) { _workContext = lifetimeScope.Resolve(); httpContext.Items[workContextKey] = _workContext; + _disposer = () => { + var events = lifetimeScope.Resolve>(); + events.Invoke(e => e.Finished(), logger); + httpContext.Items.Remove(workContextKey); lifetimeScope.Dispose(); }; @@ -97,10 +115,14 @@ namespace Orchard.Environment { readonly WorkContext _workContext; readonly Action _disposer; - public ThreadStaticScopeImplementation(ILifetimeScope lifetimeScope, ConcurrentDictionary contexts, object workContextKey) { + public ThreadStaticScopeImplementation(ILogger logger, ILifetimeScope lifetimeScope, ConcurrentDictionary contexts, object workContextKey) { _workContext = lifetimeScope.Resolve(); contexts.AddOrUpdate(workContextKey, _workContext, (a, b) => _workContext); + _disposer = () => { + var events = lifetimeScope.Resolve>(); + events.Invoke(e => e.Finished(), logger); + WorkContext removedContext; contexts.TryRemove(workContextKey, out removedContext); lifetimeScope.Dispose(); diff --git a/src/Orchard/Orchard.Framework.csproj b/src/Orchard/Orchard.Framework.csproj index 869d3417a..33457146a 100644 --- a/src/Orchard/Orchard.Framework.csproj +++ b/src/Orchard/Orchard.Framework.csproj @@ -223,6 +223,7 @@ +