当前位置: 首页>>代码示例>>C#>>正文


C# Container.InterceptWith方法代码示例

本文整理汇总了C#中Container.InterceptWith方法的典型用法代码示例。如果您正苦于以下问题:C# Container.InterceptWith方法的具体用法?C# Container.InterceptWith怎么用?C# Container.InterceptWith使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Container的用法示例。


在下文中一共展示了Container.InterceptWith方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: InterceptWithGenericArgAndPredicate_TwoInterceptors_InterceptsTheInstanceWithBothInterceptors

        public void InterceptWithGenericArgAndPredicate_TwoInterceptors_InterceptsTheInstanceWithBothInterceptors()
        {
            // Arrange
            var logger = new FakeLogger();

            var container = new Container();

            container.RegisterSingleton<ILogger>(logger);
            container.Register<ICommand, CommandThatLogsOnExecute>();

            container.InterceptWith<InterceptorThatLogsBeforeAndAfter>(IsACommandPredicate);
            container.InterceptWith<InterceptorThatLogsBeforeAndAfter2>(IsACommandPredicate);

            container.RegisterInitializer<InterceptorThatLogsBeforeAndAfter>(i => i.BeforeText = "Start1 ");
            container.RegisterInitializer<InterceptorThatLogsBeforeAndAfter2>(i => i.BeforeText = "Start2 ");
            container.RegisterInitializer<CommandThatLogsOnExecute>(c => c.ExecuteLogMessage = "Executing");
            container.RegisterInitializer<InterceptorThatLogsBeforeAndAfter>(i => i.AfterText = " Done1");
            container.RegisterInitializer<InterceptorThatLogsBeforeAndAfter2>(i => i.AfterText = " Done2");
            
            // Act
            var command = container.GetInstance<ICommand>();

            command.Execute();

            // Assert
            Assert.AreEqual("Start2 Start1 Executing Done1 Done2", logger.Message);
        }
开发者ID:MichaelSagalovich,项目名称:SimpleInjector,代码行数:27,代码来源:InterceptorExtensionsTests.cs

示例2: Intercept_CollectionWithRegistrationInstances_InterceptsTheInstances

        public void Intercept_CollectionWithRegistrationInstances_InterceptsTheInstances()
        {
            var logger = new FakeLogger();

            var container = new Container();

            container.RegisterSingleton<ILogger>(logger);
            container.RegisterCollection<ICommand>(new[] 
            {
                Lifestyle.Transient.CreateRegistration(typeof(ICommand), typeof(ConcreteCommand), container),
                Lifestyle.Transient.CreateRegistration(typeof(ConcreteCommand), container),
            });

            container.InterceptWith<InterceptorThatLogsBeforeAndAfter>(IsACommandPredicate);

            container.RegisterInitializer<InterceptorThatLogsBeforeAndAfter>(i => i.BeforeText = "Log ");

            // Act
            var commands = container.GetAllInstances<ICommand>().ToList();

            commands.ForEach(Execute);

            // Assert
            Assert.AreEqual("Log Log ", logger.Message);
        }
开发者ID:abatishchev,项目名称:SimpleInjector,代码行数:25,代码来源:InterceptorExtensionsTests.cs

示例3: Container

        public void InterceptWithGenericArgAndPredicate_InterceptingATransientWithSingletonInterceptor_ResultsInATransientProxy()
        {
            // Arrange
            var container = new Container();

            container.Register<ICommand, FakeCommand>();

            container.Register<FakeInterceptor>(Lifestyle.Singleton);

            container.InterceptWith<FakeInterceptor>(IsACommandPredicate);

            // Act
            var command1 = container.GetInstance<ICommand>();
            var command2 = container.GetInstance<ICommand>();

            // Assert
            Assert.IsFalse(object.ReferenceEquals(command1, command2), "The proxy is expected to " +
                "be transient, since the interceptee is transient.");
        }
开发者ID:MichaelSagalovich,项目名称:SimpleInjector,代码行数:19,代码来源:InterceptorExtensionsTests.cs

示例4: CallingAnInterceptedMethod_InterceptorThatChangesTheInputParameters_GetsForwardedToTheInterceptee

        public void CallingAnInterceptedMethod_InterceptorThatChangesTheInputParameters_GetsForwardedToTheInterceptee()
        {
            // Arrange  
            string expectedValue = "XYZ";

            var container = new Container();

            var interceptee = new WithOutAndRef();

            var interceptor = new DelegateInterceptor();

            interceptor.Intercepting += invocation =>
            {
                invocation.Arguments[0] = expectedValue;
            };

            container.RegisterSingleton<IWithOutAndRef>(interceptee);
            container.InterceptWith(interceptor, IsInterface);

            var intercepted = container.GetInstance<IWithOutAndRef>();

            // Act
            string refValue = "Something different";
            string unused;
            intercepted.Operate(ref refValue, out unused);

            // Assert
            Assert.AreEqual(expectedValue, interceptee.SuppliedRefValue);
        }
开发者ID:MichaelSagalovich,项目名称:SimpleInjector,代码行数:29,代码来源:InterceptorExtensionsTests.cs

示例5: CallingInterceptedMethodWithReturnValue_InterceptedWithPassThroughInterceptor_ReturnsTheExpectedValue

        public void CallingInterceptedMethodWithReturnValue_InterceptedWithPassThroughInterceptor_ReturnsTheExpectedValue()
        {
            // Arrange
            int expectedReturnValue = 3;

            var container = new Container();

            var interceptee = new WithOutAndRef { ReturnValue = expectedReturnValue };

            container.RegisterSingleton<IWithOutAndRef>(interceptee);

            container.InterceptWith<FakeInterceptor>(IsInterface);

            var intercepted = container.GetInstance<IWithOutAndRef>();

            // Act
            string unused1 = null;
            string unused2;
            int actualReturnValue = intercepted.Operate(ref unused1, out unused2);

            // Assert
            Assert.AreEqual(expectedReturnValue, actualReturnValue);
        }
开发者ID:MichaelSagalovich,项目名称:SimpleInjector,代码行数:23,代码来源:InterceptorExtensionsTests.cs

示例6: Intercept_DecoratedGenericRegistrations_WorksLikeACharm

        public void Intercept_DecoratedGenericRegistrations_WorksLikeACharm()
        {
            // Arrange
            var logger = new FakeLogger();

            var container = new Container();

            container.RegisterSingleton<ILogger>(logger);
            container.Register<ICommand, CommandThatLogsOnExecute>();

            container.Register(typeof(IValidator<>), typeof(LoggingValidator<>));

            container.RegisterDecorator(typeof(IValidator<>), typeof(LoggingValidatorDecorator<>));

            container.InterceptWith<InterceptorThatLogsBeforeAndAfter>(
                t => t.IsInterface && t.Name.StartsWith("IValidator"));

            container.RegisterInitializer<InterceptorThatLogsBeforeAndAfter>(i => i.BeforeText = "Inercepting ");
            container.RegisterInitializer<InterceptorThatLogsBeforeAndAfter>(i => i.AfterText = "Intercepted ");

            // Act
            container.GetInstance<IValidator<RealCommand>>().Validate(new RealCommand());

            // Assert
            Assert.AreEqual("Inercepting Decorating Validating Decorated Intercepted ", logger.Message);
        }
开发者ID:abatishchev,项目名称:SimpleInjector,代码行数:26,代码来源:InterceptorExtensionsTests.cs

示例7: InterceptWith_WithInterceptorWithNoPublicConstructor_ThrowsExpressiveException

        public void InterceptWith_WithInterceptorWithNoPublicConstructor_ThrowsExpressiveException()
        {
            // Arrange
            var container = new Container();

            container.Register<ICommand, FakeCommand>();

            try
            {
                // Act
                container.InterceptWith<InterceptorWithInternalConstructor>(IsACommandPredicate);

                // Assert
                Assert.Fail("Exception expected.");
            }
            catch (ActivationException ex)
            {
                Assert.IsTrue(ex.Message.Contains("For the container to be able to create " + 
                    "InterceptorExtensionsTests.InterceptorWithInternalConstructor, it should contain " +
                    "exactly one public constructor"),
                    "Actual: " + ex.Message);
            }
        }
开发者ID:MichaelSagalovich,项目名称:SimpleInjector,代码行数:23,代码来源:InterceptorExtensionsTests.cs

示例8: InterceptWithFuncAndPredicate_InterceptingWithExpressionBuiltEventArgs_RunsSuccessfully

        public void InterceptWithFuncAndPredicate_InterceptingWithExpressionBuiltEventArgs_RunsSuccessfully()
        {
            // Arrange
            var container = new Container();

            container.Register<ICommand, FakeCommand>(Lifestyle.Singleton);
            container.Register<ILogger, FakeLogger>(Lifestyle.Singleton);

            container.InterceptWith(e => new BuiltInfoInterceptor(e), type => type.IsInterface);

            // Act
            var command = container.GetInstance<ICommand>();
            var logger = container.GetInstance<ILogger>();

            command.Execute();
            logger.Log("foo");
        }
开发者ID:MichaelSagalovich,项目名称:SimpleInjector,代码行数:17,代码来源:InterceptorExtensionsTests.cs

示例9: InterceptWithFuncAndPredicate_InterceptingASingleton_ResultsInATransientProxy

        public void InterceptWithFuncAndPredicate_InterceptingASingleton_ResultsInATransientProxy()
        {
            // Arrange
            var container = new Container();

            var interceptor = new FakeInterceptor();

            container.Register<ICommand, FakeCommand>(Lifestyle.Singleton);

            container.InterceptWith(() => interceptor, IsACommandPredicate);

            // Act
            var command1 = container.GetInstance<ICommand>();
            var command2 = container.GetInstance<ICommand>();

            // Assert
            Assert.IsFalse(object.ReferenceEquals(command1, command2), "The proxy is expected to " +
                "be transient, since the interceptor is created using a Func<T>, and there is no way to " +
                "determine whether the delegate always returns the same or a new instance.");
        }
开发者ID:MichaelSagalovich,项目名称:SimpleInjector,代码行数:20,代码来源:InterceptorExtensionsTests.cs

示例10: InterceptWithFuncAndPredicate_TwoInterceptors_InterceptsTheInstanceWithBothInterceptors

        public void InterceptWithFuncAndPredicate_TwoInterceptors_InterceptsTheInstanceWithBothInterceptors()
        {
            // Arrange
            var logger = new FakeLogger();

            Func<IInterceptor> interceptorCreator1 = () => new InterceptorThatLogsBeforeAndAfter(logger)
            {
                BeforeText = "Start1 ",
                AfterText = " Done1"
            };

            Func<IInterceptor> interceptorCreator2 = () => new InterceptorThatLogsBeforeAndAfter(logger)
            {
                BeforeText = "Start2 ",
                AfterText = " Done2"
            };

            var container = new Container();

            container.RegisterSingleton<ILogger>(logger);
            container.Register<ICommand, CommandThatLogsOnExecute>();

            container.InterceptWith(interceptorCreator1, IsACommandPredicate);
            container.InterceptWith(interceptorCreator2, IsACommandPredicate);

            container.RegisterInitializer<CommandThatLogsOnExecute>(c => c.ExecuteLogMessage = "Executing");

            // Act
            var command = container.GetInstance<ICommand>();

            command.Execute();

            // Assert
            Assert.AreEqual("Start2 Start1 Executing Done1 Done2", logger.Message);
        }
开发者ID:MichaelSagalovich,项目名称:SimpleInjector,代码行数:35,代码来源:InterceptorExtensionsTests.cs

示例11: InterceptWithInstanceAndPredicate_InterceptingATransient_ResultsInATransientProxy

        public void InterceptWithInstanceAndPredicate_InterceptingATransient_ResultsInATransientProxy()
        {
            // Arrange
            var container = new Container();

            var interceptor = new FakeInterceptor();

            container.Register<ICommand, FakeCommand>();

            container.InterceptWith(interceptor, IsACommandPredicate);

            // Act
            var command1 = container.GetInstance<ICommand>();
            var command2 = container.GetInstance<ICommand>();

            // Assert
            Assert.IsFalse(object.ReferenceEquals(command1, command2), "The proxy is expected to " +
                "be transient, since the interceptee is transient.");
        }
开发者ID:MichaelSagalovich,项目名称:SimpleInjector,代码行数:19,代码来源:InterceptorExtensionsTests.cs

示例12: InterceptWith_WithInterceptorWithNoPublicConstructor_ThrowsExpressiveException

        public void InterceptWith_WithInterceptorWithNoPublicConstructor_ThrowsExpressiveException()
        {
            // Arrange
            var container = new Container();

            container.Register<ICommand, FakeCommand>();

            // Act
            Action action = () => container.InterceptWith<InterceptorWithInternalConstructor>(IsACommandPredicate);

            // Assert
            AssertThat.ThrowsWithExceptionMessageContains<ActivationException>(
                "For the container to be able to create " +
                "InterceptorExtensionsTests.InterceptorWithInternalConstructor it should have only " +
                "one public constructor",
                action);
        }
开发者ID:jamesqo,项目名称:SimpleInjector,代码行数:17,代码来源:InterceptorExtensionsTests.cs

示例13: GetInstance_OnInterceptedTypeWithInterceptorWithUnresolvableDependency_ThrowsExpressiveException

        public void GetInstance_OnInterceptedTypeWithInterceptorWithUnresolvableDependency_ThrowsExpressiveException()
        {
            // Arrange
            var container = new Container();

            container.Register<ICommand, FakeCommand>();

            // The interceptor depends on ILogger, but it is not registered.
            container.InterceptWith<InterceptorWithDependencyOnLogger>(IsACommandPredicate);

            try
            {
                // Act
                container.GetInstance<ICommand>();

                // Assert
                Assert.Fail("Exception expected.");
            }
            catch (ActivationException ex)
            {
                Assert.IsTrue(ex.Message.Contains("The constructor of type " +
                    "InterceptorExtensionsTests.InterceptorWithDependencyOnLogger contains the parameter " +
                    "with name 'logger' and type ILogger that is not registered."),
                    "Actual: " + ex.Message);
            }
        }
开发者ID:MichaelSagalovich,项目名称:SimpleInjector,代码行数:26,代码来源:InterceptorExtensionsTests.cs


注:本文中的Container.InterceptWith方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。