Esempio n. 1
0
        public void CreateFromFactory_executes_factory_with_resolved_parameters([Frozen] IFulfilsResolutionRequests resolver,
                                                                                InstanceCreator sut,
                                                                                IFactoryAdapter factory,
                                                                                ResolutionPath path,
                                                                                IServiceRegistration registration,
                                                                                object resolved,
                                                                                [MockParam(typeof(string), Name = "Foo")] ParameterInfo paramOne,
                                                                                object paramOneValue,
                                                                                [MockParam(typeof(int), Name = "Bar")] ParameterInfo paramTwo,
                                                                                object paramTwoValue)
        {
            // Arrange
            Mock.Get(factory).SetupGet(x => x.RequiresParameterResolution).Returns(true);
            Mock.Get(factory).Setup(x => x.GetParameters()).Returns(new [] { paramOne, paramTwo });
            Mock.Get(resolver)
            .Setup(x => x.Resolve(It.Is <ResolutionRequest>(r => r.Name == paramOne.Name && r.ServiceType == paramOne.ParameterType)))
            .Returns(() => ResolutionResult.Success(path, paramOneValue));
            Mock.Get(resolver)
            .Setup(x => x.Resolve(It.Is <ResolutionRequest>(r => r.Name == paramTwo.Name && r.ServiceType == paramTwo.ParameterType)))
            .Returns(() => ResolutionResult.Success(path, paramTwoValue));
            Mock.Get(factory)
            .Setup(x => x.Execute(It.Is <object[]>(e => e.Length == 2 && e[0] == paramOneValue && e[1] == paramTwoValue)))
            .Returns(resolved);

            // Act
            var result = sut.CreateFromFactory(factory, path, registration);

            // Assert
            Mock.Get(factory)
            .Verify(x => x.Execute(It.Is <object[]>(e => e.Length == 2 && e[0] == paramOneValue && e[1] == paramTwoValue)), Times.Once);
            Assert.That(result, Is.SameAs(resolved));
        }
Esempio n. 2
0
        /// <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();
        }
Esempio n. 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:CSF.FlexDi.Resolution.InstanceCreator"/> class.
        /// </summary>
        /// <param name="resolver">A service which fulfils resolution requests.</param>
        public InstanceCreator(IFulfilsResolutionRequests resolver)
        {
            if (resolver == null)
            {
                throw new ArgumentNullException(nameof(resolver));
            }

            this.resolver = resolver;
        }
Esempio n. 4
0
        public void CreateFromFactory_executes_factory_if_it_does_not_need_parameter_resolution([Frozen] IFulfilsResolutionRequests resolver,
                                                                                                InstanceCreator sut,
                                                                                                IFactoryAdapter factory,
                                                                                                ResolutionPath path,
                                                                                                IServiceRegistration registration,
                                                                                                object resolved)
        {
            // Arrange
            Mock.Get(factory).SetupGet(x => x.RequiresParameterResolution).Returns(false);
            Mock.Get(factory).Setup(x => x.Execute(It.Is <object[]>(e => e.Length == 0))).Returns(resolved);

            // Act
            var result = sut.CreateFromFactory(factory, path, registration);

            // Assert
            Mock.Get(factory).Verify(x => x.Execute(It.Is <object[]>(e => e.Length == 0)), Times.Once);
            Assert.That(result, Is.SameAs(resolved));
        }