public void ParametersPassProperlyToTarget() { IInstanceInterceptor interceptor = new InterfaceInterceptor(); MyDal target = new MyDal(); IInterceptingProxy proxy = interceptor.CreateProxy(typeof(IDal), target); CallCountHandler depositHandler = new CallCountHandler(); CallCountHandler withdrawHandler = new CallCountHandler(); proxy.SetPipeline(typeof(IDal).GetMethod("Deposit"), new HandlerPipeline(new ICallHandler[] { depositHandler })); proxy.SetPipeline(typeof(IDal).GetMethod("Withdraw"), new HandlerPipeline(new ICallHandler[] { withdrawHandler })); IDal intercepted = (IDal)proxy; intercepted.Deposit(100.0); intercepted.Deposit(25.95); intercepted.Deposit(19.95); intercepted.Withdraw(15.00); intercepted.Withdraw(6.25); Assert.AreEqual(3, depositHandler.CallCount); Assert.AreEqual(2, withdrawHandler.CallCount); Assert.AreEqual(100.0 + 25.95 + 19.95 - 15.00 - 6.25, target.Balance); }
public void ThrowingFromInterceptedMethodStillRunsAllHandlers() { MethodInfo thrower = typeof(ClassWithDefaultCtor).GetMethod("NotImplemented"); ClassWithDefaultCtor instance = WireupHelper.GetInterceptingInstance <ClassWithDefaultCtor>(); IInterceptingProxy pm = (IInterceptingProxy)instance; CallCountHandler handler = new CallCountHandler(); PostCallCountHandler postHandler = new PostCallCountHandler(); HandlerPipeline pipeline = new HandlerPipeline(new ICallHandler[] { postHandler, handler }); pm.SetPipeline(thrower, pipeline); try { instance.NotImplemented(); Assert.Fail("Should have thrown before getting here"); } catch (NotImplementedException) { // We're expecting this one } Assert.AreEqual(1, handler.CallCount); Assert.AreEqual(1, postHandler.CallsCompleted); }
private void ApplyPolicies(IInterceptor interceptor, IInterceptingProxy proxy, object target, PolicySet policies) { foreach (MethodImplementationInfo method in interceptor.GetInterceptableMethods(target.GetType(), target.GetType())) { HandlerPipeline pipeline = new HandlerPipeline(policies.GetHandlersFor(method, container)); proxy.SetPipeline(method.ImplementationMethodInfo, pipeline); } }
private void SetPipeline(object instance, string methodName, params ICallHandler[] handlers) { HandlerPipeline pipeline = new HandlerPipeline(handlers); MethodInfo targetMethod = instance.GetType().BaseType.GetMethod(methodName); IInterceptingProxy proxy = (IInterceptingProxy)instance; proxy.SetPipeline(targetMethod, pipeline); }
internal static T GetInterceptedInstance <T>(string methodName, ICallHandler handler) { MethodInfo method = typeof(T).GetMethod(methodName); T instance = GetInterceptingInstance <T>(); IInterceptingProxy pm = (IInterceptingProxy)instance; pm.SetPipeline(method, new HandlerPipeline(Seq.Collect(handler))); return(instance); }
public void CanAddHandlersToPipeline() { MethodInfo methodOne = typeof(ClassWithDefaultCtor).GetMethod("MethodOne"); ClassWithDefaultCtor instance = WireupHelper.GetInterceptingInstance <ClassWithDefaultCtor>(); IInterceptingProxy pm = (IInterceptingProxy)instance; CallCountHandler handler = new CallCountHandler(); HandlerPipeline pipeline = new HandlerPipeline(new CallCountHandler[] { handler }); pm.SetPipeline(methodOne, pipeline); }
SignatureTestTarget GetTarget() { TransparentProxyInterceptor interceptor = new TransparentProxyInterceptor(); PolicySet policySet = GetPolicies(); SignatureTestTarget target = new SignatureTestTarget(); IInterceptingProxy proxy = interceptor.CreateProxy(typeof(SignatureTestTarget), target); foreach (MethodImplementationInfo method in interceptor.GetInterceptableMethods(typeof(SignatureTestTarget), typeof(SignatureTestTarget))) { HandlerPipeline pipeline = new HandlerPipeline( policySet.GetHandlersFor(method, container)); proxy.SetPipeline(method.ImplementationMethodInfo, pipeline); } return((SignatureTestTarget)proxy); }
public void CallingMethodInvokesHandlers() { MethodInfo methodOne = typeof(ClassWithDefaultCtor).GetMethod("MethodOne"); ClassWithDefaultCtor instance = WireupHelper.GetInterceptingInstance <ClassWithDefaultCtor>(); IInterceptingProxy pm = (IInterceptingProxy)instance; CallCountHandler handler = new CallCountHandler(); PostCallCountHandler postHandler = new PostCallCountHandler(); HandlerPipeline pipeline = new HandlerPipeline(new ICallHandler[] { postHandler, handler }); pm.SetPipeline(methodOne, pipeline); instance.MethodOne(); Assert.AreEqual(1, handler.CallCount); Assert.AreEqual(1, postHandler.CallsCompleted); }
public void CanInterceptMethodsThroughProxy() { MethodInfo doSomething = typeof(MBROWithOneMethod).GetMethod("DoSomething"); CallCountHandler handler = new CallCountHandler(); MBROWithOneMethod original = new MBROWithOneMethod(); MBROWithOneMethod intercepted = new InterceptingRealProxy(original, typeof(MBROWithOneMethod)) .GetTransparentProxy() as MBROWithOneMethod; IInterceptingProxy proxy = (IInterceptingProxy)intercepted; proxy.SetPipeline(doSomething, new HandlerPipeline(Seq.Collect <ICallHandler>(handler))); int result = intercepted.DoSomething(5); Assert.AreEqual(5 * 3, result); Assert.AreEqual(1, handler.CallCount); }
public void GenericWrapperWorks() { PostCallCountHandler handler = new PostCallCountHandler(); InterceptingGenericClass <DateTime> instance = new WrapperGeneric <DateTime>(); IInterceptingProxy pm = (IInterceptingProxy)instance; MethodBase reverse = typeof(InterceptingGenericClass <DateTime>).GetMethod("Reverse"); pm.SetPipeline(reverse, new HandlerPipeline(Seq.Collect <ICallHandler>(handler))); DateTime now = DateTime.Now; string result = instance.Reverse(137); Assert.AreEqual("731", result); Assert.AreEqual(1, handler.CallsCompleted); }
public void CanGenerateProxyForClosedGeneric() { IInstanceInterceptor interceptor = new InterfaceInterceptor(); GenericImplementationOne <DateTime> target = new GenericImplementationOne <DateTime>(); IInterceptingProxy proxy = interceptor.CreateProxy(typeof(IGenericOne <DateTime>), target); CallCountHandler handler = new CallCountHandler(); proxy.SetPipeline(typeof(IGenericOne <DateTime>).GetMethod("DoSomething"), new HandlerPipeline(new ICallHandler[] { handler })); IGenericOne <DateTime> intercepted = (IGenericOne <DateTime>)proxy; DateTime now = DateTime.Now; DateTime result = intercepted.DoSomething(now); Assert.AreEqual(now, result); Assert.IsTrue(target.DidSomething); }
/// <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; } } } }
public void ProxyMapsInterfaceMethodsToTheirImplementations() { MethodInfo something = typeof(InterfaceOne).GetMethod("Something"); MethodInfo somethingImpl = typeof(MBROWithInterface).GetMethod("Something"); CallCountHandler handler = new CallCountHandler(); MBROWithInterface original = new MBROWithInterface(); MBROWithInterface intercepted = new InterceptingRealProxy(original, typeof(MBROWithOneMethod)) .GetTransparentProxy() as MBROWithInterface; HandlerPipeline pipeline = new HandlerPipeline(Seq.Collect <ICallHandler>(handler)); IInterceptingProxy proxy = (IInterceptingProxy)intercepted; proxy.SetPipeline(something, pipeline); HandlerPipeline implPipeline = proxy.GetPipeline(somethingImpl); Assert.AreSame(pipeline, implPipeline); }
public void GeneratedProxyCallsHandlers() { IInstanceInterceptor interceptor = new InterfaceInterceptor(); ImplementsInterfaceOne target = new ImplementsInterfaceOne(); IInterceptingProxy proxy = interceptor.CreateProxy(typeof(IInterfaceOne), target); CallCountHandler handler = new CallCountHandler(); proxy.SetPipeline(typeof(IInterfaceOne).GetMethod("TargetMethod"), new HandlerPipeline(new ICallHandler[] { handler })); IInterfaceOne intercepted = (IInterfaceOne)proxy; intercepted.TargetMethod(); intercepted.TargetMethod(); intercepted.TargetMethod(); Assert.AreEqual(3, handler.CallCount); }
public void RefsAndOutsAreProperlyHandled() { IInstanceInterceptor interceptor = new InterfaceInterceptor(); ImplementsHaveSomeRefsAndOuts target = new ImplementsHaveSomeRefsAndOuts(); IInterceptingProxy proxy = interceptor.CreateProxy(typeof(IHaveSomeRefsAndOuts), target); CallCountHandler handler = new CallCountHandler(); proxy.SetPipeline(typeof(IHaveSomeRefsAndOuts).GetMethod("DoSomething"), new HandlerPipeline(new ICallHandler[] { handler })); IHaveSomeRefsAndOuts intercepted = (IHaveSomeRefsAndOuts)proxy; int a; string s = "something"; intercepted.DoSomething(out a, ref s); Assert.AreEqual(37, a); Assert.AreEqual("+++something***", s); Assert.AreEqual(1, handler.CallCount); }
/// <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); } }