/// <summary> /// Initializes a new instance of the <see cref="T:CSF.FlexDi.Container"/> class. /// </summary> /// <remarks> /// <para> /// All of the parameters to this method are optional. Any which are not provided will be fulfilled using /// default implementations. /// </para> /// <para> /// Of the parameters which might be used, the most useful is likely to be <paramref name="options"/> and /// <paramref name="parentContainer"/>. /// </para> /// </remarks> /// <param name="registry">An optional service registry instance.</param> /// <param name="cache">An optional service cache instance.</param> /// <param name="resolver">An optional resolver instance.</param> /// <param name="disposer">An optional service disposer instance.</param> /// <param name="options">A set of container options.</param> /// <param name="parentContainer">An optional parent container - indicating that this container is the child of another.</param> /// <param name="resolverFactory">An optional resolver factory instance.</param> public Container(IRegistersServices registry = null, ICachesResolvedServiceInstances cache = null, IFulfilsResolutionRequests resolver = null, IDisposesOfResolvedInstances disposer = null, ContainerOptions options = null, IContainer parentContainer = null, ICreatesResolvers resolverFactory = null) { disposedValue = false; this.parentContainer = parentContainer; this.options = GetContainerOptions(options, parentContainer); constructorSelector = new ConstructorWithMostParametersSelector(this.options.UseNonPublicConstructors); this.registry = registry ?? new Registry(); this.cache = cache ?? new ResolvedServiceCache(); this.disposer = disposer ?? new ServiceInstanceDisposer(); this.registryStack = new RegistryStackFactory().CreateRegistryStack(this); this.resolver = resolver ?? GetResolver(resolverFactory); this.resolver.ServiceResolved += InvokeServiceResolved; PerformSelfRegistrations(); }
/// <summary> /// Initializes a new instance of the <see cref="ServiceWithoutRegistrationProvider"/> class. /// </summary> /// <param name="constructorSelector">Constructor selector.</param> public ServiceWithoutRegistrationProvider(ISelectsConstructor constructorSelector) { if (constructorSelector == null) { throw new ArgumentNullException(nameof(constructorSelector)); } this.constructorSelector = constructorSelector; }
/// <summary> /// Initializes a new instance of the <see cref="RegistrationHelper"/> class. /// </summary> /// <param name="constructorSelector">Constructor selector.</param> public RegistrationHelper(ISelectsConstructor constructorSelector) { if (constructorSelector == null) { throw new ArgumentNullException(nameof(constructorSelector)); } this.constructorSelector = constructorSelector; registrations = new List <IServiceRegistration>(); }
public void RegisterType_passes_constructor_selector([Frozen] ISelectsConstructor ctorSelector, RegistrationHelper sut) { // Act sut.RegisterType(typeof(SampleServiceImplementationOne)); // Assert var matchingReg = (TypeRegistration)sut.GetRegistrations().FirstOrDefault(x => x is TypeRegistration); Assert.That(matchingReg.ConstructorSelector, Is.SameAs(ctorSelector)); }
/// <summary> /// Initializes a new instance of the <see cref="TypeRegistration"/> class. /// </summary> /// <param name="implementationType">Implementation type.</param> /// <param name="constructorSelector">Constructor selector.</param> public TypeRegistration(Type implementationType, ISelectsConstructor constructorSelector) { if (constructorSelector == null) { throw new ArgumentNullException(nameof(constructorSelector)); } if (implementationType == null) { throw new ArgumentNullException(nameof(implementationType)); } this.implementationType = implementationType; this.constructorSelector = constructorSelector; }
public void GetFactoryAdapter_uses_constructor_selector(ISelectsConstructor ctorSelector, ResolutionRequest request) { // Arrange var type = typeof(SampleServiceWithConstructorParameters); var sut = new TypeRegistration(type, ctorSelector); var ctor = type.GetConstructor(new [] { typeof(string), typeof(string) }); Mock.Get(ctorSelector) .Setup(x => x.SelectConstructor(type)) .Returns(ctor); // 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)); }
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)); }
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 AssertIsValid_does_not_throw_when_implementation_type_derives_from_service_type(ISelectsConstructor ctorSelector) { // Arrange var sut = new TypeRegistration(typeof(SampleServiceImplementationOne), ctorSelector) { ServiceType = typeof(ISampleService), }; // Act & assert Assert.That(() => sut.AssertIsValid(), Throws.Nothing); }
/// <summary> /// Initializes a new instance of the <see cref="OpenGenericTypeRegistration"/> class. /// </summary> /// <param name="implementationType">Implementation type.</param> /// <param name="constructorSelector">Constructor selector.</param> public OpenGenericTypeRegistration(Type implementationType, ISelectsConstructor constructorSelector) : base(implementationType, constructorSelector) { }