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


C# Container.RegisterInitializer方法代码示例

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


在下文中一共展示了Container.RegisterInitializer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: GetInstance_CalledOnInitializerWithPredicateReturningFalse_CallsPredicateOnceAndDelegateNever

        public void GetInstance_CalledOnInitializerWithPredicateReturningFalse_CallsPredicateOnceAndDelegateNever()
        {
            // Registration
            int expectedCallCount = 0;
            int actualCallCount = 0;
            int expectedPredicateCallCount = 1;
            int actualPredicateCallCount = 0;

            var container = new Container();

            container.RegisterInitializer(context => actualCallCount++, context =>
            {
                actualPredicateCallCount++;
                return false;
            });

            // Act
            container.GetInstance<RealTimeProvider>();
            container.GetInstance<RealTimeProvider>();
            container.GetInstance<RealTimeProvider>();
            container.GetInstance<RealTimeProvider>();
            container.GetInstance<RealTimeProvider>();

            // Assert
            Assert.AreEqual(expectedCallCount, actualCallCount);
            Assert.AreEqual(expectedPredicateCallCount, actualPredicateCallCount);
        }
开发者ID:khellang,项目名称:SimpleInjector,代码行数:27,代码来源:RegisterContextualInitializerTests.cs

示例4: Container

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

            container.RegisterWithContext<IContextualLogger>(context => new ContextualLogger(context));

            container.RegisterInitializer<RepositoryThatDependsOnLogger>(_ => { });

            // Act
            var handler = container.GetInstance<RepositoryThatDependsOnLogger>();

            // Assert
            Assert.AreEqual(typeof(RepositoryThatDependsOnLogger), handler.Logger.Context.ServiceType);
            Assert.AreEqual(typeof(RepositoryThatDependsOnLogger), handler.Logger.Context.ImplementationType);
        }
开发者ID:MichaelSagalovich,项目名称:SimpleInjector,代码行数:16,代码来源:ContextDependentExtensionsTests.cs

示例5: GetInstance_OnUnregisteredConcreteTypeWithoutDependencies_CallsInstanceCreatedOnce

        public void GetInstance_OnUnregisteredConcreteTypeWithoutDependencies_CallsInstanceCreatedOnce()
        {
            // Registration
            int expectedCallCount = 1;
            int actualCallCount = 0;

            var container = new Container();

            container.RegisterInitializer(context => actualCallCount++, TruePredicate);

            // Act
            container.GetInstance<RealTimeProvider>();

            // Assert
            Assert.AreEqual(expectedCallCount, actualCallCount);
        }
开发者ID:khellang,项目名称:SimpleInjector,代码行数:16,代码来源:RegisterContextualInitializerTests.cs

示例6: Verify_WhenCurrentRequestEndsCalledDuringVerificationOutsideAnHttpContext_Succeeds

        public void Verify_WhenCurrentRequestEndsCalledDuringVerificationOutsideAnHttpContext_Succeeds()
        {
            // Arrange
            bool initializerCalled = false;

            var container = new Container();

            container.Register<DisposableCommand>();

            container.RegisterInitializer<DisposableCommand>(command =>
            {
                WebRequestLifestyle.WhenCurrentRequestEnds(container, () => command.Dispose());
                initializerCalled = true;
            });

            // Act
            container.Verify(VerificationOption.VerifyOnly);

            // Arrange
            Assert.IsTrue(initializerCalled);
        }
开发者ID:jamesqo,项目名称:SimpleInjector,代码行数:21,代码来源:WebRequestLifestyleTests.cs

示例7: Verify_RegisterForDisposalCalledDuringVerification_EnsuresDisposalOfRegisteredDisposable

        public void Verify_RegisterForDisposalCalledDuringVerification_EnsuresDisposalOfRegisteredDisposable()
        {
            // Arrange
            var disposable = new DisposableObject();

            var scopedLifestyle = new FakeScopedLifestyle(scope: null);

            var container = new Container();

            container.Register<IPlugin>(() => new DisposablePlugin());

            container.RegisterInitializer<IPlugin>(_ =>
            {
                scopedLifestyle.RegisterForDisposal(container, disposable);
            });

            // Act
            container.Verify();

            // Assert
            Assert.IsTrue(disposable.IsDisposedOnce);
        }
开发者ID:jamesqo,项目名称:SimpleInjector,代码行数:22,代码来源:ScopedLifestyleTests.cs

示例8: Dispose_ExceptionThrownDuringDisposalAfterResolvingNewInstance_DoesNotCallAnyNewActions

        public void Dispose_ExceptionThrownDuringDisposalAfterResolvingNewInstance_DoesNotCallAnyNewActions()
        {
            // Arrange
            bool scopeEndActionCalled = false;

            var scope = new Scope();

            var scopedLifestyle = new FakeScopedLifestyle(scope);

            var container = new Container();

            container.Register<IPlugin>(() => new DisposablePlugin());
            container.RegisterInitializer<IPlugin>(
                plugin => scope.WhenScopeEnds(() => scopeEndActionCalled = true));

            container.Register<DisposableObject>(() => new DisposableObject(_ =>
            {
                container.GetInstance<IPlugin>();
                throw new Exception("Bang!");
            }), scopedLifestyle);

            try
            {
                // Act
                scope.Dispose();

                // Assert
                Assert.Fail("Exception expected.");
            }
            catch
            {
                Assert.IsFalse(scopeEndActionCalled,
                    "In case of an exception, no actions will be further executed. This lowers the change " +
                    "the new exceptions are thrown from other actions that cover up the original exception.");
            }
        }
开发者ID:jamesqo,项目名称:SimpleInjector,代码行数:36,代码来源:ScopedLifestyleTests.cs

示例9: GetCurrentScope_CalledDuringVerification_ReturnsAScope

        public void GetCurrentScope_CalledDuringVerification_ReturnsAScope()
        {
            // Arrange
            Scope scopeDuringVerification = null;

            var scopedLifestyle = new FakeScopedLifestyle(scope: null);

            var container = new Container();

            container.Register<IDisposable>(() => new DisposableObject(), scopedLifestyle);

            container.RegisterInitializer<IDisposable>(_ =>
            {
                scopeDuringVerification = scopedLifestyle.GetCurrentScope(container);
            });

            // Act
            container.Verify();

            // Assert
            Assert.IsNotNull(scopeDuringVerification);
        }
开发者ID:jamesqo,项目名称:SimpleInjector,代码行数:22,代码来源:ScopedLifestyleTests.cs

示例10: GetInstance_OnRegisteredConcreteTransientTypeWithoutDependencies_CallsInstanceCreatedWithExpectedType

        public void GetInstance_OnRegisteredConcreteTransientTypeWithoutDependencies_CallsInstanceCreatedWithExpectedType()
        {
            // Registration
            var actualContexts = new List<InstanceInitializationData>();

            var container = new Container();

            container.Register<RealTimeProvider>();

            container.RegisterInitializer(actualContexts.Add, TruePredicate);

            // Act
            object instance = container.GetInstance<RealTimeProvider>();

            // Assert
            var actualContext = actualContexts.First().Context;

            Assert.AreSame(container.GetRegistration(typeof(RealTimeProvider)), actualContext.Producer);
            Assert.AreSame(container.GetRegistration(typeof(RealTimeProvider)).Registration, actualContext.Registration);
            Assert.AreSame(instance, actualContexts.First().Instance);
        }
开发者ID:khellang,项目名称:SimpleInjector,代码行数:21,代码来源:RegisterContextualInitializerTests.cs

示例11: Dispose_WhenDisposeEndsActionRegisteredDuringDisposal_CallsTheRegisteredDelegate

        public void Dispose_WhenDisposeEndsActionRegisteredDuringDisposal_CallsTheRegisteredDelegate()
        {
            // Arrange
            bool actionCalled = false;

            var disposable = new DisposableObject();

            var scope = new Scope();

            var container = new Container();

            container.Register<DisposableObject>(() => disposable, new FakeScopedLifestyle(scope));
            container.RegisterInitializer<DisposableObject>(instance =>
            {
                scope.WhenScopeEnds(() => actionCalled = true);
            });

            scope.WhenScopeEnds(() => container.GetInstance<DisposableObject>());

            // Act
            scope.Dispose();

            // Assert
            Assert.IsTrue(actionCalled);
        }
开发者ID:jamesqo,项目名称:SimpleInjector,代码行数:25,代码来源:ScopedLifestyleTests.cs

示例12: GetInstance_CalledForDecoratedCollection_CallsEventForBothTheInstanceAndTheDecorator

        public void GetInstance_CalledForDecoratedCollection_CallsEventForBothTheInstanceAndTheDecorator()
        {
            // Arrange
            var actualContexts = new List<InstanceInitializationData>();

            var container = new Container();

            container.RegisterCollection<ICommandHandler<RealCommand>>(new[] { typeof(StubCommandHandler) });

            container.RegisterInitializer(actualContexts.Add, TruePredicate);

            container.RegisterDecorator(typeof(ICommandHandler<>), typeof(RealCommandHandlerDecorator));

            // Act
            var decorator = container.GetAllInstances<ICommandHandler<RealCommand>>().Single()
                as RealCommandHandlerDecorator;

            // Assert
            // TODO: The container should actually call InstanceCreated for the IEnumerable<T> as well.
            Assert.AreEqual(2, actualContexts.Count, "Two event args were expected.");

            Assert.AreEqual(typeof(StubCommandHandler), actualContexts.First().Context.Registration.ImplementationType);
            Assert.AreEqual(typeof(RealCommandHandlerDecorator), actualContexts.Second().Context.Registration.ImplementationType);
        }
开发者ID:khellang,项目名称:SimpleInjector,代码行数:24,代码来源:RegisterContextualInitializerTests.cs

示例13: GetInstance_CalledTwiceOnSingletonWithoutDependencies_CallsInstanceCreatedOnce

        public void GetInstance_CalledTwiceOnSingletonWithoutDependencies_CallsInstanceCreatedOnce()
        {
            // Registration
            int expectedCallCount = 1;
            int actualCallCount = 0;

            var container = new Container();

            container.Register<RealTimeProvider, RealTimeProvider>(Lifestyle.Singleton);

            container.RegisterInitializer(context => actualCallCount++, TruePredicate);

            // Act
            container.GetInstance<RealTimeProvider>();
            container.GetInstance<RealTimeProvider>();

            // Assert
            Assert.AreEqual(expectedCallCount, actualCallCount);
        }
开发者ID:khellang,项目名称:SimpleInjector,代码行数:19,代码来源:RegisterContextualInitializerTests.cs

示例14: GetCurrentScope_CalledOnSameThreadDuringVerification_ReturnsTheVerificationScope

        public void GetCurrentScope_CalledOnSameThreadDuringVerification_ReturnsTheVerificationScope()
        {
            // Arrange
            Scope verificationScope = null;

            var container = new Container();

            var scope = new Scope();
            var scopedLifestyle = new FakeScopedLifestyle(scope);

            container.Register<IPlugin, DisposablePlugin>(scopedLifestyle);
            container.RegisterInitializer<DisposablePlugin>(p =>
            {
                verificationScope = scopedLifestyle.GetCurrentScope(container);
            });

            // Act
            // When calling verify, we expect DisposablePlugin to be created again.
            container.Verify();

            // Assert
            Assert.AreNotSame(verificationScope, scope);
            Assert.IsTrue(verificationScope.ToString().Contains("VerificationScope"));
        }
开发者ID:jamesqo,项目名称:SimpleInjector,代码行数:24,代码来源:ScopedLifestyleTests.cs

示例15: Container

        public void GetInstance_ResolvingScopedInstanceWhileDifferentThreadIsVerifying_DoesNotResolveInstanceFromVerificationScope()
        {
            // Arrange
            DisposablePlugin verifiedPlugin = null;
            DisposablePlugin backgroundResolvedPlugin = null;
            Task task = null;

            var container = new Container();

            var scope = new Scope();
            var lifestyle = new FakeScopedLifestyle(scope);

            container.Register<IPlugin, DisposablePlugin>(lifestyle);
            container.RegisterInitializer<DisposablePlugin>(p =>
            {
                verifiedPlugin = p;

                // Resolve on a different thread (so not during verification)
                task = Task.Run(() =>
                {
                    backgroundResolvedPlugin = (DisposablePlugin)container.GetInstance<IPlugin>();
                });

                Thread.Sleep(150);
            });

            // Act
            container.Verify();

            task.Wait();

            // Assert
            Assert.IsFalse(backgroundResolvedPlugin.IsDisposedOnce,
                "Since this instance isn't resolved during verification, but within an active scope, " +
                "The instance should not have been disposed here.");

            scope.Dispose();

            Assert.IsTrue(backgroundResolvedPlugin.IsDisposedOnce, "Now it should have been disposed.");
        }
开发者ID:jamesqo,项目名称:SimpleInjector,代码行数:40,代码来源:ScopedLifestyleTests.cs


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