49

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?

Josh
  • 2,142
  • 2
  • 23
  • 20
lwconquer
  • 865
  • 1
  • 8
  • 17

5 Answers5

63

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.

Josh
  • 2,142
  • 2
  • 23
  • 20
DarthVader
  • 52,984
  • 76
  • 209
  • 300
23

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 :)

Davin Tryon
  • 66,517
  • 15
  • 143
  • 132
6

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.

Community
  • 1
  • 1
devshorts
  • 8,572
  • 4
  • 50
  • 73
1

a Context class is used in some OOP Design patterns, e.g: - State pattern - Strategy pattern

Razvan
  • 326
  • 4
  • 13
1

According to Microsoft Official Explanation :

A context is an ordered sequence of properties that define an environment for the objects resident inside it. Contexts get created during the activation process for objects that are configured to require certain automatic services, such as synchronization, transactions, just-in-time activation, security, and so on. Multiple objects can live inside a context.

A new object's context is generally chosen based on meta-data attributes on the class. Some important types of context are:

ExecutionContext:

This is the parent context, all the other contexts are a part of it. It is the system that .NET features like Task use to capture and propagate context, but has no behavior of its own.

SecurityContext:

This is where we find any security information that would normally be confined to the current thread. If your code needs to run as a particular user, you may be impersonating that user, or ASP.NET may be doing impersonation for you. In that case, the impersonation is stored in the SecurityContext

CallContext:

This allows the programmer to store custom data that should be available for the lifetime of a logical thread. Although considered bad practice in a lot of situations, it can avoid excessive numbers of method parameters as various context is passed around the program. LogicalCallContext is a related system that works across AppDomains.

Md. Zakir Hossain
  • 1,082
  • 11
  • 24