/// <summary> /// Creates and return a filter associated with the specified key added for the context. /// </summary> /// <typeparam name="T">The type of elements of the query.</typeparam> /// <param name="key">The filter key associated to the filter.</param> /// <param name="queryFilter">The query filter to apply to the context.</param> /// <param name="isEnabled">true if the filter is enabled.</param> /// <returns>The filter created and added to the context.</returns> public static BaseQueryFilterInterceptor Filter <T>(object key, Func <IQueryable <T>, IQueryable <T> > queryFilter, bool isEnabled = true) where T : class { BaseQueryFilterInterceptor filter; // FilterByKey { if (!GlobalFiltersByKey.TryGetValue(key, out filter)) { filter = new QueryFilterInterceptor <T>(queryFilter) { IsDefaultEnabled = isEnabled }; GlobalFiltersByKey.AddOrUpdate(key, filter, (o, interceptorFilter) => filter); } } // FilterByType { if (!GlobalFilterByType.ContainsKey(typeof(T))) { GlobalFilterByType.AddOrUpdate(typeof(T), new List <BaseQueryFilterInterceptor>(), (type, list) => list); } GlobalFilterByType[typeof(T)].Add(filter); } ClearAllCache(); return(filter); }
/// <summary>Adds a query filter to the filter context associated with the specified key.</summary> /// <typeparam name="T">The type of elements of the query.</typeparam> /// <param name="key">The filter key.</param> /// <param name="filter">The filter.</param> /// <returns>The query filter added to the filter context associated with the specified ke .</returns> public BaseQueryFilterInterceptor AddFilter <T>(object key, Func <IQueryable <T>, IQueryable <T> > filter) where T : class { var queryFilter = new QueryFilterInterceptor <T>(filter); queryFilter.OwnerContext = this; // FilterByKey { FilterByKey.AddOrUpdate(key, queryFilter, (o, interceptorFilter) => queryFilter); } // FilterByType { if (!FilterByType.ContainsKey(typeof(T))) { FilterByType.TryAdd(typeof(T), new List <BaseQueryFilterInterceptor>()); } FilterByType[typeof(T)].Add(queryFilter); } ClearCache(); return(queryFilter); }