Showing posts with label MVC. Show all posts
Showing posts with label MVC. Show all posts

Saturday, November 7, 2009

MVC Anatomy

In a previous post we saw how to create a Hello World MVC application.
Now we're going to perform an anatomy to this application to know what each part of the application exactly means.

First: Where is the Model, the View and the Controller ?
MVC Controller:
In tradotional asp.net the url you type corresponds to a page on the server, so when you request yoursite/abou.aspx there must be a page named about.aspx or a 404- page not found error is thrown.

In MVC it's different, there is no correspondance between the url you type and the pages that exist in the application. rather the url corresponds to a controller action than a page on the server.

The Controller is responsible for the way the user interacts with the application.
it manages the flow control logic of the application. It determines what response to make when the user makes a browser request.

The Controller is a C# class. Our Hello World application contains a controller class named HomeController.cs, It contains only one function named:public string Index()

public string Index()

{
return "Hello World";

}
this function is called when the user types URL /Home/Index in the browser cause as we said the controller functions respond to a user request.
Final thing to note that any public method contained in a controller is exposed as a controller action, meaning that it can be invoked by any user by typing it's name in the browser.
so if you have a function declared public as the following:

public void SomeFunction()







{



// Some Code



}

Any user can call this function by typing URL/Home/SomeFunction so be careful when declaring public functions in Controllers.

MVC View:
The view is equivalent to a page in t ASP.NET. It contains HTML markup that appears on the browser.
so for example if you want to create a page called demo , in Views folder create a subfolder named demo then in this subfolder create an aspx called Index.
then go to the Controllers folder and create a new controller with the name

DemoController.cs

in DemoController.cs add the following code


public ActionResult Index()
{
return View();
}

so when the user types url/demo the demo page is displayed
Notice that you must create the controller class in this form [ViewName]Controller.cs
MVC Model:
The MVC Model Contains you business logic, Validation logic or database access logic.
since that the Hello World application is not that complicated, there are no models in this application
Conclusion:
The view contains the logic for the interface, the Controller contains the logic for redirecting the user correctly to pages and respond to the user requests. And the Model contains the business or database access logic.
The Controller should contain minimal amount of code to handle user requests only.
If the Controller contains large bulks of code, consider removing some of them to a Model class in the Models folder

Friday, November 6, 2009

MVC Hello World

Now we're going to introduce the first MVC application: "Hello World".
What do you need to start:

I will use Visual studio 2008 sp1, so let's get started.

  1. After installing the framework open visual studio and select File>New>Project then from the left tab select web then from the right tab select ASP.NET Web Application

  2. Now from the debug menu choose start without debugging or Press ctrl+f5 to see how the application looks like.


  3. Now back to visual studio look at the solution explorer, it should be like this


  4. Now let's examine these folders:
    1. We have the Content folder which contains Site.css file that defines the initial styles for the website
    2. We have the Controllers folder that contains HomeController.cs which
      contains the code for the default view and HomeController.cs which contains the
      code for managing the login process and new user registeration.
    3. An empty Models folder
    4. We have Scripts folder that contains several javascript files (JQuery and Ajax
      files).
    5. We have the Views folder that contains subfolders containing the index page
      (Home page) and the login and registeration pages.
    6. Finally we have a default.aspx and Global.asax file and web.config file.
  5. For the sake of demonstration we're going to delete the following


  6. Now Back to HomeController.cs replace the code with the following:
    public string Index()

    {
    return "Hello World";

    }
  7. Build and run you should see something like that

  8. Now you have created your first MVC Hello World application

Tuesday, November 3, 2009

What is ASP.NET MVC

So you heard of it: MVC. What is it, what does it stand for and why I need to use it, that’s what we are going to know right now !.
ASP.NET MVC is a new web development framework from Microsoft that combines the effectiveness and tidiness of model-view-controller (MVC) architecture, the most up-to-date ideas and techniques from agile development, and the best parts of the existing ASP.NET platform. It’s a complete alternative to the WebForms platform, delivering considerable
advantages for all but the most trivial of web development projects.

Why MVC ?
well its a question I asked my self when I heard about MVC. Why should I use it instead of developing with tradional asp.net ?
After reading the msdn article and by experience I found that I encounterd the following situations:
  • If you're working in an agile development environment, where the customer or your manger requires modifications or new features to your software module. when new requirements evolve, it becomes such a pain to modify your work- especially if there is a modification to the database you're working on.
  • Many datastore applications require good organization of the User-Interface parts so they can be modified easly in response to database modifications
  • In traditional asp.net the developer may have to handle the user-interface design and the database development, whick consumes a lot of time and distract the developer. So separating the user-interface design and database development efforts produces better performance of the final product.
  • You want to reuse the database access code so you don't have to write the same code in each page.
  • Migrating your application to a newer framework would be easy if there is a separation between the user-interface and the business logic.
  • reducing code tied to user interface enhances testability.
  • The actual mechanism of maintaining state across requests
    (ViewState) often results in giant blocks of data being transferred between
    client and server. It can reach hundreds of kilobytes in many real-world
    applications, and it goes back and forth with every request, frustrating site
    visitors with a long wait each time they click a button or try to move to the
    next page on a grid.
  • ASP.NET MVC–generated pages don’t contain any ViewState data, so
    they can be hundreds of kilobytes smaller than typical pages from
    ASP.NET WebForms. Despite today’s fast broadband connections, this
    bandwidth saving still gives an enormously improved end user experience.
  • It supports a new routing system that produces clean url. Instead of writing App_Pages/page1/aspx?action=show%20prop&prop_id=345 it becomes something like products/technology/Laptops. This doesn't expose your application folder and pages names and the file structure of your application, so you are free to change them as you want without changing the public internet url which users may like to keep it in their favourites for later reference.

Finally ASP.NET is built on the ASP.NET Platform so you won't feel strange when dealing with it and it won't take a long time to get your hands on ASP.NET MVC