Sunday, April 29, 2012

MVC


Model,
Model is a domain specific set of classes, which provides interface into the data (whatever data you are using behind the application). It also implements business rules as well as stores application states

View,
View is the applications user interface, which essentially renders the models in a form that user can interact with. So you can think it as a transformation of the model of the data behind the application. view allows user to interact with application.

Controller,
Controller receives requests from user and then initiate some kind of response whatever is appropriate for the application in that state based on user interaction. And then controller interacts with the models as is necessary to change state or save data. It also handle overall application flow. In web application, the controller usually response to http get or http post inputs then hands request over to the model that implements business rule and then selects the view display to the user.

User Interaction with MVC application,
1. Interacts with web page
2. Controller handles request
3. Notify model to changes
4. Controller selects a view
5. Await new user interaction

The tenets of MVC,
1. Separation of concerns : application should be modularized, so that each module focuses on own concerns. Model knows nothing about view or controller, and view knows nothing about controller.
2. Convention over configuration : keep the files in conventional folder helps configuration.
3. Keep it DRY: Don’t Repeat Yourself : a design principle that MVC embraces. Eliminate duplication of code and logic. That helps application faster and easier to maintain. exp. You can decorate a model with attributes that supports validation. So, those attributes in the model that way no matter how many views use that model the all enforce the exact same data validation rules.

Web Forms vs. MVC,
1. ASP.Net has provider model, MVC has a more powerful pluggable model.
2. Web forms almost forces combination of view and controller.
3. MVC server methods, not files,
    exp. In asp.net : file request is like this,
    http://www.example.com/index.aspx?id=5
    in mvc: method request is like this,
    http://www.example.com/Home/Details/5
        1. Maps to the Details action method
        2. Of Home controller
        3. Item Id of 5
4. MVC allows Automated Unit test
5. Control over HTML

Pass data from controller to view,
ViewData[“Message”] = ”Message Data”;
Model object

%: vs %=,
%: allows automatically html encode the data inside it. Where %= have to explicitly call html.Encode in order to encoding.

For accept user input uses Html.BeginForm()

Controller must have,
Implement IController interface
Have a name ending in “Controller”
Marked public, non abstract, no generic parameters

IController Interface,
Single purpose: find a controller and call execute method.

ControllerBase abstract base class,
Implement IController : adds controller features, such as TempData and ViewData.
You could build web site with either IController or ControllerBase

Controller Class,
1. It inherits directly from ControllerBase, so indirectly implements IController interface.
2. Added fetures includes,
    Action Methods
    Action Results
    Filters
3. Normally should implement your controllers using Controller, rather than IController or ControllerBase.


How Action invoker selects the correct action method,
Primary job of action invoker is to select appropriate action method. There could be multiple overloaded methods with same name. And action name may be different from method name.
It starts by getting action portion of route
{controller}/{action}/{id}
If the url doesn’t contain action portion, then the default route definition will be index action. Then it maps action name to the controller method.

Method must meet requirement to be action method,
It must be public and non static or shared. It cannot defined on System.Object or Controller. It cannot have NonAction attribute.

ActionName Attribute,
Normal convention is for controller method name to be same as action name. But you can give action method a different action name.
[ActionName(“MyName”)]

User interaction with MVC application,
1. user makes a request
2. controller handles request with action method
3. controller interacts with model
4. controller selects view as response
5. view awaits new user action

User Input Validation,
Server side,
data posted to server
create method checks it
response sent to the user
Client side,
Add call to EnableClientValidation in view on the top
<% Html.EnableClientValidation(); %>

Code Nugget syntax,
there are 3 main forms of code nugget
1. <% %>
2. <%= %>
3. <%: %>

use with either = or : when code returns string and always prefer the : because it HTML encodes the string and also MVC has smart enough to not double encode.
When code inserts data directly into response stream use <% %>.

No comments:

Post a Comment