Example #1
0
        /// <summary>
        /// Adds an inclusion filter for the specified example item.
        /// </summary>
        /// <typeparam name="TEntity">
        /// The type of entity on which the specified attribute is located.
        /// </typeparam>
        /// <typeparam name="TValue">
        /// The type of value specified by the selector.
        /// </typeparam>
        /// <param name="selector">
        /// The property selector.
        /// </param>
        /// <param name="inclusionValues">
        /// The inclusion values.
        /// </param>
        /// <returns>
        /// The current <see cref="ValueFilterSet{T}"/>.
        /// </returns>
        public ValueFilterSet <T> Include <TEntity, TValue>(Expression <Func <TEntity, TValue> > selector, params TValue[] inclusionValues)
        {
            if (selector == null)
            {
                throw new ArgumentNullException(nameof(selector));
            }

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

            var valueFilter = new ValueFilter(selector, FilterType.MatchesSet, inclusionValues.Cast <object>().ToArray());

            this.valueFilters.Add(valueFilter);
            return(this);
        }
Example #2
0
        /// <summary>
        /// Matches the primary key of the specified <paramref name="entity"/>.
        /// </summary>
        /// <param name="entity">
        /// The entity to match the key for.
        /// </param>
        /// <param name="definitionProvider">
        /// The definition provider for the entity.
        /// </param>
        /// <param name="explicitKeyAttributes">
        /// The explicit key attributes for this match.
        /// </param>
        /// <returns>
        /// The current <see cref="ValueFilterSet{T}"/>.
        /// </returns>
        /// <remarks>
        /// This operation will clear any existing filters.
        /// </remarks>
        public ValueFilterSet <T> MatchKey(
            [NotNull] T entity,
            [NotNull] IEntityDefinitionProvider definitionProvider,
            [NotNull] params Expression <Func <T, object> >[] explicitKeyAttributes)
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }

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

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

            this.valueFilters.Clear();
            var entityDefinition = definitionProvider.Resolve <T>();
            var keyAttributes    = explicitKeyAttributes.Any()
                                    ? explicitKeyAttributes.Select(entityDefinition.Find)
                                    : entityDefinition.PrimaryKeyAttributes;

            foreach (var keyAttribute in keyAttributes)
            {
                var entityReference = new EntityReference
                {
                    EntityAlias = keyAttribute.Entity.Alias,
                    EntityType  = keyAttribute.Entity.EntityType
                };
                var attributeLocation = new AttributeLocation(keyAttribute.PropertyInfo, entityReference);
                var valueFilter       = new ValueFilter(attributeLocation, FilterType.Equality, keyAttribute.GetValueDelegate.DynamicInvoke(entity));

                this.valueFilters.Add(valueFilter);
            }

            // Use all available values if no keys are defined.
            if (this.valueFilters.Any())
            {
                return(this);
            }

            Trace.TraceWarning($"{typeof(T).FullName} does not have any key attributes defined.");

            foreach (var attribute in entityDefinition.DirectAttributes)
            {
                var entityReference = new EntityReference
                {
                    EntityAlias = attribute.Entity.Alias,
                    EntityType  = attribute.Entity.EntityType
                };
                var attributeLocation = new AttributeLocation(attribute.PropertyInfo, entityReference);
                var valueFilter       = new ValueFilter(attributeLocation, FilterType.Equality, attribute.GetValueDelegate.DynamicInvoke(entity));
                this.valueFilters.Add(valueFilter);
            }

            return(this);
        }