public void GettingUnregisteredInstance() { var kernel = new DependencyInjectionKernel(new DependencyInjectionContainer()); // create a dependencyInjectionKernel with a DependencyInjectionContainer which has no bindings var obj = kernel.Get <SomeClass>(); Assert.NotNull(obj, "The object must not be null"); Assert.IsInstanceOf <SomeClass>(obj, "The object must be of type SomeClass"); }
public void InjectionTest() { var kernel = new DependencyInjectionKernel(new DependencyInjectionContainer()); // we dont need bindings to test the injection var obj = kernel.Get <ClassWithInjects>(); Assert.NotNull(obj, "The object must not be null"); Assert.IsInstanceOf <SomeClass>(obj.One, "The constructor injected member 'One' must be of type SomeClass"); Assert.IsInstanceOf <SomeClass2>(obj.Two, "The constructor injected member 'Two' must be of type SomeClass2"); Assert.IsInstanceOf <SomeClass>(obj.Three, "The field injected member 'Three' must be of type SomeClass"); Assert.IsInstanceOf <SomeClass2>(obj.Four, "The property injected member 'Four' must be of type SomeClass2"); }
public void SingletonInjectionTest() { var kernel = new DependencyInjectionKernel(new SingletonCreationDependencyInjectionContainer()); var object1 = kernel.Get <ISomeInterface>(); object1.SetText("first text"); var object2 = kernel.Get <ISomeInterface>(); object2.SetText("second text"); Assert.AreSame(object1, object2, "The two object must be the same due to singleton injection"); Assert.IsInstanceOf <SomeClass>(object1, "The object should be of type SomeClass"); Assert.IsInstanceOf <SomeClass>(object2, "The object should be of type SomeClass"); Assert.AreEqual("second text", object1.GetText(), "The object should contain 'second text'"); Assert.AreEqual("second text", object2.GetText(), "The object should contain 'second text'"); }
public void NewInstanceInjectionTest() { var kernel = new DependencyInjectionKernel(new NewInstanceCreationDependencyInjectionContainer()); var object1 = kernel.Get <ISomeInterface>(); object1.SetText("first text"); var object2 = kernel.Get <ISomeInterface>(); object2.SetText("second text"); Assert.AreNotSame(object1, object2, "The two objects must no be the same, there must be a new instance created for each of them."); Assert.IsInstanceOf <SomeClass>(object1, "The object should be of type SomeClass"); Assert.IsInstanceOf <SomeClass>(object2, "The object should be of type SomeClass"); Assert.AreEqual("first text", object1.GetText(), "The object should contain 'first text'"); Assert.AreEqual("second text", object2.GetText(), "The object should contain 'second text'"); }
/// <summary> /// Executes a command and adds it to the history if it implements IUndoable /// </summary> /// <param name="commandClass">command class type</param> /// <param name="args">parameters for executing commands Execute() method</param> protected override void ExecuteCommand(Type commandClass, object[] args) { var command = (ICommand)DependencyInjectionKernel.Get(commandClass); var success = command.Execute(args); // when the command was executed successfully and it is undobale we need to remember it if (success && command is IUndoable) { // increase the pointer by one UndoableCommandsPointer++; // remove items from the history the are after the pointer UndoableCommandHistory.RemoveRange(UndoableCommandsPointer, UndoableCommandHistory.Count - UndoableCommandsPointer); UndoableCommandHistory.Add((IUndoable)command); // set the pointer to the last element UndoableCommandsPointer = UndoableCommandHistory.Count - 1; // UnityEngine.Debug.Log("UndoableCommandsPointer: " + UndoableCommandsPointer); } }