예제 #1
0
        /// <summary>
        /// Resolves the complex value from the <see cref="InstancePropertyAssociation"/>.
        /// </summary>
        /// <param name="propertyValueResolutionContext">The <see cref="PropertyValueResolutionContext"/>.</param>
        /// <exception cref="ArgumentNullException">The value of '<paramref name="propertyValueResolutionContext"/>' cannot be null.</exception>
        /// <returns>The resolved value.</returns>
        public Object ResolveValue(PropertyValueResolutionContext propertyValueResolutionContext)
        {
            if (propertyValueResolutionContext == null)
            {
                throw new ArgumentNullException(nameof(propertyValueResolutionContext));
            }

            var instanceAssociation = (IInstanceValueAssociation)propertyValueResolutionContext.PropertyValue;
            var instanceDescriptor  = instanceAssociation.Instance;

            Object resolvedInstance;

            if (propertyValueResolutionContext.InstanceResolutionContext.InstanceRelationStore.TryGetRelated(instanceDescriptor, out resolvedInstance))
            {
                if (instanceDescriptor == null)
                {
                    throw new InvalidOperationException();
                }
            }

            if (resolvedInstance == null)
            {
                using (var instanceResolutionWalker = this.instanceResolutionWalkerFactory.CreateExport())
                {
                    resolvedInstance = instanceResolutionWalker.Value.ResolveInstance(propertyValueResolutionContext.InstanceResolutionContext.CreateChildContext(instanceDescriptor));
                }
            }

            return(resolvedInstance);
        }
        /// <summary>
        /// Resolves the collection item and adds it to the collection.
        /// If <paramref name="itemPropertyValueResolver"/> is null, it will be resolved for the <see cref="IValueAssociation"/>, otherweise the supplied instance will be used.
        /// </summary>
        /// <param name="collection">An instance of the created <see cref="List{T}"/>.</param>
        /// <param name="addMethod">The reference to the Add-method.</param>
        /// <param name="propertyValueResolutionContext">The <see cref="PropertyValueResolutionContext"/>.</param>
        /// <param name="itemPropertyValueResolver">The used or created resolved <see cref="IPropertyValueResolver"/> instance.</param>
        private void ResolveCollectionItemAndAdd([NotNull] Object collection, [NotNull] MethodBase addMethod,
                                                 [NotNull] PropertyValueResolutionContext propertyValueResolutionContext, [CanBeNull] ref IPropertyValueResolver itemPropertyValueResolver)
        {
            if (collection == null)
            {
                throw new ArgumentNullException(nameof(collection));
            }

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

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

            if (itemPropertyValueResolver == null)
            {
                var propertyValueResolver = this.propertyValueResolvers.First(pvr => pvr.Value.CanResolveValue(propertyValueResolutionContext.PropertyValue));

                itemPropertyValueResolver = propertyValueResolver.Value;
            }

            var resolvedValue = itemPropertyValueResolver.ResolveValue(propertyValueResolutionContext);

            addMethod.Invoke(collection, new[] { resolvedValue });
        }
        /// <summary>
        /// Resolves the primitive value from the <see cref="IValueAssociation"/>.
        /// </summary>
        /// <param name="propertyValueResolutionContext">The <see cref="PropertyValueResolutionContext"/>.</param>
        /// <exception cref="ArgumentNullException">The value of '<paramref name="propertyValueResolutionContext"/>' cannot be null.</exception>
        /// <returns>The resolved value.</returns>
        public Object ResolveValue(PropertyValueResolutionContext propertyValueResolutionContext)
        {
            if (propertyValueResolutionContext == null)
            {
                throw new ArgumentNullException(nameof(propertyValueResolutionContext));
            }

            var primitivePropertyValue = (IPrimitiveValueAssociation)propertyValueResolutionContext.PropertyValue;

            return(primitivePropertyValue.Value);
        }
        /// <summary>
        /// Resolves the collection from the <see cref="CollectionPropertyAssociation"/>.
        /// </summary>
        /// <param name="propertyValueResolutionContext">The <see cref="PropertyValueResolutionContext"/>.</param>
        /// <exception cref="ArgumentNullException">The value of '<paramref name="propertyValueResolutionContext"/>' cannot be null.</exception>
        /// <returns>The resolved value.</returns>
        public Object ResolveValue(PropertyValueResolutionContext propertyValueResolutionContext)
        {
            if (propertyValueResolutionContext == null)
            {
                throw new ArgumentNullException(nameof(propertyValueResolutionContext));
            }

            var collectionPropertyAssociation = (CollectionPropertyAssociation)propertyValueResolutionContext.PropertyValue;

            MethodBase addMethod;
            var        newCollectionInstance = this.CreateDynamicListInstance(collectionPropertyAssociation, propertyValueResolutionContext.InstanceResolutionContext, out addMethod);

            IPropertyValueResolver propertyValueResolver = null;

            foreach (var item in collectionPropertyAssociation.CollectionItemDescriptors.Cast <IValueAssociation>().ToList())
            {
                var itemPropertyResolutionContext = propertyValueResolutionContext.InstanceResolutionContext.CreatePropertyValueResolutionContext(item);

                this.ResolveCollectionItemAndAdd(newCollectionInstance, addMethod, itemPropertyResolutionContext, ref propertyValueResolver);
            }

            return(newCollectionInstance);
        }