C#C
C#4y ago
Anton

❔ ASP.NET Core search

Can someone give me a few pointers at how to implement a generic search system? My initial thought was like here https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/search?view=aspnetcore-7.0 checking every possible filter and then applying them, but a generic system would be better if there are many filters. So like a ISearchFilter that would update the built database query, a request model with ISearchFilter[] which I'd loop through to apply the filters. The thing is that the ASP.NET Core binding system will have to instantiate the right implementations of ISearchFilter by the name given in the request body, and then there's the question of how bad would be dynamic queries like this in EF Core?
// an interface kinda like this
public interface ISearchFilter<T>
{
    IQueryable<T> Apply(IQueryable<T> e);
}

// a request body kinda like this
{
    "Filters": [
        {
            "Type": "FilterType",
            "SomeProperty": 5
        },
        {
            // blah
        }
    ]
}

Is there some sort of built-in support for things like this, or do I have to roll out my own system?
Was this page helpful?