/// <summary>
        /// Get the set of <see cref="IInterceptionBehavior"/> object to be used for the given type and
        /// interceptor.
        /// </summary>
        /// <remarks>
        /// This method will return a sequence of <see cref="IInterceptionBehavior"/>s. These behaviors will
        /// only be included if their <see cref="IInterceptionBehavior.WillExecute"/> properties are true.
        /// </remarks>
        /// <param name="context">Context for the current build operation.</param>
        /// <param name="interceptor">Interceptor that will be used to invoke the behavior.</param>
        /// <param name="typeToIntercept">Type that interception was requested on.</param>
        /// <param name="implementationType">Type that implements the interception.</param>
        /// <returns></returns>
        public IEnumerable <IInterceptionBehavior> GetEffectiveBehaviors(
            IBuilderContext context, IInterceptor interceptor, Type typeToIntercept, Type implementationType)
        {
            var interceptionRequest = new CurrentInterceptionRequest(interceptor, typeToIntercept, implementationType);

            foreach (var key in BehaviorKeys)
            {
                var behavior = (IInterceptionBehavior)context.NewBuildUp(key,
                                                                         childContext => childContext.AddResolverOverrides(
                                                                             new DependencyOverride <CurrentInterceptionRequest>(interceptionRequest)));
                yield return(behavior);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Get the set of <see cref="IInterceptionBehavior"/> object to be used for the given type and
        /// interceptor.
        /// </summary>
        /// <remarks>
        /// This method will return a sequence of <see cref="IInterceptionBehavior"/>s. These behaviors will
        /// only be included if their <see cref="IInterceptionBehavior.WillExecute"/> properties are true.
        /// </remarks>
        /// <param name="context">Context for the current build operation.</param>
        /// <param name="interceptor">Interceptor that will be used to invoke the behavior.</param>
        /// <param name="typeToIntercept">Type that interception was requested on.</param>
        /// <param name="implementationType">Type that implements the interception.</param>
        /// <returns></returns>
        public IEnumerable<IInterceptionBehavior> GetEffectiveBehaviors(
            IBuilderContext context, IInterceptor interceptor, Type typeToIntercept, Type implementationType)
        {
            var interceptionRequest = new CurrentInterceptionRequest(interceptor, typeToIntercept, implementationType);

            foreach (var key in BehaviorKeys)
            {
                var behavior = (IInterceptionBehavior)context.NewBuildUp(key,
                    childContext => childContext.AddResolverOverrides(
                        new DependencyOverride<CurrentInterceptionRequest>(interceptionRequest)));
                yield return behavior;
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="PolicyInjectionBehavior"/> with the given information
        /// about what's being intercepted and the current set of injection policies.
        /// </summary>
        /// <param name="interceptionRequest">Information about what will be injected.</param>
        /// <param name="policies">Current injection policies.</param>
        /// <param name="container">Unity container that can be used to resolve call handlers.</param>
        public PolicyInjectionBehavior(CurrentInterceptionRequest interceptionRequest, InjectionPolicy[] policies,
            IUnityContainer container)
        {
            var allPolicies = new PolicySet(policies);
            bool hasHandlers = false;

            var manager = new PipelineManager();

            foreach (MethodImplementationInfo method in
                interceptionRequest.Interceptor.GetInterceptableMethods(
                    interceptionRequest.TypeToIntercept, interceptionRequest.ImplementationType))
            {
                bool hasNewHandlers = manager.InitializePipeline(method,
                    allPolicies.GetHandlersFor(method, container));
                hasHandlers = hasHandlers || hasNewHandlers;
            }
            pipelineManager = hasHandlers ? manager : null;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="PolicyInjectionBehavior"/> with the given information
        /// about what's being intercepted and the current set of injection policies.
        /// </summary>
        /// <param name="interceptionRequest">Information about what will be injected.</param>
        /// <param name="policies">Current injection policies.</param>
        /// <param name="container">Unity container that can be used to resolve call handlers.</param>
        public PolicyInjectionBehavior(CurrentInterceptionRequest interceptionRequest, InjectionPolicy[] policies,
                                       IUnityContainer container)
        {
            var  allPolicies = new PolicySet(policies);
            bool hasHandlers = false;

            var manager = new PipelineManager();

            foreach (MethodImplementationInfo method in
                     interceptionRequest.Interceptor.GetInterceptableMethods(
                         interceptionRequest.TypeToIntercept, interceptionRequest.ImplementationType))
            {
                bool hasNewHandlers = manager.InitializePipeline(method,
                                                                 allPolicies.GetHandlersFor(method, container));
                hasHandlers = hasHandlers || hasNewHandlers;
            }
            pipelineManager = hasHandlers ? manager : null;
        }
        /// <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();
            var proxyInterface = interfaces.FirstOrDefault();
            var additionalInterfaces = interfaces.Skip(1).ToArray();

            CurrentInterceptionRequest request = new CurrentInterceptionRequest(interceptor, proxyInterface, value.GetType());
            InjectionPolicy[] policies = new InjectionPolicy[] { new AttributeDrivenPolicy() };
            PolicyInjectionBehavior behaviour = new PolicyInjectionBehavior(request, policies, null);

            var proxy = Microsoft.Practices.Unity.InterceptionExtension.Intercept.ThroughProxyWithAdditionalInterfaces(
                    proxyInterface,
                    value,
                    interceptor,
                    new[] { behaviour },
                    additionalInterfaces);

            return proxy;
        }