Example #1
0
        public void TestPolymorphicBinding()
        {
            binder.Bind <ISimpleInterface> ().Bind <IAnotherSimpleInterface> ().To <PolymorphicClass> ();

            ISimpleInterface callOnce = binder.GetInstance <ISimpleInterface> () as ISimpleInterface;

            Assert.NotNull(callOnce);
            Assert.IsInstanceOf <PolymorphicClass> (callOnce);

            IAnotherSimpleInterface callAgain = binder.GetInstance <IAnotherSimpleInterface> () as IAnotherSimpleInterface;

            Assert.NotNull(callAgain);
            Assert.IsInstanceOf <PolymorphicClass> (callAgain);
        }
        public void TestPolymorphicSingleton()
        {
            binder.Bind <ISimpleInterface> ().Bind <IAnotherSimpleInterface> ().To <PolymorphicClass> ().ToSingleton();

            ISimpleInterface callOnce = binder.GetInstance <ISimpleInterface> ();

            Assert.NotNull(callOnce);
            Assert.IsInstanceOf <PolymorphicClass> (callOnce);

            IAnotherSimpleInterface callAgain = binder.GetInstance <IAnotherSimpleInterface> ();

            Assert.NotNull(callAgain);
            Assert.IsInstanceOf <PolymorphicClass> (callAgain);

            callOnce.intValue = 42;

            Assert.AreSame(callOnce, callAgain);
            Assert.AreEqual(42, (callAgain  as ISimpleInterface).intValue);
        }
        public void TestRuntimePolymorphism()
        {
            string jsonString = "[{\"Bind\":[\"strange.unittests.ISimpleInterface\",\"strange.unittests.IAnotherSimpleInterface\"],\"To\":\"strange.unittests.PolymorphicClass\"}]";

            binder.ConsumeBindings(jsonString);

            IBinding binding = binder.GetBinding <ISimpleInterface> ();

            Assert.NotNull(binding);

            ISimpleInterface instance = binder.GetInstance(typeof(ISimpleInterface)) as ISimpleInterface;

            Assert.IsInstanceOf <PolymorphicClass> (instance);

            IBinding binding2 = binder.GetBinding <IAnotherSimpleInterface> ();

            Assert.NotNull(binding2);

            IAnotherSimpleInterface instance2 = binder.GetInstance(typeof(IAnotherSimpleInterface)) as IAnotherSimpleInterface;

            Assert.IsInstanceOf <PolymorphicClass> (instance2);
        }