/// <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> /// Called during the chain of responsibility for a build operation. The /// PostBuildUp method is called when the chain has finished the PreBuildUp /// phase and executes in reverse order from the PreBuildUp calls. /// </summary> /// <param name="context">Context of the build operation.</param> public override void PostBuildUp(IBuilderContext context) { // If it's already been intercepted, don't do it again. if (context.Existing is IInterceptingProxy) { return; } Type originalType; if (!BuildKey.TryGetType(context.OriginalBuildKey, out originalType)) { return; } Type typeToIntercept; IInstanceInterceptionPolicy interceptionPolicy = FindInterceptorPolicy(context, out typeToIntercept); if (interceptionPolicy != null) { IInstanceInterceptor interceptor = interceptionPolicy.Interceptor; if (interceptor.CanIntercept(typeToIntercept)) { IUnityContainer container = BuilderContext.NewBuildUp <IUnityContainer>(context); InjectionPolicy[] policies = BuilderContext.NewBuildUp <InjectionPolicy[]>(context); PolicySet allPolicies = new PolicySet(policies); IInterceptingProxy proxy = interceptor.CreateProxy(typeToIntercept, context.Existing); bool hasHandlers = false; foreach (MethodImplementationInfo method in interceptor.GetInterceptableMethods(typeToIntercept, context.Existing.GetType())) { HandlerPipeline pipeline = new HandlerPipeline(allPolicies.GetHandlersFor(method, container)); if (pipeline.Count > 0) { proxy.SetPipeline(interceptor.MethodInfoForPipeline(method), pipeline); hasHandlers = true; } } if (hasHandlers) { context.Existing = proxy; } } } }
/// <summary> /// Called during the chain of responsibility for a build operation. The /// PostBuildUp method is called when the chain has finished the PreBuildUp /// phase and executes in reverse order from the PreBuildUp calls. /// </summary> /// <remarks>In this class, PostBuildUp checks to see if the object was proxyable, /// and if it was, wires up the handlers.</remarks> /// <param name="context">Context of the build operation.</param> public override void PostBuildUp(IBuilderContext context) { Guard.ArgumentNotNull(context, "context"); IInterceptingProxy proxy = context.Existing as IInterceptingProxy; if (proxy == null) { return; } ITypeInterceptionPolicy interceptionPolicy = GetInterceptionPolicy(context); Type typeToIntercept = BuildKey.GetType(context.BuildKey); PolicySet interceptionPolicies = new PolicySet(BuilderContext.NewBuildUp <InjectionPolicy[]>(context)); IUnityContainer currentContainer = BuilderContext.NewBuildUp <IUnityContainer>(context); foreach (MethodImplementationInfo item in interceptionPolicy.Interceptor.GetInterceptableMethods(typeToIntercept, typeToIntercept)) { HandlerPipeline pipeline = new HandlerPipeline( interceptionPolicies.GetHandlersFor(item, currentContainer)); proxy.SetPipeline(interceptionPolicy.Interceptor.MethodInfoForPipeline(item), pipeline); } }