Ejemplo n.º 1
0
        private void SetInterceptionBehaviorFor(Type typeToIntercept, Type typeOfInstance, string name)
        {
            if (!AllMatchingRulesApply(typeToIntercept, typeOfInstance))
            {
                return;
            }

            if (!TypeIsInterface(typeToIntercept))
            {
                return;
            }

            if (_interceptor.CanIntercept(typeOfInstance))
            {
                Container.Configure <unity.Interception>().SetDefaultInterceptorFor(typeOfInstance, _interceptor);
            }

            else if (_interceptor.CanIntercept(typeToIntercept))
            {
                Container.Configure <unity.Interception>().SetDefaultInterceptorFor(typeToIntercept, _interceptor);
            }

            _behaviors.ForEach(b =>
            {
                var interceptionBehavior = new InterceptionBehavior(b);
                interceptionBehavior.AddPolicies(typeToIntercept, typeOfInstance, name, Context.Policies);
            });
        }
        public override void PreBuildUp(IBuilderContext context)
        {
            Guard.ArgumentNotNull(context, "context");
            if (!this.CanIntercept(context))
            {
                return;
            }
            IInstanceInterceptionPolicy interceptionPolicy = FindInterceptionPolicy <IInstanceInterceptionPolicy>(context, true);

            if (null == interceptionPolicy)
            {
                IInstanceInterceptor interceptor = context.NewBuildUp <IInterceptor>(typeof(IInterceptor).AssemblyQualifiedName) as IInstanceInterceptor;
                if (null == interceptor)
                {
                    return;
                }
                if (!interceptor.CanIntercept(context.BuildKey.Type))
                {
                    return;
                }
                context.Policies.Set <IInstanceInterceptionPolicy>(new FixedInstanceInterceptionPolicy(interceptor), context.BuildKey);
                context.Policies.Clear <ITypeInterceptionPolicy>(context.BuildKey);
            }

            IInterceptionBehaviorsPolicy interceptionBehaviorsPolicy = FindInterceptionPolicy <IInterceptionBehaviorsPolicy>(context, true);

            if (null == interceptionBehaviorsPolicy)
            {
                var policyInjectionBehavior = new InterceptionBehavior <PolicyInjectionBehavior>();
                policyInjectionBehavior.AddPolicies(context.OriginalBuildKey.Type, context.BuildKey.Type, context.BuildKey.Name, context.Policies);
            }
        }
        /// <summary>
        /// Add policies to the <paramref name="policies"/> to configure the container with
        /// an appropriate <see cref="IInstanceInterceptionPolicy"/>
        /// </summary>
        /// <param name="serviceType">Type of the interface being registered. This parameter is
        /// ignored by this class.</param>
        /// <param name="implementationType">Type to register.</param>
        /// <param name="name">Name used to resolve the type object.</param>
        /// <param name="policies">Policy list to add policies to.</param>
        public override void AddPolicies(Type serviceType, Type implementationType, string name, IPolicyList policies)
        {
            var key = new NamedTypeBuildKey(implementationType);

            policies.Set <IInstanceInterceptionPolicy>(new FixedInstanceInterceptionPolicy(Interceptor), key);

            var piabInjectionMember = new InterceptionBehavior <PolicyInjectionBehavior>();

            piabInjectionMember.AddPolicies(serviceType, implementationType, name, policies);
        }
    private void OnRegister(object sender, RegisterEventArgs e)
    {
        if (ShouldIntercept(e))
        {
            IUnityContainer container = sender as IUnityContainer;

            var i = new Interceptor <InterfaceInterceptor>();
            i.AddPolicies(e.TypeFrom, e.TypeTo, e.Name, Context.Policies);
            behaviors.ForEach((b) =>
            {
                var ib = new InterceptionBehavior(b);
                ib.AddPolicies(e.TypeFrom, e.TypeTo, e.Name, Context.Policies);
            }
                              );
        }
    }
        /// <summary>
        /// The on register.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void OnRegister(object sender, RegisterEventArgs e)
        {
            if (this.InterceptionBehaviors != null && this.InterceptionBehaviors.Count > 0)
            {
                var container = sender as IUnityContainer;

                if (e != null && e.TypeFrom != null && e.TypeFrom.IsInterface && !e.TypeFrom.IsAssignableFrom(typeof(ICallHandler)))
                {
                    var interception = container.Configure <Interception>().SetInterceptorFor(e.TypeFrom, e.Name, new InterfaceInterceptor());

                    var behaviorsToAdd = this.InterceptionBehaviors.Where(b => b.ShouldIntercept(e));
                    foreach (var interceptConfig in behaviorsToAdd)
                    {
                        foreach (var interceptionBehavior in interceptConfig.InterceptionBehaviors)
                        {
                            var unityBehavior = new InterceptionBehavior(interceptionBehavior);
                            unityBehavior.AddPolicies(e.TypeFrom, e.TypeTo, e.Name, this.Context.Policies);
                        }
                    }
                }
            }
        }