为什么 HttpContext 是抽象 class 而不是接口?

Why is HttpContext an abstract class instead of an interface?

为什么 HttpContext 是抽象 class 而不是接口?

class是public抽象,所有方法都是public抽象。我不明白为什么这个 class 是抽象的 class。

为什么它是抽象的class而不是接口?

关于这个话题有很多意见(例如here and here)。尽管 ASP.NET 团队通常使用接口,但我可以想到他们在这种情况下选择抽象 class 的几个原因:

版本控制
我不希望 HttpContext class 改变很多,但是抽象 classes 版本比接口更容易(它们实际上根本没有版本),因为它们可以部分实现使用virtual 关键字。

封装
抽象 classes 封装了一组功能,其中接口为特定功能提供了更多的契约。 类 可能只实现一个抽象 class,这对于 HttpContext 实现是有意义的,例如 DefaultHttpContext.

向后兼容性
虽然 ASP.NET Core 完全重写了 ASP.NET,但多年来开发人员习惯于针对 HttpContext class 进行编程。两者 class 有很多共同点。


请记住,我只是猜测,也许 ASP.NET 团队的一些人可以启发我们。

这是我从使用 ASP.NET Core 的 Microsoft 的 Daniel Roth 那里得到的回复。

I believe in this case using an abstract class enables adding members in future versions, which you can't do with an interface.