mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
- An autofac events module for dynamic proxy creation when an interface of type IEventHandler is resolved through DI.
- Intercept method on the proxy calls Notify on the event-bus with appropriate conversion of event name and parameters. --HG-- branch : dev
This commit is contained in:
23
src/Orchard/Events/EventsInterceptor.cs
Normal file
23
src/Orchard/Events/EventsInterceptor.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System.Linq;
|
||||
using Castle.Core.Interceptor;
|
||||
|
||||
namespace Orchard.Events {
|
||||
public class EventsInterceptor : IInterceptor {
|
||||
private readonly IEventBus _eventBus;
|
||||
|
||||
public EventsInterceptor(IEventBus eventBus) {
|
||||
_eventBus = eventBus;
|
||||
}
|
||||
|
||||
public void Intercept(IInvocation invocation) {
|
||||
var interfaceName = invocation.Method.DeclaringType.Name;
|
||||
var methodName = invocation.Method.Name;
|
||||
|
||||
var data = invocation.Method.GetParameters()
|
||||
.Select((parameter, index) => new { parameter.Name, Value = invocation.Arguments[index] })
|
||||
.ToDictionary(kv => kv.Name, kv => kv.Value.ToString());
|
||||
|
||||
_eventBus.Notify(interfaceName + "_" + methodName, data);
|
||||
}
|
||||
}
|
||||
}
|
10
src/Orchard/Events/EventsModule.cs
Normal file
10
src/Orchard/Events/EventsModule.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using Autofac;
|
||||
|
||||
namespace Orchard.Events {
|
||||
public class EventsModule : Module {
|
||||
protected override void Load(ContainerBuilder builder) {
|
||||
builder.RegisterSource(new EventsRegistrationSource());
|
||||
base.Load(builder);
|
||||
}
|
||||
}
|
||||
}
|
43
src/Orchard/Events/EventsRegistrationSource.cs
Normal file
43
src/Orchard/Events/EventsRegistrationSource.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Autofac;
|
||||
using Autofac.Builder;
|
||||
using Autofac.Core;
|
||||
using Castle.Core.Interceptor;
|
||||
using Castle.DynamicProxy;
|
||||
|
||||
namespace Orchard.Events {
|
||||
public class EventsRegistrationSource : IRegistrationSource {
|
||||
private readonly DefaultProxyBuilder _proxyBuilder;
|
||||
|
||||
public EventsRegistrationSource() {
|
||||
_proxyBuilder = new DefaultProxyBuilder();
|
||||
}
|
||||
|
||||
public IEnumerable<IComponentRegistration> RegistrationsFor(Service service, Func<Service, IEnumerable<IComponentRegistration>> registrationAccessor) {
|
||||
var serviceWithType = service as IServiceWithType;
|
||||
if (serviceWithType == null)
|
||||
yield break;
|
||||
|
||||
var serviceType = serviceWithType.ServiceType;
|
||||
if (!serviceType.IsInterface || !typeof(IEventHandler).IsAssignableFrom(serviceType))
|
||||
yield break;
|
||||
|
||||
var interfaceProxyType = _proxyBuilder.CreateInterfaceProxyTypeWithoutTarget(
|
||||
serviceType,
|
||||
new Type[0],
|
||||
ProxyGenerationOptions.Default);
|
||||
|
||||
|
||||
var rb = RegistrationBuilder
|
||||
.ForDelegate((ctx, parameters) => {
|
||||
var interceptors = new IInterceptor[] { new EventsInterceptor(ctx.Resolve<IEventBus>()) };
|
||||
var args = new object[] { interceptors, null };
|
||||
return Activator.CreateInstance(interfaceProxyType, args);
|
||||
})
|
||||
.As(service);
|
||||
|
||||
yield return rb.CreateRegistration();
|
||||
}
|
||||
}
|
||||
}
|
4
src/Orchard/Events/IEventHandler.cs
Normal file
4
src/Orchard/Events/IEventHandler.cs
Normal file
@@ -0,0 +1,4 @@
|
||||
namespace Orchard.Events {
|
||||
public interface IEventHandler {
|
||||
}
|
||||
}
|
@@ -181,6 +181,9 @@
|
||||
<Compile Include="Environment\Configuration\ShellSettings.cs" />
|
||||
<Compile Include="Environment\StandaloneEnvironment.cs" />
|
||||
<Compile Include="Events\DefaultOrchardEventBus.cs" />
|
||||
<Compile Include="Events\EventsInterceptor.cs" />
|
||||
<Compile Include="Events\EventsModule.cs" />
|
||||
<Compile Include="Events\EventsRegistrationSource.cs" />
|
||||
<Compile Include="Events\IEventBus.cs" />
|
||||
<Compile Include="Events\IEventBusHandler.cs" />
|
||||
<Compile Include="Environment\Extensions\Folders\AreaFolders.cs" />
|
||||
@@ -190,6 +193,7 @@
|
||||
<Compile Include="Environment\Extensions\Models\Feature.cs" />
|
||||
<Compile Include="Environment\Extensions\Models\FeatureDescriptor.cs" />
|
||||
<Compile Include="Environment\Extensions\OrchardFeatureAttribute.cs" />
|
||||
<Compile Include="Events\IEventHandler.cs" />
|
||||
<Compile Include="Modules\IModule.cs" />
|
||||
<Compile Include="Modules\IModuleFeature.cs" />
|
||||
<Compile Include="Modules\IModuleService.cs" />
|
||||
|
Reference in New Issue
Block a user