コード例 #1
0
        public void ShouldResolveDependencies()
        {
            var sut = new NSubContainer();

            sut.Register <ITestInterface, TestImplSingleCtor>(NSubLifetime.Transient);
            sut.Register <ClassWithDependency, ClassWithDependency>(NSubLifetime.Transient);

            var result = sut.Resolve <ClassWithDependency>();

            Assert.That(result.Dep, Is.AssignableTo <TestImplSingleCtor>());
        }
コード例 #2
0
        public void ShouldUseNewRegistrationOnRepeatedRegister()
        {
            var sut = new NSubContainer();

            sut.Register <ITestInterface, TestImplSingleCtor>(NSubLifetime.Transient);
            sut.Register <ITestInterface, TestImplSingleCtor2>(NSubLifetime.Transient);

            var result = sut.Resolve <ITestInterface>();

            Assert.That(result, Is.AssignableTo <TestImplSingleCtor2>());
        }
コード例 #3
0
        public void ShouldAllowToResolveDependencyInFactoryMethod()
        {
            var sut = new NSubContainer();

            sut.Register <ITestInterface, TestImplSingleCtor>(NSubLifetime.Transient);

            sut.Register <ClassWithDependency>(r => new ClassWithDependency(r.Resolve <ITestInterface>()), NSubLifetime.Transient);

            var result = sut.Resolve <ClassWithDependency>();

            Assert.That(result, Is.Not.Null);
        }
コード例 #4
0
        public void ShouldUseRegistrationFromForkContainerIfRequestComesFromParentContainerRegistration()
        {
            var sut = new NSubContainer();

            sut.Register <ClassWithDependency, ClassWithDependency>(NSubLifetime.Transient);
            sut.Register <ITestInterface, TestImplSingleCtor>(NSubLifetime.Transient);
            var sutFork = sut.Customize().Register <ITestInterface, TestImplSingleCtor2>(NSubLifetime.Transient);

            var sutForkResult = sutFork.Resolve <ClassWithDependency>();

            Assert.That(sutForkResult.Dep, Is.AssignableTo <TestImplSingleCtor2>());
        }
コード例 #5
0
        public void ShouldReturnNewInstanceForSameRequestForTransientLifetime()
        {
            var sut = new NSubContainer();

            sut.Register <ClassWithMultipleDependencies, ClassWithMultipleDependencies>(NSubLifetime.Transient);
            sut.Register <ClassWithDependency, ClassWithDependency>(NSubLifetime.Transient);

            sut.Register <ITestInterface, TestImplSingleCtor>(NSubLifetime.Transient);

            var result = sut.Resolve <ClassWithMultipleDependencies>();

            Assert.That(result.TestInterfaceDep, Is.Not.SameAs(result.ClassWithDependencyDep.Dep));
        }
コード例 #6
0
        public void ShouldPinDecoratedRegistrationAtRegistrationTime()
        {
            var sut = new NSubContainer();

            sut.Register <ITestInterface, TestImplSingleCtor>(NSubLifetime.Transient);
            var sutFork = sut.Customize().Decorate <ITestInterface>((impl, r) => new TestImplDecorator(impl));

            // Override registration. Very rare case
            sut.Register <ITestInterface, TestImplSingleCtor2>(NSubLifetime.Transient);
            var result = sutFork.Resolve <ITestInterface>();

            Assert.That(result, Is.TypeOf <TestImplDecorator>());
            Assert.That(((TestImplDecorator)result).Inner, Is.TypeOf <TestImplSingleCtor>());
        }
コード例 #7
0
        public void ShouldReturnSameValueWithinSameExplicitScope()
        {
            var sut = new NSubContainer();

            sut.Register <ITestInterface, TestImplSingleCtor>(NSubLifetime.PerScope);
            sut.Register <ClassWithDependency, ClassWithDependency>(NSubLifetime.Transient);

            var scope   = sut.CreateScope();
            var result1 = scope.Resolve <ClassWithDependency>();
            var result2 = scope.Resolve <ClassWithDependency>();

            Assert.That(result1, Is.Not.SameAs(result2));
            Assert.That(result1.Dep, Is.SameAs(result2.Dep));
        }
コード例 #8
0
        public void ShouldReturnSameInstanceWhenResolvingDependencyInFactoryMethodForPerScopeLifetime()
        {
            var sut = new NSubContainer();

            sut.Register <ClassWithDependency, ClassWithDependency>(NSubLifetime.Transient);

            sut.Register <ITestInterface, TestImplSingleCtor>(NSubLifetime.PerScope);
            sut.Register <ClassWithMultipleDependencies>(
                r => new ClassWithMultipleDependencies(r.Resolve <ITestInterface>(), r.Resolve <ClassWithDependency>()),
                NSubLifetime.Transient);

            var result = sut.Resolve <ClassWithMultipleDependencies>();

            Assert.That(result.TestInterfaceDep, Is.SameAs(result.ClassWithDependencyDep.Dep));
        }
コード例 #9
0
        public void ShouldThrowIfTryToRegisterTypeWithMultipleCtors()
        {
            var sut = new NSubContainer();

            var ex = Assert.Throws <ArgumentException>(() => sut.Register <ITestInterface, TestImplMultipleCtors>(NSubLifetime.Transient));

            Assert.That(ex.Message, Contains.Substring("single public constructor"));
        }
コード例 #10
0
        public void ShouldReturnNewInstanceForEachRequestForTransientLifetime()
        {
            var sut = new NSubContainer();

            sut.Register <ITestInterface, TestImplSingleCtor>(NSubLifetime.Transient);

            var result1 = sut.Resolve <ITestInterface>();
            var result2 = sut.Resolve <ITestInterface>();

            Assert.That(result2, Is.Not.SameAs(result1));
        }
コード例 #11
0
        public void ShouldDecorateWhenRegisteredOnSameContainer()
        {
            var sut = new NSubContainer();

            sut.Register <ITestInterface, TestImplSingleCtor>(NSubLifetime.Transient);

            sut.Decorate <ITestInterface>((impl, r) => new TestImplDecorator(impl));
            var result = sut.Resolve <ITestInterface>();

            Assert.That(result, Is.TypeOf <TestImplDecorator>());
        }
コード例 #12
0
        public void ShouldAllowToRegisterTypeWithMultipleCtorsUsingFactoryMethod()
        {
            var sut = new NSubContainer();

            sut.Register(_ => new TestImplMultipleCtors("42"), NSubLifetime.Transient);

            var result = sut.Resolve <TestImplMultipleCtors>();

            Assert.That(result, Is.Not.Null);
            Assert.That(result.Value, Is.EqualTo("42"));
        }
コード例 #13
0
        public void ShouldReturnSameInstanceForNewRequestForSingletonLifetime()
        {
            var sut = new NSubContainer();

            sut.Register <ITestInterface, TestImplSingleCtor>(NSubLifetime.Singleton);

            var result1 = sut.Resolve <ITestInterface>();
            var result2 = sut.Resolve <ITestInterface>();

            Assert.That(result2, Is.SameAs(result1));
        }
コード例 #14
0
        public void ShouldReturnFromParentContainerIfNoForkCustomizations()
        {
            var sut = new NSubContainer();

            sut.Register <ITestInterface, TestImplSingleCtor>(NSubLifetime.Transient);

            var sutFork = sut.Customize().Customize().Customize();

            var result = sutFork.Resolve <ITestInterface>();

            Assert.That(result, Is.AssignableTo <TestImplSingleCtor>());
        }
コード例 #15
0
        public void ShouldUseSameLifetimeForDecorator_SingletonCase()
        {
            var sut = new NSubContainer();

            sut.Register <ITestInterface, TestImplSingleCtor>(NSubLifetime.Singleton);

            sut.Decorate <ITestInterface>((impl, r) => new TestImplDecorator(impl));
            var result1 = sut.Resolve <ITestInterface>();
            var result2 = sut.Resolve <ITestInterface>();

            Assert.That(result1, Is.SameAs(result2));
            Assert.That(((TestImplDecorator)result1).Inner, Is.SameAs(((TestImplDecorator)result2).Inner));
        }
コード例 #16
0
        public void ShouldNotModifyOriginalContainerOnCustomize()
        {
            var sut = new NSubContainer();

            sut.Register <ITestInterface, TestImplSingleCtor>(NSubLifetime.Transient);

            var sutFork = sut.Customize().Register <ITestInterface, TestImplSingleCtor2>(NSubLifetime.Transient);

            var sutResult     = sut.Resolve <ITestInterface>();
            var sutForkResult = sutFork.Resolve <ITestInterface>();

            Assert.That(sutResult, Is.AssignableTo <TestImplSingleCtor>());
            Assert.That(sutForkResult, Is.AssignableTo <TestImplSingleCtor2>());
        }
コード例 #17
0
        public void ShouldDecorateTheExistingRegistration()
        {
            var sut = new NSubContainer();

            sut.Register <ITestInterface, TestImplSingleCtor>(NSubLifetime.PerScope);

            var sutFork = sut
                          .Customize()
                          .Decorate <ITestInterface>((impl, r) => new TestImplDecorator(impl));
            var result = sutFork.Resolve <ITestInterface>();

            Assert.That(result, Is.TypeOf <TestImplDecorator>());
            Assert.That(((TestImplDecorator)result).Inner, Is.TypeOf <TestImplSingleCtor>());
        }
コード例 #18
0
        public void ShouldBePossibleToCreateNestedDecorators()
        {
            var sut = new NSubContainer();

            sut.Register <ITestInterface, TestImplSingleCtor>(NSubLifetime.PerScope);

            var sutFork = sut
                          .Customize()
                          .Decorate <ITestInterface>((impl, r) => new TestImplDecorator(impl));
            var sutForkFork = sutFork
                              .Customize()
                              .Decorate <ITestInterface>((impl, r) => new TestImplDecorator(impl));
            var result = sutForkFork.Resolve <ITestInterface>();

            var mostInner = ((result as TestImplDecorator)?.Inner as TestImplDecorator)?.Inner;

            Assert.That(mostInner, Is.TypeOf <TestImplSingleCtor>());
        }