public void SimpleTest()
 {
     var preInvokeInfo = string.Empty;
     var postInvokeInfo = string.Empty;
     var exceptionHandlerInfo = string.Empty;
     var cc = new CrossCuttingConcerns();
     cc.PreInvoke = (instanceId, methodName, parameters) =>
     {
         preInvokeInfo = methodName + "_preInvokeInfo";
     };
     cc.PostInvoke = (instanceId, methodName, parameters) =>
     {
         postInvokeInfo = methodName + "_postInvokeInfo";
     };
     cc.ExceptionHandler = (instanceId, methodName, parameters, exception) =>
     {
         exceptionHandlerInfo = methodName + "_exceptionHandlerInfo";
         return false; //do not throw
     };
     var t = Interceptor.Intercept<ISimpleMath>(new SimpleMath(), cc);
     var a = t.Add(1, 2);
     Assert.IsFalse(string.IsNullOrEmpty(preInvokeInfo));
     Assert.IsFalse(string.IsNullOrEmpty(postInvokeInfo));
     Assert.IsTrue(string.IsNullOrEmpty(exceptionHandlerInfo));
     var b = t.Divide(4, 0);
     Assert.IsFalse(string.IsNullOrEmpty(preInvokeInfo));
     Assert.IsFalse(string.IsNullOrEmpty(postInvokeInfo));
     Assert.IsFalse(string.IsNullOrEmpty(exceptionHandlerInfo));
 }
示例#2
0
        public static TTarget Intercept <TTarget>(int id, TTarget target, CrossCuttingConcerns crossCuttingConcerns, ISerializer serializer = null) where TTarget : class
        {
            if (!typeof(TTarget).IsInterface)
            {
                throw new ArgumentException("TTarget not an interface");
            }
            if (null == target)
            {
                throw new ArgumentNullException("target");
            }
            if (null == serializer)
            {
                serializer = new DefaultSerializer();
            }
            TTarget interceptedTarget = ProxyFactory.CreateProxy <TTarget>(typeof(InterceptChannel),
                                                                           typeof(InterceptPoint),
                                                                           new InterceptPoint
            {
                Id     = id,
                Target = target,
                Cut    = crossCuttingConcerns
            },
                                                                           serializer);

            return(interceptedTarget);
        }
示例#3
0
        public static TTarget Intercept <TTarget>(int id, TTarget target, CrossCuttingConcerns crossCuttingConcerns) where TTarget : class
        {
            if (!(typeof(TTarget)).IsInterface())
            {
                throw new ArgumentException("TTarget not an interface");
            }
            if (null == target)
            {
                throw new ArgumentNullException("target");
            }
            TTarget interceptedTarget = ProxyFactory.CreateProxy <TTarget>(typeof(InterceptChannel),
                                                                           typeof(InterceptPoint),
                                                                           new InterceptPoint
            {
                Id     = id,
                Target = target,
                Cut    = crossCuttingConcerns
            });

            return(interceptedTarget);
        }
        public void SimpleTimingTest()
        {
            var preInvokeInfo = string.Empty;
            var postInvokeInfo = string.Empty;
            var exceptionHandlerInfo = string.Empty;
            var cc = new CrossCuttingConcerns();
            cc.PreInvoke = (instanceId, methodName, parameters) =>
            {
                preInvokeInfo = methodName + "_preInvokeInfo";
            };
            cc.PostInvoke = (instanceId, methodName, parameters) =>
            {
                postInvokeInfo = methodName + "_postInvokeInfo";
            };
            cc.ExceptionHandler = (instanceId, methodName, parameters, exception) =>
            {
                exceptionHandlerInfo = methodName + "_exceptionHandlerInfo";
                return false; //do not throw
            };
            var t = Interceptor.Intercept<ISimpleMath>(new SimpleMath(), cc);
            var a = t.Add(1, 2);
            Assert.IsFalse(string.IsNullOrEmpty(preInvokeInfo));
            Assert.IsFalse(string.IsNullOrEmpty(postInvokeInfo));
            Assert.IsTrue(string.IsNullOrEmpty(exceptionHandlerInfo));

            var sw = Stopwatch.StartNew();
            var t2 = Interceptor.Intercept<ISimpleMath2>(new SimpleMath2(), cc);
            sw.Stop();
            var interceptCtorTicks = sw.ElapsedTicks;
            var interceptCtorTs = sw.Elapsed;
            sw.Reset();
            var c = t2.Add(2, 3);
            sw.Stop();
            var interceptAddTicks = sw.ElapsedTicks;
            var interceptAddTs = sw.Elapsed;

            sw.Reset();
            var t3 = Interceptor.Intercept<ISimpleMath2>(new SimpleMath2(), cc);
            sw.Stop();
            var interceptCtorTicks2 = sw.ElapsedTicks;
            var interceptCtorTs2 = sw.Elapsed;
            sw.Reset();
            var c2 = t3.Add(2, 3);
            sw.Stop();
            var interceptAddTicks2 = sw.ElapsedTicks;
            var interceptAddTs2 = sw.Elapsed;

            sw.Reset();
            var t4 = new SimpleMath2();
            sw.Stop();
            var plainCtorTicks = sw.ElapsedTicks;
            var plainCtorTs = sw.Elapsed;
            sw.Reset();
            var c3 = t4.Add(2, 3);
            sw.Stop();
            var plainAddTicks = sw.ElapsedTicks;
            var plainAddTs = sw.Elapsed;

            Assert.IsTrue(plainCtorTicks <= interceptCtorTicks);
            Assert.IsTrue(plainAddTicks <= interceptAddTicks);

            Assert.IsTrue(plainCtorTicks <= interceptCtorTicks2);
            Assert.IsTrue(plainAddTicks <= interceptAddTicks2);

            Assert.IsTrue(plainCtorTs <= interceptCtorTs);
            Assert.IsTrue(plainAddTs <= interceptAddTs);

            Assert.IsTrue(plainCtorTs <= interceptCtorTs2);
            Assert.IsTrue(plainAddTs <= interceptAddTs2);
        }
示例#5
0
 public static TTarget Intercept <TTarget>(TTarget target, CrossCuttingConcerns crossCuttingConcerns, ISerializer serializer = null) where TTarget : class
 {
     return(Intercept <TTarget>(0, target, crossCuttingConcerns, serializer));
 }
示例#6
0
 public static TTarget Intercept <TTarget>(TTarget target, CrossCuttingConcerns crossCuttingConcerns) where TTarget : class
 {
     return(Intercept <TTarget>(0, target, crossCuttingConcerns));
 }