Should a Repository return IEnumerable , IQueryable or List?

后端 未结 4 1204
星月不相逢
星月不相逢 2020-12-31 02:44

I\'d like to make my application as flexible as possible, but not dig myself into a hole by making my Interface too specific.

What is the best object

4条回答
  •  醉话见心
    2020-12-31 03:48

    How likely are you to ever need to return a custom implementation of IEnumerable (not a collection) from your DAL? (To answer this question, look at your previous projects and count how many of those or of yield returns you have around.)

    If the answer is "not very", I'd just return ICollection or even arrays (if you want to prevent the query results from being inadvertently modified.) In a pinch, if you ever need to change a query to "stream" results with a custom IEnumerable, you can always have the old method call the new one and materialise the results to keep compatibility with older clients.

提交回复
热议问题