예제 #1
0
        public virtual void WhenRegister_ShouldGetLevel2Properly()
        {
            // Arrange
            IIoCContainer             target       = this.CreateTarget();
            RegisterTestObjectContext registration = new RegisterTestObjectContext()
            {
                Container = target,
                Settings  = new RegisterLevelContext[]
                {
                    new RegisterLevelContext()
                    {
                        UseFactory = false, Settings = IocRegisterSettings.None
                    },
                    new RegisterLevelContext()
                    {
                        UseFactory = false, Settings = IocRegisterSettings.None
                    },
                    new RegisterLevelContext()
                    {
                        UseFactory = false, Settings = IocRegisterSettings.None
                    }
                }
            };

            this.Register(registration);

            // Act
            ILevel2 actual = target.Resolve <ILevel2>();

            // Assert
            this.AssertProperObjectTree(actual, registration);
        }
예제 #2
0
        public virtual void WhenRegisterFactorySingleton_ShouldGetLevel3Properly()
        {
            // Arrange
            IIoCContainer             target       = this.CreateTarget();
            RegisterTestObjectContext registration = new RegisterTestObjectContext()
            {
                Container = target,
                Settings  = new RegisterLevelContext[]
                {
                    new RegisterLevelContext()
                    {
                        UseFactory = false, Settings = IocRegisterSettings.None
                    },
                    new RegisterLevelContext()
                    {
                        UseFactory = false, Settings = IocRegisterSettings.None
                    },
                    new RegisterLevelContext()
                    {
                        UseFactory = true, Settings = IocRegisterSettings.Singleton
                    }
                }
            };

            this.Register(registration);

            // Act
            ILevel3 actual1 = target.Resolve <ILevel3>();
            ILevel3 actual2 = target.Resolve <ILevel3>();

            // Assert
            actual1.Should().BeSameAs(actual2);
            this.AssertProperObjectTree(actual1, registration);
        }
예제 #3
0
        private void AssertProperObjectTree(ILevel root, RegisterTestObjectContext registration)
        {
            root.IsValid().Should().BeTrue();

            for (int i = 0; i < registration.Settings.Length; ++i)
            {
                if (registration.Settings[i].UseFactory)
                {
                    this.factoryLevelInvoked.Count(x => x == i).Should().Be(1);
                }
            }
        }
예제 #4
0
        private void Register(RegisterTestObjectContext context)
        {
            if (context.Settings[0].UseFactory)
            {
                context.Container.Register <ILevel1>(resolver =>
                {
                    this.factoryLevelInvoked.Add(0);
                    return(new Level1Object(resolver.Resolve <ILevel2>(), resolver.Resolve <ILevel3>()));
                },
                                                     context.Settings[0].Settings);
            }
            else
            {
                context.Container.Register <ILevel1, Level1Object>(context.Settings[0].Settings);
            }

            if (context.Settings[1].UseFactory)
            {
                context.Container.Register <ILevel2>(resolver =>
                {
                    this.factoryLevelInvoked.Add(1);
                    return(new Level2Object(resolver.Resolve <ILevel3>()));
                },
                                                     context.Settings[1].Settings);
            }
            else
            {
                context.Container.Register <ILevel2, Level2Object>(context.Settings[1].Settings);
            }

            if (context.Settings[2].UseFactory)
            {
                context.Container.Register <ILevel3>(resolver =>
                {
                    this.factoryLevelInvoked.Add(2);
                    return(new Level3Object());
                },
                                                     context.Settings[2].Settings);
            }
            else
            {
                context.Container.Register <ILevel3, Level3Object>(context.Settings[2].Settings);
            }

            this.NotifyRegistrationFinished(context.Container);
        }
예제 #5
0
        public virtual void WhenRegisterAsSingleton_ShouldCreateOnce()
        {
            // Arrange
            IIoCContainer             target       = this.CreateTarget();
            RegisterTestObjectContext registration = new RegisterTestObjectContext()
            {
                Container = target,
                Settings  = new RegisterLevelContext[]
                {
                    new RegisterLevelContext()
                    {
                        UseFactory = false, Settings = IocRegisterSettings.Singleton
                    },
                    new RegisterLevelContext()
                    {
                        UseFactory = false, Settings = IocRegisterSettings.None
                    },
                    new RegisterLevelContext()
                    {
                        UseFactory = false, Settings = IocRegisterSettings.None
                    }
                }
            };

            this.Register(registration);

            // Act
            ILevel1 actual1Level1 = target.Resolve <ILevel1>();
            ILevel1 actual2Level1 = target.Resolve <ILevel1>();
            ILevel2 actual1Level2 = target.Resolve <ILevel2>();
            ILevel2 actual2Level2 = target.Resolve <ILevel2>();
            ILevel3 actual1Level3 = target.Resolve <ILevel3>();
            ILevel3 actual2Level3 = target.Resolve <ILevel3>();


            // Assert
            actual1Level1.Should().BeSameAs(actual2Level1);
            actual1Level2.Should().NotBeSameAs(actual2Level2);
            actual1Level3.Should().NotBeSameAs(actual2Level3);
        }