Understanding controllers in Asp.net MVC

Understanding controllers in Asp.net MVC:




Controllers in Asp.net MVC:



"In simple words, a controller is a contract/bridge between a model and the view.
Here is the flow:
A controller is used for main request processing logic. If a page has to talk with database, the controller sends a request to the model, model performs its task with db and returns some response or db records back to the controller then controller sends this response to the view"
As this type of question lend itself too many religiously answers, I believe that the is no "one way to rule them all". Now in the Monorail and ASP.NET MVC (and also Roar of course), a Controller is simply a collection of Actions. The correct question then is "What is the role of Action".

In book, the role of the Action is to separate the Domain Model from the presentation, both in terms of data structures, and in concerns. Everything that is specialized to the fact that the interface with the domain is through WEB requests, is the controller's layer responsibility. That includes data binding and transformations, dealing with Authentication (but not authorization), and making decisions for the view templates. So, an action will take parameters from the incoming request (a web is not a Domain concern), bind these to a meaningful data that can be send to the Domain as a Query or Command, in the Domain's language, without no cookies, FORM, QueryString, and other "web stuff". It would also, when viewing data, will transform the domain objects that got returned from the Model, into a View Model, that in the same book mentioned earlier is a model separated from the Domain model, and is in charge of supplying the view-template with all the data and the decision making it needs. So, for eg., the view should not ask if (view.User.IsAdmin) and render an "EDIT" button, but instead the Controller's action will have made this question, and supplied the view with a decision, for the view to ask if (view.ShouldRenderEditButton)
So, the Controllers layer separates WEB concerns from DOMAIN concerns.


Asp.net Related Other Post:





Comments

Popular posts from this blog