Esempio n. 1
0
        public void ShouldThrowExceptionWhenPublicConstructorNotFoundInHierarchy()
        {
            IContainer container = new InfuseContainer();

            container.Register <NoPublicConstructor, NoPublicConstructor>();
            container.Register <DependsOnNonPublicConstructorClass, DependsOnNonPublicConstructorClass>();

            Assert.Throws <InvalidOperationException>(() => {
                var oops = container.Resolve <DependsOnNonPublicConstructorClass>();
            });
        }
Esempio n. 2
0
        public static IContainer ConfigureInfuseContainer()
        {
            IContainer infuseContainer = new InfuseContainer();

            infuseContainer.RegisterSingleton <IHomeRepository, HomeRepository>();

            infuseContainer.Register <HomeController, HomeController>();
            infuseContainer.Register <AccountController, AccountController>();
            infuseContainer.Register <ManageController, ManageController>();
            infuseContainer.Register <MoviesController, MoviesController>();
            infuseContainer.Register <HelloWorldController, HelloWorldController>();

            return(infuseContainer);
        }
Esempio n. 3
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());
        }
Esempio n. 4
0
        public void DuplicateTypeRegistrationThrowsError()
        {
            var container = new InfuseContainer();

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


            Assert.Throws <ArgumentException>(() => container.RegisterSingleton <IPerson, PersonB>());
        }
Esempio n. 5
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);
        }
Esempio n. 6
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);
        }
Esempio n. 7
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);
        }
Esempio n. 8
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);
        }
Esempio n. 9
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());
        }
Esempio n. 10
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());
        }
Esempio n. 11
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());
        }
Esempio n. 12
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());
        }
Esempio n. 13
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());
        }