简体   繁体   中英

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. 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?

You can think about the context as a wrapper for related "things" such as HttpContext, DbContext, ObjectContext. ie: HttpContext contains any information you can reach for HTTP related operations.

DbContext contains the methods and properties for database communication. Likewise 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. 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 :)

A context is commonly a storage mechanism for a group of actions. HttpContext , for example

Encapsulates all HTTP-specific information about an individual HTTP request.

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 .

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 设计模式,例如: - 状态模式 - 策略模式

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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