应我的服务回报什么接口? IQueryable的,IList的,IEnumerable的?我的、接口、IQueryable、IList

2023-09-03 08:56:55 作者:我的世界只缺你一个

想象我有一个具有搜索开始具有一定的字符串的所有汽车的方法的SearchService层;

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<Car> CarsStartingWith(string startWith){
        //magic
    }
}

我要服务的使用?什么接口 IQueryable的可以呈现在我的应用程序的其余部分很好的流体界面。 IEnumerable的已经懒的方面去用它。 IList的仅仅是最实用的。

What interface should my service use? IQueryable can render for a nice fluid interface in the rest of my application. IEnumerable has the lazy aspect going with it. IList is just the most practical.

我想有我的所有服务返回相同的界面的一致性,使整个事情变得更加简单。 ICollection的可能也许是一种选择也是如此,但它只是提供了这么一点......

I would like to have all my services return the same interface for consistency makes the whole thing a lot easier. ICollection could maybe be an option too, but it just offers so little...

推荐答案

如果您希望所有的服务回报相同的接口,那么我可能会去为的IEnumerable&LT;&GT;

If you want all your services to return the same interface then I would probably go for IEnumerable<>.

这将是很容易对您的呼叫转换为的IQueryable 或创建一个列表,如果必要的:

It'll be easy enough for your callers to convert to IQueryable or create a List, if necessary:

IEnumerable<Car> cars = Searcher.CarsStartingWith("L");
var carsList = cars.ToList();
var queryableCars = cars.AsQueryable();