/// <summary>
        /// Intercepts an exported value.
        /// </summary>
        /// <param name="value">The value to be intercepted.</param>
        /// <returns>Intercepted value.</returns>
        public object Intercept(object value)
        {
            var interfaces = value.GetType().GetInterfaces();

            ProxyFactory factory = new ProxyFactory(value);

            foreach (var intf in interfaces)
            {
                factory.AddInterface(intf);
            }

            if (this.advices != null)
            {
                foreach (var advice in this.advices)
                {
                    factory.AddAdvice(advice);
                }
            }
            else
            {
                foreach (var advisor in this.advisors)
                {
                    factory.AddAdvisor(advisor);
                }
            }

            return factory.GetProxy();
        }
        /// <summary>
        /// Initialize the proxy.
        /// </summary>
        private void InitializeProxy()
        {
            if (this.adviceChain.Length == 0)
            {
                return;
            }

            var factory = new ProxyFactory();

            foreach (var advice in this.adviceChain)
            {
                factory.AddAdvisor(new DefaultPointcutAdvisor(TruePointcut.True, advice));
            }

            factory.ProxyTargetType = false;
            factory.AddInterface(typeof(IContainerDelegate));
            factory.Target = this.containerDelegate;
            this.proxy     = (IContainerDelegate)factory.GetProxy();
        }
Exemple #3
0
        public void Example_of_how_to_use_Spring_programmatic_interception_with_pointcuts()
        {
            ProxyFactory factory = new ProxyFactory(new ServiceCommand());

            factory.AddAdvisor(new DefaultPointcutAdvisor(
                                   new SdkRegularExpressionMethodPointcut("DoExecute"),
                                   new ConsoleLoggingAroundAdvice()
                                   ));
            ICommand command = (ICommand)factory.GetProxy();

            command.DoExecute();
            // Console output:
            // Advice executing...
            // DoExecute()
            // Advice executed

            command.Execute();
            // Console output:
            // Execute()
        }
Exemple #4
0
        /// <summary>
        /// Create an AOP proxy for the given object.
        /// </summary>
        /// <param name="targetType">Type of the object.</param>
        /// <param name="targetName">The name of the object.</param>
        /// <param name="specificInterceptors">The set of interceptors that is specific to this
        /// object (may be empty but not null)</param>
        /// <param name="targetSource">The target source for the proxy, already pre-configured to access the object.</param>
        /// <returns>The AOP Proxy for the object.</returns>
        protected virtual object CreateProxy(Type targetType, string targetName, object[] specificInterceptors, ITargetSource targetSource)
        {
            ProxyFactory proxyFactory = CreateProxyFactory();

            // copy our properties (proxyTargetClass) inherited from ProxyConfig
            proxyFactory.CopyFrom(this);

            object target = targetSource.GetTarget();


            if (!ProxyTargetType)
            {
                // Must allow for introductions; can't just set interfaces to
                // the target's interfaces only.
                Type[] targetInterfaceTypes = AopUtils.GetAllInterfacesFromType(targetType);
                foreach (Type interfaceType in targetInterfaceTypes)
                {
                    proxyFactory.AddInterface(interfaceType);
                }
            }


            IAdvisor[] advisors = BuildAdvisors(targetName, specificInterceptors);

            foreach (IAdvisor advisor in advisors)
            {
                if (advisor is IIntroductionAdvisor)
                {
                    proxyFactory.AddIntroduction((IIntroductionAdvisor)advisor);
                }
                else
                {
                    proxyFactory.AddAdvisor(advisor);
                }
            }
            proxyFactory.TargetSource = targetSource;
            CustomizeProxyFactory(proxyFactory);

            proxyFactory.IsFrozen = freezeProxy;
            return(proxyFactory.GetProxy());
        }
Exemple #5
0
        public T Create <T>(params Aspect[] with) where T : new()
        {
            var proxy = new ProxyFactory
            {
                ProxyTargetType = true,
                TargetSource    = new InheritanceBasedAopTargetSource(typeof(T))
            };

            foreach (var aspect in with)
            {
                proxy.AddAdvisor(new SpringAspect
                {
                    Aspect = aspect
                });
            }

            var proxyTypeBuilder = new InheritanceAopProxyTypeBuilder(proxy);
            var proxyType        = proxyTypeBuilder.BuildProxyType();

            return((T)proxyType.GetConstructors()[0].Invoke(new object[] { proxy }));
        }
Exemple #6
0
        /// <summary>
        /// Add PersistenceExceptionTranslationAdvice to candidate object if it is a match.
        /// Create AOP proxy if necessary or add advice to existing advice chain.
        /// </summary>
        /// <param name="instance">The new object instance.</param>
        /// <param name="objectName">The name of the object.</param>
        /// <returns>
        /// The object instance to use, wrapped with either the original or a wrapped one.
        /// </returns>
        /// <exception cref="Spring.Objects.ObjectsException">
        /// In case of errors.
        /// </exception>
        public object PostProcessAfterInitialization(object instance, string objectName)
        {
            IAdvised advised = instance as IAdvised;
            Type     targetType;

            if (advised != null)
            {
                targetType = advised.TargetSource.TargetType;
            }
            else
            {
                targetType = instance.GetType();
            }
            if (targetType == null)
            {
                // Can't do much here
                return(instance);
            }

            if (AopUtils.CanApply(this.persistenceExceptionTranslationAdvisor, targetType, ReflectionUtils.GetInterfaces(targetType)))
            {
                if (advised != null)
                {
                    advised.AddAdvisor(this.persistenceExceptionTranslationAdvisor);
                    return(instance);
                }
                else
                {
                    ProxyFactory proxyFactory = new ProxyFactory(instance);
                    // copy our properties inherited from ProxyConfig
                    proxyFactory.CopyFrom(this);
                    proxyFactory.AddAdvisor(this.persistenceExceptionTranslationAdvisor);
                    return(proxyFactory.GetProxy());
                }
            }
            else
            {
                return(instance);
            }
        }
 protected virtual void AddPersistenceExceptionTranslation(ProxyFactory pf, IPersistenceExceptionTranslator pet)
 {
     pf.AddAdvisor(new PersistenceExceptionTranslationAdvisor(pet, typeof(RepositoryAttribute)));
 }
        /// <summary>
        /// Create an AOP proxy for the given object.
        /// </summary>
        /// <param name="objectType">Type of the object.</param>
        /// <param name="objectName">The name of the object.</param>
        /// <returns>The AOP Proxy for the object.</returns>
        protected virtual ProxyFactory CreateProxyFactory(Type objectType, string objectName)
        {
            ProxyFactory proxyFactory = new ProxyFactory();
            proxyFactory.ProxyTargetAttributes = this.ProxyTargetAttributes;
            proxyFactory.TargetSource = new InheritanceBasedAopTargetSource(objectType);
            if (!ProxyInterfaces)
            {
                proxyFactory.Interfaces = Type.EmptyTypes;
            }

            IList<IAdvisor> advisors = ResolveInterceptorNames();
            foreach (IAdvisor advisor in advisors)
            {
                if (advisor is IIntroductionAdvisor)
                {
                    proxyFactory.AddIntroduction((IIntroductionAdvisor)advisor);
                }
                else
                {
                    proxyFactory.AddAdvisor(advisor);
                }
            }

            return proxyFactory;
        }