C# 什么是上下文?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12240420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is a context?
提问by lwconquer
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. Do I understand this concept correctly? If so, in what circumstances do I need to create a context class in my own class hierarchy?
在我看来,Context 类是一个控制台,其对象可以调用任何包含的函数,例如 WCF Ria 服务中的 Datacontext 和 DomainContext。我正确理解这个概念吗?如果是这样,在什么情况下我需要在自己的类层次结构中创建上下文类?
Beside DataContext, what other well-known Context classes does the .net framework have?
除了 DataContext 之外,.net 框架还有哪些其他著名的 Context 类?
采纳答案by DarthVader
You can think about the context as a wrapper for related "things" such as HttpContext, DbContext, ObjectContext. i.e.: HttpContext contains any information you can reach for HTTP related operations.
您可以将上下文视为相关“事物”的包装器,例如 HttpContext、DbContext、ObjectContext。即: HttpContext 包含您可以访问的 HTTP 相关操作的任何信息。
DbContext contains the methods and properties for database communication. Likewise ObjectContext.
DbContext 包含用于数据库通信的方法和属性。同样 ObjectContext。
I would say it's a placeholder or container of related things for something.
我会说它是某物的占位符或相关事物的容器。
回答by Davin Tryon
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 contextof 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 HttpContextobject. Which values will change based on what Http operation is being handled. For example, the url will change in HttpContext.Current.Request.Uri. Hope that puts it in context for you :)
.NET 中另一个著名的上下文是HttpContext对象。哪些值将根据正在处理的 Http 操作而改变。例如,网址将在HttpContext.Current.Request.Uri. 希望能把它放在你的上下文中:)
回答by devshorts
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. 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.
对于您的 WCF 示例,“上下文”是服务。不同的服务有不同的上下文。上下文可以根据需要细化。有些是广泛的,如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.
所有的上下文都是这样,它们只是封装了特定动作集的逻辑。
Hereis another post describing the context design pattern.
这是另一篇描述上下文设计模式的文章。
回答by Razvan
a Context class is used in some OOP Design patterns, e.g: - State pattern - Strategy pattern
Context 类用于一些 OOP 设计模式,例如: - 状态模式 - 策略模式

