MediatR icon indicating copy to clipboard operation
MediatR copied to clipboard

Question about Query with params-keyword

Open BH4NG opened this issue 5 years ago • 2 comments

For simple Querries, I use QuerySpecification<Item>, which typically contains 1 or 2 properties of the Item. But how would I go about querrying Items based on user-input, with a whole range of possible properties to check? Eg. User A wants to search Items by Id, Color & Shape, User B wants to search Items by Color, Shape & Weight.. I would expect all querries to go to the same endpoint.

Up untill about 5 possible inputted parameters, I can handle it with an If-statement, but the application is growing and users should be able to search for Items by Color, Shape, Weight, even by Item.SubItem.Cost, etc, totalling up to around 30 params. I don't wan't to write 30² queryspecifications depending on the user input combinations.

I've written an endpoint which takes params object[] as input but am stuck in the QuerySpecification. Do I override Expression like any other query or do I need something in between? I'm kind of lost at sea where to actually begin on this, so any input is much appreciated

Thanks!

BH4NG avatar Dec 08 '20 18:12 BH4NG

If you have around 30 parameters users can search on, there's nothing wrong with writing a (complex) query builder for those parameters Yes, you will end up with a lot of ifs/switches, but that's exactly the logic you are asking for.

On a sidenote, it looks like you want to provide your users with search capabilities often seen on e-commerce sites (the sidebar which let you filter on all kind of product features). This is called "faceted search", and (fulltext) search engines like Solr and Elasticsearch are particularly good and fast in doing this. It might be worth looking at that instead of building a complex (and slow) SQL query.

remcoros avatar Dec 11 '20 11:12 remcoros

@remcoros Thank you very much for your answer, I'm very happy with it! Will look into both options and follow what feels good! Thank you again 🥇

BH4NG avatar Dec 11 '20 13:12 BH4NG