Exemplo n.º 1
0
        private ConstructorInjectionDirective DetermineConstructorInjectionDirective(IContext context)
        {
            var directives = context.Plan.GetAll <ConstructorInjectionDirective>().ToList();

            if (directives.Count == 0)
            {
                throw new ActivationException(ExceptionFormatter.NoConstructorsAvailable(context));
            }

            if (directives.Count == 1)
            {
                return(directives[0]);
            }

            var bestDirectives =
                directives
                .GroupBy(directive => this.ConstructorScorer.Score(context, directive))
                .OrderByDescending(g => g.Key)
                .First()
                .ToList();

            if (bestDirectives.Count > 1)
            {
                throw new ActivationException(ExceptionFormatter.ConstructorsAmbiguous(context, bestDirectives));
            }

            return(bestDirectives[0]);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates an instance within the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns>The created instance.</returns>
        public virtual object Create(IContext context)
        {
            Ensure.ArgumentNotNull(context, "context");

            if (context.Plan == null)
            {
                context.Plan = this.Planner.GetPlan(this.GetImplementationType(context.Request.Service));
            }

            if (!context.Plan.Has <ConstructorInjectionDirective>())
            {
                throw new ActivationException(ExceptionFormatter.NoConstructorsAvailable(context));
            }

            var directives     = context.Plan.GetAll <ConstructorInjectionDirective>();
            var bestDirectives = directives
                                 .GroupBy(option => this.ConstructorScorer.Score(context, option))
                                 .OrderByDescending(g => g.Key)
                                 .First();

            if (bestDirectives.Skip(1).Any())
            {
                throw new ActivationException(ExceptionFormatter.ConstructorsAmbiguous(context, bestDirectives));
            }

            var directive = bestDirectives.Single();
            var arguments = directive.Targets.Select(target => this.GetValue(context, target)).ToArray();

            return(directive.Injector(arguments));
        }
Exemplo n.º 3
0
        private ConstructorInjectionDirective DetermineConstructorInjectionDirective(IContext context)
        {
            var directives = context.Plan.ConstructorInjectionDirectives;

            if (directives.Count == 1)
            {
                return(directives[0]);
            }
            IGrouping <int, ConstructorInjectionDirective> bestDirectives =
                directives
                .GroupBy(option => this.ConstructorScorer.Score(context, option))
                .OrderByDescending(g => g.Key)
                .FirstOrDefault();

            if (bestDirectives == null)
            {
                throw new ActivationException(ExceptionFormatter.NoConstructorsAvailable(context));
            }

            return(bestDirectives.SingleOrThrowException(
                       () => new ActivationException(ExceptionFormatter.ConstructorsAmbiguous(context, bestDirectives))));
        }