简体   繁体   English

什么是上下文?

[英]What is a context?

It seems to me that a Context class is a control console whose object can invoke any included functions, such as Datacontext and DomainContext in WCF Ria service.在我看来,Context 类是一个控制台,其对象可以调用任何包含的函数,例如 WCF Ria 服务中的 Datacontext 和 DomainContext。 Do I understand this concept correctly?我正确理解这个概念吗? If so, in what circumstances do I need to create a context class in my own class hierarchy?如果是这样,在什么情况下我需要在自己的类层次结构中创建上下文类?

Beside DataContext, what other well-known Context classes does the .net framework have?除了 DataContext 之外,.net 框架还有哪些其他著名的 Context 类?

You can think about the context as a wrapper for related "things" such as HttpContext, DbContext, ObjectContext.您可以将上下文视为相关“事物”的包装器,例如 HttpContext、DbContext、ObjectContext。 ie: HttpContext contains any information you can reach for HTTP related operations.即: HttpContext 包含您可以访问的 HTTP 相关操作的任何信息。

DbContext contains the methods and properties for database communication. DbContext 包含用于数据库通信的方法和属性。 Likewise ObjectContext.同样 ObjectContext。

I would say it's a placeholder or container of related things for something.我会说它是某物的占位符或相关事物的容器。

To me, a context object defines a set of values and/or functions that are bound to the current execution path.对我来说,上下文对象定义了一组绑定到当前执行路径的值和/或函数。 In other words, just like speaking about a technical topic in the context of a job interview is different than speaking about the same topic at a nerd dinner, the context changes based on factors that affect the runtime environment of the consuming code.换句话说,就像在面试的背景下谈论一个话题技术比在一个书呆子晚餐谈论同一主题的不同,对环境的变化基础上,影响消费的代码的运行时环境的因素。 That seems abstract, but I can't think of a better way to describe it at the moment!这看起来很抽象,但我现在想不出更好的方法来描述它!

Another famous context in .NET is the HttpContext object. .NET 中另一个著名的上下文是HttpContext对象。 Which values will change based on what Http operation is being handled.哪些值将根据正在处理的 Http 操作而改变。 For example, the url will change in HttpContext.Current.Request.Uri .例如,url 将在HttpContext.Current.Request.Uri更改。 Hope that puts it in context for you :)希望能把它放在你的上下文中:)

A context is commonly a storage mechanism for a group of actions.上下文通常是一组动作的存储机制。 HttpContext , for example HttpContext例如

Encapsulates all HTTP-specific information about an individual HTTP request.封装有关单个 HTTP 请求的所有特定于 HTTP 的信息。

For your WCF example, the "context" is the service.对于您的 WCF 示例,“上下文”是服务。 Different services have different contexts.不同的服务有不同的上下文。 Contexts can be as granular as you want.上下文可以根据需要细化。 Some are broad, like the DomainContext , and some are granular, like HttpContext .有些是广泛的,如DomainContext ,有些是细粒度的,如HttpContext

Contexts are everywhere, make them when you need to access or set like minded data or functions to things that can be decoupled.上下文无处不在,当您需要访问或设置志同道合的数据或功能时,可以使用它们来解耦。

All contexts are like this, they just encapsulate logic for particular action sets.所有的上下文都是这样,它们只是封装了特定动作集的逻辑。

Here is another post describing the context design pattern. 是另一篇描述上下文设计模式的文章。

Context 类用于一些 OOP 设计模式,例如: - 状态模式 - 策略模式

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

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