What’s the best way to set up a controller to sort by many (possibly null) criteria? Say for example, I was building a site that sold cars. My CarController has a function Index() which returns an IList of cars to the view, and details on each car are rendered with a partial view.
What’s the best way to structure this? Especially if there are a lot of criteria: Car Type (aka SUV), Car Brand, Car Model, Car Year, Car Price, Car Color, bool IsNew, or if I want to sort by closest to me etc… I’m using NHibernate as my ORM. Should I have just a ton of possible NHibernate queries and figure out which one to choose based on if/then’s in the controller? Or is there a simpler way.
Thanks so much for the help.
I have tasks in my system and created special class for searching:
One method in that class applies filters:
This is one of methods used for filtering:
This is Entity Framework, but it can be easily done in nHibernate too by adding where clauses to IQuery.
To search I have a method in controller:
Having one class for filtering is nice, because it can be serialized and save to database for future use. This way user can reuse filter parameters.
You would have:
If list is empty, you don’t apply filter.