public void CanRegisterComponentNamedForTwoServices() { //-- arrange IComponentContainerBuilder builderUnderTest = new ComponentContainerBuilder(); //-- act builderUnderTest.RegisterComponentType <ComponentOne>().NamedForServices <ITestComponent, IAnyComponent>("A"); builderUnderTest.RegisterComponentType <ComponentTwo>().NamedForServices <ITestComponent, IAnyComponent>("B"); var container = ((IInternalComponentContainerBuilder)builderUnderTest).CreateComponentContainer(isRootContainer: true); var resolved1a = container.ResolveNamed <IAnyComponent>("A"); var resolved1b = container.ResolveNamed <ITestComponent>("A"); var resolved2a = container.ResolveNamed <IAnyComponent>("B"); var resolved2b = container.ResolveNamed <ITestComponent>("B"); //-- assert resolved1a.Should().BeOfType <ComponentOne>(); resolved1b.Should().BeOfType <ComponentOne>(); resolved2a.Should().BeOfType <ComponentTwo>(); resolved2b.Should().BeOfType <ComponentTwo>(); }
public void CanRegisterComponentTypeForArrayOfServices() { //-- arrange IComponentContainerBuilder builderUnderTest = new ComponentContainerBuilder(); //-- act builderUnderTest.RegisterComponentType <ComponentOne>().ForServices(typeof(IServiceOne), typeof(IAnyComponent)); //-- assert var container = ((IInternalComponentContainerBuilder)builderUnderTest).CreateComponentContainer(isRootContainer: true); var asService1 = container.Resolve <IServiceOne>(); var asService2 = container.Resolve <IAnyComponent>(); var asSelf = container.Resolve <ComponentOne>(); asService1.Should().NotBeNull(); asService1.Should().BeAssignableTo <ComponentOne>(); asService2.Should().NotBeNull(); asService2.Should().BeAssignableTo <ComponentOne>(); asSelf.Should().NotBeNull(); }
public void Merge_ResolveAll_ReturnsAllComponentsRegisteredForService() { //-- arrange var builder1 = new ComponentContainerBuilder(); builder1.RegisterComponentType <ComponentA>().ForService <IAnyComponent>(); var containerUnderTest = builder1.CreateComponentContainer(isRootContainer: true); //-- act var builder2 = new ComponentContainerBuilder(); builder2.RegisterComponentType <ComponentB>().ForService <IAnyComponent>(); containerUnderTest.Merge(builder2); //-- assert var resolved = containerUnderTest.ResolveAll <IAnyComponent>().ToArray(); resolved.Length.Should().Be(2); resolved.OfType <ComponentA>().Count().Should().Be(1); resolved.OfType <ComponentB>().Count().Should().Be(1); }
public void CanRegisterFallbackComponent() { //-- arrange IComponentContainerBuilder builderUnderTest = new ComponentContainerBuilder(); //-- act builderUnderTest.RegisterComponentType <ComponentOne>().ForService <ITestComponent>(); builderUnderTest.RegisterComponentType <ComponentTwo>().ForService <ITestComponent>().AsFallback(); var container = ((IInternalComponentContainerBuilder)builderUnderTest).CreateComponentContainer(isRootContainer: true); var resolved = container.Resolve <ITestComponent>(); //-- assert resolved.Should().NotBeNull(); resolved.Should().BeOfType <ComponentOne>(); }
public void CanPassConstructorParameterByType() { //-- arrange IComponentContainerBuilder builderUnderTest = new ComponentContainerBuilder(); //-- act builderUnderTest.RegisterComponentType <ComponentOne>().SingleInstance(); builderUnderTest.RegisterComponentType <ComponentThree>().WithParameter <int>(123); //-- assert var container = ((IInternalComponentContainerBuilder)builderUnderTest).CreateComponentContainer(isRootContainer: true); var resolved = container.Resolve <ComponentThree>(); resolved.Should().NotBeNull(); resolved.Number.Should().Be(123); resolved.One.Should().BeSameAs(container.Resolve <ComponentOne>()); }
public void CanResolveAll() { //-- arrange var builder = new ComponentContainerBuilder(); builder.RegisterComponentType <ComponentA>().ForService <IAnyComponent>(); builder.RegisterComponentType <ComponentB>().ForService <IAnyComponent>(); var containerUnderTest = builder.CreateComponentContainer(isRootContainer: true); //-- act var resolved = containerUnderTest.ResolveAll <IAnyComponent>().ToArray(); //-- assert resolved.Length.Should().Be(2); resolved.OfType <ComponentA>().Count().Should().Be(1); resolved.OfType <ComponentB>().Count().Should().Be(1); }
public void CanRegisterTransientComponentType() { //-- arrange IComponentContainerBuilder builderUnderTest = new ComponentContainerBuilder(); //-- act builderUnderTest.RegisterComponentType <ComponentOne>(); // no need to specify - default is InstancePerDependency builderUnderTest.RegisterComponentType <ComponentTwo>().InstancePerDependency(); //-- assert var container = ((IInternalComponentContainerBuilder)builderUnderTest).CreateComponentContainer(isRootContainer: true); var one1 = container.Resolve <ComponentOne>(); var one2 = container.Resolve <ComponentOne>(); var two1 = container.Resolve <ComponentTwo>(); var two2 = container.Resolve <ComponentTwo>(); one2.Should().NotBeSameAs(one1); two2.Should().NotBeSameAs(two1); }
public void CanRegisterSingletonComponentType() { //-- arrange IComponentContainerBuilder builderUnderTest = new ComponentContainerBuilder(); //-- act builderUnderTest.RegisterComponentType <ComponentOne>().SingleInstance(); //-- assert var container = ((IInternalComponentContainerBuilder)builderUnderTest).CreateComponentContainer(isRootContainer: true); var resolved1 = container.Resolve <ComponentOne>(); var resolved2 = container.Resolve <ComponentOne>(); resolved2.Should().BeSameAs(resolved1); }
public void CanInjectContainerAsDependency() { //-- arrange IComponentContainerBuilder builderUnderTest = new ComponentContainerBuilder(); //-- act builderUnderTest.RegisterComponentType <ComponentFour>(); var container = ((IInternalComponentContainerBuilder)builderUnderTest).CreateComponentContainer(isRootContainer: true); var resolved = container.Resolve <ComponentFour>(); //-- assert resolved.Should().NotBeNull(); resolved.Container.Should().NotBeNull(); resolved.Container.Should().BeSameAs(container); }
public void CanRegisterComponentTypeNonGeneric() { //-- arrange IComponentContainerBuilder builderUnderTest = new ComponentContainerBuilder(); //-- act builderUnderTest.RegisterComponentType(typeof(ComponentOne)) .SingleInstance() .ForService <IServiceOne>(); //-- assert var container = ((IInternalComponentContainerBuilder)builderUnderTest).CreateComponentContainer(isRootContainer: true); var asService = container.Resolve <IServiceOne>(); var asSelf = container.Resolve <ComponentOne>(); asSelf.Should().NotBeNull(); asService.Should().BeSameAs(asSelf); }
public void Merge_Resolve_ReturnsLastComponentRegisteredForService() { //-- arrange var builder1 = new ComponentContainerBuilder(); builder1.RegisterComponentType <ComponentA>().ForService <IAnyComponent>(); var containerUnderTest = builder1.CreateComponentContainer(isRootContainer: true); //-- act var builder2 = new ComponentContainerBuilder(); builder2.RegisterComponentType <ComponentB>().ForService <IAnyComponent>(); containerUnderTest.Merge(builder2); //-- assert var resolved = containerUnderTest.Resolve <IAnyComponent>(); resolved.Should().NotBeNull(); resolved.Should().BeAssignableTo <ComponentB>(); }