public void TestExpressionProvider() { var entities = this.database.CreateOtherEntities(10); var provider = new ServiceCollection() .AddFluentRestBuilder() .Services .BuildServiceProvider(); var dictionary1 = new FilterExpressionProviderDictionary <OtherEntity>(provider) .AddStringFilter(e => e.Name) .AddNumericFilter(e => e.Id) .AddNumericFilter(e => e.IntValue) .AddDateTimeFilter(e => e.CreatedOn) .AddBooleanFilter(e => e.Active) .AddBooleanFilter(e => e.Status); var dictionary2 = new FilterExpressionProviderDictionary <OtherEntity>(provider) .AddPropertyFilters(); foreach (var dictionary in new[] { dictionary1, dictionary2 }) { var emptyExpression = dictionary[nameof(OtherEntity.Name)] .Resolve(FilterType.GreaterThan, "0"); Assert.Null(emptyExpression); var expression = dictionary[nameof(OtherEntity.Name)] .Resolve(FilterType.Contains, "Name"); using (var context = this.database.Create()) { var resultEntities = context.OtherEntities .Where(expression) .ToList(); Assert.Equal(entities, resultEntities, new PropertyComparer <OtherEntity>()); } } }
/// <summary> /// Apply filter logic to the received <see cref="IQueryable{T}"/>. /// <para> /// Matches the query parameters with the keys of the given filter dictionary. /// Implement <see cref="IFilterByClientRequestInterpreter"/> for custom behavior. /// </para> /// <para>Requires <see cref="HttpContextProviderAttribute"/> to be set.</para> /// </summary> /// <typeparam name="TSource">The type of the value.</typeparam> /// <param name="observable">The parent observable.</param> /// <param name="factory"> /// A factory function to create a dictionary of supported filter expressions. /// </param> /// <returns>An instance of <see cref="IProviderObservable{TSource}"/>.</returns> public static IProviderObservable <IQueryable <TSource> > ApplyFilterByClientRequest <TSource>( this IProviderObservable <IQueryable <TSource> > observable, Func <FilterExpressionProviderDictionary <TSource>, IFilterExpressionProviderDictionary <TSource> > factory) { var filterProvider = new FilterExpressionProviderDictionary <TSource>( observable.ServiceProvider); var filterDictionary = factory(filterProvider); return(observable.ApplyFilterByClientRequest(filterDictionary)); }
public async Task TestEmptyUseCase() { var entities = this.database.CreateEnumeratedEntities(10); var filterDictionary = new FilterExpressionProviderDictionary <Entity>(this.provider); var resultEntities = await Observable.Single( this.database.Create().Entities, this.provider) .ApplyFilterByClientRequest(filterDictionary) .ToList(); Assert.Equal(entities, resultEntities, new PropertyComparer <Entity>()); }