Beispiel #1
0
        public void EnsureDefaultRegistrationLifecycleIsTransient()
        {
            var container = new InfuseContainer();

            container.Register <Logger, Logger>();

            var logger1 = container.Resolve <Logger>();
            var logger2 = container.Resolve <Logger>();

            Assert.NotSame(logger1, logger2);
        }
Beispiel #2
0
        public void SingletonsHasOnlyOneInstancePerContainer()
        {
            IContainer container = new InfuseContainer();

            container.Register <IChat, Chatroom>(LifecycleType.Singleton);
            container.RegisterSingleton <IPerson, PersonA>();

            string talkMsg = "Hello";

            var a    = container.Resolve <IPerson>();
            var chat = container.Resolve <IChat>();

            a.Say(talkMsg);

            Assert.Equal(talkMsg, chat.Receive());
        }
Beispiel #3
0
        public void TransientDoesNotRegisterAsSingleton_ButManagesSingletonWithinIt()
        {
            IContainer container = new InfuseContainer();

            container.Register <IChat, Chatroom>(LifecycleType.Singleton);
            container.RegisterTransient <IPerson, PersonA>();

            string talkMsg = "Hello";

            var a = container.Resolve <IPerson>();
            var b = container.Resolve <IPerson>();

            Assert.NotSame(a, b);

            a.Say(talkMsg);
            Assert.Equal(talkMsg, b.Listen());
        }
Beispiel #4
0
        public void ShouldThrowExceptionWhenTypeNotRegistered()
        {
            var container = new InfuseContainer();

            Assert.Throws <ArgumentException>(() => {
                var pet = container.Resolve <Logger>();
            });
        }
Beispiel #5
0
        public void SingletonsAreSharedBetweenMultipleTransient()
        {
            IContainer container = new InfuseContainer();

            container.Register <IChat, Chatroom>(LifecycleType.Singleton);
            container.RegisterTransient <PersonA, PersonA>();
            container.RegisterTransient <PersonB, PersonB>();

            string talkMsg = "Hello";

            var a = container.Resolve <PersonA>();
            var b = container.Resolve <PersonB>();

            Assert.NotSame(a, b);

            a.Say(talkMsg);
            Assert.Equal(talkMsg, b.Listen());
        }
Beispiel #6
0
        public void ShouldThrowExceptionWhenPublicConstructorNotFound()
        {
            IContainer container = new InfuseContainer();

            container.Register <NoPublicConstructor, NoPublicConstructor>();

            Assert.Throws <InvalidOperationException>(() => {
                var oops = container.Resolve <NoPublicConstructor>();
            });
        }
Beispiel #7
0
        public void RegisterTransientTypeWithinASingletonTypeGetsAutoPromotedToSingleton()
        {
            IContainer container = new InfuseContainer();

            container.RegisterSingleton <IPerson, PersonA>();
            container.Register <IChat, Chatroom>(LifecycleType.Transient);


            string talkMsg = "Hello";

            var a = container.Resolve <IPerson>();
            var b = container.Resolve <IPerson>();

            Assert.Same(a, b);

            a.Say(talkMsg);

            Assert.Equal(talkMsg, b.Listen());
        }
Beispiel #8
0
        public void SameTypeMultipleInstances()
        {
            IContainer container = new InfuseContainer();

            container.Register <IChat, Chatroom>(LifecycleType.Singleton);

            container.RegisterTransient <IPerson, PersonA>();
            container.RegisterTransient <IPerson, PersonB>();

            string talkMsg = "Hello";

            var a = container.Resolve <IPerson>(); //resole A?
            var b = container.Resolve <IPerson>(); //resolve B?

            Assert.NotSame(a, b);

            a.Say(talkMsg);
            Assert.Equal(talkMsg, b.Listen());
        }
Beispiel #9
0
        public void RegisterTypeWithParametersAndResolve()
        {
            var container = new InfuseContainer();

            container.Register <IChat, Chatroom>(LifecycleType.Singleton);
            container.RegisterSingleton <IPerson, PersonA>();

            var person = container.Resolve <IPerson>();

            Assert.NotNull(person);
        }
Beispiel #10
0
        public void RegisterAsTransientUsingExtensionAndResolve()
        {
            var container = new InfuseContainer();

            container.RegisterTransient <Logger, Logger>();

            var logger = container.Resolve <Logger>();

            Assert.NotNull(logger);
            Assert.IsType <Logger>(logger);
        }
Beispiel #11
0
        public void RegisterAsTransientAndResolve()
        {
            var container = new InfuseContainer();

            container.Register <Logger, Logger>(LifecycleType.Transient);

            var logger = container.Resolve <Logger>();

            Assert.NotNull(logger);
            Assert.IsType <Logger>(logger);
        }
Beispiel #12
0
        public void ShouldThrowExceptionWhenPublicConstructorNotFoundInHierarchyOfMixedClasses()
        {
            IContainer container = new InfuseContainer();

            container.Register <NoPublicConstructor, NoPublicConstructor>();
            container.Register <PerfectlyNormalClass, PerfectlyNormalClass>();
            container.Register <DependsOnMixedConstructorClass, DependsOnMixedConstructorClass>();

            Assert.Throws <InvalidOperationException>(() => {
                var oops = container.Resolve <DependsOnMixedConstructorClass>();
            });
        }
Beispiel #13
0
        public void DuplicateTypeRegistrationOverwritesThePreviousOne()
        {
            var container = new InfuseContainer();

            container.Register <IChat, Chatroom>(LifecycleType.Singleton);

            container.RegisterSingleton <IPerson, PersonA>();
            container.RegisterSingleton <IPerson, PersonB>();

            var person = container.Resolve <IPerson>();

            Assert.IsType <PersonB>(person);
        }
Beispiel #14
0
        public void SingletonIsSharedBetweenHierarchy()
        {
            var container = new InfuseContainer();

            container.Register <Logger, Logger>(LifecycleType.Singleton);
            container.Register <TestLoggerController, TestLoggerController>(LifecycleType.Singleton);
            container.Register <TestLoggerRepository, TestLoggerRepository>(LifecycleType.Singleton);

            string str = "Hello";

            var a = container.Resolve <TestLoggerController>();

            a.Update(str);

            Assert.Equal(str, a.Fetch());
        }