Esempio n. 1
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 = Planner.GetPlan(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 => 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 => GetValue(context, target)).ToArray();

            return(directive.Injector(arguments));
        }
Esempio n. 2
0
        private Tuple <ConstructorInfo, ParameterInfo[]> GetCtor(Type concrete, IDictionary <string, object> arguments)
        {
            var constructor = new ConstructorScorer(concrete, arguments).GetConstructor();

            return(Tuple.Create(constructor, constructor.GetParameters()));
        }