public void InjectProperties(IComponentContext context, object instance, bool overrideSetValues)
		{
			if (context == null) throw new ArgumentNullException("context");
			if (instance == null) throw new ArgumentNullException("instance");

			var instanceType = instance.GetType();

			foreach (var property in instanceType.GetProperties(
				BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty))
			{
				var propertyType = property.PropertyType;

				if (propertyType.IsValueType && !propertyType.IsEnum)
					continue;

				if (property.GetIndexParameters().Length != 0)
					continue;

				if (!context.IsRegistered(propertyType))
					continue;

				var accessors = property.GetAccessors(false);
				if (accessors.Length == 1 && accessors[0].ReturnType != typeof(void))
					continue;

				if (!overrideSetValues &&
					accessors.Length == 2 &&
					(property.GetValue(instance, null) != null))
					continue;

				IComponentRegistration registration;
				var service = new TypedService(propertyType);
				if (!context.ComponentRegistry.TryGetRegistration(service, out registration))
					throw new ComponentNotRegisteredException(service);

				var lookup = context.ResolveLookup(service, registration, Enumerable.Empty<Parameter>());
				try
				{
					if (lookup.Preparing)
						lookup.SharedInstanceActivation += (s, ea) => property.SetValue(instance, s, null);
					else
					{
						var propertyValue = lookup.Factory();
						property.SetValue(instance, propertyValue, null);
					}
				}
				catch (DependencyResolutionException dre)
				{
					dre.Lookups.Push(lookup);
					throw;
				}
			}
		}
Esempio n. 2
0
        /// <summary>
        /// Resolve an instance of the provided registration within the context.
        /// </summary>
        /// <param name="context">provided context</param>
        /// <param name="service">Service which is trying to be resolved</param>
        /// <param name="registration">The registration.</param>
        /// <param name="parameters">Parameters for the instance.</param>
        /// <returns>
        /// The component instance.
        /// </returns>
        /// <exception cref="ComponentNotRegisteredException"/>
        /// <exception cref="Revenj.Extensibility.Autofac.Core.DependencyResolutionException"/>
        public static object ResolveComponent(this IComponentContext context, Service service, IComponentRegistration registration, IEnumerable <Parameter> parameters)
        {
            var lookup = context.ResolveLookup(service, registration, parameters);

            try
            {
                return(lookup.Factory());
            }
            catch (DependencyResolutionException dre)
            {
                dre.Lookups.Push(lookup);
                throw;
            }
        }
Esempio n. 3
0
		/// <summary>
		/// Returns true if the parameter is able to provide a value to a particular site.
		/// </summary>
		/// <param name="pi">Constructor, method, or property-mutator parameter.</param>
		/// <param name="context">The component context in which the value is being provided.</param>
		/// <param name="valueProvider">If the result is true, the valueProvider parameter will
		/// be set to a function that will lazily retrieve the parameter value. If the result is false,
		/// will be set to null.</param>
		/// <returns>True if a value can be supplied; otherwise, false.</returns>
		public override bool CanSupplyValue(ParameterInfo pi, IComponentContext context, out Func<object> valueProvider)
		{
			IComponentRegistration registration;
			var ts = new TypedService(pi.ParameterType);
			if (context.ComponentRegistry.TryGetRegistration(ts, out registration))
			{
				var lookup = context.ResolveLookup(ts, registration, Enumerable.Empty<Parameter>());
				try
				{
					valueProvider = lookup.Factory;
				}
				catch (DependencyResolutionException dre)
				{
					dre.Lookups.Push(lookup);
					throw;
				}
				return true;
			}
			valueProvider = null;
			return false;
		}
Esempio n. 4
0
        /// <summary>
        /// Returns true if the parameter is able to provide a value to a particular site.
        /// </summary>
        /// <param name="pi">Constructor, method, or property-mutator parameter.</param>
        /// <param name="context">The component context in which the value is being provided.</param>
        /// <param name="valueProvider">If the result is true, the valueProvider parameter will
        /// be set to a function that will lazily retrieve the parameter value. If the result is false,
        /// will be set to null.</param>
        /// <returns>True if a value can be supplied; otherwise, false.</returns>
        public override bool CanSupplyValue(ParameterInfo pi, IComponentContext context, out Func <object> valueProvider)
        {
            IComponentRegistration registration;
            var ts = new TypedService(pi.ParameterType);

            if (context.ComponentRegistry.TryGetRegistration(ts, out registration))
            {
                var lookup = context.ResolveLookup(ts, registration, Enumerable.Empty <Parameter>());
                try
                {
                    valueProvider = lookup.Factory;
                }
                catch (DependencyResolutionException dre)
                {
                    dre.Lookups.Push(lookup);
                    throw;
                }
                return(true);
            }
            valueProvider = null;
            return(false);
        }
Esempio n. 5
0
        /// <summary>
        /// Try to retrieve a service from the context.
        /// </summary>
        /// <param name="context">The context from which to resolve the service.</param>
        /// <param name="service">The service to resolve.</param>
        /// <param name="instance">The resulting component instance providing the service, or null.</param>
        /// <param name="parameters">The parameters.</param>
        /// <returns>
        /// True if a component providing the service is available.
        /// </returns>
        /// <exception cref="DependencyResolutionException"/>
        public static bool TryResolveService(this IComponentContext context, Service service, IEnumerable <Parameter> parameters, out object instance)
        {
            IComponentRegistration registration;

            if (!context.ComponentRegistry.TryGetRegistration(service, out registration))
            {
                instance = null;
                return(false);
            }

            var lookup = context.ResolveLookup(service, registration, parameters);

            try
            {
                instance = lookup.Factory();
            }
            catch (DependencyResolutionException dre)
            {
                dre.Lookups.Push(lookup);
                throw;
            }
            return(true);
        }
Esempio n. 6
0
        public void InjectProperties(IComponentContext context, object instance, bool overrideSetValues)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            var instanceType = instance.GetType();

            foreach (var property in instanceType.GetProperties(
                         BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty))
            {
                var propertyType = property.PropertyType;

                if (propertyType.IsValueType && !propertyType.IsEnum)
                {
                    continue;
                }

                if (property.GetIndexParameters().Length != 0)
                {
                    continue;
                }

                if (!context.IsRegistered(propertyType))
                {
                    continue;
                }

                var accessors = property.GetAccessors(false);
                if (accessors.Length == 1 && accessors[0].ReturnType != typeof(void))
                {
                    continue;
                }

                if (!overrideSetValues &&
                    accessors.Length == 2 &&
                    (property.GetValue(instance, null) != null))
                {
                    continue;
                }

                IComponentRegistration registration;
                var service = new TypedService(propertyType);
                if (!context.ComponentRegistry.TryGetRegistration(service, out registration))
                {
                    throw new ComponentNotRegisteredException(service);
                }

                var lookup = context.ResolveLookup(service, registration, Enumerable.Empty <Parameter>());
                try
                {
                    if (lookup.Preparing)
                    {
                        lookup.SharedInstanceActivation += (s, ea) => property.SetValue(instance, s, null);
                    }
                    else
                    {
                        var propertyValue = lookup.Factory();
                        property.SetValue(instance, propertyValue, null);
                    }
                }
                catch (DependencyResolutionException dre)
                {
                    dre.Lookups.Push(lookup);
                    throw;
                }
            }
        }