Should I return IEnumerable or IQueryable from my DAL?

后端 未结 4 738
眼角桃花
眼角桃花 2020-12-22 23:52

I know this could be opinion, but I\'m looking for best practices.

As I understand, IQueryable implements IEnumerable, so

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-23 00:22

    It depends on what behavior you want.

    • Returning an IList tells the caller that they've received all of the data they've requested
    • Returning an IEnumerable tells the caller that they'll need to iterate over the result and it might be lazily loaded.
    • Returning an IQueryable tells the caller that the result is backed by a Linq provider that can handle certain classes of queries, putting the burden on the caller to form a performant query.

    While the latter gives the caller a lot of flexibility (assuming your repository fully supports it), it's the hardest to test and, arguably, the least deterministic.

提交回复
热议问题