/// <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)); }
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); }
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>()); }
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)); }