public void TestAfterInject()
        {
            var eventCalled = false;

            IReflectionCache cache = new ReflectionCache();
            IBinder binder = new Binder();
            IInjector injector = new Injector(cache, binder);
            var instanceToInject = new MockClassVerySimple();

            injector.afterInject += delegate(IInjector source, ref object instance, ReflectedClass reflectedClass) {
                //The if below is just to avoid checking when injecting on MockIClass.
                if (reflectedClass.type != typeof(MockClassVerySimple)) return;

                Assert.AreEqual(injector, source);
                Assert.AreEqual(instanceToInject, instance);
                Assert.AreEqual(typeof(MockIClass), instanceToInject.field.GetType());

                eventCalled = true;
            };

            binder.Bind<IMockInterface>().To<MockIClass>();
            injector.Inject(instanceToInject);

            Assert.IsTrue(eventCalled);
        }
Beispiel #2
0
        public void TestAfterInject()
        {
            var eventCalled = false;

            IReflectionCache cache    = new ReflectionCache();
            IBinder          binder   = new Binder();
            IInjector        injector = new Injector(cache, binder, ResolutionMode.ALWAYS_RESOLVE);
            var instanceToInject      = new MockClassVerySimple();

            injector.afterInject += delegate(IInjector source, ref object instance, ReflectedClass reflectedClass) {
                //The if below is just to avoid checking when injecting on MockIClass.
                if (reflectedClass.type != typeof(MockClassVerySimple))
                {
                    return;
                }

                Assert.AreEqual(injector, source);
                Assert.AreEqual(instanceToInject, instance);
                Assert.AreEqual(typeof(MockIClass), instanceToInject.field.GetType());

                eventCalled = true;
            };

            binder.Bind <IMockInterface>().To <MockIClass>();
            injector.Inject(instanceToInject);

            Assert.IsTrue(eventCalled);
        }
Beispiel #3
0
        public void TestBindWhenIntoByInstance()
        {
            var instance1 = new MockClassSimple();
            var instance2 = new MockClassVerySimple();

            var container = new InjectionContainer();

            container.Bind <IMockInterface>().To <MockIClassWithAttributes>().WhenIntoInstance(instance1);
            container.Bind <IMockInterface>().To <MockIClassWithoutAttributes>().WhenIntoInstance(instance2);

            container.Inject(instance1);
            container.Inject(instance2);

            Assert.AreEqual(typeof(MockIClassWithAttributes), instance1.field.GetType());
            Assert.AreEqual(typeof(MockIClassWithoutAttributes), instance2.field.GetType());
        }
Beispiel #4
0
        public void TestBindAsIdentifierWhenIntoByInstance()
        {
            var instance1 = new MockClassVerySimple();
            var instance2 = new MockClassSimple();

            var container = new InjectionContainer();

            container.Bind<IMockInterface>()
                .To<MockIClassWithAttributes>().WhenIntoInstance(instance1);
            container.Bind<IMockInterface>()
                .To<MockIClassWithoutAttributes>().WhenIntoInstance(instance2).As("singleton");
            container.Bind<IMockInterface>()
                .To<MockIClass>().WhenInto<MockClassSimple>().WhenIntoInstance(instance2).As("test");

            container.Inject(instance1);
            container.Inject(instance2);

            Assert.AreEqual(typeof(MockIClassWithAttributes), instance1.field.GetType());
            Assert.IsNull(instance2.field);
            Assert.AreEqual(typeof(MockIClassWithoutAttributes), instance2.property.GetType());
        }
Beispiel #5
0
        public void TestBindAsIdentifierWhenIntoByInstance()
        {
            var instance1 = new MockClassVerySimple();
            var instance2 = new MockClassSimple();

            var container = new InjectionContainer();

            container.Bind <IMockInterface>()
            .To <MockIClassWithAttributes>().WhenIntoInstance(instance1);
            container.Bind <IMockInterface>()
            .To <MockIClassWithoutAttributes>().WhenIntoInstance(instance2).As("singleton");
            container.Bind <IMockInterface>()
            .To <MockIClass>().WhenInto <MockClassSimple>().WhenIntoInstance(instance2).As("test");

            container.Inject(instance1);
            container.Inject(instance2);

            Assert.AreEqual(typeof(MockIClassWithAttributes), instance1.field.GetType());
            Assert.IsNull(instance2.field);
            Assert.AreEqual(typeof(MockIClassWithoutAttributes), instance2.property.GetType());
        }
Beispiel #6
0
        public void TestBindWhenComplexConditionField()
        {
            var instance1 = new MockClassVerySimple();
            var instance2 = new MockClassSimple();
            var instance3 = new MockClassSimple();

            var container = new InjectionContainer();

            container.Bind <IMockInterface>().To <MockIClass>().When(context =>
                                                                     context.member.Equals(InjectionMember.Field) &&
                                                                     context.parentInstance.Equals(instance3)
                                                                     );

            container.Inject(instance1);
            container.Inject(instance2);
            container.Inject(instance3);

            Assert.IsNull(instance1.field);
            Assert.IsNull(instance2.field);
            Assert.IsNull(instance2.property);
            Assert.AreEqual(typeof(MockIClass), instance3.field.GetType());
            Assert.IsNull(instance3.property);
        }
Beispiel #7
0
        public void TestBindWhenComplexCondition()
        {
            var instance1 = new MockClassVerySimple();
            var instance2 = new MockClassSimple();
            var instance3 = new MockClassSimple();

            var container = new InjectionContainer();

            container.Bind<IMockInterface>().To<MockIClass>().When(context =>
                    context.member.Equals(InjectionMember.Field) &&
                    context.parentInstance.Equals(instance3)
                );

            container.Inject(instance1);
            container.Inject(instance2);
            container.Inject(instance3);

            Assert.IsNull(instance1.field);
            Assert.IsNull(instance2.field);
            Assert.IsNull(instance2.property);
            Assert.AreEqual(typeof(MockIClass), instance3.field.GetType());
            Assert.IsNull(instance3.property);
        }
Beispiel #8
0
        public void TestBindWhenIntoByInstance()
        {
            var instance1 = new MockClassSimple();
            var instance2 = new MockClassVerySimple();

            var container = new InjectionContainer();

            container.Bind<IMockInterface>().To<MockIClassWithAttributes>().WhenIntoInstance(instance1);
            container.Bind<IMockInterface>().To<MockIClassWithoutAttributes>().WhenIntoInstance(instance2);

            container.Inject(instance1);
            container.Inject(instance2);

            Assert.AreEqual(typeof(MockIClassWithAttributes), instance1.field.GetType());
            Assert.AreEqual(typeof(MockIClassWithoutAttributes), instance2.field.GetType());
        }