예제 #1
0
        public void LongChain()
        {
            var container = new CryoContainer {
                CircularDependencyReaction = Reaction.LogWarning
            };

            container.RegisterSingleton <Class1>();
            container.RegisterSingleton <Class2>();
            container.RegisterSingleton <Class3>();
            container.RegisterSingleton <Class4>();
            container.Resolve <Class2>();
        }
예제 #2
0
        public void InitializeCalled()
        {
            var container = new CryoContainer {
                CircularDependencyReaction = Reaction.LogWarning
            };

            container.RegisterSingleton <ClassA>();
            container.RegisterSingleton <ClassB>();

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

            Assert.IsTrue(a.InitializeCalled);
            Assert.IsTrue(a.ClassB.InitializeCalled);
        }
예제 #3
0
        private static void CreateContainer()
        {
            _container = new CryoContainer();
            _container.RegisterSingleton <SingletonCounter>(GlobalSingleton, LifeTime.Global);
            _container.RegisterSingleton <SingletonCounter>(SceneSingleton, LifeTime.Scene);
            _container.RegisterSingleton <SingletonCounter>(ExternalSingleton, LifeTime.External);

            _container.RegisterType <TypeCounter>(GlobalType, LifeTime.Global);
            _container.RegisterType <TypeCounter>(SceneType, LifeTime.Scene);
            _container.RegisterType <TypeCounter>(ExternalType, LifeTime.External);

            _container.RegisterInstance(new InstanceCounter(), GlobalInstance, LifeTime.Global);
            _container.RegisterInstance(new InstanceCounter(), SceneInstance, LifeTime.Scene);
            _container.RegisterInstance(new InstanceCounter(), ExternalInstance, LifeTime.External);
        }
예제 #4
0
    protected override void SetupContainer(CryoContainer container)
    {
        base.SetupContainer(container);

        var colorDatabase = ColorDatabaseLoader.LoadDatabase();

        container.RegisterInstance <IColorDatabase>(colorDatabase, LifeTime.Global);

        container.RegisterSingleton <IPathfindingAlgorithm, AstarAlgorithm>(LifeTime.Scene);

        container.RegisterSingleton <IErrorMessageFactory, ErrorMessageFactory>(LifeTime.Global);
        container.RegisterSingleton <IErrorMessageController, ErrorMessageController>(LifeTime.Global);

        container.RegisterSceneObject <UIGridView>(LifeTime.Scene);
    }
예제 #5
0
        public void TestBaseSingleton()
        {
            _counter = 0;
            var container = new CryoContainer();

            container.RegisterSingleton <MyBaseClass, MyClass>(CreateMyClass);

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

            Assert.IsNotNull(a);
            Assert.IsInstanceOf(typeof(MyClass), a);
            Assert.AreEqual(1, ((MyClass)a).InstanceId);
            Assert.AreEqual(1, _counter);

            a = container.Resolve <MyBaseClass>();
            Assert.IsNotNull(a);
            Assert.IsInstanceOf(typeof(MyClass), a);
            Assert.AreEqual(1, ((MyClass)a).InstanceId);
            Assert.AreEqual(1, _counter);

            a = container.Resolve <MyBaseClass>();
            Assert.IsNotNull(a);
            Assert.IsInstanceOf(typeof(MyClass), a);
            Assert.AreEqual(1, ((MyClass)a).InstanceId);
            Assert.AreEqual(1, _counter);
        }
예제 #6
0
        public void TestSingletonByName()
        {
            var container = new CryoContainer();

            container.RegisterSingleton <MyClazz>("MyClazz");

            Assert.IsNull(container.WeakResolveByName <MyClazz>("MyClazz"));
            var clazz = container.ResolveByName <MyClazz>("MyClazz");

            Assert.IsNotNull(clazz);

            var clazz2 = container.WeakResolveByName <MyClazz>("MyClazz");

            Assert.IsNotNull(clazz2);
            Assert.AreSame(clazz, clazz2);

            try
            {
                container.WeakResolve <MyClazz>();
                Assert.Fail("Exception expected");

                //ascxasxasxasx
            }
            catch (ContainerException)
            {
                return;
            }
        }
예제 #7
0
        public void RefResolved()
        {
            var container = new CryoContainer {
                CircularDependencyReaction = Reaction.LogWarning
            };

            container.RegisterSingleton <ClassA>();
            container.RegisterSingleton <ClassB>();

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

            Assert.IsNotNull(a);
            Assert.IsNotNull(a.ClassB);
            Assert.IsNotNull(a.ClassB.ClassA);
            Assert.AreSame(a, a.ClassB.ClassA);
        }
예제 #8
0
        public void SelfReference()
        {
            var container = new CryoContainer {
                CircularDependencyReaction = Reaction.LogWarning
            };

            container.RegisterSingleton <ClassC>();
            container.Resolve <ClassC>();
        }
    public void OnSetup()
    {
        var container = new CryoContainer();

        container.RegisterInstance(new Mock <IErrorMessageController>().Object);
        container.RegisterSingleton <IPathfindingAlgorithm, AstarAlgorithm>();

        _pathfindingAlgorithm = container.Resolve <IPathfindingAlgorithm>();
    }
예제 #10
0
        public void ExternalInGlobal()
        {
            var container = new CryoContainer {
                LifetimeErrorReaction = Reaction.ThrowException
            };

            container.RegisterSingleton <MySingleton>(LifeTime.External);
            container.RegisterType <MyInstance>(LifeTime.Global);
            container.Resolve <MyInstance>();
        }
예제 #11
0
        public void RefSet()
        {
            var container = new CryoContainer {
                OnCircularDependency = Reaction.LogWarning
            };

            container.RegisterSingleton <ClassA>();
            container.RegisterSingleton <ClassB>();

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

            Assert.IsNotNull(a.BackRef);
            Assert.AreSame(a, a.BackRef);

            var b = a.ClassB;

            Assert.IsNotNull(b.BackRef);
            Assert.AreSame(b, b.BackRef);
        }
예제 #12
0
        public void SceneInExternal()
        {
            var container = new CryoContainer {
                OnLifetimeError = Reaction.ThrowException
            };

            container.RegisterSingleton <MySingleton>(LifeTime.Scene);
            container.RegisterType <MyInstance>(LifeTime.External);
            container.Resolve <MyInstance>();
        }
예제 #13
0
        public void ExceptionThrown()
        {
            var container = new CryoContainer {
                OnCircularDependency = Reaction.ThrowException
            };

            container.RegisterSingleton <ClassA>();
            container.RegisterSingleton <ClassB>();

            try
            {
                container.Resolve <ClassA>();
                Assert.Fail("Exception expected");
            }
            catch (CircularDependencyException)
            {
                // expected
            }
        }
예제 #14
0
        public void LongChainThrow()
        {
            var container = new CryoContainer {
                CircularDependencyReaction = Reaction.ThrowException
            };

            container.RegisterSingleton <Class1>();
            container.RegisterSingleton <Class2>();
            container.RegisterSingleton <Class3>();
            container.RegisterSingleton <Class4>();

            try
            {
                container.Resolve <Class3>();
                Assert.Fail("Exception expected");
            }
            catch (CircularDependencyException)
            {
            }
        }
예제 #15
0
        public void ExceptionThrown()
        {
#if UNITY_EDITOR
            LogAssert.ignoreFailingMessages = true;
#endif
            var container = new CryoContainer {
                CircularDependencyReaction = Reaction.ThrowException
            };
            container.RegisterSingleton <ClassA>();
            container.RegisterSingleton <ClassB>();

            try
            {
                container.Resolve <ClassA>();
                Assert.Fail("Exception expected");
            }
            catch (CircularDependencyException)
            {
                // expected
            }
        }
예제 #16
0
        public void TestSingleton()
        {
            var container = new CryoContainer();

            container.RegisterSingleton <MainClass>();
            container.RegisterAlias <IFirstInterface, MainClass>();
            container.RegisterAlias <ISecondInterface, MainClass>();

            var firstInterface  = container.Resolve <IFirstInterface>();
            var secondInterface = container.Resolve <ISecondInterface>();
            var mainClass       = container.Resolve <MainClass>();

            Assert.AreSame(mainClass, firstInterface);
            Assert.AreSame(mainClass, secondInterface);
        }
예제 #17
0
        public void TestSingleton()
        {
            var container = new CryoContainer();

            container.RegisterSingleton <MyClazz>();

            Assert.IsNull(container.TryResolve <MyClazz>());
            var clazz = container.Resolve <MyClazz>();

            Assert.IsNotNull(clazz);

            var clazz2 = container.TryResolve <MyClazz>();

            Assert.IsNotNull(clazz2);
            Assert.AreSame(clazz, clazz2);
        }
예제 #18
0
        public void SelfReferenceThrow()
        {
            var container = new CryoContainer {
                CircularDependencyReaction = Reaction.ThrowException
            };

            container.RegisterSingleton <ClassC>();

            try
            {
                container.Resolve <ClassC>();
                Assert.Fail("Exception expected");
            }
            catch (CircularDependencyException)
            {
                // expected
            }
        }
예제 #19
0
        public void SceneInGlobal()
        {
            var container = new CryoContainer {
                OnLifetimeError = Reaction.ThrowException
            };

            container.RegisterSingleton <MySingleton>(LifeTime.Scene);
            container.RegisterType <MyInstance>(LifeTime.Global);

            try
            {
                container.Resolve <MyInstance>();
                Assert.Fail("Exception expected");
            }
            catch (WrongLifetimeException)
            {
                // expected
            }
        }
예제 #20
0
        public void TestAiasWithName()
        {
            var container = new CryoContainer();

            container.RegisterSingleton <MainClass>();
            container.RegisterAlias <IFirstInterface, MainClass>("AliasWithName");

            var firstInterface = container.ResolveByName <IFirstInterface>("AliasWithName");
            var mainClass      = container.Resolve <MainClass>();

            Assert.AreSame(mainClass, firstInterface);

            try
            {
                container.Resolve <IFirstInterface>();
                Assert.Fail("Expected CryoDI.ContainerException : Can't resolve type CryoDI.Tests.IFirstInterface");
            }
            catch (ContainerException ex)
            {
                // expected CryoDI.ContainerException : Can't resolve type CryoDI.Tests.MainClass
            }
        }
예제 #21
0
        public void TestSingleton()
        {
            _counter = 0;
            var container = new CryoContainer();

            container.RegisterSingleton <MyClass>(CreateMyClass);

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

            Assert.IsNotNull(a);
            Assert.AreEqual(1, a.InstanceId);
            Assert.AreEqual(1, _counter);

            a = container.Resolve <MyClass>();
            Assert.IsNotNull(a);
            Assert.AreEqual(1, a.InstanceId);
            Assert.AreEqual(1, _counter);

            a = container.Resolve <MyClass>();
            Assert.IsNotNull(a);
            Assert.AreEqual(1, a.InstanceId);
            Assert.AreEqual(1, _counter);
        }
예제 #22
0
 protected override void SetupContainer(CryoContainer container)
 {
     container.RegisterSingleton <EventEmitter>(LifeTime.Scene);
     container.RegisterSceneObject <TicTacToe>("Application", LifeTime.Scene);
 }
예제 #23
0
 protected override void SetupContainer(CryoContainer container)
 {
     container.RegisterSingleton <EventEmitter>(LifeTime.Scene);
 }