Search code examples
aspnetboilerplate

[aspnetboilerplate]How to extend PagedAndSortedResultRequestDto to accept filters?


I am trying to pass filters to PagedAndSortedResultRequest.

public class PagedAndSortedResultRequestDto : PagedResultRequestDto, IPagedAndSortedResultRequest
{
    public virtual string Sorting { get; set; }
}

I want to pass a simple list of name-value pairs to this DTO in order to filter the result set.

The current implementation does not allow me to do it so I have to create a new class inheriting PagedAndSortedResultRequestDto. I am wondering if it is the right path to achieve my goal and there is any milestone to accommodate filter in the framework.

Thanks


Solution

  • As advised by @AlperEbicoglu, here goes my solution. The solution requires three parts. Inherited request dto class, AppService passing the class, and overridden CreateFilteredQuery method inside of the AppService.

    //Inherited Request DTO Class
    [Serializable]
    public class MyResultRequestDto : PagedAndSortedResultRequestDto
    {
        public virtual int? ChannelId { get; set; }
    }
    
    //AppService 
    public class MyAppService : AsyncCrudAppService<Tenant, MyDto, int, MyResultRequestDto, CreateMyDto, MyDto>, IMyAppService
    {
    
        //Overridden CreateFilteredQuery inside of MyAppService
        protected override IQueryable<MyEntity> CreateFilteredQuery(MyResultRequestDto input)
        {
            var query = Repository.GetAll();
    
            if (input.ChannelId.HasValue)
            {
                query = query.Where(t => t.ChannelId == input.ChannelId.Value);
            }
            return query;
        }
    //...
    }
    

    Hope it helps anyone who needs it.