mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Allowing modules to add interceptors to registered components
Removes reference to Autofac contrib dynamicproxy assembly Shell factories enable dynamic proxy as dependencies are registered CurrentUser and CurrentSite interceptors updated for new api Castle.DynamicProxy2 v2.1 is used to avoid rebuilding nhib binaries --HG-- branch : dev
This commit is contained in:
@@ -5,8 +5,8 @@ using System.Web.Routing;
|
||||
using Autofac;
|
||||
using NUnit.Framework;
|
||||
using Orchard.Environment;
|
||||
using Orchard.Environment.AutofacUtil;
|
||||
using Orchard.Environment.Configuration;
|
||||
using Orchard.Environment.ShellBuilders;
|
||||
using Orchard.Setup.Controllers;
|
||||
using Orchard.Setup.ViewModels;
|
||||
using Orchard.UI.Notify;
|
||||
|
@@ -6,7 +6,7 @@ using NUnit.Framework;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.Drivers;
|
||||
using Orchard.ContentManagement.Handlers;
|
||||
using Orchard.Environment.ShellBuilders;
|
||||
using Orchard.Environment.AutofacUtil;
|
||||
using Orchard.Mvc.ViewModels;
|
||||
using Orchard.UI.Zones;
|
||||
|
||||
|
@@ -0,0 +1,141 @@
|
||||
using System;
|
||||
using Autofac;
|
||||
using Autofac.Core;
|
||||
using Autofac.Features.Metadata;
|
||||
using Castle.Core.Interceptor;
|
||||
using NUnit.Framework;
|
||||
using Orchard.Environment.AutofacUtil.DynamicProxy2;
|
||||
|
||||
namespace Orchard.Tests.Environment.AutofacUtil.DynamicProxy2 {
|
||||
[TestFixture]
|
||||
public class DynamicProxyTests {
|
||||
[Test]
|
||||
public void ContextAddedToMetadataWhenRegistered() {
|
||||
var context = new DynamicProxyContext();
|
||||
|
||||
var builder = new ContainerBuilder();
|
||||
builder.RegisterType<SimpleComponent>().EnableDynamicProxy(context);
|
||||
|
||||
var container = builder.Build();
|
||||
|
||||
var meta = container.Resolve<Meta<SimpleComponent>>();
|
||||
Assert.That(meta.Metadata, Has.Some.Property("Key").EqualTo("Orchard.Environment.AutofacUtil.DynamicProxy2.DynamicProxyContext.ProxyContextKey"));
|
||||
Assert.That(meta.Metadata["Orchard.Environment.AutofacUtil.DynamicProxy2.DynamicProxyContext.ProxyContextKey"], Is.SameAs(context));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ProxyContextReturnsTrueIfTypeHasBeenProxied() {
|
||||
var context = new DynamicProxyContext();
|
||||
|
||||
Type proxyType;
|
||||
Assert.That(context.TryGetProxy(typeof(SimpleComponent), out proxyType), Is.False);
|
||||
Assert.That(context.TryGetProxy(typeof(SimpleComponent), out proxyType), Is.False);
|
||||
Assert.That(proxyType, Is.Null);
|
||||
|
||||
context.AddProxy(typeof(SimpleComponent));
|
||||
Assert.That(context.TryGetProxy(typeof(SimpleComponent), out proxyType), Is.True);
|
||||
Assert.That(context.TryGetProxy(typeof(SimpleComponent), out proxyType), Is.True);
|
||||
Assert.That(proxyType, Is.Not.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AddProxyCanBeCalledMoreThanOnce() {
|
||||
var context = new DynamicProxyContext();
|
||||
context.AddProxy(typeof(SimpleComponent));
|
||||
|
||||
Type proxyType;
|
||||
Assert.That(context.TryGetProxy(typeof(SimpleComponent), out proxyType), Is.True);
|
||||
Assert.That(proxyType, Is.Not.Null);
|
||||
|
||||
Type proxyType2;
|
||||
context.AddProxy(typeof(SimpleComponent));
|
||||
Assert.That(context.TryGetProxy(typeof(SimpleComponent), out proxyType2), Is.True);
|
||||
|
||||
Assert.That(proxyType2, Is.SameAs(proxyType));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InterceptorAddedToContextFromModules() {
|
||||
var context = new DynamicProxyContext();
|
||||
|
||||
var builder = new ContainerBuilder();
|
||||
builder.RegisterType<SimpleComponent>().EnableDynamicProxy(context);
|
||||
builder.RegisterModule(new SimpleInterceptorModule());
|
||||
|
||||
builder.Build();
|
||||
|
||||
Type proxyType;
|
||||
Assert.That(context.TryGetProxy(typeof(SimpleComponent), out proxyType), Is.True);
|
||||
Assert.That(proxyType, Is.Not.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ResolvedObjectIsSubclass() {
|
||||
var context = new DynamicProxyContext();
|
||||
|
||||
var builder = new ContainerBuilder();
|
||||
builder.RegisterType<SimpleComponent>().EnableDynamicProxy(context);
|
||||
builder.RegisterModule(new SimpleInterceptorModule());
|
||||
|
||||
var container = builder.Build();
|
||||
|
||||
var simpleComponent = container.Resolve<SimpleComponent>();
|
||||
Assert.That(simpleComponent, Is.InstanceOf<SimpleComponent>());
|
||||
Assert.That(simpleComponent, Is.Not.TypeOf<SimpleComponent>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InterceptorCatchesMethodCallOnlyFromContainerWithInterceptor() {
|
||||
var context = new DynamicProxyContext();
|
||||
|
||||
var builder1 = new ContainerBuilder();
|
||||
builder1.RegisterType<SimpleComponent>().EnableDynamicProxy(context);
|
||||
builder1.RegisterModule(new SimpleInterceptorModule());
|
||||
var container1 = builder1.Build();
|
||||
|
||||
var simple1 = container1.Resolve<SimpleComponent>();
|
||||
|
||||
var builder2 = new ContainerBuilder();
|
||||
builder2.RegisterType<SimpleComponent>().EnableDynamicProxy(context);
|
||||
var container2 = builder2.Build();
|
||||
|
||||
var simple2 = container2.Resolve<SimpleComponent>();
|
||||
|
||||
Assert.That(simple2.SimpleMethod(), Is.EqualTo("default return value"));
|
||||
Assert.That(simple1.SimpleMethod(), Is.EqualTo("different return value"));
|
||||
}
|
||||
}
|
||||
|
||||
public class SimpleComponent {
|
||||
public virtual string SimpleMethod() {
|
||||
return "default return value";
|
||||
}
|
||||
}
|
||||
|
||||
public class SimpleInterceptorModule : Module {
|
||||
protected override void Load(ContainerBuilder builder) {
|
||||
builder.RegisterType<SimpleInterceptor>();
|
||||
|
||||
base.Load(builder);
|
||||
}
|
||||
|
||||
protected override void AttachToComponentRegistration(
|
||||
IComponentRegistry componentRegistry,
|
||||
IComponentRegistration registration) {
|
||||
|
||||
if (DynamicProxyContext.From(registration) != null)
|
||||
registration.InterceptedBy<SimpleInterceptor>();
|
||||
}
|
||||
}
|
||||
|
||||
public class SimpleInterceptor : IInterceptor {
|
||||
public void Intercept(IInvocation invocation) {
|
||||
if (invocation.Method.Name == "SimpleMethod") {
|
||||
invocation.ReturnValue = "different return value";
|
||||
}
|
||||
else {
|
||||
invocation.Proceed();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -9,8 +9,8 @@ using Autofac.Integration.Web;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using Orchard.Environment;
|
||||
using Orchard.Environment.AutofacUtil;
|
||||
using Orchard.Environment.Configuration;
|
||||
using Orchard.Environment.ShellBuilders;
|
||||
using Orchard.Mvc;
|
||||
using Orchard.Mvc.ModelBinders;
|
||||
using Orchard.Mvc.Routes;
|
||||
|
@@ -43,6 +43,14 @@
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\lib\autofac\Autofac.Integration.Web.Mvc.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Castle.Core, Version=1.2.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\lib\Castle Windsor 2.0\bin\Castle.Core.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Castle.DynamicProxy2, Version=2.1.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\lib\Castle Windsor 2.0\bin\Castle.DynamicProxy2.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="FluentNHibernate, Version=1.0.0.593, Culture=neutral, PublicKeyToken=8aa435e3cb308880, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\lib\fluentnhibernate\FluentNHibernate.dll</HintPath>
|
||||
@@ -143,6 +151,7 @@
|
||||
<Compile Include="Data\Builders\SessionFactoryBuilderTests.cs" />
|
||||
<Compile Include="Data\RepositoryTests.cs" />
|
||||
<Compile Include="Data\StubLocator.cs" />
|
||||
<Compile Include="Environment\AutofacUtil\DynamicProxy2\DynamicProxyTests.cs" />
|
||||
<Compile Include="Environment\Configuration\AppDataFolderTests.cs" />
|
||||
<Compile Include="Environment\DefaultCompositionStrategyTests.cs" />
|
||||
<Compile Include="Environment\DefaultOrchardHostTests.cs" />
|
||||
|
@@ -3,9 +3,9 @@ using System.Collections.Generic;
|
||||
using Autofac;
|
||||
using Autofac.Core;
|
||||
|
||||
namespace Orchard.Environment.ShellBuilders {
|
||||
namespace Orchard.Environment.AutofacUtil {
|
||||
public class ContainerUpdater : ContainerBuilder {
|
||||
ICollection<Action<IComponentRegistry>> _configurationActions = new List<Action<IComponentRegistry>>();
|
||||
readonly ICollection<Action<IComponentRegistry>> _configurationActions = new List<Action<IComponentRegistry>>();
|
||||
|
||||
public override void RegisterCallback(Action<IComponentRegistry> configurationAction) {
|
||||
_configurationActions.Add(configurationAction);
|
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Autofac.Core.Activators.Reflection;
|
||||
|
||||
namespace Orchard.Environment.AutofacUtil.DynamicProxy2 {
|
||||
class ConstructorFinderWrapper : IConstructorFinder {
|
||||
private readonly IConstructorFinder _constructorFinder;
|
||||
private readonly DynamicProxyContext _dynamicProxyContext;
|
||||
|
||||
public ConstructorFinderWrapper(IConstructorFinder constructorFinder, DynamicProxyContext dynamicProxyContext) {
|
||||
_constructorFinder = constructorFinder;
|
||||
_dynamicProxyContext = dynamicProxyContext;
|
||||
}
|
||||
|
||||
public IEnumerable<ConstructorInfo> FindConstructors(Type targetType) {
|
||||
Type proxyType;
|
||||
if (_dynamicProxyContext.TryGetProxy(targetType, out proxyType)) {
|
||||
return _constructorFinder.FindConstructors(proxyType);
|
||||
}
|
||||
return _constructorFinder.FindConstructors(targetType);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,97 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Autofac;
|
||||
using Autofac.Builder;
|
||||
using Autofac.Core;
|
||||
using Castle.Core.Interceptor;
|
||||
using Castle.DynamicProxy;
|
||||
|
||||
namespace Orchard.Environment.AutofacUtil.DynamicProxy2 {
|
||||
public class DynamicProxyContext {
|
||||
const string ProxyContextKey = "Orchard.Environment.AutofacUtil.DynamicProxy2.DynamicProxyContext.ProxyContextKey";
|
||||
const string InterceptorServicesKey = "Orchard.Environment.AutofacUtil.DynamicProxy2.DynamicProxyContext.InterceptorServicesKey";
|
||||
|
||||
readonly IProxyBuilder _proxyBuilder = new DefaultProxyBuilder();
|
||||
readonly IDictionary<Type, Type> _cache = new Dictionary<Type, Type>();
|
||||
|
||||
/// <summary>
|
||||
/// Static method to resolve the context for a component registration. The context is set
|
||||
/// by using the registration builder extension method EnableDynamicProxy(context).
|
||||
/// </summary>
|
||||
public static DynamicProxyContext From(IComponentRegistration registration) {
|
||||
object value;
|
||||
if (registration.Metadata.TryGetValue(ProxyContextKey, out value))
|
||||
return value as DynamicProxyContext;
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called indirectly from the EnableDynamicProxy extension method.
|
||||
/// Modifies a registration to support dynamic interception if needed, and act as a normal type otherwise.
|
||||
/// </summary>
|
||||
public void EnableDynamicProxy<TLimit, TConcreteReflectionActivatorData, TRegistrationStyle>(
|
||||
IRegistrationBuilder<TLimit, TConcreteReflectionActivatorData, TRegistrationStyle> registrationBuilder)
|
||||
where TConcreteReflectionActivatorData : ConcreteReflectionActivatorData {
|
||||
|
||||
// associate this context. used later by static DynamicProxyContext.From() method.
|
||||
registrationBuilder.WithMetadata(ProxyContextKey, this);
|
||||
|
||||
// put a shim in place. this will return constructors for the proxy class if it interceptors have been added.
|
||||
registrationBuilder.ActivatorData.ConstructorFinder = new ConstructorFinderWrapper(
|
||||
registrationBuilder.ActivatorData.ConstructorFinder, this);
|
||||
|
||||
// when component is being resolved, this even handler will place the array of appropriate interceptors as the first argument
|
||||
registrationBuilder.OnPreparing(e => {
|
||||
object value;
|
||||
if (e.Component.Metadata.TryGetValue(InterceptorServicesKey, out value)) {
|
||||
var interceptorServices = (IEnumerable<Service>)value;
|
||||
var interceptors = interceptorServices.Select(service => e.Context.Resolve(service)).Cast<IInterceptor>().ToArray();
|
||||
var parameter = new PositionalParameter(0, interceptors);
|
||||
e.Parameters = new[] { parameter }.Concat(e.Parameters).ToArray();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called indirectly from the InterceptedBy extension method.
|
||||
/// Adds services to the componenent's list of interceptors, activating the need for dynamic proxy
|
||||
/// </summary>
|
||||
public void AddInterceptorService(IComponentRegistration registration, Service service) {
|
||||
AddProxy(registration.Activator.LimitType);
|
||||
|
||||
var interceptorServices = Enumerable.Empty<Service>();
|
||||
object value;
|
||||
if (registration.Metadata.TryGetValue(InterceptorServicesKey, out value)) {
|
||||
interceptorServices = (IEnumerable<Service>)value;
|
||||
}
|
||||
|
||||
registration.Metadata[InterceptorServicesKey] = interceptorServices.Concat(new[] { service }).Distinct().ToArray();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Ensures that a proxy has been generated for the particular type in this context
|
||||
/// </summary>
|
||||
public void AddProxy(Type type) {
|
||||
Type proxyType;
|
||||
if (_cache.TryGetValue(type, out proxyType))
|
||||
return;
|
||||
|
||||
lock (_cache) {
|
||||
if (_cache.TryGetValue(type, out proxyType))
|
||||
return;
|
||||
|
||||
_cache[type] = _proxyBuilder.CreateClassProxy(type, ProxyGenerationOptions.Default);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if a proxy has been generated for the given type, and returns it.
|
||||
/// </summary>
|
||||
public bool TryGetProxy(Type type, out Type proxyType) {
|
||||
return _cache.TryGetValue(type, out proxyType);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using Autofac.Builder;
|
||||
using Autofac.Core;
|
||||
using Autofac.Features.Scanning;
|
||||
|
||||
namespace Orchard.Environment.AutofacUtil.DynamicProxy2 {
|
||||
public static class DynamicProxyExtensions {
|
||||
|
||||
public static IRegistrationBuilder<TLimit, TConcreteReflectionActivatorData, TRegistrationStyle> EnableDynamicProxy<TLimit, TConcreteReflectionActivatorData, TRegistrationStyle>(
|
||||
this IRegistrationBuilder<TLimit, TConcreteReflectionActivatorData, TRegistrationStyle> rb,
|
||||
DynamicProxyContext dynamicProxyContext)
|
||||
where TConcreteReflectionActivatorData : ConcreteReflectionActivatorData {
|
||||
|
||||
dynamicProxyContext.EnableDynamicProxy(rb);
|
||||
|
||||
return rb;
|
||||
}
|
||||
|
||||
public static IRegistrationBuilder<TLimit, ScanningActivatorData, TRegistrationStyle> EnableDynamicProxy<TLimit, TRegistrationStyle>(
|
||||
this IRegistrationBuilder<TLimit, ScanningActivatorData, TRegistrationStyle> rb,
|
||||
DynamicProxyContext dynamicProxyContext) {
|
||||
|
||||
rb.ActivatorData.ConfigurationActions.Add((t, rb2) => rb2.EnableDynamicProxy(dynamicProxyContext));
|
||||
return rb;
|
||||
}
|
||||
|
||||
public static void InterceptedBy<TService>(this IComponentRegistration cr) {
|
||||
var dynamicProxyContext = DynamicProxyContext.From(cr);
|
||||
if (dynamicProxyContext == null)
|
||||
throw new ApplicationException(string.Format("Component {0} was not registered with EnableDynamicProxy", cr.Activator.LimitType));
|
||||
|
||||
dynamicProxyContext.AddInterceptorService(cr, new TypedService(typeof(TService)));
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,28 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Autofac;
|
||||
using Autofac.Core;
|
||||
using AutofacContrib.DynamicProxy2;
|
||||
|
||||
namespace Orchard.Environment {
|
||||
//public class ExtensibleInterceptionModule : InterceptionModule {
|
||||
// public ExtensibleInterceptionModule(IEnumerable<IComponentInterceptorProvider> providers)
|
||||
// : base(new CombinedProvider(providers.Concat(new[] { new FlexibleInterceptorProvider() })), new FlexibleInterceptorAttacher()) {
|
||||
// }
|
||||
|
||||
// class CombinedProvider : IComponentInterceptorProvider {
|
||||
// private readonly IEnumerable<IComponentInterceptorProvider> _providers;
|
||||
|
||||
// public CombinedProvider(IEnumerable<IComponentInterceptorProvider> providers) {
|
||||
// _providers = providers;
|
||||
// }
|
||||
|
||||
// public IEnumerable<Service> GetInterceptorServices(IComponentDescriptor descriptor) {
|
||||
// return _providers
|
||||
// .SelectMany(x => x.GetInterceptorServices(descriptor))
|
||||
// .Distinct()
|
||||
// .ToList();
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
}
|
@@ -1,6 +1,6 @@
|
||||
using Autofac;
|
||||
using Autofac.Integration.Web;
|
||||
using Orchard.Environment.ShellBuilders;
|
||||
using Orchard.Environment.AutofacUtil;
|
||||
|
||||
namespace Orchard.Environment {
|
||||
|
||||
|
@@ -3,6 +3,7 @@ using Autofac;
|
||||
using Autofac.Builder;
|
||||
using Autofac.Core;
|
||||
using Autofac.Integration.Web;
|
||||
using Orchard.Environment.AutofacUtil;
|
||||
using Orchard.Environment.Configuration;
|
||||
using Orchard.Environment.ShellBuilders;
|
||||
using Orchard.Extensions;
|
||||
|
@@ -1,6 +1,8 @@
|
||||
using System.Collections.Generic;
|
||||
using Autofac;
|
||||
using Autofac.Core;
|
||||
using Orchard.Environment.AutofacUtil;
|
||||
using Orchard.Environment.AutofacUtil.DynamicProxy2;
|
||||
using Orchard.Environment.Configuration;
|
||||
|
||||
namespace Orchard.Environment.ShellBuilders {
|
||||
@@ -19,9 +21,12 @@ namespace Orchard.Environment.ShellBuilders {
|
||||
return null;
|
||||
}
|
||||
|
||||
var dynamicProxyContext = new DynamicProxyContext();
|
||||
|
||||
// add module types to container being built
|
||||
var addingModulesAndServices = new ContainerUpdater();
|
||||
addingModulesAndServices.RegisterInstance(settings).As<IShellSettings>();
|
||||
addingModulesAndServices.RegisterInstance(dynamicProxyContext);
|
||||
addingModulesAndServices.RegisterType<DefaultOrchardShell>().As<IOrchardShell>().SingleInstance();
|
||||
|
||||
foreach (var moduleType in _compositionStrategy.GetModuleTypes()) {
|
||||
@@ -32,7 +37,7 @@ namespace Orchard.Environment.ShellBuilders {
|
||||
foreach (var serviceType in _compositionStrategy.GetDependencyTypes()) {
|
||||
foreach (var interfaceType in serviceType.GetInterfaces()) {
|
||||
if (typeof(IDependency).IsAssignableFrom(interfaceType)) {
|
||||
var registrar = addingModulesAndServices.RegisterType(serviceType).As(interfaceType);
|
||||
var registrar = addingModulesAndServices.RegisterType(serviceType).As(interfaceType).EnableDynamicProxy(dynamicProxyContext);
|
||||
if (typeof(ISingletonDependency).IsAssignableFrom(interfaceType)) {
|
||||
registrar.SingleInstance();
|
||||
}
|
||||
|
@@ -10,6 +10,8 @@ using Autofac.Core;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.Handlers;
|
||||
using Orchard.Data.Builders;
|
||||
using Orchard.Environment.AutofacUtil;
|
||||
using Orchard.Environment.AutofacUtil.DynamicProxy2;
|
||||
using Orchard.Environment.Configuration;
|
||||
using Orchard.Extensions;
|
||||
using Orchard.Localization;
|
||||
@@ -39,9 +41,12 @@ namespace Orchard.Environment.ShellBuilders {
|
||||
return null;
|
||||
}
|
||||
|
||||
var dynamicProxyContext = new DynamicProxyContext();
|
||||
|
||||
var shellScope = _container.BeginLifetimeScope();
|
||||
var builder = new ContainerUpdater();
|
||||
// standard services needed in safe mode
|
||||
builder.RegisterInstance(dynamicProxyContext);
|
||||
builder.RegisterType<DefaultOrchardShell>().As<IOrchardShell>().InstancePerLifetimeScope();
|
||||
builder.RegisterType<RoutePublisher>().As<IRoutePublisher>().InstancePerLifetimeScope();
|
||||
builder.RegisterType<ModelBinderPublisher>().As<IModelBinderPublisher>().InstancePerLifetimeScope();
|
||||
@@ -67,7 +72,7 @@ namespace Orchard.Environment.ShellBuilders {
|
||||
foreach (var serviceType in dependencies) {
|
||||
foreach (var interfaceType in serviceType.GetInterfaces()) {
|
||||
if (typeof(IDependency).IsAssignableFrom(interfaceType)) {
|
||||
var registrar = builder.RegisterType(serviceType).As(interfaceType);
|
||||
var registrar = builder.RegisterType(serviceType).As(interfaceType).EnableDynamicProxy(dynamicProxyContext);
|
||||
if (typeof(ISingletonDependency).IsAssignableFrom(interfaceType)) {
|
||||
registrar.SingleInstance();
|
||||
}
|
||||
|
@@ -6,6 +6,7 @@ using System.Web.Routing;
|
||||
using Autofac;
|
||||
using Autofac.Integration.Web;
|
||||
using Autofac.Integration.Web.Mvc;
|
||||
using Orchard.Environment.AutofacUtil.DynamicProxy2;
|
||||
using Orchard.Mvc.Filters;
|
||||
using Orchard.Extensions;
|
||||
using Autofac.Core;
|
||||
@@ -13,9 +14,11 @@ using Autofac.Core;
|
||||
namespace Orchard.Mvc {
|
||||
public class MvcModule : Module {
|
||||
private readonly IExtensionManager _extensionManager;
|
||||
private readonly DynamicProxyContext _dynamicProxyContext;
|
||||
|
||||
public MvcModule(IExtensionManager extensionManager) {
|
||||
public MvcModule(IExtensionManager extensionManager, DynamicProxyContext dynamicProxyContext) {
|
||||
_extensionManager = extensionManager;
|
||||
_dynamicProxyContext = dynamicProxyContext;
|
||||
}
|
||||
|
||||
protected override void Load(ContainerBuilder moduleBuilder) {
|
||||
@@ -25,6 +28,7 @@ namespace Orchard.Mvc {
|
||||
moduleBuilder.RegisterType<FilterResolvingActionInvoker>().As(actionInvokerService).InstancePerDependency();
|
||||
|
||||
moduleBuilder.RegisterControllers(new OrchardControllerIdentificationStrategy(extensions), assemblies.ToArray())
|
||||
.EnableDynamicProxy(_dynamicProxyContext)
|
||||
.InjectActionInvoker(actionInvokerService).InstancePerDependency();
|
||||
|
||||
moduleBuilder.Register(ctx => HttpContextBaseFactory(ctx)).As<HttpContextBase>().InstancePerDependency();
|
||||
|
@@ -43,14 +43,14 @@
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\lib\autofac\Autofac.Integration.Web.Mvc.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="AutofacContrib.DynamicProxy2, Version=2.1.13.813, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\lib\autofac-contrib\AutofacContrib.DynamicProxy2.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Castle.Core, Version=1.1.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\lib\Castle Windsor 2.0\bin\Castle.Core.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Castle.DynamicProxy2, Version=2.1.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\lib\Castle Windsor 2.0\bin\Castle.DynamicProxy2.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="FluentNHibernate, Version=1.0.0.593, Culture=neutral, PublicKeyToken=8aa435e3cb308880, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\lib\fluentnhibernate\FluentNHibernate.dll</HintPath>
|
||||
@@ -150,10 +150,12 @@
|
||||
<Compile Include="Data\Builders\SqlServerBuilder.cs" />
|
||||
<Compile Include="Data\Conventions\StringLengthConvention.cs" />
|
||||
<Compile Include="Data\SessionFactoryHolder.cs" />
|
||||
<Compile Include="Environment\AutofacUtil\DynamicProxy2\DynamicProxyContext.cs" />
|
||||
<Compile Include="Environment\AutofacUtil\DynamicProxy2\DynamicProxyExtensions.cs" />
|
||||
<Compile Include="Environment\AutofacUtil\DynamicProxy2\ConstructorFinderWrapper.cs" />
|
||||
<Compile Include="Environment\Configuration\AppDataFolder.cs" />
|
||||
<Compile Include="Environment\Configuration\ShellSettingsLoader.cs" />
|
||||
<Compile Include="Environment\ExtensibleInterceptionModule.cs" />
|
||||
<Compile Include="Environment\ShellBuilders\ContainerUpdater.cs" />
|
||||
<Compile Include="Environment\AutofacUtil\ContainerUpdater.cs" />
|
||||
<Compile Include="Environment\ShellBuilders\DefaultShellContainerFactory.cs" />
|
||||
<Compile Include="Environment\ShellBuilders\IShellContainerFactory.cs" />
|
||||
<Compile Include="Environment\ShellBuilders\SafeModeShellContainerFactory.cs" />
|
||||
|
@@ -1,43 +1,30 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Autofac;
|
||||
using Autofac.Core;
|
||||
using AutofacContrib.DynamicProxy2;
|
||||
using Castle.Core.Interceptor;
|
||||
using Orchard.Environment.AutofacUtil.DynamicProxy2;
|
||||
using Module = Autofac.Module;
|
||||
|
||||
namespace Orchard.Security {
|
||||
//public class SecurityModule : Module {
|
||||
// protected override void AttachToComponentRegistration(IComponentRegistry componentRegistry, IComponentRegistration registration) {
|
||||
// var implementationType = registration.Activator.LimitType;
|
||||
// var property = FindProperty(implementationType);
|
||||
public class SecurityModule : Module {
|
||||
protected override void AttachToComponentRegistration(IComponentRegistry componentRegistry, IComponentRegistration registration) {
|
||||
var implementationType = registration.Activator.LimitType;
|
||||
var property = FindProperty(implementationType);
|
||||
|
||||
// if (property != null) {
|
||||
|
||||
// }
|
||||
// }
|
||||
if (property != null) {
|
||||
registration.InterceptedBy<ISecurityModuleInterceptor>();
|
||||
}
|
||||
}
|
||||
|
||||
// public IEnumerable<Service> GetInterceptorServices(IComponentDescriptor descriptor) {
|
||||
// var property = FindProperty(descriptor.BestKnownImplementationType);
|
||||
// if (property != null) {
|
||||
// if (property.GetGetMethod(true).IsVirtual == false) {
|
||||
// throw new ApplicationException(string.Format("CurrentUser property must be virtual on class {0}", descriptor.BestKnownImplementationType.FullName));
|
||||
// }
|
||||
// yield return new TypedService(typeof(ISecurityModuleInterceptor));
|
||||
// }
|
||||
// }
|
||||
|
||||
// private static PropertyInfo FindProperty(Type type) {
|
||||
// return type.GetProperty("CurrentUser",
|
||||
// BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
|
||||
// null,
|
||||
// typeof(IUser),
|
||||
// new Type[0],
|
||||
// null);
|
||||
// }
|
||||
//}
|
||||
private static PropertyInfo FindProperty(Type type) {
|
||||
return type.GetProperty("CurrentUser",
|
||||
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
|
||||
null,
|
||||
typeof(IUser),
|
||||
new Type[0],
|
||||
null);
|
||||
}
|
||||
}
|
||||
|
||||
public interface ISecurityModuleInterceptor : IInterceptor, IDependency {
|
||||
|
||||
|
@@ -1,34 +1,30 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using Autofac;
|
||||
using Autofac.Core;
|
||||
using AutofacContrib.DynamicProxy2;
|
||||
using Castle.Core.Interceptor;
|
||||
using Orchard.Security;
|
||||
using Orchard.Environment.AutofacUtil.DynamicProxy2;
|
||||
using Module = Autofac.Module;
|
||||
|
||||
namespace Orchard.Settings {
|
||||
//public class SettingsModule : Module, IComponentInterceptorProvider {
|
||||
// public IEnumerable<Service> GetInterceptorServices(IComponentDescriptor descriptor) {
|
||||
// var property = FindProperty(descriptor.BestKnownImplementationType);
|
||||
// if (property != null) {
|
||||
// if (property.GetGetMethod(true).IsVirtual == false) {
|
||||
// throw new ApplicationException(string.Format("CurrentSite property must be virtual on class {0}", descriptor.BestKnownImplementationType.FullName));
|
||||
// }
|
||||
// yield return new TypedService(typeof(ISettingsModuleInterceptor));
|
||||
// }
|
||||
// }
|
||||
public class SettingsModule : Module {
|
||||
protected override void AttachToComponentRegistration(IComponentRegistry componentRegistry, IComponentRegistration registration) {
|
||||
var implementationType = registration.Activator.LimitType;
|
||||
var property = FindProperty(implementationType);
|
||||
|
||||
// private static PropertyInfo FindProperty(Type type) {
|
||||
// return type.GetProperty("CurrentSite",
|
||||
// BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
|
||||
// null,
|
||||
// typeof(ISite),
|
||||
// new Type[0],
|
||||
// null);
|
||||
// }
|
||||
//}
|
||||
if (property != null) {
|
||||
registration.InterceptedBy<ISettingsModuleInterceptor>();
|
||||
}
|
||||
}
|
||||
|
||||
private static PropertyInfo FindProperty(Type type) {
|
||||
return type.GetProperty("CurrentSite",
|
||||
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
|
||||
null,
|
||||
typeof(ISite),
|
||||
new Type[0],
|
||||
null);
|
||||
}
|
||||
}
|
||||
|
||||
public interface ISettingsModuleInterceptor : IInterceptor, IDependency {
|
||||
|
||||
|
Reference in New Issue
Block a user