简体   繁体   English

上下文是什么意思?

[英]What does Context mean?

This word never makes sense to me.这个词对我来说从来没有意义。 I wouldn't understand ussualy why Database Access Objects are named with this naming convention and also i have seen many times this word used in others codes which wasn't about Database objects.我通常不明白为什么数据库访问对象以这种命名约定命名,而且我在其他与数据库对象无关的代码中多次看到这个词。

Can someone explain mean of this word in programming to someone who is not english and can give some examples about general usage in programming area ?有人可以向不会英语的人解释这个词在编程中的含义,并且可以举例说明编程领域的一般用法吗?

Something is typically called a "context" in computer programming when that something encapsulates some kind of state.当某种东西封装了某种状态时,通常在计算机编程中称为“上下文”。

In the example of Linq 2 sql or EF, you have a Data Context, or Object Context.. these encapsulate the state of your data model, including connections and versioning.在 Linq 2 sql 或 EF 的示例中,您有一个数据上下文或对象上下文……它们封装了数据模型的状态,包括连接和版本控制。

In the case of HttpContext, it's encapsulating the state of an Http connection (which is normally considered stateless, but HttpContext tries to give state to it).在 HttpContext 的情况下,它封装了 Http 连接的状态(通常被认为是无状态的,但 HttpContext 尝试为其提供状态)。

In english, if we refer to context, we refer to information surrounding something that allows you to understand the entire situation in which that something exists.在英语中,如果我们提到上下文,我们指的是围绕某物的信息,这些信息可以让您了解某物存在的整个情况。 For example, we may say a statement is "taken out of context".例如,我们可以说一个陈述是“脱离上下文的”。 That would mean a statement by itself doesn't necessarily reveal all the information.这意味着声明本身并不一定会透露所有信息。

Out of context:脱离上下文:

People are tasty.人很好吃。

In Context:在上下文中:

We should never say or think that people are tasty.我们永远不应该说或认为人们好吃。

Without the "context", the statement has a different meaning.没有“上下文”,语句就有不同的含义。 Programming has taken the term to similarly refer to the data surrounding something that gives it more meaning.编程已经用这个术语来类似地指代围绕着赋予它更多意义的事物的数据。

in .NET AFAIK, We have Httpcontext in web and ObjectContext in Entity Framework.在 .NET AFAIK 中,我们在 web 中有 Httpcontext,在 Entity Framework 中有 ObjectContext。 I'm not aware of any other use of context in .NET framework, but there may be more usages.我不知道 .NET 框架中上下文的任何其他用途,但可能有更多用途。 So, here's a simple explanation about the two I know.所以,这是对我所知道的两个的简单解释。

  • HttpContext:上下文:
    Encapsulates all HTTP-specific information about an individual HTTP request.封装有关单个 HTTP 请求的所有特定于 HTTP 的信息。 Properties of this class include the Request object, the Response object, the Session object, and an AllErrors property which keeps an array of Exception objects occured during the current request.此类的属性包括 Request 对象、Response 对象、Session 对象和 AllErrors 属性,该属性保存在当前请求期间发生的 Exception 对象数组。 It's simply a wrapper class.它只是一个包装类。

  • ObjectContext: Quoting from : http://cgeers.wordpress.com/2009/02/21/entity-framework-objectcontext/#objectcontext ObjectContext:引用自: http : //cgeers.wordpress.com/2009/02/21/entity-framework-objectcontext/#objectcontext

    every object returned by a query (Linq To Entities, Entities SQL…) is automatically attached to an object context.查询返回的每个对象(Linq To Entities、Entities SQL...)都会自动附加到对象上下文。 This context tracks the changes applied to these objects so that it can later figure out how to persist these changes to the underlying data store.此上下文跟踪应用于这些对象的更改,以便稍后确定如何将这些更改持久保存到底层数据存储。
    This object context is represented by a class fittingly named ObjectContext.这个对象上下文由一个恰当地命名为 ObjectContext 的类表示。 The ObjectContext encapsulates a couple of things, namely: ObjectContext 封装了一些东西,即:

    • The connection to the underlying data store (database)与底层数据存储(数据库)的连接
    • Metadata describing the Entity Data Model (EDM)描述实体数据模型 (EDM) 的元数据
    • An ObjectStateManager for tracking changes to the objects用于跟踪对象更改的 ObjectStateManager

So, it seems like it is used basically when we want to manage some logically relative objects.所以,当我们想要管理一些逻辑上相关的对象时,它似乎基本上是使用的。 Objects that we can put in one logical context.我们可以放在一个逻辑上下文中的对象。 (eg entities in EF or Request/Response/Session/etc in HttpContext) (例如 EF 中的实体或 HttpContext 中的 Request/Response/Session/etc)

I know this is old however the word Context has appeared even more in the language as of late.我知道这是旧的但是上下文一词最近在语言中出现得更多。 According to the MSDN a context is: 根据 MSDN ,上下文是:

Defines an environment for the objects that are resident inside it and for which a policy can be enforced.为驻留在其中的对象定义环境,并且可以对其实施策略。

From this, I interpret that it is a container that must adhere to a set of rules that are global in which the Context will exist.由此,我将其解释为一个容器,它必须遵守一组全局规则,Context 将在其中存在。 DbContext, HttpContext, and ViewContext to name a few are very easy to pack down into that definition. DbContext、HttpContext 和 ViewContext 等很容易打包到该定义中。

This also implies that you can define, extend, your own context on top of an existing context.这也意味着您可以在现有上下文之上定义、扩展您自己的上下文。 An example would be to take the HttpContext and derive a RestfulContext that setups the HttpContext to handle Restful policies over Http.一个例子是采用 HttpContext 并派生一个 RestfulContext 来设置 HttpContext 以处理 Http 上的 Restful 策略。 Likewise, you could derive a HateOasContext which would further refine what policy is applied to the objects inside the Context.同样,您可以派生一个 HateOasContext,它将进一步细化应用于 Context 内的对象的策略。

上下文存储了理解给定场景的关键信息......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM