/// <summary>
        /// Configure the component so that any properties whose types are registered in the
        /// container will be wired to instances of the appropriate service.
        /// </summary>
        /// <param name="wiringFlags">Set wiring options such as circular dependency wiring support.</param>
        /// <returns>A registration builder allowing further configuration of the component.</returns>
        public IRegistrationBuilder <TLimit, TActivatorData, TRegistrationStyle> PropertiesAutowired(PropertyWiringFlags wiringFlags)
        {
            var injector = new AutowiringPropertyInjector();

            var allowCircularDependencies = 0 != (int)(wiringFlags & PropertyWiringFlags.AllowCircularDependencies);
            var preserveSetValues         = 0 != (int)(wiringFlags & PropertyWiringFlags.PreserveSetValues);

            if (allowCircularDependencies)
            {
                RegistrationData.ActivatedHandlers.Add((s, e) => injector.InjectProperties(e.Context, e.Instance, !preserveSetValues));
            }
            else
            {
                RegistrationData.ActivatingHandlers.Add((s, e) => injector.InjectProperties(e.Context, e.Instance, !preserveSetValues));
            }

            return(this);
        }
        public IRegistrationBuilder <TLimit, TActivatorData, TRegistrationStyle> PropertiesAutowired(PropertyWiringFlags wiringFlags)
        {
            AutowiringPropertyInjector injector = new AutowiringPropertyInjector();
            bool flag = PropertyWiringFlags.Default != (wiringFlags & PropertyWiringFlags.AllowCircularDependencies);
            bool preserveSetValues = PropertyWiringFlags.Default != (wiringFlags & PropertyWiringFlags.PreserveSetValues);

            if (flag)
            {
                this.RegistrationData.ActivatedHandlers.Add(delegate(object s, ActivatedEventArgs <object> e) {
                    injector.InjectProperties(e.Context, e.Instance, !preserveSetValues);
                });
            }
            else
            {
                this.RegistrationData.ActivatingHandlers.Add(delegate(object s, ActivatingEventArgs <object> e) {
                    injector.InjectProperties(e.Context, e.Instance, !preserveSetValues);
                });
            }
            return(this);
        }
Example #3
0
        public void NullCheckTests()
        {
            var ctx              = new ContainerBuilder().Build();
            var instance         = new object();
            var propertySelector = new DefaultPropertySelector(true);
            var parameters       = new Parameter[0];

            Assert.Throws <ArgumentNullException>(() => AutowiringPropertyInjector.InjectProperties(null, instance, propertySelector, parameters));
            Assert.Throws <ArgumentNullException>(() => AutowiringPropertyInjector.InjectProperties(ctx, null, propertySelector, parameters));
            Assert.Throws <ArgumentNullException>(() => AutowiringPropertyInjector.InjectProperties(ctx, instance, null, parameters));
            Assert.Throws <ArgumentNullException>(() => AutowiringPropertyInjector.InjectProperties(ctx, instance, propertySelector, null));
        }
        /// <summary>
        /// Configure the component so that any properties whose types are registered in the
        /// container and follow specific criteria will be wired to instances of the appropriate service.
        /// </summary>
        /// <param name="propertySelector">Selector to determine which properties should be injected.</param>
        /// <param name="allowCircularDependencies">Determine if circular dependencies should be allowed or not.</param>
        /// <returns>A registration builder allowing further configuration of the component.</returns>
        public IRegistrationBuilder <TLimit, TActivatorData, TRegistrationStyle> PropertiesAutowired(IPropertySelector propertySelector, bool allowCircularDependencies)
        {
            if (allowCircularDependencies)
            {
                RegistrationData.ActivatedHandlers.Add((s, e) => AutowiringPropertyInjector.InjectProperties(e.Context, e.Instance, propertySelector, e.Parameters));
            }
            else
            {
                RegistrationData.ActivatingHandlers.Add((s, e) => AutowiringPropertyInjector.InjectProperties(e.Context, e.Instance, propertySelector, e.Parameters));
            }

            return(this);
        }
Example #5
0
 /// <summary>
 /// Set any null-valued properties on <paramref name="instance"/> that can be
 /// resolved by the container.
 /// </summary>
 /// <typeparam name="TService">Type of instance. Used only to provide method chaining.</typeparam>
 /// <param name="context">The context from which to resolve the service.</param>
 /// <param name="instance">The instance to inject properties into.</param>
 /// <returns><paramref name="instance"/>.</returns>
 public static TService InjectUnsetProperties <TService>(this IComponentContext context, TService instance)
 {
     AutowiringPropertyInjector.InjectProperties(context, instance, DefaultPropertySelector.PreserveSetValueInstance);
     return(instance);
 }
Example #6
0
 /// <summary>
 /// Set any properties on <paramref name="instance"/> that can be resolved by service and that satisfy the
 /// constraints imposed by <paramref name="propertySelector"/>
 /// </summary>
 /// <typeparam name="TService">Type of instance. Used only to provide method chaining.</typeparam>
 /// <param name="context">The context from which to resolve the service.</param>
 /// <param name="instance">The instance to inject properties into.</param>
 /// <param name="propertySelector">Selector to determine with properties should be injected.</param>
 /// <returns><paramref name="instance"/>.</returns>
 public static TService InjectProperties <TService>(this IComponentContext context, TService instance, IPropertySelector propertySelector)
 {
     AutowiringPropertyInjector.InjectProperties(context, instance, propertySelector);
     return(instance);
 }
Example #7
0
 /// <summary>
 /// Set any null-valued properties on <paramref name="instance"/> that can be
 /// resolved by the container.
 /// </summary>
 /// <typeparam name="TService">Type of instance. Used only to provide method chaining.</typeparam>
 /// <param name="context">The context from which to resolve the service.</param>
 /// <param name="instance">The instance to inject properties into.</param>
 /// <returns><paramref name="instance"/>.</returns>
 public static TService InjectUnsetProperties <TService>(this IComponentContext context, TService instance)
 {
     AutowiringPropertyInjector.InjectProperties(context, instance, false);
     return(instance);
 }