22

I'm starting to see Contexts everywhere I look. In ASP.NET MVC, there are ControllerContexts, RequestContexts, HttpContexts, FormContexts. In Entity Framework, you have ObjectContexts and DbContexts. Ninject has Ninject.Activation.IContext.

What the heck is a context?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Hobbes
  • 370
  • 2
  • 7
  • 32
    To answer this question you'll need to provide more context ... – Noon Silk Dec 17 '10 at 05:23
  • maybe an object that holds a status? – thejh Dec 17 '10 at 05:24
  • 2
    @Noon Silk -- I'll context *you* =) – Hobbes Dec 17 '10 at 05:27
  • A lot of great food for thought here. Everyone agrees that context objects provide the environment you are working in. Noon Silk answers my unasked question about why I see contexts so much in the MVC world. VinayC hints that the context object is mutable (which is true, for example, in the case of Entity Framework: lazy-loading options are set via the ObjectContext). I feel as if I'm still missing some important idea, however, like maybe it's part of a Design Pattern. – Hobbes Dec 17 '10 at 06:30
  • @Hobbes After all that enlightenment you may vote that answers up ... – Dr. belisarius Dec 17 '10 at 06:36
  • @Hobbes: Sorry if I didn't make it clear, the design pattern is called Dependency Injection: http://en.wikipedia.org/wiki/Dependency_injection (that being, the ability to set all, dependencies (one of those being "context"), of an object externally). – Noon Silk Dec 17 '10 at 06:37
  • I don't buy the DI explanation. Everything is called a context these days in the Java EE world. Web applications themselves, once deployed, are referred to internally as "contexts". Each web application becomes a servlet context with a context path and a context.xml file (if deploying in Tomcat). That doesn't seem like DI to me. And of course, the contexts all have contexts. The only one that makes sense to me is `TransactionContext`. – Brandon May 28 '14 at 17:37

6 Answers6

8

Well, it's kind of a dependency-injection thing, that allows people to say 'Here is the environment you will operate in'. Generally, they provide, unsurprisingly, the "context" for whatever it is. I.e., some state. Perhaps the URL, perhaps some HTTP headers, whatever.

You are seeing a lot of them because ASP.NET is focused on testing and hence allows these items to be "swapped in", such that you can provide your own context implementations (i.e. define your own state), so that you can run tests appropriately and successfully.

If you're wondering what state is, well it's just various bits of data that are "given", by the environment. I.e. today it is cold in the office. This is part of the state. But perhaps I want to run my test when it is hot in the office, so I would be able to subclass OfficeContext and return the appropriate state for the appropriate method/etc.

Noon Silk
  • 54,084
  • 6
  • 88
  • 105
4

IMO, Context denotes some (possibly mutable) state about some thing. Typically context would be cross-cutting layers and often carries domain neutral data across layers.

VinayC
  • 47,395
  • 5
  • 59
  • 72
  • "Typically context would be cross-cutting layers and often carries domain neutral data across layers" -- I'm a little lost here. Can you give some examples? – Hobbes Dec 17 '10 at 05:35
  • @Hobbes, now days, layered applications are norms - each layer concentrate on some aspect - for example, a data access layer would worry only about data persistence. So I was referring that typically, context would be used by all such layers and they carry information across layers. But unlike business data that also flows across layers (from say data store to your UI and back), context are typically part of application frameworks and carry related data (that is mostly not about business). – VinayC Dec 17 '10 at 05:40
3

Context is information outside of the scope of the thing you're currently doing but which has implications that may be essential.

Imagine if someone asks you the meaning of the English word "fly". There are multiple definitions: the buzzing little inspects or the sustained act of gliding through air. In order to give the right answer you need the context which tells you which definition they're looking for - are they reading a book about Diptera or the Wright brothers?

Regarding computing, say you're implementing an HTTP handler. It might be able to generate a response without knowing anything else (Hello, World!) but it's more likely that it needs the context of the HTTP request information - what were the request parameters, acceptable encoding types, etc. so it can produce a meaningful response to the user agent.

maerics
  • 151,642
  • 46
  • 269
  • 291
1

I think of them as being like your environment variables and profile settings in a telnet/ssh session. They bundle together different settings to allow tools to perform differently based on the context (i.e. environment) they're run in.

Brad Mace
  • 27,194
  • 17
  • 102
  • 148
0

The above answers are by and large quite good. The only thing I would add is that its most common usage is as a "glue" to lower layers of a system. Generally the system in question is some kind of "container" system, where your code is executed inside of a larger code base that hides a lot of execution details from you. The context is the abstracted interface to that larger system.

user430788
  • 2,143
  • 2
  • 17
  • 17
0

IMO, it's just another argument. In my (limited) experience, I've seen it be the calling class. You have to know what you're doing to do what you're doing well. Context is what you're doing, what's happening/running.

John
  • 15,418
  • 12
  • 44
  • 65