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


C# Container.RegisterWithContext方法代码示例

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


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

示例1: RegisterWithContext_CalledForAlreadyRegisteredService_ThrowsExpectedException

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

            container.Register<IContextualLogger, ContextualLogger>();

            // Act
            Action action = () => 
                container.RegisterWithContext<IContextualLogger>(context => new ContextualLogger(context));

            // Assert
            AssertThat.Throws<InvalidOperationException>(action);
        }
开发者ID:MichaelSagalovich,项目名称:SimpleInjector,代码行数:14,代码来源:ContextDependentExtensionsTests.cs

示例2: GetInstance_ResolvingAConcreteTypeThatDependsOnAContextDependentType_InjectsExpectedType

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

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

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

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

示例3: GetInstance_ResolvingAnInterfaceWhosImplementationDependsOnAContextDependentType_InjectsExpectedType

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

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

            container.Register<IRepository, RepositoryThatDependsOnLogger>();

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

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

示例4: GetInstance_TypeWithInjectionMethodWithContextualDependency_InjectsTheContextualDependencyAsExpected

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

            container.Options.EnableMethodInjectionWith<InjectAttribute>();

            container.Register<ICommand, ConcreteCommand>();

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

            // Act
            var service = container.GetInstance<ClassWithInjectionMethod>();

            // Assert
            var contextualLogger = (ContextualLogger)service.Logger;

            var parentType = contextualLogger.Context.ImplementationType;

            Assert.AreEqual(typeof(ClassWithInjectionMethod), parentType,
                "These injected dependencies are expected to pass through the complete pipeline, which " +
                "means interception should take place.");
        }
开发者ID:BrettJaner,项目名称:SimpleInjector,代码行数:23,代码来源:MethodInjectionExtensionsTests.cs

示例5: GetInstance_CalledDirectlyOnTheContextDependentType_InjectsADependencyContextWithoutServiceType

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

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

            // Act
            var logger = container.GetInstance<IContextualLogger>() as ContextualLogger;

            // Assert
            Assert.AreEqual(null, logger.Context.ServiceType);
            Assert.AreEqual(null, logger.Context.ImplementationType);
        }
开发者ID:MichaelSagalovich,项目名称:SimpleInjector,代码行数:14,代码来源:ContextDependentExtensionsTests.cs

示例6: GetInstance_TypeWithContextMultipleLevelsWithAllSingletons_GetsInjectedWithExpectedContext

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

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

            container.Register<IRepository, RepositoryThatDependsOnLogger>(Lifestyle.Singleton);

            container.Register<IService, ServiceThatDependsOnRepository>(Lifestyle.Singleton);

            container.GetRegistration(typeof(IRepository)).Registration.SuppressDiagnosticWarning(
                DiagnosticType.PotentialLifestyleMismatch, "Depending on ContextualLogger is fine.");

            container.GetRegistration(typeof(IService)).Registration.SuppressDiagnosticWarning(
                DiagnosticType.PotentialLifestyleMismatch, "Depending on ContextualLogger is fine.");

            // Act
            var service = container.GetInstance<IService>() as ServiceThatDependsOnRepository;

            // Assert
            var logger = service.InjectedRepository.Logger;

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

示例7: GetInstance_TypeWithContextRegisteredMultipleLevelsDeep_GetsInjectedWithExpectedContext

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

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

            container.Register<IRepository, RepositoryThatDependsOnLogger>();

            // Act
            var service = container.GetInstance<ServiceThatDependsOnRepository>();

            // Assert
            var logger = service.InjectedRepository.Logger;

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

示例8: AutoInjectPropertiesWithAttribute_MixedWithRegisterWithContext_ShouldInjectContextualDependency

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

            // This attribute must be applied before RegisterWithContext gets applied, since the registered
            // events adds new 
            container.Options.AutoWirePropertiesWithAttribute<Inject2Attribute>();

            container.RegisterWithContext<ILogger>(context => new ContextualLogger(context));
                        
            // Act
            var service = container.GetInstance<ServiceWithAttributedProperties>();

            // Assert
            AssertThat.IsInstanceOfType(typeof(ContextualLogger), service.Logger1);
            Assert.AreEqual(
                typeof(ServiceWithAttributedProperties).Name,
                ((ContextualLogger)service.Logger1).Context.ImplementationType.Name);
        }
开发者ID:khellang,项目名称:SimpleInjector,代码行数:20,代码来源:PropertyInjectionExtensionsTests.cs


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