Friday, October 21, 2011

IQueryable can be an antipattern

IQueryable is proving a little bit dangerous. If someone casts it off to a list before it is appropriate the query may not be as "filtered" as it might otherwise be and might bring back too many records causing a performance problem. Example: One pulls all of the records of foo via an IQueryable, then makes the IQueryable jump through some hoops, filtering down the record set, and finally casts the subset of all of the records of foo off to a list. That is fine, however, the door is open for another developer to find reason to cast the IQueryable off to a list "further upstream" (such as at the point where we are getting all of the foo records) thus negating any efficiency in using IQueryable. It's also hard to safeguard against this. Craig suggested that perhaps the boundaries of repository interfaces should denote where an IQueryable is cast off to a list and that all of the progressive spooling up of an IQueryable should be done in the repository itself.

No comments:

Post a Comment