Example #1
0
        public void SelfReferencingConstrainedOpenGenericServicesCanBeResolved()
        {
            // Arrange
            var collection = new TestServiceCollection();

            collection.AddTransient(typeof(IFakeOpenGenericService <>), typeof(FakeOpenGenericService <>));
            collection.AddTransient(typeof(IFakeOpenGenericService <>), typeof(ClassWithSelfReferencingConstraint <>));
            var poco = new PocoClass();

            collection.AddSingleton(poco);
            var selfComparable = new ClassImplementingIComparable();

            collection.AddSingleton(selfComparable);
            var provider = CreateServiceProvider(collection);
            // Act
            var allServices         = provider.GetServices <IFakeOpenGenericService <ClassImplementingIComparable> >().ToList();
            var constrainedServices = provider.GetServices <IFakeOpenGenericService <PocoClass> >().ToList();

            // Assert
            Assert.Equal(2, allServices.Count);
            Assert.Same(selfComparable, allServices[0].Value);
            Assert.Same(selfComparable, allServices[1].Value);
            Assert.Equal(1, constrainedServices.Count);
            Assert.Same(poco, constrainedServices[0].Value);
        }
Example #2
0
        public void SelfReferencingConstrainedOpenGenericServicesCanBeResolved()
        {
            var container  = new StashboxContainer();
            var poco       = new PocoClass();
            var comparable = new ClassImplementingIComparable();

            container.Register(typeof(IFakeOpenGenericService <>), typeof(FakeOpenGenericService <>))
            .Register(typeof(IFakeOpenGenericService <>), typeof(ClassWithSelfReferencingConstraint <>))
            .RegisterInstance(poco)
            .RegisterInstance(comparable);

            var allServices         = container.ResolveAll <IFakeOpenGenericService <ClassImplementingIComparable> >().ToList();
            var constrainedServices = container.ResolveAll <IFakeOpenGenericService <PocoClass> >().ToList();

            Assert.AreEqual(2, allServices.Count);
            Assert.AreSame(comparable, allServices[0].Value);
            Assert.AreSame(comparable, allServices[1].Value);
            Assert.AreEqual(1, constrainedServices.Count);
            Assert.AreSame(poco, constrainedServices[0].Value);
        }