Exemplo n.º 1
0
        public void TestBindWhenComplexConditionByMemberName()
        {
            var container = new InjectionContainer(ResolutionMode.RETURN_NULL);

            container.Bind <IMockInterface>().To <MockIClass>().When(context =>
                                                                     context.memberName.Equals("property2")
                                                                     );
            container.Bind <IMockInterface>().To <MockIClass>().When(context =>
                                                                     context.memberName.Equals("field2")
                                                                     );
            container.Bind <MockClassToDepend>().ToSelf().When(context =>
                                                               context.member.Equals(InjectionMember.Method) &&
                                                               context.memberName.Equals("field2")
                                                               );
            container.Bind <MockClassInjectAll>().ToSingleton();

            var instance = container.Resolve <MockClassInjectAll>();

            Assert.IsNull(instance.property1);
            Assert.AreEqual(typeof(MockIClass), instance.property2.GetType());
            Assert.IsNull(instance.property3);

            Assert.IsNull(instance.field1);
            Assert.AreEqual(typeof(MockIClass), instance.field2.GetType());
            Assert.IsNull(instance.field3);

            Assert.AreNotEqual(instance.property2, instance.field2);

            Assert.IsNull(instance.fieldFromConstructor1);
            Assert.IsNull(instance.fieldFromConstructor2);

            Assert.IsNull(instance.fieldFromMethod1);
            Assert.IsNotNull(instance.fieldFromMethod2);
        }
Exemplo n.º 2
0
        public void TestBindAsIdentifier()
        {
            var container = new InjectionContainer();

            container.Bind<IMockInterface>().To<MockIClassWithAttributes>();
            container.Bind<IMockInterface>().To<MockIClassWithoutAttributes>().As("singleton");
            container.Bind<IMockInterface>().To<MockIClass>().As("test");

            var instance = container.Resolve<MockClassSimple>();

            Assert.AreEqual(typeof(MockIClassWithAttributes), instance.field.GetType());
            Assert.AreEqual(typeof(MockIClassWithoutAttributes), instance.property.GetType());
        }
Exemplo n.º 3
0
        public void TestBindAsIdentifier()
        {
            var container = new InjectionContainer();

            container.Bind <IMockInterface>().To <MockIClassWithAttributes>();
            container.Bind <IMockInterface>().To <MockIClassWithoutAttributes>().As("singleton");
            container.Bind <IMockInterface>().To <MockIClass>().As("test");

            var instance = container.Resolve <MockClassSimple>();

            Assert.AreEqual(typeof(MockIClassWithAttributes), instance.field.GetType());
            Assert.AreEqual(typeof(MockIClassWithoutAttributes), instance.property.GetType());
        }
Exemplo n.º 4
0
        public void TestBindWhenIntoByType()
        {
            var container = new InjectionContainer();

            container.Bind <IMockInterface>().To <MockIClassWithAttributes>().WhenInto(typeof(MockClassVerySimple));
            container.Bind <IMockInterface>().To <MockIClassWithoutAttributes>().WhenInto(typeof(MockClassSimple));

            var instance1 = container.Resolve <MockClassVerySimple>();
            var instance2 = container.Resolve <MockClassSimple>();

            Assert.AreEqual(typeof(MockIClassWithAttributes), instance1.field.GetType());
            Assert.AreEqual(typeof(MockIClassWithoutAttributes), instance2.field.GetType());
        }
Exemplo n.º 5
0
        public void TestResolveMultiple()
        {
            var container = new InjectionContainer();

            container.Bind <IMockInterface>().To <MockIClass>();
            container.Bind <IMockInterface>().To <MockIClassWithAttributes>();
            container.Bind <IMockInterface>().To <MockIClassWithoutAttributes>();

            var instance = container.Resolve <MockClassMultiple>();

            Assert.AreEqual(3, instance.list.Length);
            Assert.AreEqual(typeof(MockIClass), instance.list[0].GetType());
            Assert.AreEqual(typeof(MockIClassWithAttributes), instance.list[1].GetType());
            Assert.AreEqual(typeof(MockIClassWithoutAttributes), instance.list[2].GetType());
        }
Exemplo n.º 6
0
        public void TestResolutionModeReturnNullBound()
        {
            var container = new InjectionContainer(ResolutionMode.ALWAYS_RESOLVE);

            container.Bind <IMockInterface1>().ToSingleton <MockClassManyInterfaces>();

            var instance = container.Resolve <IMockInterface1>();

            Assert.NotNull(instance);
        }
Exemplo n.º 7
0
        public void TestResolveFromFactoryInstance()
        {
            var container = new InjectionContainer();

            container.Bind <MockIClassWithAttributes>().ToFactory(new MockFactory());
            var instance = container.Resolve <MockIClassWithAttributes>();

            Assert.NotNull(instance);
            Assert.AreEqual("Created from a Factory", instance.field1);
            Assert.AreEqual("Created from a Factory", instance.field2);
            Assert.AreEqual("Created from a Factory", instance.field3);
        }
Exemplo n.º 8
0
        public void TestResolveToNamespaceTransient()
        {
            var container = new InjectionContainer();

            container.Bind <IMockInterface>().ToNamespace("Adic.Tests");
            var instance = container.ResolveAll <IMockInterface>();

            Assert.AreEqual(3, instance.Length);
            Assert.IsTrue(instance[0] is MockIClass);
            Assert.IsTrue(instance[1] is MockIClassWithoutAttributes);
            Assert.IsTrue(instance[2] is MockIClassWithAttributes);
        }
Exemplo n.º 9
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());
        }
Exemplo n.º 10
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());
        }
Exemplo n.º 11
0
        public void TestResolutionModeReturnNullSingleton()
        {
            var container = new InjectionContainer(ResolutionMode.RETURN_NULL);

            container.Bind <IMockInterface>().ToSingleton <MockIClassWithAttributes>();

            var instance = container.Resolve <IMockInterface>();

            Assert.IsNotNull(instance);
            Assert.AreEqual(typeof(MockIClassWithAttributes), instance.GetType());
            Assert.Null(((MockIClassWithAttributes)instance).field4);
            Assert.Null(((MockIClassWithAttributes)instance).property4);
        }
Exemplo n.º 12
0
        public override void SetupContainers()
        {
            IInjectionContainer container = new InjectionContainer()
                                            .RegisterExtension <UnityBindingContainerExtension>()
                                            .RegisterExtension <EventCallerContainerExtension>();

            IInjectionService injectionService = new InjectionService(container);

            container
            .Bind <IInjectionService>().To(injectionService)
            .Bind <IEntityGeneratorService>().ToSingleton <EntityGeneratorService>();

            BindControllers(container);

            AddContainer(container);
        }
Exemplo n.º 13
0
        public void TestResolveToNamespaceSingleton()
        {
            var container = new InjectionContainer();

            container.Bind <IMockInterface>().ToNamespaceSingleton("Adic.Tests");
            var bindings = container.GetBindingsFor <IMockInterface>();
            var instance = container.ResolveAll <IMockInterface>();

            Assert.AreEqual(3, instance.Length);
            Assert.IsTrue(instance[0] is MockIClass);
            Assert.IsTrue(instance[1] is MockIClassWithoutAttributes);
            Assert.IsTrue(instance[2] is MockIClassWithAttributes);
            Assert.IsTrue(instance[0] == bindings[0].value);
            Assert.IsTrue(instance[1] == bindings[1].value);
            Assert.IsTrue(instance[2] == bindings[2].value);
        }
Exemplo n.º 14
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);
        }
Exemplo n.º 15
0
        public void TestResolveToNamespaceTransient()
        {
            var container = new InjectionContainer();

            container.Bind<IMockInterface>().ToNamespace("Adic.Tests");
            var instance = container.ResolveAll<IMockInterface>();

            Assert.AreEqual(3, instance.Length);
            Assert.IsTrue(instance[0] is MockIClass);
            Assert.IsTrue(instance[1] is MockIClassWithoutAttributes);
            Assert.IsTrue(instance[2] is MockIClassWithAttributes);
        }
Exemplo n.º 16
0
        public void TestResolveToNamespaceSingleton()
        {
            var container = new InjectionContainer();

            container.Bind<IMockInterface>().ToNamespaceSingleton("Adic.Tests");
            var bindings = container.GetBindingsFor<IMockInterface>();
            var instance = container.ResolveAll<IMockInterface>();

            Assert.AreEqual(3, instance.Length);
            Assert.IsTrue(instance[0] is MockIClass);
            Assert.IsTrue(instance[1] is MockIClassWithoutAttributes);
            Assert.IsTrue(instance[2] is MockIClassWithAttributes);
            Assert.IsTrue(instance[0] == bindings[0].value);
            Assert.IsTrue(instance[1] == bindings[1].value);
            Assert.IsTrue(instance[2] == bindings[2].value);
        }
Exemplo n.º 17
0
        public void TestResolveMultiple()
        {
            var container = new InjectionContainer();

            container.Bind<IMockInterface>().To<MockIClass>();
            container.Bind<IMockInterface>().To<MockIClassWithAttributes>();
            container.Bind<IMockInterface>().To<MockIClassWithoutAttributes>();

            var instance = container.Resolve<MockClassMultiple>();

            Assert.AreEqual(3, instance.list.Length);
            Assert.AreEqual(typeof(MockIClass), instance.list[0].GetType());
            Assert.AreEqual(typeof(MockIClassWithAttributes), instance.list[1].GetType());
            Assert.AreEqual(typeof(MockIClassWithoutAttributes), instance.list[2].GetType());
        }
Exemplo n.º 18
0
        public void TestResolveFromFactoryType()
        {
            var container = new InjectionContainer();

            container.Bind<MockIClassWithAttributes>().ToFactory(typeof(MockFactory));
            var instance = container.Resolve<MockIClassWithAttributes>();

            Assert.NotNull(instance);
            Assert.AreEqual("Created from a Factory", instance.field1);
            Assert.AreEqual("Created from a Factory", instance.field2);
            Assert.AreEqual("Created from a Factory", instance.field3);
        }
Exemplo n.º 19
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);
        }
Exemplo n.º 20
0
        public void TestBindWhenIntoByType()
        {
            var container = new InjectionContainer();

            container.Bind<IMockInterface>().To<MockIClassWithAttributes>().WhenInto(typeof(MockClassVerySimple));
            container.Bind<IMockInterface>().To<MockIClassWithoutAttributes>().WhenInto(typeof(MockClassSimple));

            var instance1 = container.Resolve<MockClassVerySimple>();
            var instance2 = container.Resolve<MockClassSimple>();

            Assert.AreEqual(typeof(MockIClassWithAttributes), instance1.field.GetType());
            Assert.AreEqual(typeof(MockIClassWithoutAttributes), instance2.field.GetType());
        }
Exemplo n.º 21
0
 public void Install(InjectionContainer.IInjectionContainer container, Common.IQueryStore queryStore)
 {
     container.Bind<IHttpHost, AspNetRoutedHost>();
     container.Bind<IItemManager, ThreadItemManager>();
 }