名为“something-context”的类背后的想法是什么?
名为 x-context 的对象(例如 Sharepoint 中的 SPContext、ASP.NET 中的 HttpContext)背后的想法是什么?我假设这些对象仅具有方法和属性来涵盖有关当前请求的所有内容(根据上面的两个示例),或者在 OrderContext 对象的情况下,包含有关与用户会话相关的订单的详细信息。
然而,这听起来就像一个名为 OrderManager 的类。
那么后缀为“Context”的类的思想是什么呢?类名什么时候应该以 Manager 结尾?
What is the thinking behind objects named x-context (e.g. SPContext in Sharepoint, HttpContext in ASP.NET)? I would assume these objects just have methods and properties to cover everything regarding the current request (as per the two examples above), or in the case of an OrderContext object, contains details about the order relating to user session.
However, this sounds just like a class called OrderManager.
So what is the thinking of a class with the suffix "Context"? And when should a class name end with the word Manager?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
HttpContext 之所以命名为 HttpContext,是因为它是当前 HTTP 请求的上下文。您在 HttpContext、SPContext 和 OrderContext 示例中给出的用于调用类 Something-Context 的基本原理对我来说似乎很正确。
假设的 HttpManager 类可以管理与 HTTP 相关的任何内容。也许它覆盖当前的 HTTP 请求,也许它充当 HTTP 客户端并发出新的 HTTP 请求,也许它设置和拆除 HTTP 服务器。这个名字真的不会告诉你。
命名一个类 ...Manager 实际上有点代码味道,原因如下:
HttpContext is named HttpContext because it's the context of the current HTTP request. The rationale that you give in your HttpContext, SPContext, and OrderContext examples for calling classes something-context seems spot on to me.
A hypothetical HttpManager class could manage anything relating to HTTP. Maybe it covers the current HTTP request, maybe it acts as an HTTP client and makes new HTTP requests, maybe it sets up and tears down HTTP servers. The name really doesn't tell you.
Naming a class ...Manager is actually a bit of a code smell, for the following reasons: