コード例 #1
0
        /// <summary>
        /// Intercept the given type specifying the predicate an the hook option
        /// </summary>
        public static void InterceptWith <TInterceptor>(this Container container, Func <Type, bool> predicate, InterceptionHookOption hookOption)
            where TInterceptor : class, IInterceptor
        {
            var interceptWith = new InterceptionHelper(e => BuildInterceptorExpression <TInterceptor>(container), predicate, GetProxyGenerationHook(hookOption));

            container.ExpressionBuilt += interceptWith.OnExpressionBuilt;
        }
コード例 #2
0
        public void GlobalAndAttributeInterceptorConfigureOrderTest()
        {
            // ARRANGE
            RealServiceExecuted.ResetExecuted();
            MyInterceptor.ResetExecuted();
            MyOtherInterceptor.ResetExecuted();
            InterceptorsCalled.ResetList();

            unityContainer.RegisterType <IMyFooService, MyFooServiceWithAttributeInterceptor>();
            unityContainer = InterceptionHelper.InterceptContainer(unityContainer, new IInterceptor[] { new MyOtherInterceptor() }, new InterceptionOptions {
                GlobalInterceptorsOrder = GlobalInterceptorsOrder.AfterAttributeInterceptors
            });

            var myService = unityContainer.Resolve <IMyFooService>();

            // ACT
            myService.Execute();

            // ASSERT
            Assert.IsTrue(RealServiceExecuted.Executed);
            Assert.IsTrue(MyInterceptor.ExecutedBefore);
            Assert.IsTrue(MyInterceptor.ExecutedAfter);
            Assert.IsTrue(MyOtherInterceptor.ExecutedBefore);
            Assert.IsTrue(MyOtherInterceptor.ExecutedAfter);
            Assert.IsTrue(InterceptorsCalled.List[0].GetType() == typeof(MyOtherInterceptor));
            Assert.IsTrue(InterceptorsCalled.List[1].GetType() == typeof(MyInterceptor));
        }
        public static void InterceptWith(
            this Container container, Predicate <Type> predicate, Func <IInterceptor[]> interceptorsCreator)
        {
            var interceptWith =
                new InterceptionHelper(predicate, e => Expression.Invoke(Expression.Constant(interceptorsCreator)));

            container.ExpressionBuilt += interceptWith.OnExpressionBuilt;
        }
        public static void InterceptWith(
            this Container container, Predicate <Type> predicate, params IInterceptor[] interceptors)
        {
            var interceptWith =
                new InterceptionHelper(predicate, e => Expression.Constant(interceptors));

            container.ExpressionBuilt += interceptWith.OnExpressionBuilt;
        }
コード例 #5
0
    public static void InterceptWith(this Container container, Func <ExpressionBuiltEventArgs, IInterceptor> interceptorCreator, Func <Type, bool> predicate)
    {
        var interceptWith = new InterceptionHelper()
        {
            BuildInterceptorExpression = e => Expression.Invoke(Expression.Constant(interceptorCreator), Expression.Constant(e)), Predicate = type => predicate(type)
        };

        container.ExpressionBuilt += interceptWith.OnExpressionBuilt;
    }
コード例 #6
0
    public static void InterceptWith(this Container container, IInterceptor interceptor, Func <Type, bool> predicate)
    {
        var interceptWith = new InterceptionHelper()
        {
            BuildInterceptorExpression = e => Expression.Constant(interceptor), Predicate = predicate
        };

        container.ExpressionBuilt += interceptWith.OnExpressionBuilt;
    }
コード例 #7
0
ファイル: DotAppStart.cs プロジェクト: yycx0328/Dot.Utility
        protected override void RaiseComposeOperations()
        {
            InterceptionHelper.LoadByIOCSettings();

            base.RaiseComposeOperations();

            ObjectContainer.RegisterType <ITypeAdapterFactory, AutomapperTypeAdapterFactory>();
            InitLogConfig();
        }
        public static void InterceptWith <TInterceptor>(
            this Container container, Predicate <Type> predicate)
            where TInterceptor : class, IInterceptor
        {
            container.Options.ConstructorResolutionBehavior.GetConstructor(typeof(TInterceptor));

            var interceptWith =
                new InterceptionHelper(predicate, e => BuildInterceptorExpressions(container, typeof(TInterceptor)));

            container.ExpressionBuilt += interceptWith.OnExpressionBuilt;
        }
コード例 #9
0
    public static void InterceptWith <TInterceptor>(this Container container, Func <Type, bool> predicate) where TInterceptor : class, IInterceptor
    {
        container.Options.ConstructorResolutionBehavior.GetConstructor(typeof(TInterceptor));

        var interceptWith = new InterceptionHelper()
        {
            BuildInterceptorExpression = e => BuildInterceptorExpression <TInterceptor>(container), Predicate = type => predicate(type)
        };

        container.ExpressionBuilt += interceptWith.OnExpressionBuilt;
    }
        public static void Intercept <TInterface>(this Container container, params Type[] interceptors)
        {
            var interceptWith =
                new InterceptionHelper(
                    type => type == typeof(TInterface) ||
                    type.GetTypeInfo().ImplementedInterfaces.Any(x => x == typeof(TInterface)),
                    e => BuildInterceptorExpressions(container, interceptors),
                    type => typeof(TInterface));

            container.ExpressionBuilt += interceptWith.OnExpressionBuilt;
        }
        public static void InterceptWith(
            this Container container, Predicate <Type> predicate, Func <ExpressionBuiltEventArgs, IInterceptor> interceptorCreator)
        {
            var interceptWith =
                new InterceptionHelper(
                    predicate,
                    e => Expression.NewArrayInit(
                        typeof(IInterceptor),
                        Expression.Invoke(
                            Expression.Constant(interceptorCreator),
                            Expression.Constant(e))));

            container.ExpressionBuilt += interceptWith.OnExpressionBuilt;
        }
コード例 #12
0
        public static void InterceptWith(this Container container,
                                         IInterceptor interceptor, Func <Type, bool> predicate)
        {
            RequiresIsNotNull(container, "container");
            RequiresIsNotNull(interceptor, "interceptor");
            RequiresIsNotNull(predicate, "predicate");

            var interceptWith = new InterceptionHelper(container)
            {
                BuildInterceptorExpression = e => Expression.Constant(interceptor),
                Predicate = predicate
            };

            container.ExpressionBuilt += interceptWith.OnExpressionBuilt;
        }
        public static void InterceptWith(
            this Container container, Predicate <Type> predicate, params Type[] interceptors)
        {
            foreach (var interceptor in interceptors)
            {
                if (interceptor.GetTypeInfo().ImplementedInterfaces.All(interfaceType => interfaceType != typeof(IInterceptor)))
                {
                    throw new ArgumentException($"{interceptor} is not implemant {typeof(IInterceptor)}");
                }
            }

            var interceptWith =
                new InterceptionHelper(predicate, e => BuildInterceptorExpressions(container, interceptors));

            container.ExpressionBuilt += interceptWith.OnExpressionBuilt;
        }
コード例 #14
0
        public static void InterceptWith(this Container container,
                                         Func <IInterceptor> interceptorCreator, Func <Type, bool> predicate)
        {
            RequiresIsNotNull(container, nameof(container));
            RequiresIsNotNull(interceptorCreator, nameof(interceptorCreator));
            RequiresIsNotNull(predicate, nameof(predicate));

            var interceptWith = new InterceptionHelper(container)
            {
                BuildInterceptorExpression =
                    e => Expression.Invoke(Expression.Constant(interceptorCreator)),
                Predicate = type => predicate(type)
            };

            container.ExpressionBuilt += interceptWith.OnExpressionBuilt;
        }
コード例 #15
0
        public void MustNotInterceptAttributeTest()
        {
            RealServiceExecuted.ResetExecuted();
            MyInterceptor.ResetExecuted();

            unityContainer.RegisterType <IMyFooService, MyFooServiceWithDoNotInterceptAttribute>();
            unityContainer = InterceptionHelper.InterceptContainer(unityContainer, new IInterceptor[] { new MyInterceptor() });

            var myService = unityContainer.Resolve <IMyFooService>();

            // ACT
            myService.Execute();

            // ASSERT
            Assert.IsTrue(RealServiceExecuted.Executed);
            Assert.IsFalse(MyInterceptor.ExecutedBefore);
            Assert.IsFalse(MyInterceptor.ExecutedAfter);
        }
コード例 #16
0
        public static void InterceptWith <TInterceptor>(this Container container,
                                                        Func <Type, bool> predicate)
            where TInterceptor : class, IInterceptor
        {
            RequiresIsNotNull(container, "container");
            RequiresIsNotNull(predicate, "predicate");
            container.Options.ConstructorResolutionBehavior.GetConstructor(
                typeof(TInterceptor), typeof(TInterceptor));

            var interceptWith = new InterceptionHelper(container)
            {
                BuildInterceptorExpression =
                    e => BuildInterceptorExpression <TInterceptor>(container),
                Predicate = predicate
            };

            container.ExpressionBuilt += interceptWith.OnExpressionBuilt;
        }
コード例 #17
0
        public void OrderedAttributeInterceptorTest()
        {
            // ARRANGE
            RealServiceExecuted.ResetExecuted();
            InterceptorsCalled.ResetList();

            unityContainer.RegisterType <IMyFooService, MyFooServiceWithOrderedAttributeInterceptor>();
            unityContainer = InterceptionHelper.InterceptContainer(unityContainer, new IInterceptor[] { });

            var myService = unityContainer.Resolve <IMyFooService>();

            // ACT
            myService.Execute();

            // ASSERT
            Assert.IsTrue(RealServiceExecuted.Executed);
            Assert.IsTrue(InterceptorsCalled.List[0].GetType() == typeof(MyInterceptor));
            Assert.IsTrue(InterceptorsCalled.List[1].GetType() == typeof(MyOtherInterceptor));
        }
コード例 #18
0
        public void GlobalInterceptorTypesOverloadTest()
        {
            // ARRANGE
            RealServiceExecuted.ResetExecuted();
            MyInterceptor.ResetExecuted();

            unityContainer.RegisterType <IMyFooService, MyFooService>();
            unityContainer = InterceptionHelper.InterceptContainer(unityContainer, new Type[] { typeof(MyInterceptor) });

            var myService = unityContainer.Resolve <IMyFooService>();

            // ACT
            myService.Execute();

            // ASSERT
            Assert.IsTrue(RealServiceExecuted.Executed);
            Assert.IsTrue(MyInterceptor.ExecutedBefore);
            Assert.IsTrue(MyInterceptor.ExecutedAfter);
        }
コード例 #19
0
        public void AttributeInterceptorTest()
        {
            // ARRANGE
            RealServiceExecuted.ResetExecuted();
            MyInterceptor.ResetExecuted();

            unityContainer.RegisterType <IMyFooService, MyFooServiceWithAttributeInterceptor>();
            unityContainer = InterceptionHelper.InterceptContainer(unityContainer, new IInterceptor[] { });

            var myService = unityContainer.Resolve <IMyFooService>();

            // ACT
            myService.Execute();

            // ASSERT
            Assert.IsTrue(RealServiceExecuted.Executed);
            Assert.IsTrue(MyInterceptor.ExecutedBefore);
            Assert.IsTrue(MyInterceptor.ExecutedAfter);
        }
コード例 #20
0
        public static void InterceptWith <TInterceptor>(
            this Container container,
            Lifestyle lifestyle,
            Func <Type, bool> predicate)
            where TInterceptor : class, IInterceptor
        {
            lifestyle = lifestyle ?? container.Options.LifestyleSelectionBehavior
                        .SelectLifestyle(typeof(TInterceptor));

            var producer =
                lifestyle.CreateProducer <TInterceptor, TInterceptor>(container);

            var interceptWith = new InterceptionHelper
            {
                BuildInterceptorExpression = e => producer.BuildExpression(),
                Predicate = type => predicate(type)
            };

            container.ExpressionBuilt += interceptWith.OnExpressionBuilt;
        }