What is a context?

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.

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.

Tags:

C#