public void TestLocalOverridesCrossContext() { Parent.injectionBinder.Bind<TestModel>().ToSingleton().CrossContext(); //bind the cross context binding. TestModel initialChildOneModel = new TestModel(); initialChildOneModel.Value = 1000; ChildOne.injectionBinder.Bind<TestModel>().ToValue(initialChildOneModel); //Bind a local override in this child TestModel parentModel = Parent.injectionBinder.GetInstance<TestModel>() as TestModel; //Get the instance from the parent injector (The cross context binding) TestModel childOneModel = ChildOne.injectionBinder.GetInstance<TestModel>() as TestModel; Assert.AreSame(initialChildOneModel, childOneModel); // The value from getInstance is the same as the value we just mapped as a value locally Assert.AreNotSame(childOneModel, parentModel); //The value from getinstance is NOT the same as the cross context value. We have overidden the cross context value locally TestModel childTwoModel = ChildTwo.injectionBinder.GetInstance<TestModel>() as TestModel; Assert.IsNotNull(childTwoModel); Assert.AreNotSame(childOneModel, childTwoModel); //These two are different objects, the childTwoModel being cross context, and childone being the override Assert.AreSame(parentModel, childTwoModel); //Both cross context models are the same parentModel.Value++; Assert.AreEqual(1, childTwoModel.Value); //cross context model should be changed parentModel.Value++; Assert.AreEqual(1000, childOneModel.Value); //local model is not changed Assert.AreEqual(2, parentModel.Value); //cross context model is changed }
public void TestFactory() { TestModel parentModel = new TestModel(); Parent.injectionBinder.Bind<TestModel>().To<TestModel>().CrossContext(); TestModel parentModelTwo = Parent.injectionBinder.GetInstance<TestModel>() as TestModel; Assert.AreNotSame(parentModel, parentModelTwo); //As it's a factory, we should not have the same objects TestModel childOneModel = ChildOne.injectionBinder.GetInstance<TestModel>() as TestModel; Assert.IsNotNull(childOneModel); TestModel childTwoModel = ChildTwo.injectionBinder.GetInstance<TestModel>() as TestModel; Assert.IsNotNull(childTwoModel); Assert.AreNotSame(childOneModel, childTwoModel); //These two should be DIFFERENT Assert.AreEqual(0, parentModel.Value); parentModel.Value++; Assert.AreEqual(0, childOneModel.Value); //doesn't change parentModel.Value++; Assert.AreEqual(0, childTwoModel.Value); //doesn't change }
public void TestValue() { TestModel parentModel = new TestModel(); Parent.injectionBinder.Bind<TestModel>().ToValue(parentModel).CrossContext(); //bind it once here and it should be accessible everywhere TestModel parentModelTwo = Parent.injectionBinder.GetInstance<TestModel>() as TestModel; Assert.AreSame(parentModel, parentModelTwo); //Assure that this value is correct TestModel childOneModel = ChildOne.injectionBinder.GetInstance<TestModel>() as TestModel; Assert.IsNotNull(childOneModel); TestModel childTwoModel = ChildTwo.injectionBinder.GetInstance<TestModel>() as TestModel; Assert.IsNotNull(childTwoModel); Assert.AreSame(childOneModel, childTwoModel); //These two should be the same object Assert.AreEqual(0, parentModel.Value); parentModel.Value++; Assert.AreEqual(1, childOneModel.Value); parentModel.Value++; Assert.AreEqual(2, childTwoModel.Value); }