Implemented IWorkContextEvents extension point. Allows plugging into work context lifetime events.

--HG--
branch : 1.x
This commit is contained in:
Piotr Szmyd
2013-04-05 01:30:55 +02:00
parent c940f83474
commit f68f72e416
7 changed files with 63 additions and 5 deletions

View File

@@ -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<WorkContextAccessor>().As<IWorkContextAccessor>();
builder.RegisterModule(new LoggingModule());
var stubFactory = new LoggingModuleTests.StubFactory();
builder.RegisterInstance(stubFactory).As<ILoggerFactory>();
builder.RegisterAutoMocking();
}

View File

@@ -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<ShellContextFactory>().As<IShellContextFactory>();
builder.RegisterModule(new WorkContextModule());
builder.RegisterType<WorkContextAccessor>().As<IWorkContextAccessor>();
builder.RegisterAutoMocking(Moq.MockBehavior.Strict);
builder.RegisterModule(new LoggingModule());
var stubFactory = new LoggingModuleTests.StubFactory();
builder.RegisterInstance(stubFactory).As<ILoggerFactory>();
builder.RegisterAutoMocking(MockBehavior.Strict);
_container = builder.Build();
}

View File

@@ -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<DefaultProcessingEngine>().As<IProcessingEngine>();
builder.RegisterModule(new WorkContextModule());
builder.RegisterType<WorkContextAccessor>().As<IWorkContextAccessor>();
builder.RegisterModule(new LoggingModule());
var stubFactory = new LoggingModuleTests.StubFactory();
builder.RegisterInstance(stubFactory).As<ILoggerFactory>();
builder.RegisterAutoMocking();
_container = builder.Build();

View File

@@ -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<StubCacheManager>().As<ICacheManager>();
rootBuilder.RegisterType<StubAsyncTokenProvider>().As<IAsyncTokenProvider>();
rootBuilder.RegisterType<StubParallelCacheContext>().As<IParallelCacheContext>();
rootBuilder.RegisterModule(new LoggingModule());
var stubFactory = new LoggingModuleTests.StubFactory();
rootBuilder.RegisterInstance(stubFactory).As<ILoggerFactory>();
_rootContainer = rootBuilder.Build();
_containerA = _rootContainer.BeginLifetimeScope(

View File

@@ -0,0 +1,16 @@
namespace Orchard.Environment {
/// <summary>
/// Describes events fired during work context lifetime.
/// </summary>
public interface IWorkContextEvents : IUnitOfWorkDependency {
/// <summary>
/// Fired when the work context is started.
/// </summary>
void Started();
/// <summary>
/// Fired when the work context is finished.
/// </summary>
void Finished();
}
}

View File

@@ -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<WorkContextProperty<HttpContextBase>>().Value = httpContext;
var events = workLifetime.Resolve<IEnumerable<IWorkContextEvents>>();
var logger = workLifetime.Resolve<ILogger>(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<IEnumerable<IWorkContextEvents>>();
var logger = workLifetime.Resolve<ILogger>(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<WorkContext>();
httpContext.Items[workContextKey] = _workContext;
_disposer = () => {
var events = lifetimeScope.Resolve<IEnumerable<IWorkContextEvents>>();
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<object, WorkContext> contexts, object workContextKey) {
public ThreadStaticScopeImplementation(ILogger logger, ILifetimeScope lifetimeScope, ConcurrentDictionary<object, WorkContext> contexts, object workContextKey) {
_workContext = lifetimeScope.Resolve<WorkContext>();
contexts.AddOrUpdate(workContextKey, _workContext, (a, b) => _workContext);
_disposer = () => {
var events = lifetimeScope.Resolve<IEnumerable<IWorkContextEvents>>();
events.Invoke(e => e.Finished(), logger);
WorkContext removedContext;
contexts.TryRemove(workContextKey, out removedContext);
lifetimeScope.Dispose();

View File

@@ -223,6 +223,7 @@
<Compile Include="Environment\HostComponentsConfigModule.cs" />
<Compile Include="Environment\Extensions\ICriticalErrorProvider.cs" />
<Compile Include="Environment\IOrchardFrameworkAssemblies.cs" />
<Compile Include="Environment\IWorkContextEvents.cs" />
<Compile Include="Environment\ViewsBackgroundCompilation.cs" />
<Compile Include="Environment\Warmup\WarmupUtility.cs" />
<Compile Include="Environment\WorkContextImplementation.cs" />