Example #1
0
        /// <summary>
        /// Produces the Linq expression representing the entire filter descriptors collection.
        /// </summary>
        /// <param name="filterDescriptors">Collection of filters</param>
        /// <param name="filterOperator">The operator used to combine filters</param>
        /// <param name="expressionCache">Cache for storing built expressions</param>
        /// <returns>Produced linq expression, which can be <c>null</c> if there are no filter descriptors.</returns>
        public static Expression BuildFiltersExpression(
            FilterDescriptorCollection filterDescriptors,
            FilterDescriptorLogicalOperator filterOperator,
            ExpressionCache expressionCache)
        {
            Debug.Assert(filterDescriptors != null, "Unexpected null filterDescriptors");

            Expression filtersExpression = null;

            foreach (FilterDescriptor filterDescriptor in filterDescriptors)
            {
                // Ignored filters will not have a cache entry
                if (expressionCache.ContainsKey(filterDescriptor))
                {
                    Expression filterExpression = expressionCache[filterDescriptor];

                    if (filtersExpression == null)
                    {
                        filtersExpression = filterExpression;
                    }
                    else if (filterOperator == FilterDescriptorLogicalOperator.And)
                    {
                        filtersExpression = Expression.AndAlso(filtersExpression, filterExpression);
                    }
                    else
                    {
                        filtersExpression = Expression.OrElse(filtersExpression, filterExpression);
                    }
                }
            }

            return(filtersExpression);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="FilterCollectionManager"/> class.
        /// </summary>
        /// <param name="sourceCollection">The collection of <see cref="FilterDescriptor"/>s to manage</param>
        /// <param name="expressionCache">The cache with entries for the <see cref="FilterDescriptor"/>s</param>
        /// <param name="validationAction">The callback for validating items that are added or changed</param>
        public FilterCollectionManager(FilterDescriptorCollection sourceCollection, ExpressionCache expressionCache, Action <FilterDescriptor> validationAction)
        {
            if (sourceCollection == null)
            {
                throw new ArgumentNullException("sourceCollection");
            }
            if (expressionCache == null)
            {
                throw new ArgumentNullException("expressionCache");
            }
            if (validationAction == null)
            {
                throw new ArgumentNullException("validationAction");
            }

            this.ExpressionCache              = expressionCache;
            this._sourceCollection            = sourceCollection;
            this.ValidationAction             = (item) => validationAction((FilterDescriptor)item);
            this.AsINotifyPropertyChangedFunc = (item) => ((FilterDescriptor)item).Notifier;

            this.AddCollection(this._sourceCollection);
        }