Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        public void TestBaseType()
        {
            _counter = 0;
            var container = new CryoContainer();

            container.RegisterType <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(2, ((MyClass)a).InstanceId);
            Assert.AreEqual(2, _counter);

            a = container.Resolve <MyBaseClass>();
            Assert.IsNotNull(a);
            Assert.IsInstanceOf(typeof(MyClass), a);
            Assert.AreEqual(3, ((MyClass)a).InstanceId);
            Assert.AreEqual(3, _counter);
        }
Exemplo n.º 3
0
        public void SceneInExternal()
        {
            var container = new CryoContainer {
                OnLifetimeError = Reaction.ThrowException
            };

            container.RegisterSingleton <MySingleton>(LifeTime.Scene);
            container.RegisterType <MyInstance>(LifeTime.External);
            container.Resolve <MyInstance>();
        }
Exemplo n.º 4
0
        public void ExternalInGlobal()
        {
            var container = new CryoContainer {
                LifetimeErrorReaction = Reaction.ThrowException
            };

            container.RegisterSingleton <MySingleton>(LifeTime.External);
            container.RegisterType <MyInstance>(LifeTime.Global);
            container.Resolve <MyInstance>();
        }
Exemplo n.º 5
0
        public void TestType()
        {
            var container = new CryoContainer();

            container.RegisterType <MyClazz>();

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

            Assert.IsNotNull(clazz);
            Assert.IsNull(container.TryResolve <MyClazz>());
        }
Exemplo n.º 6
0
        public void TestResolveSimilarParams()
        {
            var container = new CryoContainer();

            container.RegisterType <ClassWithSimilarParams>();

            var val = container.Resolve <ClassWithSimilarParams>(new Param {
                Name = "Two", Value = 2
            }, "Hello, world!", new Param("One", 1));

            Assert.AreEqual(1, val.One);
            Assert.AreEqual(2, val.Two);
            Assert.AreEqual("Hello, world!", val.Str);
        }
Exemplo n.º 7
0
        public void TestSimplePropertiesResolve()
        {
            var container = new CryoContainer();

            container.RegisterInstance("private base", "PrivateBase");
            container.RegisterInstance("protected base", "ProtectedBase");
            container.RegisterInstance("public base", "PublicBase");
            container.RegisterType <ClassBase>();


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

            a.Check();
        }
Exemplo n.º 8
0
        public void TestResolveFailed()
        {
            var container = new CryoContainer();

            container.RegisterType <ClassWithParams>();

            try
            {
                container.Resolve <ClassWithParams>();
                Assert.Fail("Expected exception not happened");
            }
            catch (ContainerException)
            {
            }
        }
Exemplo n.º 9
0
        public void TestResolveSimilarParamsFailed2()
        {
            var container = new CryoContainer();

            container.RegisterType <ClassWithSimilarParams>();

            try
            {
                container.Resolve <ClassWithSimilarParams>(1, "Hello, world!", 2);
                Assert.Fail("Expected exception not happened");
            }
            catch (ContainerException)
            {
            }
        }
Exemplo n.º 10
0
        public void TestResolve()
        {
            var container = new CryoContainer();

            container.RegisterType <ClassWithParams>();

            var now    = DateTime.Now;
            var stream = new MemoryStream();

            var val = container.Resolve <ClassWithParams>(stream, now, 3, 1.12f);

            Assert.AreEqual(now, val.DateTimeParam);
            Assert.AreEqual(stream, val.DisposableParam);
            Assert.AreEqual(3, val.IntParam);
            Assert.AreEqual(1.12f, val.FloatParam, 0.000001);
        }
Exemplo n.º 11
0
        public void TestDerivedPropertiesResolve()
        {
            var container = new CryoContainer();

            container.RegisterInstance("private base", "PrivateBase");
            container.RegisterInstance("protected base", "ProtectedBase");
            container.RegisterInstance("public base", "PublicBase");

            container.RegisterInstance("private derived", "PrivateDerived");
            container.RegisterInstance("protected derived", "ProtectedDerived");
            container.RegisterInstance("public derived", "PublicDerived");
            container.RegisterType <ClassDerived>();

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

            a.Check();
        }
Exemplo n.º 12
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
            }
        }
Exemplo n.º 13
0
        public void TestType()
        {
            _counter = 0;
            var container = new CryoContainer();

            container.RegisterType <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(2, a.InstanceId);
            Assert.AreEqual(2, _counter);

            a = container.Resolve <MyClass>();
            Assert.IsNotNull(a);
            Assert.AreEqual(3, a.InstanceId);
            Assert.AreEqual(3, _counter);
        }
Exemplo n.º 14
0
 protected override void SetupContainer(CryoContainer container)
 {
     base.SetupContainer(container);
     container.RegisterType <IController, TankInput, TankShooter>();
     container.RegisterType <IController, TankInput, TankMovement>();
 }