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>(); }
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); }
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); }
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); }
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); }
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; } }
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); }
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>(); }
public void ExternalInGlobal() { var container = new CryoContainer { LifetimeErrorReaction = Reaction.ThrowException }; container.RegisterSingleton <MySingleton>(LifeTime.External); container.RegisterType <MyInstance>(LifeTime.Global); container.Resolve <MyInstance>(); }
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); }
public void SceneInExternal() { var container = new CryoContainer { OnLifetimeError = Reaction.ThrowException }; container.RegisterSingleton <MySingleton>(LifeTime.Scene); container.RegisterType <MyInstance>(LifeTime.External); container.Resolve <MyInstance>(); }
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 } }
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) { } }
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 } }
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); }
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); }
public void SelfReferenceThrow() { var container = new CryoContainer { CircularDependencyReaction = Reaction.ThrowException }; container.RegisterSingleton <ClassC>(); try { container.Resolve <ClassC>(); Assert.Fail("Exception expected"); } catch (CircularDependencyException) { // expected } }
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 } }
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 } }
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); }
protected override void SetupContainer(CryoContainer container) { container.RegisterSingleton <EventEmitter>(LifeTime.Scene); container.RegisterSceneObject <TicTacToe>("Application", LifeTime.Scene); }
protected override void SetupContainer(CryoContainer container) { container.RegisterSingleton <EventEmitter>(LifeTime.Scene); }