[Question] ApplyFilter
See original GitHub issueHey Guys,
I hope I describe this clearly. I keep having issues with the PagiationQuery and in specific with the ApplyFilterWithoutPagination
part.
I’ve made an Contract
which has InventoryLines
. The code below is the InventoryLinesPaginationQuery
using XVI.Application.Features.InventoryLines.Caching;
using XVI.Application.Features.InventoryLines.DTOs;
namespace XVI.Application.Features.InventoryLines.Queries.Pagination;
public class InventoryLinesWithPaginationQueryByContractId : PaginationFilterBase, ICacheableRequest<PaginatedData<InventoryLineDto>>
{
[OperatorComparison(OperatorType.Equal)]
public required int Id { get; set; }
[CompareTo("ProductName")] // <-- This filter will be applied to Name or Description.
[StringFilterOptions(StringFilterOption.Contains)]
public string? Keyword { get; set; }
public InventoryLineByContractIdListView ListView { get; set; } =
InventoryLineByContractIdListView.All; //<-- When the user selects a different ListView,
// a custom query expression is executed on the filter.
public override string ToString()
{
return $"Listview:{ListView},Search:{Keyword},Sort:{Sort},SortBy:{SortBy},{Page},{PerPage}";
}
[IgnoreFilter] public string CacheKey => InventoryLineCacheKey.GetPaginationCacheKey($"{this}");
[IgnoreFilter] public MemoryCacheEntryOptions? Options => InventoryLineCacheKey.MemoryCacheEntryOptions;
}
public class InventoryLinesWithPaginationByContractIdQueryHandler : IRequestHandler<
InventoryLinesWithPaginationQueryByContractId, PaginatedData<InventoryLineDto>>
{
private readonly IApplicationDbContext _context;
private readonly IMapper _mapper;
private readonly IStringLocalizer<InventoryLinesWithPaginationByContractIdQueryHandler> _localizer;
public InventoryLinesWithPaginationByContractIdQueryHandler(
IApplicationDbContext context,
IMapper mapper,
IStringLocalizer<InventoryLinesWithPaginationByContractIdQueryHandler> localizer
)
{
_context = context;
_mapper = mapper;
_localizer = localizer;
}
public async Task<PaginatedData<InventoryLineDto>> Handle(InventoryLinesWithPaginationQueryByContractId request,
CancellationToken cancellationToken)
{
var iQuerybleWithFilter= _context.InventoryLines.Where(x => x.ContractId == request.Id)
.ApplyFilter(request);
var resultsWithFilter = iQuerybleWithFilter.ToList(); // Yields 0 results
var iQuerableWithoutFilter= _context.InventoryLines.Where(x => x.ContractId == request.Id);
var resultsWithoutFilter = iQuerableWithoutFilter.ToList(); // Yields 1 result
// TODO: Implement InventoryLinesWithPaginationQueryHandler method
var data = await _context.InventoryLines.Where(x => x.ContractId == request.Id)
.ApplyFilterWithoutPagination(request)
.ProjectTo<InventoryLineDto>(_mapper.ConfigurationProvider)
.PaginatedDataAsync(request.Page, request.PerPage);
return data;
}
}
public class InventoryLinesByContractIdPaginationSpecification : Specification<InventoryLine>
{
public InventoryLinesByContractIdPaginationSpecification(InventoryLinesWithPaginationQueryByContractId query)
{
}
}
public enum InventoryLineByContractIdListView
{
[Description("All")] All,
}
There is factually 1 InventoryLine
where the ContractId Matches the request.id.
But why does ApplyFilter (or for that matter ApplyFilterWithoutPagination) take it away? I’m confused. As you can see in the code I cleaned all the filtering code (I had tons more listviews) but deleted them all
Edit: The question is in the handle method
Extra note @neozhu Thank you for your reply on my previous issue. Very warm and welcome.
This project is beautiful and once I learned more I hope I can write a README about the project itself cause I’ve some trouble finding my way trough the project. Its a beautiful setup and I love it tho. But lets say I don’t want caching on a page. Do I delete the code? should I put 0 minutes cache. Should I use a command or should I use a <Name>Query to export data. (Like the log page uses a command and the product page export pdf uses a query). Tho I love learning this project it feels it has everything I ever need for the setup of a cool application!
Issue Analytics
- State:
- Created 2 months ago
- Comments:6 (2 by maintainers)
Top GitHub Comments
Regarding the usage of the
toString
override, it is primarily used to generate a cache key. When the query conditions are the same, the generated key remains constant, which helps avoid redundant database queries.If you want to implement more complex condition combinations, I suggest referring to the
SearchProductSpecification.cs
file. It provides an example of how to handle advanced search criteria.Sorry for my absence in replies, but I’m currently on vacation in Norway.
May I ask why you are debugging a .NET project (C#) in a Java based IDE? Of course I understand that you can install plugins to make that work, however I don’t understand why you would choose that over Rider.
Of course it could be that you don’t have all the IDE’s since those subscriptions are quite expensive. Luckily I can still enjoy the student benefits 😊
Nevertheless that still makes me wonder how you prefer an IDE which isn’t natively written for C# over Visual Studio. I find Visual Studio horrible btw, but I find it hard to believe Intelliji with a C# plugin works better than Visual Studio.
Kind regards, Bram