上下文到底是什么?

发布于 2024-10-07 17:18:10 字数 187 浏览 10 评论 0原文

我开始随处看到上下文。在ASP.NET MVC中,有ControllerContexts、RequestContexts、HttpContexts、FormContexts。在实体框架中,您有 ObjectContext 和 DbContext。 Ninject 有 Ninject.Activation.IContext。

上下文到底是什么?

I'm starting to see Contexts everywhere I look. In ASP.NET MVC, there are ControllerContexts, RequestContexts, HttpContexts, FormContexts. In Entity Framework, you have ObjectContexts and DbContexts. Ninject has Ninject.Activation.IContext.

What the heck is a context?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

握住我的手 2024-10-14 17:18:10

嗯,这是一种依赖注入的东西,它允许人们说“这是你将在其中操作的环境”。一般来说,毫不奇怪,它们提供了无论是什么的“背景”。即,某些状态。也许是 URL,也许是一些 HTTP 标头,等等。

您会看到很多它们,因为 ASP.NET 专注于测试,因此允许“交换”这些项目,以便您可以提供您自己的上下文实现(即定义您自己的状态) ,以便您可以正确且成功地运行测试。

如果您想知道状态是什么,那么它只是环境“给出”的各种数据。即今天办公室很冷。这是国家的一部分。但也许我想在办公室很热的时候运行测试,这样我就能够子类化 OfficeContext 并为适当的方法/等等返回适当的状态。

Well, it's kind of a dependency-injection thing, that allows people to say 'Here is the environment you will operate in'. Generally, they provide, unsurprisingly, the "context" for whatever it is. I.e., some state. Perhaps the URL, perhaps some HTTP headers, whatever.

You are seeing a lot of them because ASP.NET is focused on testing and hence allows these items to be "swapped in", such that you can provide your own context implementations (i.e. define your own state), so that you can run tests appropriately and successfully.

If you're wondering what state is, well it's just various bits of data that are "given", by the environment. I.e. today it is cold in the office. This is part of the state. But perhaps I want to run my test when it is hot in the office, so I would be able to subclass OfficeContext and return the appropriate state for the appropriate method/etc.

囍笑 2024-10-14 17:18:10

IMO,上下文表示有关某些事物的某些(可能可变的)状态。通常,上下文是横切层,并且通常跨层携带领域中立的数据。

IMO, Context denotes some (possibly mutable) state about some thing. Typically context would be cross-cutting layers and often carries domain neutral data across layers.

蹲墙角沉默 2024-10-14 17:18:10

上下文是超出您当前正在做的事情范围的信息,但其含义可能是重要的。

想象一下,如果有人问你英语单词“fly”的含义。有多种定义:嗡嗡作响的小检查或在空气中持续滑行的行为。为了给出正确的答案,您需要上下文来告诉您他们正在寻找哪个定义 - 他们正在读一本关于双翅目或莱特兄弟的书吗?

关于计算,假设您正在实现一个 HTTP 处理程序。它可能能够在不知道任何其他信息的情况下生成响应(Hello,World!),但更有可能的是它需要 HTTP 请求信息的上下文 - 请求参数是什么,可接受的编码类型,等等,以便它可以向用户代理产生有意义的响应。

Context is information outside of the scope of the thing you're currently doing but which has implications that may be essential.

Imagine if someone asks you the meaning of the English word "fly". There are multiple definitions: the buzzing little inspects or the sustained act of gliding through air. In order to give the right answer you need the context which tells you which definition they're looking for - are they reading a book about Diptera or the Wright brothers?

Regarding computing, say you're implementing an HTTP handler. It might be able to generate a response without knowing anything else (Hello, World!) but it's more likely that it needs the context of the HTTP request information - what were the request parameters, acceptable encoding types, etc. so it can produce a meaningful response to the user agent.

稳稳的幸福 2024-10-14 17:18:10

我认为它们就像 telnet/ssh 会话中的环境变量和配置文件设置。它们将不同的设置捆绑在一起,以允许工具根据它们运行的​​上下文(即环境)执行不同的操作。

I think of them as being like your environment variables and profile settings in a telnet/ssh session. They bundle together different settings to allow tools to perform differently based on the context (i.e. environment) they're run in.

小耗子 2024-10-14 17:18:10

IMO,这只是另一个论点。根据我(有限的)经验,我发现它是调用类。您必须知道您在做什么才能做好您正在做的事情。上下文是你正在做什么、正在发生/正在运行的事情。

IMO, it's just another argument. In my (limited) experience, I've seen it be the calling class. You have to know what you're doing to do what you're doing well. Context is what you're doing, what's happening/running.

没企图 2024-10-14 17:18:10

上面的回答总体来说还是不错的。我唯一要补充的是,它最常见的用途是作为系统较低层的“粘合剂”。一般来说,所讨论的系统是某种“容器”系统,您的代码在更大的代码库中执行,该代码库向您隐藏了许多执行细节。上下文是更大系统的抽象接口。

The above answers are by and large quite good. The only thing I would add is that its most common usage is as a "glue" to lower layers of a system. Generally the system in question is some kind of "container" system, where your code is executed inside of a larger code base that hides a lot of execution details from you. The context is the abstracted interface to that larger system.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文