示例#1
0
        private void AddDefaultTypeInterceptor(Type typeToIntercept, IPolicyList policies)
        {
            ITypeInterceptionPolicy policy;

            if (_interceptor != null)
            {
                policy = new FixedTypeInterceptionPolicy((ITypeInterceptor)_interceptor);
            }
            else
            {
                policy = new ResolvedTypeInterceptionPolicy(_interceptorKey);
            }

            policies.Set(policy, typeToIntercept);
        }
        private void AddDefaultTypeInterceptor <TPolicySet>(ref TPolicySet policies)
            where TPolicySet : IPolicySet
        {
            ITypeInterceptionPolicy policy;

            if (_interceptor != null)
            {
                policy = new FixedTypeInterceptionPolicy((ITypeInterceptor)_interceptor);
            }
            else
            {
                policy = new ResolvedTypeInterceptionPolicy(_interceptorKey);
            }

            policies.Set(typeof(ITypeInterceptionPolicy), policy);
        }
示例#3
0
        /// <summary>
        /// API to configure interception for a type.
        /// </summary>
        /// <param name="typeToIntercept">Type to intercept.</param>
        /// <param name="name">Name type is registered under.</param>
        /// <param name="interceptor">Interceptor to use.</param>
        /// <returns>This extension object.</returns>
        public Interception SetInterceptorFor(Type typeToIntercept, string name, ITypeInterceptor interceptor)
        {
            Guard.ArgumentNotNull(typeToIntercept, "typeToIntercept");
            Guard.ArgumentNotNull(interceptor, "interceptor");
            GuardTypeInterceptable(typeToIntercept, interceptor);

            var key = new NamedTypeBuildKey(typeToIntercept, name);

            var policy = new FixedTypeInterceptionPolicy(interceptor);

            Context.Policies.Set <ITypeInterceptionPolicy>(policy, key);

            // add policy injection behavior if using this configuration API to set the interceptor
            var interceptionBehaviorsPolicy = new InterceptionBehaviorsPolicy();

            interceptionBehaviorsPolicy.AddBehaviorKey(NamedTypeBuildKey.Make <PolicyInjectionBehavior>());
            Context.Policies.Set <IInterceptionBehaviorsPolicy>(interceptionBehaviorsPolicy, key);

            return(this);
        }
        public override void PreBuildUp(IBuilderContext context)
        {
            Guard.ArgumentNotNull(context, "context");
            if (!this.CanIntercept(context))
            {
                return;
            }
            if (context.Existing != null)
            {
                return;
            }
            var interceptionPolicy       = FindInterceptionPolicy <ITypeInterceptionPolicy>(context);
            ITypeInterceptor interceptor = null;

            if (interceptionPolicy == null)
            {
                interceptor = context.NewBuildUp <IInterceptor>(typeof(IInterceptor).AssemblyQualifiedName) as ITypeInterceptor;
                if (null == interceptor)
                {
                    return;
                }
                if (!interceptor.CanIntercept(context.BuildKey.Type))
                {
                    return;
                }
                interceptionPolicy = new FixedTypeInterceptionPolicy(interceptor);
                context.Policies.Set <ITypeInterceptionPolicy>(interceptionPolicy, context.BuildKey);
                context.Policies.Clear <IInstanceInterceptionPolicy>(context.BuildKey);
            }
            var interceptionBehaviorsPolicy = FindInterceptionPolicy <IInterceptionBehaviorsPolicy>(context);

            if (null == interceptionBehaviorsPolicy)
            {
                var policyInjectionBehavior = new InterceptionBehavior <PolicyInjectionBehavior>();
                policyInjectionBehavior.AddPolicies(context.OriginalBuildKey.Type, context.BuildKey.Type, context.BuildKey.Name, context.Policies);
            }
        }