예제 #1
0
파일: Container.cs 프로젝트: csf-dev/FlexDi
        /// <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();
        }
예제 #2
0
        /// <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;
        }
예제 #3
0
        /// <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>();
        }
예제 #4
0
        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));
        }
예제 #5
0
        /// <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;
        }
예제 #6
0
        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));
        }
예제 #7
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));
        }
예제 #8
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);
        }
예제 #9
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>());
        }
예제 #10
0
        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);
        }
예제 #11
0
 /// <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)
 {
 }