- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在使用 ASP.NET MVC 5 中的 Entity Framework 构建一个访问我的数据库的服务。所以我首先用他的界面编写一个基本服务。但我担心一件小事。
通常我返回 IEnumerable<T>
在我的界面中,但这通常会导致 ToList()
在代码中调用。
所以我想知道在这样的界面中返回什么最好。我可以返回 IList
但恐怕它可能太多了,我不需要 IList
提供的所有方法。 .
这是我的代码:
public interface IBaseService<T>
{
T Add(T obj);
T Update(T obj);
T Remove(string id);
T Get(string id);
ICollection<T> Find(Expression<Func<bool, T>> func);
ICollection<T> GetAll();
}
最佳答案
你说的
Usually I return an IEnumerable in my interface, but that often leads to ToList() calls in the code.
你必须找出为什么你调用ToList()
在结果上。您可能(但不太可能)想要修改结果(添加\删除项目)。从 Find
等方法返回可修改集合通常不是一个好的做法。或 GetAll
, 但你可以返回 ICollection<T>
或 IList<T>
(如果您还需要通过索引器快速访问或在特定位置插入\删除)。
但是您调用 ToList
的可能性更大弄清楚那里有多少元素和/或访问特定元素。如果你只需要 Count
- 返回 IReadOnlyCollection<T>
延伸 IEnumerable<T>
只有 Count
属性(property)。如果您还需要通过索引器快速访问 - 返回 IReadOnlyList<T>
延伸 IReadOnlyCollection<T>
带索引器。
两者都是 Array
和 List<T>
实现所有这些接口(interface),这样您就可以按原样返回这些类:
static IReadOnlyList<string> Test() {
return new string[0]; // fine
}
然而,返回时List
这样 - 调用者可能会将其转换回 IList<T>
并仍然修改它。这通常不是问题,但如果你想避免这种情况,请使用 AsReadOnly
在列表中(它不会将任何内容复制到新列表中,所以不用担心性能):
static IReadOnlyList<string> Test() {
return new List<string>().AsReadOnly(); // not required but good practice
}
关于c# - 服务接口(interface)中的 IList、ICollection 或 IEnumerable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43201931/
注意:这与 this other question 相似,但不完全相同 我已经实现了一个 IBusinessCollection界面。它来自 ICollection ,以及旧的非通用 ICollect
配置 AutoMapper 以映射的最佳/最简单方法是什么 ICollection至 ICollection至 ICollection ? 我有一个如下所示的 DomainModel: public
只想对syntactic sygar做简单的扩展: public static bool IsNotEmpty(this ICollection obj) { return ((obj !=
这个问题在这里已经有了答案: Why does IEnumerable inherit from IEnumerable? (4 个回答) 8年前关闭。 IEnumerable实现 IEnumerab
我正在尝试编写一个查询,从我的连接数据中获取国家列表。 Places是List . var zonedCountries = (from dz in db.DeliveryZones.
我有一组消息,每个消息定义为: namespace DatabaseDesign.Models { public class PrivateMessageDetail {
C# 中的 ICollection 中是否有一些方法可以添加另一个集合的所有元素?现在我必须始终为此编写 foreach 循环: ICollection allLetters = ... //some
转换 ICollection 的推荐方式是什么?至 ICollection其中 Bar工具 IBar ? 是不是就这么简单 collection = new List(); ICollection =
我用网格创建了表单来可视化任何集合( ICollection , ICollection )对象。 之后我创建了调试器可视化类(从 Microsoft.VisualStudio.DebuggerVis
我正在尝试基于 Stack 创建自定义集合.当我看 Stack [来自元数据] 在 Visual Studio 中,它显示 Stack实现 ICollection ,这将要求它实现 ICollecti
例如: public interface IFoo { //... ICollection Children { get; } //... } public class Foo
这个问题在这里已经有了答案: Why is it considered bad to expose List? [duplicate] (6 个答案) 关闭 9 年前。 我正在使用 FxCop 工具
这个问题在这里已经有了答案: Why does List implement IList, ICollection and IEnumerable? (4 个答案) 关闭 7 年前。 为什么ILis
我正在使用用 C# 创建的库。我一直致力于将一些代码移植到 F#,但必须使用 C# 库中的相当多的基础类型。 一段代码需要计算一个值列表并将其分配给类中的公共(public)字段/属性。该字段是一个包
此代码不起作用,但是: public virtual ICollection items { get { return (ICollection)items.Where(e => e.isVisibl
这个问题在这里已经有了答案: C# - sorting by a property (3 个答案) how to sort a collection by datetime in c# (4 个答案
这很奇怪,我试图在我的构造函数中使用 List 初始化我的 ICollection 并且发生了这种情况: Schedules = new List(); //OK CateringItems = ne
我想遍历设备上安装的所有语音。 在 TextSoSpeech 元数据中,我看到有 namespace Android.Speech.Tts { public class TextToSpeec
我有以下代码。为什么它总是采用“take(ICollection a)”方法?我认为它自己的对象应该是 LinkedList 或 HashSet,所以它应该调用另外两个 take 方法。 class
我的应用程序客户端/服务器有问题。我使用 MVVM 模式。在我看来,我有一个 DataGrid,它在我的 ViewModel 中与一个 ICollection 绑定(bind),代码行如下: publ
我是一名优秀的程序员,十分优秀!