private static bool CanDecorate(ServiceRegistration registration, IAspectConfiguration aspectConfiguration) { var serviceType = registration.ServiceType.GetTypeInfo(); var implType = registration.GetImplType().GetTypeInfo(); if (implType.IsProxy() || !implType.CanInherited()) { return(false); } if (_excepts.Any(x => implType.Name.Matches(x)) || _excepts.Any(x => implType.Namespace.Matches(x))) { return(false); } if (!serviceType.CanInherited() || serviceType.IsNonAspect()) { return(false); } var aspectValidator = new AspectValidatorBuilder(aspectConfiguration).Build(); if (!aspectValidator.Validate(serviceType, true) && !aspectValidator.Validate(implType, false)) { return(false); } return(true); }
public ServiceTable(IAspectConfiguration configuration) { var aspectValidatorBuilder = new AspectValidatorBuilder(configuration); _proxyTypeGenerator = new ProxyTypeGenerator(aspectValidatorBuilder); _serviceValidator = new ServiceValidator(aspectValidatorBuilder); _linkedServiceDefinitions = new ConcurrentDictionary <Type, LinkedList <ServiceDefinition> >(); _linkedGenericServiceDefinitions = new ConcurrentDictionary <Type, LinkedList <ServiceDefinition> >(); }
private void Kernel_ComponentModelCreated(ComponentModel model) { var aspectValidator = new AspectValidatorBuilder(_aspectConfiguration).Build(); if (aspectValidator.Validate(model.Implementation, false) || model.Services.Any(x => aspectValidator.Validate(x, true))) { model.Interceptors.AddIfNotInCollection(InterceptorReference.ForType <AspectCoreInterceptor>()); } }
private static void ComponentRegistration_Activating(object sender, ActivatingEventArgs <object> e) { if (e.Instance == null || e.Instance.IsProxy()) { return; } if (!(e.Component.Activator is ReflectionActivator || e.Component.Activator is DelegateActivator || e.Component.Activator is InstanceActivator)) { return; } var limitType = e.Instance.GetType(); if (!limitType.GetTypeInfo().CanInherited()) { return; } if (excepts.Any(x => limitType.Name.Matches(x)) || excepts.Any(x => limitType.Namespace.Matches(x))) { return; } var services = e.Component.Services.Select(x => ((IServiceWithType)x).ServiceType).ToList(); if (!services.All(x => x.GetTypeInfo().CanInherited()) || services.All(x => x.GetTypeInfo().IsNonAspect())) { return; } var aspectValidator = new AspectValidatorBuilder(e.Context.Resolve <IAspectConfiguration>()).Build(); if (services.All(x => !aspectValidator.Validate(x, true)) && !aspectValidator.Validate(limitType, false)) { return; } var proxyTypeGenerator = e.Context.Resolve <IProxyTypeGenerator>(); Type proxyType; object instance; var interfaceType = services.FirstOrDefault(x => x.GetTypeInfo().IsInterface); if (interfaceType == null) { var baseType = services.FirstOrDefault(x => x.GetTypeInfo().IsClass) ?? limitType; proxyType = proxyTypeGenerator.CreateClassProxyType(baseType, limitType); var activator = new ReflectionActivator(proxyType, new DefaultConstructorFinder(type => type.GetTypeInfo().DeclaredConstructors.ToArray()), new MostParametersConstructorSelector(), new AParameter[0], new AParameter[0]); instance = activator.ActivateInstance(e.Context, e.Parameters); } else { proxyType = proxyTypeGenerator.CreateInterfaceProxyType(interfaceType, limitType); instance = Activator.CreateInstance(proxyType, new object[] { e.Context.Resolve <IAspectActivatorFactory>(), e.Instance }); } var propertyInjector = e.Context.Resolve <IPropertyInjectorFactory>().Create(instance.GetType()); propertyInjector.Invoke(instance); e.Instance = instance; e.Component.RaiseActivating(e.Context, e.Parameters, ref instance); }
public void CreateInterfaceProxyType_Wit_ImplType() { var configuration = new AspectConfiguration(); configuration.Interceptors.AddTyped <EnableParameterAspectInterceptor>(); var validatorBuilder = new AspectValidatorBuilder(configuration); var proxyTypeGenerator = new ProxyTypeGenerator(validatorBuilder); var proxyType = proxyTypeGenerator.CreateInterfaceProxyType(typeof(IFakeOpenGenericService <>), typeof(FakeOpenGenericService <>)); var instance = Activator.CreateInstance(proxyType.MakeGenericType(typeof(PocoClass)), new object[] { null, new FakeOpenGenericService <PocoClass>(null) }); var field = instance.GetType().GetTypeInfo().GetField("_implementation", BindingFlags.Instance | BindingFlags.NonPublic); var targetInstance = field.GetValue(instance); Assert.NotEqual(instance, targetInstance); Assert.NotEqual(instance.GetType(), targetInstance.GetType()); }
public static IAspectValidator GetAspectValidator(IAspectConfigure configure) { var handlers = new List <IAspectValidationHandler> { new AccessibleAspectValidationHandler(), new AttributeAspectValidationHandler(), new CacheAspectValidationHandler(), new GlobalAspectValidationHandler(configure), new DynamicallyAspectValidationHandler(), new IgnoreAspectValidationHandler(configure), new NonAspectValidationHandler() }; var aspectValidatorBuilder = new AspectValidatorBuilder(handlers); return(aspectValidatorBuilder.Build()); }
public static IServiceContainer RegisterDynamicProxy(this IServiceContainer container, IAspectConfiguration aspectConfig, Action <IAspectConfiguration> configure = null) { if (container == null) { throw new ArgumentNullException(nameof(container)); } aspectConfig = aspectConfig ?? new AspectConfiguration(); foreach (var m in _nonAspect) { aspectConfig.NonAspectPredicates.AddNamespace(m); } configure?.Invoke(aspectConfig); container.AddSingleton <IServiceFactory>(container); container.AddSingleton <IServiceContainer>(container); container.AddSingleton <IAspectConfiguration>(aspectConfig) .AddTransient(typeof(IManyEnumerable <>), typeof(ManyEnumerable <>)) .AddSingleton <IServiceProvider, LightInjectServiceResolver>() .AddSingleton <IServiceResolver, LightInjectServiceResolver>() .AddSingleton <IScopeResolverFactory, LightInjectScopeResolverFactory>() .AddSingleton <IAspectContextFactory, AspectContextFactory>() .AddSingleton <IAspectActivatorFactory, AspectActivatorFactory>() .AddSingleton <IProxyGenerator, ProxyGenerator>() .AddSingleton <IParameterInterceptorSelector, ParameterInterceptorSelector>() .AddSingleton <IPropertyInjectorFactory, PropertyInjectorFactory>() .AddSingleton <IInterceptorCollector, InterceptorCollector>() .AddSingleton <IInterceptorSelector, ConfigureInterceptorSelector>(nameof(ConfigureInterceptorSelector)) .AddSingleton <IInterceptorSelector, AttributeInterceptorSelector>(nameof(AttributeInterceptorSelector)) // To register multiple services, you should set a name for each implement type. .AddSingleton <IAdditionalInterceptorSelector, AttributeAdditionalInterceptorSelector>() .AddSingleton <IAspectValidatorBuilder, AspectValidatorBuilder>() .AddSingleton <IAspectBuilderFactory, AspectBuilderFactory>() .AddSingleton <IProxyTypeGenerator, ProxyTypeGenerator>() .AddSingleton <IAspectCachingProvider, AspectCachingProvider>() .AddSingleton <IAspectExceptionWrapper, AspectExceptionWrapper>(); var aspectValidator = new AspectValidatorBuilder(aspectConfig).Build(); container.Decorate(aspectValidator.CreateDecorator()); return(container); }
public void Execute(ResolveRequestContext context, Action <ResolveRequestContext> next) { next(context); if (context.Instance == null || context.Instance.IsProxy()) { return; } if (!(context.Registration.Activator is ReflectionActivator || context.Registration.Activator is DelegateActivator || context.Registration.Activator is InstanceActivator)) { return; } var limitType = context.Instance.GetType(); if (!limitType.GetTypeInfo().CanInherited()) { return; } if (excepts.Any(x => limitType.Name.Matches(x)) || excepts.Any(x => limitType.Namespace.Matches(x))) { return; } var services = context.Registration.Services.Select(x => ((IServiceWithType)x).ServiceType).ToList(); if (!services.All(x => x.GetTypeInfo().CanInherited()) || services.All(x => x.GetTypeInfo().IsNonAspect())) { return; } var aspectValidator = new AspectValidatorBuilder(context.Resolve <IAspectConfiguration>()).Build(); if (services.All(x => !aspectValidator.Validate(x, true)) && !aspectValidator.Validate(limitType, false)) { return; } var proxyTypeGenerator = context.Resolve <IProxyTypeGenerator>(); Type proxyType; object instance; var interfaceType = services.FirstOrDefault(x => x.GetTypeInfo().IsInterface); if (interfaceType == null) { var baseType = services.FirstOrDefault(x => x.GetTypeInfo().IsClass) ?? limitType; proxyType = proxyTypeGenerator.CreateClassProxyType(baseType, limitType); //Autofac.Core.Activators.Reflection.ReflectionActivator var constructorSelector = new MostParametersConstructorSelector(); var constructorFinder = new DefaultConstructorFinder(type => type.GetTypeInfo().DeclaredConstructors.ToArray()); var availableConstructors = constructorFinder.FindConstructors(proxyType); if (availableConstructors.Length == 0) { throw new NoConstructorsFoundException(proxyType, $"No constructors on type '{proxyType}' can be found with the constructor finder '{constructorFinder}'."); } var binders = new ConstructorBinder[availableConstructors.Length]; for (var idx = 0; idx < availableConstructors.Length; idx++) { binders[idx] = new ConstructorBinder(availableConstructors[idx]); } var allBindings = GetAllBindings(binders, context, context.Parameters); var selectedBinding = constructorSelector.SelectConstructorBinding(allBindings, context.Parameters); instance = selectedBinding.Instantiate(); } else { proxyType = proxyTypeGenerator.CreateInterfaceProxyType(interfaceType, limitType); instance = Activator.CreateInstance(proxyType, new object[] { context.Resolve <IAspectActivatorFactory>(), context.Instance }); } var propertyInjector = context.Resolve <IPropertyInjectorFactory>().Create(instance.GetType()); propertyInjector.Invoke(instance); context.Instance = instance; }