public static void InjectProperties(IComponentContext context, object instance, IPropertySelector propertySelector)
        {
            if (context == null) throw new ArgumentNullException(nameof(context));
            if (instance == null) throw new ArgumentNullException(nameof(instance));
            if (propertySelector == null) throw new ArgumentNullException(nameof(propertySelector));

            var instanceType = instance.GetType();

            foreach (var property in instanceType
                .GetRuntimeProperties()
                .Where(pi => pi.CanWrite))
            {
                var propertyType = property.PropertyType;

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

                if (propertyType.IsArray && propertyType.GetElementType().GetTypeInfo().IsValueType)
                    continue;

                if (propertyType.IsGenericEnumerableInterfaceType() && propertyType.GetTypeInfo().GenericTypeArguments[0].GetTypeInfo().IsValueType)
                    continue;

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

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

                if (!propertySelector.InjectProperty(property, instance))
                    continue;

                var propertyValue = context.Resolve(propertyType, new NamedParameter(InstanceTypeNamedParameter, instanceType));
                property.SetValue(instance, propertyValue, null);
            }
        }
Ejemplo n.º 2
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);
 }
        /// <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);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="PropertyMapperDelegateBuilder{T}"/> class.
 /// </summary>
 /// <param name="methodSkeleton">
 /// A <see cref="IMethodSkeleton{T}"/> implementation that
 /// represents the method skeleton for which to emit the method body.
 /// </param>
 /// <param name="methodSelector">
 /// The <see cref="IMethodSelector"/> implementation that is responsible for providing a get method
 /// that targets an <see cref="IDataRecord"/> instance.
 /// </param>
 /// <param name="propertySelector">
 /// A <see cref="IPropertySelector"/> that is responsible for selecting the properties that will have its
 /// value read from an <see cref="IDataRecord"/> instance.
 /// </param>
 public PropertyMapperDelegateBuilder(IMethodSkeleton <T> methodSkeleton, IMethodSelector methodSelector, IPropertySelector propertySelector)
     : base(methodSkeleton, methodSelector)
 {
     this.propertySelector = propertySelector;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ManyToOneExpressionBuilder"/> class.
 /// </summary>
 /// <param name="propertyMapper">The <see cref="IPropertyMapper"/> instance that is responsible for
 /// mapping <see cref="IDataRecord"/> fields to properties.</param>
 /// <param name="complexPropertySelector">The <see cref="IPropertySelector"/> instance that is responsible for selecting
 /// complex properties from a given <see cref="Type"/>.</param>
 /// <param name="dataRecordMapperFactory">A function delegate used to create <see cref="DataRecordMapper{T}"/></param>
 public ManyToOneExpressionBuilder(IPropertyMapper propertyMapper, IPropertySelector complexPropertySelector, Func <Type, object> dataRecordMapperFactory)
 {
     this.propertyMapper          = propertyMapper;
     this.complexPropertySelector = complexPropertySelector;
     this.dataRecordMapperFactory = dataRecordMapperFactory;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="CachedPropertySelector"/> class.
 /// </summary>
 /// <param name="propertySelector">
 /// The <see cref="IPropertySelector"/> responsible for selecting a set of properties from a given <see cref="Type"/>.
 /// </param>
 public CachedPropertySelector(IPropertySelector propertySelector)
 {
     this.propertySelector = propertySelector;
 }
Ejemplo n.º 7
0
		/// <summary>
		/// Set the <see cref="IPropertySelector"/> for this <see cref="Example"/>.
		/// </summary>
		/// <param name="selector">The <see cref="IPropertySelector"/> to determine which properties to include.</param>
		/// <returns>This <see cref="Example"/> instance.</returns>
		/// <remarks>
		/// This should be used when a custom <see cref="IPropertySelector"/> has
		/// been implemented.  Otherwise use the methods <see cref="Example.ExcludeNulls"/> 
		/// or <see cref="Example.ExcludeNone"/> to set the <see cref="IPropertySelector"/>
		/// to the <see cref="IPropertySelector"/>s built into NHibernate.
		/// </remarks>
		public Example SetPropertySelector(IPropertySelector selector)
		{
			_selector = selector;
			return this;
		}
Ejemplo n.º 8
0
 /// <summary>
 /// Initialize a new instance of the <see cref="Example" /> class for a particular
 /// entity.
 /// </summary>
 /// <param name="entity">The <see cref="Object"/> that the Example is being built from.</param>
 /// <param name="selector">The <see cref="IPropertySelector"/> the Example should use.</param>
 protected Example(object entity, IPropertySelector selector)
 {
     _entity   = entity;
     _selector = selector;
 }
Ejemplo n.º 9
0
        MyPropertiesAutowired <TLimit, TActivatorData, TRegistrationStyle>(
            this IRegistrationBuilder <TLimit, TActivatorData, TRegistrationStyle> registration, PropertyWiringOptions wiringFlags = PropertyWiringOptions.None, IPropertySelector propertySelector = null)
        {
            var preserveSetValues         = (int)(wiringFlags & PropertyWiringOptions.PreserveSetValues) != 0;
            var allowCircularDependencies = (int)(wiringFlags & PropertyWiringOptions.AllowCircularDependencies) != 0;

            if (propertySelector == null)
            {
                propertySelector = new DefaultPropertySelector(preserveSetValues);
            }
            if (allowCircularDependencies)
            {
                registration.RegistrationData.ActivatedHandlers.Add((s, e) => AutowiringPropertyInjector.InjectProperties(e.Context, e.Instance, propertySelector, e.Parameters));
            }
            else
            {
                registration.RegistrationData.ActivatingHandlers.Add((s, e) => AutowiringPropertyInjector.InjectProperties(e.Context, e.Instance, propertySelector, e.Parameters));
            }

            return(registration);
            //return registration.PropertiesAutowired(new DefaultPropertySelector(preserveSetValues), allowCircularDependencies);
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PropertyMapper"/> class.
 /// </summary>
 /// <param name="propertySelector">The <see cref="IPropertySelector"/> that is responsible for selecting the 
 /// target properties for a given <see cref="Type"/>.</param>
 /// <param name="columnSelector">The <see cref="IColumnSelector"/> that is responsible for selecting column names 
 /// and column ordinals from a given <see cref="IDataRecord"/>.</param>
 public PropertyMapper(IPropertySelector propertySelector, IColumnSelector columnSelector)
 {
     this.propertySelector = propertySelector;
     this.columnSelector = columnSelector;
 }
Ejemplo n.º 11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CachedPropertySelector"/> class.
 /// </summary>
 /// <param name="propertySelector">
 /// The <see cref="IPropertySelector"/> responsible for selecting a set of properties from a given <see cref="Type"/>.
 /// </param>
 public CachedPropertySelector(IPropertySelector propertySelector)
 {
     this.propertySelector = propertySelector;
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PropertyMapper"/> class.
 /// </summary>
 /// <param name="propertySelector">The <see cref="IPropertySelector"/> that is responsible for selecting the
 /// target properties for a given <see cref="Type"/>.</param>
 /// <param name="columnSelector">The <see cref="IColumnSelector"/> that is responsible for selecting column names
 /// and column ordinals from a given <see cref="IDataRecord"/>.</param>
 public PropertyMapper(IPropertySelector propertySelector, IColumnSelector columnSelector)
 {
     this.propertySelector = propertySelector;
     this.columnSelector   = columnSelector;
 }
Ejemplo n.º 13
0
 public QualifiedProperties()
 {
     Path       = PathDefinition.Empty;
     Properties = new IPropertySelector[0];
 }
        /// <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)
        {
            ResolvePipeline.Use(nameof(PropertiesAutowired), PipelinePhase.Activation, (ctxt, next) =>
            {
                // Continue down the pipeline.
                next(ctxt);

                if (!ctxt.NewInstanceActivated)
                {
                    return;
                }

                if (allowCircularDependencies)
                {
                    var capturedInstance = ctxt.Instance;

                    // If we are allowing circular deps, then we need to run when all requests have completed (similar to Activated).
                    ctxt.RequestCompleting += (o, args) =>
                    {
                        var evCtxt = args.RequestContext;
                        AutowiringPropertyInjector.InjectProperties(evCtxt, capturedInstance !, propertySelector, evCtxt.Parameters);
                    };
Ejemplo n.º 15
0
        public static void InjectProperties(IComponentContext context, object instance, IPropertySelector propertySelector, IEnumerable <Parameter> parameters)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }

            if (propertySelector == null)
            {
                throw new ArgumentNullException(nameof(propertySelector));
            }

            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            var resolveParameters = parameters as Parameter[] ?? parameters.ToArray();

            var instanceType         = instance.GetType();
            var injectableProperties = InjectableProperties.GetOrAdd(instanceType, type => GetInjectableProperties(type).ToArray());

            for (var index = 0; index < injectableProperties.Length; index++)
            {
                var property = injectableProperties[index];

                if (!propertySelector.InjectProperty(property, instance))
                {
                    continue;
                }

                var setParameter  = property.SetMethod.GetParameters()[0];
                var valueProvider = (Func <object>)null;
                var parameter     = resolveParameters.FirstOrDefault(p => p.CanSupplyValue(setParameter, context, out valueProvider));
                if (parameter != null)
                {
                    var setter = PropertySetters.GetOrAdd(property, MakeFastPropertySetter);
                    setter(instance, valueProvider());
                    continue;
                }

                var propertyService       = new TypedService(property.PropertyType);
                var instanceTypeParameter = new NamedParameter(InstanceTypeNamedParameter, instanceType);
                if (context.TryResolveService(propertyService, new Parameter[] { instanceTypeParameter }, out var propertyValue))
                {
                    var setter = PropertySetters.GetOrAdd(property, MakeFastPropertySetter);
                    setter(instance, propertyValue);
                }
            }
        }
Ejemplo n.º 16
0
        public static void InjectProperties(IComponentContext context, object instance, IPropertySelector propertySelector, IEnumerable <Parameter> parameters)
        {
            //判断instance是否是代理类型,如果是代理类型,则取Target
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }

            if (propertySelector == null)
            {
                throw new ArgumentNullException(nameof(propertySelector));
            }

            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }
            var unproxyedInstance     = UnwrapProxy(instance);
            var instanceType          = instance.GetType();
            var unproxyedInstanceType = unproxyedInstance.GetType();

            foreach (var property in unproxyedInstanceType
                     .GetRuntimeProperties()
                     .Where(pi => pi.CanWrite))
            {
                var propertyType = property.PropertyType;

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

                if (propertyType.IsArray && propertyType.GetElementType().GetTypeInfo().IsValueType)
                {
                    continue;
                }

                if (propertyType.IsGenericEnumerableInterfaceType() && propertyType.GetTypeInfo().GenericTypeArguments[0].GetTypeInfo().IsValueType)
                {
                    continue;
                }

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

                if (!propertySelector.InjectProperty(property, instance))
                {
                    continue;
                }

                var setParameter  = property.SetMethod.GetParameters().First();
                var valueProvider = (Func <object>)null;
                var parameter     = parameters.FirstOrDefault(p => p.CanSupplyValue(setParameter, context, out valueProvider));
                if (parameter != null)
                {
                    property.SetValue(unproxyedInstance, valueProvider(), null);
                    continue;
                }

                object propertyValue;
                var    propertyService       = new TypedService(propertyType);
                var    instanceTypeParameter = new NamedParameter(InstanceTypeNamedParameter, unproxyedInstanceType);
                if (context.TryResolveService(propertyService, new Parameter[] { instanceTypeParameter }, out propertyValue))
                {
                    property.SetValue(unproxyedInstance, propertyValue, null);
                }
            }
        }
Ejemplo n.º 17
0
 /// <summary>
 /// Set the <see cref="IPropertySelector"/> for this <see cref="Example"/>.
 /// </summary>
 /// <param name="selector">The <see cref="IPropertySelector"/> to determine which properties to include.</param>
 /// <returns>This <see cref="Example"/> instance.</returns>
 /// <remarks>
 /// This should be used when a custom <see cref="IPropertySelector"/> has
 /// been implemented.  Otherwise use the methods <see cref="Example.ExcludeNulls"/>
 /// or <see cref="Example.ExcludeNone"/> to set the <see cref="IPropertySelector"/>
 /// to the <see cref="IPropertySelector"/>s built into NHibernate.
 /// </remarks>
 public Example SetPropertySelector(IPropertySelector selector)
 {
     _selector = selector;
     return(this);
 }
Ejemplo n.º 18
0
        public static void InjectProperties(IComponentContext context, object instance, IPropertySelector propertySelector)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }
            if (propertySelector == null)
            {
                throw new ArgumentNullException(nameof(propertySelector));
            }

            var instanceType = instance.GetType();

            foreach (var property in instanceType
                     .GetRuntimeProperties()
                     .Where(pi => pi.CanWrite))
            {
                var propertyType = property.PropertyType;

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

                if (propertyType.IsArray && propertyType.GetElementType().GetTypeInfo().IsValueType)
                {
                    continue;
                }

                if (propertyType.IsGenericEnumerableInterfaceType() && propertyType.GetTypeInfo().GenericTypeArguments[0].GetTypeInfo().IsValueType)
                {
                    continue;
                }

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

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

                if (!propertySelector.InjectProperty(property, instance))
                {
                    continue;
                }

                var propertyValue = context.Resolve(propertyType, new NamedParameter(InstanceTypeNamedParameter, instanceType));
                property.SetValue(instance, propertyValue, null);
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="AnnotatedPropertyDependencySelector"/> class.
 /// </summary>
 /// <param name="propertySelector">The <see cref="IPropertySelector"/> that is
 /// responsible for selecting a list of injectable properties.</param>
 public AnnotatedPropertyDependencySelector(IPropertySelector propertySelector)
     : base(propertySelector)
 {
 }
Ejemplo n.º 20
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PropertyDependencySelector"/> class.
 /// </summary>
 /// <param name="propertySelector">The <see cref="IPropertySelector"/> that is
 /// responsible for selecting a list of injectable properties.</param>
 public PropertyDependencySelector(IPropertySelector propertySelector)
 {
     PropertySelector = propertySelector;
 }
Ejemplo n.º 21
0
		/// <summary>
		/// Initialize a new instance of the <see cref="Example" /> class for a particular
		/// entity.
		/// </summary>
		/// <param name="entity">The <see cref="Object"/> that the Example is being built from.</param>
		/// <param name="selector">The <see cref="IPropertySelector"/> the Example should use.</param>
		protected Example(object entity, IPropertySelector selector)
		{
			_entity = entity;
			_selector = selector;
		}
Ejemplo n.º 22
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ExtensibleActionInvoker"/> class.
 /// </summary>
 /// <param name="propertySelector">The inject property selector.</param>
 public ExtensibleActionInvoker(IPropertySelector propertySelector)
 {
     this._propertySelector = propertySelector;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="AnnotatedPropertyDependencySelector"/> class.
 /// </summary>
 /// <param name="propertySelector">The <see cref="IPropertySelector"/> that is 
 /// responsible for selecting a list of injectable properties.</param>
 public AnnotatedPropertyDependencySelector(IPropertySelector propertySelector)
     : base(propertySelector)
 {
 }
Ejemplo n.º 24
0
 /// <summary>
 /// Initializes a new instance of the <see cref="OneToManyExpressionBuilder"/> class.
 /// </summary>
 /// <param name="propertyMapper">The <see cref="IPropertyMapper"/> that is responsible for mapping fields/columns from an <see cref="IDataRecord"/> to
 /// the properties of a <see cref="Type"/>.</param>
 /// <param name="collectionPropertySelector">The <see cref="IPropertySelector"/> that is responsible for selecting collection properties from a given <see cref="Type"/>.</param>
 /// <param name="dataRecordMapperFactory">A function delegate used to create the <see cref="IDataReaderMapper{T}"/> needed for each collection property.</param>
 public OneToManyExpressionBuilder(IPropertyMapper propertyMapper, IPropertySelector collectionPropertySelector, Func <Type, object> dataRecordMapperFactory)
 {
     this.propertyMapper             = propertyMapper;
     this.collectionPropertySelector = collectionPropertySelector;
     this.dataRecordMapperFactory    = dataRecordMapperFactory;
 }