Exemplo n.º 1
0
        public void AbstractClassConstrainedOpenGenericServicesCanBeResolved()
        {
            // Arrange
            var collection = new TestServiceCollection();

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

            collection.AddSingleton(poco);
            var classInheritingClassInheritingAbstractClass = new ClassInheritingClassInheritingAbstractClass();

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

            // Assert
            Assert.Equal(2, allServices.Count);
            Assert.Same(classInheritingClassInheritingAbstractClass, allServices[0].Value);
            Assert.Same(classInheritingClassInheritingAbstractClass, allServices[1].Value);
            Assert.Equal(1, constrainedServices.Count);
            Assert.Same(poco, constrainedServices[0].Value);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
        public void ConstrainedOpenGenericServicesCanBeResolved()
        {
            // Arrange
            var collection = new TestServiceCollection();

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

            collection.AddSingleton(poco);
            collection.AddSingleton <IFakeSingletonService, FakeService>();
            var provider = CreateServiceProvider(collection);
            // Act
            var allServices         = provider.GetServices <IFakeOpenGenericService <PocoClass> >().ToList();
            var constrainedServices = provider.GetServices <IFakeOpenGenericService <IFakeSingletonService> >().ToList();
            var singletonService    = provider.GetService <IFakeSingletonService>();

            // Assert
            Assert.Equal(2, allServices.Count);
            Assert.Same(poco, allServices[0].Value);
            Assert.Same(poco, allServices[1].Value);
            Assert.Equal(1, constrainedServices.Count);
            Assert.Same(singletonService, constrainedServices[0].Value);
        }