예제 #1
0
        public void ContainerAutomaticallyResolvesAllWhenInjectingArrays()
        {
            ILogger[]       expected  = new ILogger[] { new MockLogger(), new SpecialLogger() };
            IUnityContainer container = new UnityContainer()
                                        .RegisterInstance("one", expected[0])
                                        .RegisterInstance("two", expected[1]);

            TypeWithArrayConstructorParameter result = container.Resolve <TypeWithArrayConstructorParameter>();

            CollectionAssertExtensions.AreEqual(expected, result.Loggers);
        }
        public void CanResolveArrayForConstructorParameter()
        {
            ILogger o1 = new MockLogger();
            ILogger o2 = new SpecialLogger();

            IUnityContainer container
                = new UnityContainer()
                  .RegisterInstance <ILogger>("o1", o1)
                  .RegisterInstance <ILogger>("o2", o2);

            TypeWithArrayConstructorParameter resolved = container.Resolve <TypeWithArrayConstructorParameter>();

            Assert.IsNotNull(resolved.loggers);
            Assert.AreEqual(2, resolved.loggers.Length);
            Assert.AreSame(o1, resolved.loggers[0]);
            Assert.AreSame(o2, resolved.loggers[1]);
        }
예제 #3
0
        public void CanConfigureContainerToCallConstructorWithArrayParameterWithNonGenericVersion()
        {
            ILogger o1 = new MockLogger();
            ILogger o2 = new SpecialLogger();

            IUnityContainer container = new UnityContainer()
                                        .RegisterType <TypeWithArrayConstructorParameter>(new InjectionConstructor(typeof(ILogger[])))
                                        .RegisterInstance <ILogger>("o1", o1)
                                        .RegisterInstance <ILogger>("o2", o2);

            TypeWithArrayConstructorParameter resolved = container.Resolve <TypeWithArrayConstructorParameter>();

            Assert.NotNull(resolved.Loggers);
            Assert.Equal(2, resolved.Loggers.Length);
            Assert.Same(o1, resolved.Loggers[0]);
            Assert.Same(o2, resolved.Loggers[1]);
        }
예제 #4
0
        public void CanConfigureContainerToInjectSpecificValuesIntoAnArray()
        {
            ILogger logger2 = new SpecialLogger();

            IUnityContainer container = new UnityContainer()
                                        .RegisterType <TypeWithArrayConstructorParameter>(
                new InjectionConstructor(
                    new ResolvedArrayParameter <ILogger>(
                        new ResolvedParameter <ILogger>("log1"),
                        typeof(ILogger),
                        logger2)))
                                        .RegisterType <ILogger, MockLogger>()
                                        .RegisterType <ILogger, SpecialLogger>("log1");

            TypeWithArrayConstructorParameter result = container.Resolve <TypeWithArrayConstructorParameter>();

            Assert.Equal(3, result.Loggers.Length);
            AssertExtensions.IsInstanceOfType(result.Loggers[0], typeof(SpecialLogger));
            AssertExtensions.IsInstanceOfType(result.Loggers[1], typeof(MockLogger));
            Assert.Same(logger2, result.Loggers[2]);
        }