What interface should my service return? IQueryable, IList, IEnumerable?

后端 未结 7 2221
孤独总比滥情好
孤独总比滥情好 2021-01-31 23:26

Imagine I have a SearchService layer that has a method to search all cars starting with a certain string;

public static class Searcher{
    public IAnInterface&l         


        
7条回答
  •  佛祖请我去吃肉
    2021-01-31 23:41

    Short answer: It depends.

    Longer answer: Return the richest type that your client code is going to need. IList works in most cases if you don't need lazy loading. You can still use Linq to query against an IList or IEnumerable. If lazy loading is a requirement, then go with IEnumerable or IQueryable.

    Side note: Returning the same interface for all services might seem like a noble goal but given different client usage patterns, you may want to return different interfaces.

提交回复
热议问题