I have the following database schema :
Store
StoreId
Name
...
Order
OrderId
StoreId
....
I have a asp.net mvc backend application with a data repostory like this (simplified):
public IQueryable<Order> GetOrders() {
return storeDB.Orders
.OrderByDescending(o => o.DateDue)
.ThenByDescending(o => o.DateCreated);
}
I have similar code to show sales, employees, products etc … I want to put a dropdown somewhere in the application to allow me to set a global filter to show only orders, employees, products, etc … from a given StoreId, if storeId is null I should show all records from all stores (remove the filter) what is the best way to do it ?
Should I set the storeId in the session and filter the data in the controller? should I add a storeId parameter in my datalayer and evalute if it’s null or not before applying the filter?
I would go for a
List<Int>orList<Guid>or whateverDataTypeyou use for theStoreIDand use this list as a parameter in the method. Then you can eventually also show orders for multiple stores, or for a single store or for all stores. And then simply use an if statement to detect of the list contains something or nothing, something like:This is a nice setup according to me. Don’t store the filter parameters in a Session or whatever. In the way described above the method can be used from / by multiple functional “places” in your application.