Exemplo n.º 1
0
        /// <summary>
        /// Registers a component by its type.  The type used here is the type which will be instantiated when the component
        /// is resolved: The implementation of your component.
        /// </summary>
        /// <returns>A builder instance.</returns>
        /// <param name="concreteType">The implementation type of the component to be registered.</param>
        public IAsBuilderWithCacheability RegisterType(Type concreteType)
        {
            if (concreteType == null)
            {
                throw new ArgumentNullException(nameof(concreteType));
            }

            ServiceRegistration registration;

            if (concreteType.GetTypeInfo().IsGenericTypeDefinition)
            {
                registration = new OpenGenericTypeRegistration(concreteType, constructorSelector)
                {
                    ServiceType = concreteType
                };
            }
            else
            {
                registration = new TypeRegistration(concreteType, constructorSelector)
                {
                    ServiceType = concreteType
                };
            }

            registrations.Add(registration);
            return(new RegistrationBuilder(registration));
        }
Exemplo n.º 2
0
        public void AssertIsValid_does_not_throw_when_implementation_type_is_same_as_service_type(ISelectsConstructor ctorSelector)
        {
            // Arrange
            var sut = new OpenGenericTypeRegistration(typeof(GenericService <>), ctorSelector)
            {
                ServiceType = typeof(GenericService <>),
            };

            // Act & assert
            Assert.That(() => sut.AssertIsValid(), Throws.Nothing);
        }
Exemplo n.º 3
0
        public void AssertIsValid_throws_exception_if_implementation_type_does_not_derive_from_service_type(ISelectsConstructor ctorSelector)
        {
            // Arrange
            var sut = new OpenGenericTypeRegistration(typeof(GenericService <>), ctorSelector)
            {
                ServiceType = typeof(IOtherGenericService <>),
            };

            // Act & assert
            Assert.That(() => sut.AssertIsValid(), Throws.InstanceOf <InvalidTypeRegistrationException>());
        }
Exemplo n.º 4
0
        public void GetFactoryAdapter_uses_constructor_selector(ISelectsConstructor ctorSelector)
        {
            // Arrange
            var genericType = typeof(GenericService <>);
            var closedType  = typeof(GenericService <string>);
            var sut         = new OpenGenericTypeRegistration(genericType, ctorSelector);
            var ctor        = closedType.GetConstructor(Type.EmptyTypes);

            Mock.Get(ctorSelector)
            .Setup(x => x.SelectConstructor(closedType))
            .Returns(ctor);
            var request = new ResolutionRequest(closedType);

            // Act
            var result = sut.GetFactoryAdapter(request);

            // Assert
            Assert.That(result, Is.Not.Null);
            Assert.That(result, Is.InstanceOf <ConstructorFactory>());
            var ctorFactory = (ConstructorFactory)result;

            Assert.That(ctorFactory.Constructor, Is.SameAs(ctor));
        }