Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What exactly is meant by the word 'context' in MVC? I know we get a databaseContext, a filterContext and a httpContext to name a few.
Is it synonymous with 'object'? Could these just as easily have been named databaseObject, filterObject etc.

If I had to write a custom class with the name of 'FooContext', what would the suffix 'context' be telling me about 'Foo'?

What I have tried:

Googling it revealed the following:
"A Context is a collection of data, often stored in a Map or in a custom class which acts as a struct with accessors and modifiers. It is used for maintaining state and for sharing information within a system."

This doesn't tell me anything. Absolutely anything stored in a class is a collection of data.
Posted
Updated 14-Aug-17 22:06pm
Comments
Peter_in_2780 15-Aug-17 3:39am    
To me, the big clue is "used for maintaining state". I don't know MVC, but anywhere else I met the term that's what I understand.
Kornfeld Eliyahu Peter 15-Aug-17 4:09am    
Context is a way of grouping operations together on different part of the same entity (parts, that are represented by different objects)...
The name probably comes from the Unit of Work pattern, which is one of the building blocks of EF implementation (Repository pattern is the other one)...

1 solution

In MVC, your controller action is fired, whenever a proper route is hit by a http call. The context is the working environment of that specific run of the action. It can consist of several things, as you have noticed. Of course, these are objects (as everything else). Different contexts can behave differently; some are sort of static between action runs, some will be different each time.
HttpContext, for example, will contain information about the http call including information from the client, static and runtime information about the web server, but also data that is added by the elements of the processing pipeline. Like session data, or authentication information. Elements in the pipeline will use (mostly extend, but also read) this context (running environment - if you like). Actually, your action is one step in this pipeline. You might want to use the environment, or not. For example, you might want to access the raw request body or header.

You could get the context objects as parameters of the action, but that would be unnecessary headaches for you.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900