Exemplo n.º 1
0
        private void DefineProxyType(IServiceFactory servicefactory, ProxyDefinition proxyDefinition)
        {
            //container.Register<ILogFactory, LogFactory>();
            //container.Register<ILogBase,log.Logger>();

            proxyDefinition.Implement(() => servicefactory.GetInstance<IInterceptor>("LogInterceptor"));
        }
        public void Create_TargetWithEventWithoutInterceptingAddRemove_CanCreateProxyType()
        {
            var proxyDefinition = new ProxyDefinition(typeof(ClassWithEvent));

            proxyDefinition.Implement(() => new SampleInterceptor(), method => method.Name == "ToString");
            Type proxyType = CreateProxyType(proxyDefinition);

            Assert.True(typeof(ClassWithEvent).IsAssignableFrom(proxyType));
        }
 public void Execute_InterceptedMethodWithTargetReturnType_ReturnsProxy()
 {
     var proxyBuilder = CreateProxyBuilder();
     var proxyDefinition = new ProxyDefinition(typeof(ClassWithTargetReturnType));
     proxyDefinition.Implement(() => new SampleInterceptor());
     var proxyType = proxyBuilder.GetProxyType(proxyDefinition);
     var instance = (ClassWithTargetReturnType)Activator.CreateInstance(proxyType);
     Assert.IsAssignableFrom(typeof (IProxy), instance);
 }
        private T CreateProxy <T>(IInterceptor interceptor, string methodName)
        {
            var proxyDefinition = new ProxyDefinition(typeof(T));

            proxyDefinition.Implement(() => interceptor, info => info.Name == methodName);
            Type proxyType = CreateProxyBuilder().GetProxyType(proxyDefinition);

            return((T)Activator.CreateInstance(proxyType, (object)null));
        }
        private T CreateProxy <T>(IInterceptor interceptor, T target)
        {
            var proxyDefinition = new ProxyDefinition(typeof(T));

            proxyDefinition.Implement(() => interceptor, info => info.Name == "Execute");
            Type proxyType = CreateProxyBuilder().GetProxyType(proxyDefinition);

            return((T)Activator.CreateInstance(proxyType, new Lazy <T>(() => target)));
        }
Exemplo n.º 6
0
        private ProxyDefinition CreateProxyDefinition(Type serviceType)
        {
            var proxyDefinition = new ProxyDefinition(serviceType, () => null);

            proxyDefinition.Implement(
                () => new ServiceInterceptor(serviceClient),
                method => method.DeclaringType == serviceType);
            return(proxyDefinition);
        }
        private T CreateProxy <T>(T target, string methodName)
        {
            var proxyDefinition = new ProxyDefinition(typeof(T));
            var interceptor     = CreateProceedingInterceptor();

            proxyDefinition.Implement(() => interceptor, m => m.Name == methodName);
            Type proxyType = CreateProxyBuilder().GetProxyType(proxyDefinition);

            return((T)Activator.CreateInstance(proxyType, new Lazy <T>(() => target)));
        }
        public void GetProxyType_InterceptedVirtualMethod_ReturnsSubClass()
        {
            var proxyDefinition = new ProxyDefinition(typeof(ClassWithVirtualMethod));

            proxyDefinition.Implement(() => null, info => info.Name == "Execute");

            Type proxyType = CreateProxyType(proxyDefinition);

            Assert.IsTrue(proxyType.IsSubclassOf(typeof(ClassWithVirtualMethod)));
        }
Exemplo n.º 9
0
        public void GetProxyType_InterceptedVirtualMethod_ReturnsSubClass()
        {
            var proxyDefinition = new ProxyDefinition(typeof(ClassWithVirtualMethod));

            proxyDefinition.Implement(() => null, info => info.Name == "Execute");

            Type proxyType = CreateProxyType(proxyDefinition);

            Assert.IsTrue(proxyType.IsSubclassOf(typeof(ClassWithVirtualMethod)));
        }
Exemplo n.º 10
0
        private T CreateProxy <T>(T target, IInterceptor interceptor)
        {
            ProxyBuilder    proxyBuilder    = new ProxyBuilder();
            ProxyDefinition proxyDefinition = new ProxyDefinition(typeof(T), () => target);

            proxyDefinition.Implement(() => interceptor);
            var proxyType = proxyBuilder.GetProxyType(proxyDefinition);
            var proxy     = (T)Activator.CreateInstance(proxyType);

            return(proxy);
        }
        public void ShouldHandleCallsToVirtualMemberInConstructor()
        {
            var proxyBuilder    = CreateProxyBuilder();
            var proxyDefinition = new ProxyDefinition(typeof(ClassWithVirtualMemberCallInConstructor));

            proxyDefinition.Implement(() => new SampleInterceptor(), info => true);
            var proxyType = proxyBuilder.GetProxyType(proxyDefinition);
            var instance  = Activator.CreateInstance(proxyType);

            Assert.NotNull(instance);
        }
        public void Execute_InterceptedMethodWithTargetReturnType_ReturnsProxy()
        {
            var proxyBuilder    = CreateProxyBuilder();
            var proxyDefinition = new ProxyDefinition(typeof(ClassWithTargetReturnType));

            proxyDefinition.Implement(() => new SampleInterceptor());
            var proxyType = proxyBuilder.GetProxyType(proxyDefinition);
            var instance  = (ClassWithTargetReturnType)Activator.CreateInstance(proxyType);

            Assert.IsAssignableFrom(typeof(IProxy), instance);
        }
        public void GetProxyType_InterceptedVirtualMethod_DefinesParameterName()
        {
            var proxyDefinition = new ProxyDefinition(typeof(ClassWithVirtualMethodTakingParameter));

            proxyDefinition.Implement(() => new SampleInterceptor(), m => m.Name == "Execute");

            Type proxyType = CreateProxyType(proxyDefinition);

            var parameter = proxyType.GetMethod("Execute").GetParameters().Single();

            Assert.Equal("id", parameter.Name);
        }
        public void GetProxyType_NonGenericMethod_DeclaresPrivateStaticInterceptedMethodInfoField()
        {
            var proxyDefinition = new ProxyDefinition(typeof(IMethodWithNoParameters));

            proxyDefinition.Implement(() => null, info => info.Name == "Execute");

            var proxyType = CreateProxyType(proxyDefinition);

            FieldInfo interceptedMethodInfoField = proxyType.GetField("ExecuteTargetMethodInfo0", BindingFlags.NonPublic | BindingFlags.Static);

            Assert.NotNull(interceptedMethodInfoField);
        }
Exemplo n.º 15
0
        public void Create_TargetWithConstructorParameter_PassesValueToBaseClass()
        {
            var proxyDefinition = new ProxyDefinition(typeof(ClassWithConstructor));

            proxyDefinition.Implement(() => new SampleInterceptor(), info => info.Name == "Execute");

            Type proxyType = CreateProxyType(proxyDefinition);

            var proxy = (ClassWithConstructor)Activator.CreateInstance(proxyType, "SomeValue");

            Assert.AreEqual("SomeValue", proxy.Value);
        }
Exemplo n.º 16
0
        public void Execute_VirtualMethod_CallsMethodInBaseClass()
        {
            var proxyDefinition = new ProxyDefinition(typeof(ClassWithVirtualMethod));

            proxyDefinition.Implement(() => new SampleInterceptor(), info => info.Name == "Execute");

            Type proxyType = CreateProxyType(proxyDefinition);

            var proxy = (ClassWithVirtualMethod)Activator.CreateInstance(proxyType);

            proxy.Execute();
        }
        public void Execute_VirtualMethod_CallsMethodInBaseClass()
        {                        
            var proxyDefinition = new ProxyDefinition(typeof(ClassWithVirtualMethod));

            proxyDefinition.Implement(() => new SampleInterceptor(), info => info.Name == "Execute");

            Type proxyType = CreateProxyType(proxyDefinition);

            var proxy = (ClassWithVirtualMethod)Activator.CreateInstance(proxyType);

            proxy.Execute();
        }
        public void GetProxyType_SingleInterceptor_DeclaresPublicLazyInterceptorFactoryField()
        {
            var proxyDefinition = new ProxyDefinition(typeof(ITarget));

            proxyDefinition.Implement(() => null, info => true);

            var proxyType = CreateProxyType(proxyDefinition);

            FieldInfo targetField = proxyType.GetField("InterceptorFactory0", BindingFlags.Public | BindingFlags.Static);

            Assert.Equal(typeof(Func <IInterceptor>), targetField.FieldType);
        }
        public void GetProxyType_SingleInterceptor_DeclaresPrivateLazyInterceptorField()
        {
            var proxyDefinition = new ProxyDefinition(typeof(ITarget));

            proxyDefinition.Implement(() => null, info => true);

            var proxyType = CreateProxyType(proxyDefinition);

            FieldInfo targetField = proxyType.GetField("interceptor0", BindingFlags.NonPublic | BindingFlags.Instance);

            Assert.Equal(typeof(Lazy <IInterceptor>), targetField.FieldType);
        }
        public void ShouldReturnGetHashCode()
        {
            var proxyBuilder    = CreateProxyBuilder();
            var proxyDefinition = new ProxyDefinition(typeof(IFoo), () => null);

            proxyDefinition.Implement(() => new SampleInterceptor());
            var proxyType = proxyBuilder.GetProxyType(proxyDefinition);
            var instance1 = (IFoo)Activator.CreateInstance(proxyType);
            var instance2 = (IFoo)Activator.CreateInstance(proxyType);

            Assert.NotEqual(instance1.GetHashCode(), instance2.GetHashCode());
        }
        public void ShouldCopyConstructorParameterAttributes()
        {
            var proxyBuilder    = CreateProxyBuilder();
            var proxyDefinition = new ProxyDefinition(typeof(ClassWithAttributedParameterInConstructor));

            proxyDefinition.Implement(() => new SampleInterceptor(), info => true);
            var proxyType       = proxyBuilder.GetProxyType(proxyDefinition);
            var constructorInfo = proxyType.GetConstructors().Single();
            var parameterInfo   = constructorInfo.GetParameters().Single();

            Assert.NotNull(parameterInfo.GetCustomAttribute <CustomConstructorParameterAttribute>());
        }
        public void Execute_AbstractMethod_InvokeInterceptor()
        {
            var interceptorMock = new Mock <IInterceptor>();
            var proxyBuilder    = new ProxyBuilder();
            var proxyDefinition = new ProxyDefinition(typeof(ClassWithAbstractMethod), () => null);

            proxyDefinition.Implement(() => interceptorMock.Object);
            var proxyType = proxyBuilder.GetProxyType(proxyDefinition);
            var instance  = (ClassWithAbstractMethod)Activator.CreateInstance(proxyType);

            instance.Execute();
            interceptorMock.Verify(interceptor => interceptor.Invoke(It.IsAny <IInvocationInfo>()), Times.Once);
        }
Exemplo n.º 23
0
        public static T CreateDuck <T>(this object target)
            where T : class
        {
            if (target is IMethodInvoker invoker)
            {
                return(invoker.CreateDuck <T>());
            }

            var definition = new ProxyDefinition(typeof(T), () => null);

            definition.Implement(() => new DuckTypeInterceptor(target));
            return(definition.CreateProxy <T>());
        }
        public void Execute_MultipleInterceptors_InvokesInterceptorInSequenceAccordingToMethodSelectors()
        {
            // Arrange
            var targetMock = new Mock <IClassWithThreeMethods>();
            var result     = new StringBuilder();

            targetMock.Setup(t => t.A()).Callback(() => result.Append("A"));
            targetMock.Setup(t => t.B()).Callback(() => result.Append("B"));
            targetMock.Setup(t => t.C()).Callback(() => result.Append("C"));

            var firstInterceptorMock = new Mock <IInterceptor>();

            firstInterceptorMock.Setup(i => i.Invoke(It.IsAny <IInvocationInfo>())).Callback(() => result.Append("1")).Returns <IInvocationInfo>(ii => ii.Proceed());
            var secondInterceptorMock = new Mock <IInterceptor>();

            secondInterceptorMock.Setup(i => i.Invoke(It.IsAny <IInvocationInfo>())).Callback(() => result.Append("2")).Returns <IInvocationInfo>(ii => ii.Proceed());
            var thirdInterceptorMock = new Mock <IInterceptor>();

            thirdInterceptorMock.Setup(i => i.Invoke(It.IsAny <IInvocationInfo>())).Callback(() => result.Append("3")).Returns <IInvocationInfo>(ii => ii.Proceed());

            var proxyDefinition = new ProxyDefinition(typeof(IClassWithThreeMethods), () => targetMock.Object);

            proxyDefinition.Implement(() => firstInterceptorMock.Object, m => m.Name == "A");
            proxyDefinition.Implement(() => secondInterceptorMock.Object, m => m.Name == "B");
            proxyDefinition.Implement(() => thirdInterceptorMock.Object, m => m.Name == "A" || m.Name == "B" || m.Name == "C");
            var proxyBuilder = CreateProxyBuilder();
            var type         = proxyBuilder.GetProxyType(proxyDefinition);
            var instance     = (IClassWithThreeMethods)Activator.CreateInstance(type);

            instance.A();
            Assert.Equal("13A", result.ToString());
            result.Clear();
            instance.B();
            Assert.Equal("23B", result.ToString());
            result.Clear();
            instance.C();
            Assert.Equal("3C", result.ToString());
        }
        public void ShouldCallBaseEquals()
        {
            var proxyBuilder    = CreateProxyBuilder();
            var proxyDefinition = new ProxyDefinition(typeof(ClassWithOverriddenEquals));

            proxyDefinition.Implement(() => new SampleInterceptor(), info => true);
            var proxyType = proxyBuilder.GetProxyType(proxyDefinition);
            var instance1 = (ClassWithOverriddenEquals)Activator.CreateInstance(proxyType);
            var instance2 = (ClassWithOverriddenEquals)Activator.CreateInstance(proxyType);

            instance1.Name = "TEST";
            instance2.Name = "TEST";
            Assert.True(instance1.Equals(instance2));
        }
Exemplo n.º 26
0
        public void Create_InterceptedTargetWithVirtualProperty_PassesValueToBaseClass()
        {
            var proxyDefinition = new ProxyDefinition(typeof(ClassWithVirtualProperty));

            proxyDefinition.Implement(() => new SampleInterceptor(), info => info.Name == "ToString");

            Type proxyType = CreateProxyType(proxyDefinition);

            var proxy = (ClassWithVirtualProperty)Activator.CreateInstance(proxyType);

            proxy.Value = "SomeValue";

            Assert.AreEqual("SomeValue", proxy.value);
        }
        public void Execute_ProxyDefinitionWithoutMethodSelector_DoesNotInterceptObjectMethods()
        {
            var             interceptorMock = new Mock <IInterceptor>();
            var             targetMock      = new Mock <IMethodWithNoParameters>();
            ProxyDefinition proxyDefinition = new ProxyDefinition(typeof(IMethodWithNoParameters), () => targetMock.Object);

            proxyDefinition.Implement(() => interceptorMock.Object);
            var instance = CreateProxyFromDefinition <IMethodWithNoParameters>(proxyDefinition);

            instance.GetHashCode();
            instance.Equals(instance);
            instance.ToString();
            instance.GetType();
            interceptorMock.Verify(i => i.Invoke(It.IsAny <IInvocationInfo>()), Times.Never());
        }
        public void GetProxyType_ClassWithProtectedConstructor_ImplementsPublicConstructor()
        {
            var interceptorMock = new Mock <IInterceptor>();
            var proxyBuilder    = new ProxyBuilder();
            var proxyDefinition = new ProxyDefinition(typeof(FooWithProtectedConstructor), () => null);

            proxyDefinition.Implement(() => interceptorMock.Object);
            var proxyType = proxyBuilder.GetProxyType(proxyDefinition);
            var instance  = (FooWithProtectedConstructor)Activator.CreateInstance(proxyType);

            instance.Execute();

            interceptorMock.Verify(interceptor => interceptor.Invoke(It.IsAny <IInvocationInfo>()), Times.Once);

            //instance.Execute();
            //interceptorMock.Verify(i => i.Invoke(It.IsAny<IInvocationInfo>()), Times.Once);
        }
        public void Execute_WithoutProceed_DoesCallTargetFactory()
        {
            int callCount       = 0;
            var proxyDefinition = new ProxyDefinition(typeof(IMethodWithNoParameters), () =>
            {
                callCount++;
                return(null);
            });

            var interceptorMock = CreateInterceptorMock(null);

            proxyDefinition.Implement(() => interceptorMock.Object, m => m.Name == "Execute");
            var instance = CreateProxyFromDefinition <IMethodWithNoParameters>(proxyDefinition);

            instance.Execute();

            Assert.Equal(0, callCount);
        }
Exemplo n.º 30
0
        void DefineProxyType(ProxyDefinition proxyDefinition)
        {
            bool predicate(MethodInfo m, Type t) => m.GetGenericArguments().Any() && t.IsAssignableFrom(m.GetGenericArguments().First());

            proxyDefinition.Implement(() => container.GetInstance <ILoggingInterceptor>(), m => predicate(m, typeof(IStringAction)) || predicate(m, typeof(IVoidAction)));

            var customInterceptors = container.GetAllInstances <ICustomInterceptor>();

            foreach (var customInterceptor in customInterceptors)
            {
                proxyDefinition.Implement(() => customInterceptor, m => predicate(m, typeof(IAction)) || predicate(m, typeof(IWorkflowAction)));
            }

            proxyDefinition.Implement(() => container.GetInstance <IWorkflowInterceptor>(), m => predicate(m, typeof(IWorkflowAction)));
            proxyDefinition.Implement(() => container.GetInstance <IExecutionContextInterceptor>(), m => predicate(m, typeof(IStringAction)) || predicate(m, typeof(IVoidAction)));
            proxyDefinition.Implement(() => container.GetInstance <IActionInterceptor>(), m => predicate(m, typeof(IStringAction)));
            proxyDefinition.Implement(() => container.GetInstance <IVoidActionInterceptor>(), m => predicate(m, typeof(IVoidAction)));
        }
        public void Create_TargetWithConstructorParameter_PassesValueToBaseClass()
        {
            var proxyDefinition = new ProxyDefinition(typeof(ClassWithConstructor));

            proxyDefinition.Implement(() => new SampleInterceptor(), info => info.Name == "Execute");

            Type proxyType = CreateProxyType(proxyDefinition);

            var proxy = (ClassWithConstructor)Activator.CreateInstance(proxyType, "SomeValue");
            
            Assert.AreEqual("SomeValue", proxy.Value);
        }
        public void Create_TargetWithEventWithoutInterceptingAddRemove_CanCreateProxyType()
        {
            var proxyDefinition = new ProxyDefinition(typeof(ClassWithEvent));
            proxyDefinition.Implement(() => new SampleInterceptor(), method => method.Name == "ToString");            
            Type proxyType = CreateProxyType(proxyDefinition);
            Assert.True(typeof(ClassWithEvent).IsAssignableFrom(proxyType));

        }
        public void Execute_AbstractMethod_InvokeInterceptor()
        {
            var interceptorMock = new Mock<IInterceptor>();
            var proxyBuilder = new ProxyBuilder();
            var proxyDefinition = new ProxyDefinition(typeof(ClassWithAbstractMethod), () => null);
            proxyDefinition.Implement(() => interceptorMock.Object);            
            var proxyType = proxyBuilder.GetProxyType(proxyDefinition);
            var instance = (ClassWithAbstractMethod)Activator.CreateInstance(proxyType);
            instance.Execute();
            interceptorMock.Verify(interceptor => interceptor.Invoke(It.IsAny<IInvocationInfo>()), Times.Once);

        }
Exemplo n.º 34
0
 private static void DefineProxyType(ProxyDefinition definition, IInterceptor myInterceptor)
 {
     definition.Implement(
         () => myInterceptor,
         m => m.IsDeclaredBy(definition.TargetType) && m.IsPublic);
 }
Exemplo n.º 35
0
 private static void ImplementServiceInterface(
     Type serviceType, ProxyDefinition proxyDefinition)
 {
     proxyDefinition.Implement(() => new ServiceInterceptor(container), m => m.IsDeclaredBy(serviceType));
 }
 private static void ImplementDisposeMethod(IServiceFactory serviceFactory, ProxyDefinition proxyDefinition)
 {
     proxyDefinition.Implement(
         () => new HubDisposeInterceptor(serviceFactory),
         m => m.Name == "Dispose" && m.GetParameters().Any(p => p.ParameterType == typeof(bool)));
 }
 private void Apply(IServiceFactory factory, ProxyDefinition proxyDefinition)
 {
     proxyDefinition.Implement(factory.GetInstance<AopProxy>);
 }
        public void Create_NonInterceptedTargetWithVirtualProperty_PassesValueToBaseClass()
        {
            var proxyDefinition = new ProxyDefinition(typeof(ClassWithVirtualProperty));
            proxyDefinition.Implement(() => new SampleInterceptor(), info => false);
                

            Type proxyType = CreateProxyType(proxyDefinition);

            var proxy = (ClassWithVirtualProperty)Activator.CreateInstance(proxyType);

            proxy.Value = "SomeValue";
            
            Assert.AreEqual("SomeValue", proxy.value);
        }
Exemplo n.º 39
0
 private static void ImplementDisposeMethod(IServiceFactory serviceFactory, ProxyDefinition proxyDefinition)
 {
     proxyDefinition.Implement(
         () => new HubDisposeInterceptor(serviceFactory),
         m => m.Name == "Dispose" && m.GetParameters().Any(p => p.ParameterType == typeof(bool)));
 }        
Exemplo n.º 40
0
 private static void DefineProxyType(ProxyDefinition definition, IInterceptor myInterceptor)
 {
     definition.Implement(
         () => myInterceptor,
         m => m.IsDeclaredBy(definition.TargetType) && m.IsPublic);
 }
Exemplo n.º 41
0
 private static void DefineProxyType(IServiceFactory serviceFactory, ProxyDefinition proxyDefinition)
 {
     proxyDefinition.Implement(() => serviceFactory.GetInstance <IInterceptor>("pid"));
     proxyDefinition.Implement(() => serviceFactory.GetInstance <IInterceptor>("log"));
 }
 private static void DefineProxyType(IServiceFactory serviceFactory, ProxyDefinition proxyDefinition)
 {
     proxyDefinition.Implement(() => new FirstInterceptor());
     proxyDefinition.Implement(() => new SecondInterceptor());
 }