public void ItShouldUpdateImplementationTypeIfSpecified()
        {
            // Act
            IGenerationContext context = _initialValue.Customize(implementationType: GetType());

            // Assert
            context.ConstructionContextDefinition.ImplementationType.Should().Be(GetType());
            context.ConstructionContextDefinition.ServiceType.Should().Be(typeof(void));
            context.ConstructionContextDefinition.RecipientType.Should().Be(typeof(void));
        }
예제 #2
0
        /// <inheritdoc/>
        public override string GetInstanceExpression(IGenerationContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            // Get the expressions for the all the constructor parameters.
            IEnumerable <IParameterExpression> expressions = GetParameterExpressions(context);
            IEnumerable <string> parameterExpressions      =
                from e in expressions
                let ctx = context.Customize(recipientType: Type, serviceType: e.Type)
                          select e.GetInstanceExpression(ctx);

            // Join the parameters expressions.
            string parameters =
                string.Join(
                    "," + Environment.NewLine + "    ",
                    parameterExpressions.Select(p => CodeGen.Indent(p)));

            // Create the new Expression.
            string expression = string.IsNullOrEmpty(parameters)
                ? $"new {Type.ToCompileName()}()"
                : $"new {Type.ToCompileName()}({Environment.NewLine}    {parameters})";

            return(expression);
        }
        /// <summary>
        /// Creates a customized <see cref="IGenerationContext"/> where the
        /// <see cref="IGenerationContext.ConstructionContextDefinition"/> is updated with the specified parameters.
        /// </summary>
        /// <param name="context">The <see cref="IGenerationContext"/> to customize.</param>
        /// <param name="implementationType">
        /// The <see cref="ConstructionContextDefinition.ImplementationType"/>.
        /// </param>
        /// <param name="serviceType">The <see cref="ConstructionContextDefinition.ServiceType"/>.</param>
        /// <param name="recipientType">The <see cref="ConstructionContextDefinition.RecipientType"/>.</param>
        /// <returns>
        /// A customized <see cref="IGenerationContext"/> where the
        /// <see cref="IGenerationContext.ConstructionContextDefinition"/> is updated with the specified parameters.
        /// </returns>
        public static IGenerationContext Customize(
            this IGenerationContext context,
            Type implementationType = null,
            Type serviceType        = null,
            Type recipientType      = null)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            implementationType = implementationType ?? context.ConstructionContextDefinition.ImplementationType;
            serviceType        = serviceType ?? context.ConstructionContextDefinition.ServiceType;
            recipientType      = recipientType ?? context.ConstructionContextDefinition.RecipientType;

            var definition = new ConstructionContextDefinition(implementationType, serviceType, recipientType);
            IGenerationContext customization = context.Customize(definition);

            return(customization);
        }