Пример #1
0
        /*----------------------------------------------------------------------------------------*/
        /// <summary>
        /// Executed when the instance is being initialized.
        /// </summary>
        /// <param name="context">The activation context.</param>
        /// <returns>A value indicating whether to proceed or stop the execution of the strategy chain.</returns>
        public override StrategyResult Initialize(IContext context)
        {
            IList <FieldInjectionDirective> directives = context.Plan.Directives.GetAll <FieldInjectionDirective>();

            if (directives.Count > 0)
            {
                var contextFactory  = context.Binding.Components.ContextFactory;
                var injectorFactory = context.Binding.Components.InjectorFactory;
                var converter       = context.Binding.Components.Converter;

                foreach (FieldInjectionDirective directive in directives)
                {
                    // Create a new context in which the field's value will be activated.
                    IContext injectionContext = contextFactory.CreateChild(context,
                                                                           directive.Member, directive.Target, directive.Argument.Optional);

                    // Resolve the value to inject into the field.
                    object value = directive.Argument.Resolver.Resolve(context, injectionContext);

                    // Convert the value if necessary.
                    if (!converter.Convert(value, directive.Target.Type, out value))
                    {
                        throw new ActivationException(ExceptionFormatter.CouldNotConvertValueForInjection(context, directive.Target, value));
                    }

                    // Get an injector that can inject the value.
                    IFieldInjector injector = injectorFactory.GetInjector(directive.Member);

                    // Inject the value.
                    injector.Set(context.Instance, value);
                }
            }

            return(StrategyResult.Proceed);
        }
Пример #2
0
        /*----------------------------------------------------------------------------------------*/
        private static object[] ResolveArguments(IContext context, MethodInjectionDirective directive)
        {
            var contextFactory = context.Binding.Components.ContextFactory;
            var converter      = context.Binding.Components.Converter;

            var arguments = new object[directive.Arguments.Count];

            int index = 0;

            foreach (Argument argument in directive.Arguments)
            {
                // Create a new context in which the parameter's value will be activated.
                IContext injectionContext = contextFactory.CreateChild(context,
                                                                       directive.Member, argument.Target, argument.Optional);

                // Resolve the value to inject for the parameter.
                object value = argument.Resolver.Resolve(context, injectionContext);

                // Convert the value if necessary.
                if (!converter.Convert(value, argument.Target.Type, out value))
                {
                    throw new ActivationException(ExceptionFormatter.CouldNotConvertValueForInjection(context, argument.Target, value));
                }

                arguments[index] = value;
                index++;
            }

            return(arguments);
        }
        /*----------------------------------------------------------------------------------------*/
        /// <summary>
        /// Executed when the instance is being initialized.
        /// </summary>
        /// <param name="context">The activation context.</param>
        /// <returns>A value indicating whether to proceed or stop the execution of the strategy chain.</returns>
        public override StrategyResult Initialize(IContext context)
        {
            IList <PropertyInjectionDirective> directives = context.Plan.Directives.GetAll <PropertyInjectionDirective>();

            if (directives.Count > 0)
            {
                var contextFactory  = context.Binding.Components.ContextFactory;
                var injectorFactory = context.Binding.Components.InjectorFactory;
                var converter       = context.Binding.Components.Converter;

                foreach (PropertyInjectionDirective directive in directives)
                {
                    // First, try to get the value from a context parameter.
                    object value = context.Parameters.GetValueOf <PropertyValueParameter>(directive.Target.Name, context);

                    // Next, try to get the value from a binding parameter.
                    if (value == null)
                    {
                        value = context.Binding.Parameters.GetValueOf <PropertyValueParameter>(directive.Target.Name, context);
                    }

                    // If no overrides have been declared, activate a service of the proper type to use as the value.
                    if (value == null)
                    {
                        // Create a new context in which the property's value will be activated.
                        IContext injectionContext = contextFactory.CreateChild(context,
                                                                               directive.Member, directive.Target, directive.Argument.Optional);

                        // Resolve the value to inject into the property.
                        value = directive.Argument.Resolver.Resolve(context, injectionContext);
                    }

                    // Convert the value if necessary.
                    if (!converter.Convert(value, directive.Target.Type, out value))
                    {
                        throw new ActivationException(ExceptionFormatter.CouldNotConvertValueForInjection(context, directive.Target, value));
                    }

                    // Get an injector that can set the property's value.
                    IPropertyInjector injector = injectorFactory.GetInjector(directive.Member);

                    // Inject the value.
                    injector.Set(context.Instance, value);
                }
            }

            return(StrategyResult.Proceed);
        }
Пример #4
0
        /*----------------------------------------------------------------------------------------*/
        /// <summary>
        /// Resolves the arguments for the constructor defined by the specified constructor injection
        /// directive.
        /// </summary>
        /// <param name="context">The context in which the activation is occurring.</param>
        /// <param name="directive">The directive describing the injection constructor.</param>
        /// <returns>An array of arguments that can be passed to the constructor.</returns>
        protected virtual object[] ResolveConstructorArguments(IContext context, ConstructorInjectionDirective directive)
        {
            var contextFactory = context.Binding.Components.ContextFactory;
            var converter      = context.Binding.Components.Converter;

            var arguments = new object[directive.Arguments.Count];

            int index = 0;

            foreach (Argument argument in directive.Arguments)
            {
                // First, try to get the value from a context parameter.
                object value = context.Parameters.GetValueOf <ConstructorArgumentParameter>(argument.Target.Name, context);

                // Next, try to get the value from a binding parameter.
                if (value == null)
                {
                    value = context.Binding.Parameters.GetValueOf <ConstructorArgumentParameter>(argument.Target.Name, context);
                }

                // If no overrides have been declared, activate a service of the proper type to use as the value.
                if (value == null)
                {
                    // Create a new context in which the parameter's value will be activated.
                    IContext injectionContext = contextFactory.CreateChild(context, directive.Member,
                                                                           argument.Target, argument.Optional);

                    // Resolve the value to inject for the parameter.
                    value = argument.Resolver.Resolve(context, injectionContext);
                }

                // Convert the value if necessary.
                if (!converter.Convert(value, argument.Target.Type, out value))
                {
                    throw new ActivationException(ExceptionFormatter.CouldNotConvertValueForInjection(context, argument.Target, value));
                }

                arguments[index++] = value;
            }

            return(arguments);
        }