本文整理汇总了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);
}
示例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);
}
示例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);
}
示例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.");
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}