Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

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.

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top