I use the following method template in my web API to search for objects based on a set of criteria:
public IEnumerable<Employee> Search(SearchCriteria searchCriteria)
{
var employees = _dbContext.Employees;
if(searchCriteria.Age.HasValue)
{
employees = employees.Where(e => e.Age == searchCriteria.Age.Value);
}
if(searchCriteria...)
{
employees = employees.Where(e => ...);
}
return employees;
}
If no search criteria is specified, then the property of the SearchCriteria object will be null, and I just don’t filter based on this criteria. If specified, it will be relevant and will be used for filtering.
The problem, from a design point of view, is that if I really want to employeeshave no age, I just can’t do it this way, as I use nullto determine if I will use this criterion.
What other approach should I study to cover both cases?
, URI , -API , , ?
mysite.com/api/employee?keyword=a&age=null
mysite.com/api/employee?keyword=a