[英]Why Dbset implement IEnumerable when it already implements IQueryable?
当Dbset已经具有IQueryable时,它似乎实现了IEnumerable,但这并不会使IEnum的实现成为多余,因为IQueryable已经实现了IEnum。
First of all, we should correct your terminology: 首先,我们应该更正您的术语:
IQueryable already implements IEnum. IQueryable已实现IEnum。
IQueryable
doesn't implement anything. IQueryable
没有实现任何东西。 It inherits IEnum
, which means that every class that implements IQueryable
must implement the members of IEnum
as well as the members of IQueryable
. 它继承了 IEnum
,这意味着实现IQueryable
每个类都必须实现IEnum
的成员以及IQueryable
的成员。
I think your question actually is this: why is DbSet
defined as implementing IEnum
, when it implements IQueryable
so it has to implement IEnum
anyway. 我认为您的问题实际上是这样的:为什么将DbSet
定义为实现IEnum
,当它实现IQueryable
时,无论如何都必须实现IEnum
。
Technically, stating that DbSet
implements IEnum
is redundant. 从技术上讲,声明DbSet
实现IEnum
是多余的。
Which leaves the question, Why would anyone unnecessarily state a base interface? 剩下的问题是,为什么有人会不必要地声明基本接口?
I'll quote this blog article by Eric Lippert: https://blogs.msdn.microsoft.com/ericlippert/2011/04/04/so-many-interfaces/ 我将引用埃里克·利珀特(Eric Lippert)撰写的此博客文章: https : //blogs.msdn.microsoft.com/ericlippert/2011/04/04/so-many-interfaces/
Perhaps because they believe that doing so makes the code easier to understand and more self-documenting. 也许是因为他们相信这样做会使代码更易于理解和更多地自我记录。
Or, perhaps the developer wrote the code as 或者,也许开发人员将代码编写为
interface I1 {} 接口I1 {}
interface I2 {} 接口I2 {}
interface I3 : I1, I2 {} 接口I3:I1,I2 {}
and the realized, oh, wait a minute, I2 should inherit from I1. 然后意识到,哦,等等,I2应该继承自I1。 Why should making that edit then require the developer to go back and change the declaration of I3 to not contain explicit mention of I1? 为什么要进行该编辑然后要求开发人员返回并更改I3的声明以不包含对I1的明确提及? I see no reason to force developers to remove redundant information. 我没有理由强迫开发人员删除多余的信息。
Sometimes you need all rows of a specific table without any additional where
clause, in IEnumerable
format. 有时,您需要IEnumerable
格式的特定表的所有行,而没有任何其他where
子句。 in these circumstances, you can directly cast the DbSet
to IEnumerable
. 在这种情况下,您可以将DbSet
直接DbSet
为IEnumerable
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.