public void ShouldAllowMultipleTargetsByDefault()
        {
            var targets = new TargetContainer();

            RegisterTwoTargets(targets, typeof(int));

            var result = targets.FetchAll(typeof(int));

            Assert.NotNull(result);
            Assert.Equal(2, result.Count());
        }
예제 #2
0
        public void Covariant_ShouldFetchAllMatches()
        {
            // Arrange
            var targets = new TargetContainer();

            targets.RegisterType <Covariant <BaseClass>, ICovariant <BaseClass> >();
            targets.RegisterType <Covariant <BaseClassChild>, ICovariant <BaseClassChild> >();
            targets.RegisterType <Covariant <BaseClassGrandchild>, ICovariant <BaseClassGrandchild> >();

            // Act
            var fetched = targets.FetchAll(typeof(ICovariant <BaseClass>)).ToArray();

            // Assert
            Assert.Equal(3, fetched.Length);
        }
        public void ShouldNotFetchConstrainedGenericForIncompatibleType()
        {
            // Arrange
            ITargetContainer targets = new TargetContainer();
            var expected             = Target.ForType(typeof(Generic <>));
            var notexpected          = Target.ForType(typeof(ConstrainedGeneric <>));

            targets.Register(expected, typeof(IGeneric <>));
            targets.Register(notexpected, typeof(IGeneric <>));

            // Act
            var single = targets.Fetch(typeof(IGeneric <string>));
            var all    = targets.FetchAll(typeof(IGeneric <string>));

            // Assert
            Assert.Same(expected, single);
            Assert.Single(all, expected);
        }
예제 #4
0
        public void Covariant_ShouldNotRetrieveConstrained()
        {
            // Arrange
            var targets     = new TargetContainer();
            var expected    = Target.ForType(typeof(Covariant <>));
            var notExpected = Target.ForType(typeof(ConstrainedCovariant <>));

            targets.Register(expected, typeof(ICovariant <>));
            targets.Register(notExpected, typeof(ICovariant <>));

            // Act
            var single = targets.Fetch(typeof(ICovariant <string>));
            var all    = targets.FetchAll(typeof(ICovariant <string>));

            // Assert
            Assert.Same(expected, single);
            Assert.Single(all, expected);
        }
        public void ShouldFetchConstrainedGenericInsteadOfOpen()
        {
            // registration order matters, for now, when registering constrained generics
            // because they are registered against the open generic type.  Therefore, if
            // an unconstrained open generic is registered *after* one with constraints, then
            // that will win for any single-service Fetch.

            // Arrange
            ITargetContainer targets = new TargetContainer();
            var openTarget           = Target.ForType(typeof(Generic <>));
            var constrainedTarget    = Target.ForType(typeof(ConstrainedGeneric <>));

            targets.Register(openTarget, typeof(IGeneric <>));
            targets.Register(constrainedTarget, typeof(IGeneric <>));

            // Act
            var fetched = targets.Fetch(typeof(IGeneric <BaseClassChild>));
            // this should return both as they both apply
            var all = targets.FetchAll(typeof(IGeneric <BaseClassChild>));

            // Assert
            Assert.Same(constrainedTarget, fetched);
            Assert.Equal(new[] { openTarget, constrainedTarget }, all);
        }