Wednesday, March 25, 2009

Welcome a brand new starting series of posts: MVC Madness

Hi. A few months ago I've started to read about the ASP.NET MVC. And I loved what I saw there. I thought about comparing it to existing MVC frameworks in another languages and here we go, I've decided to make a series of posts with examples and snippets from each framework. Here what we have at the moment:
  • ASP.NET MVC 1.0 - the new released framework which allows us to have better control on page, cleaner HTML source, nice url formatting - routing, and all this is still ASP.NET
  • Symfony - a PHP framework which I will learn about, and you will obviously gonna help me with this.
  • Lift - also a new 1.0 framework which is based on Scala. Scala is a object-oriented and at the same time functionnal programming language which sits on top of Virtual Machine and lets you have a scalable application using the Scala itself and the libraries from the language which it exploits. For example Scala which sits on the top of Java VM, can use java libraries in Scala by importing them, and by that we achieve an interop language with objected-oriented and functionnal paradigm at the same time. Well some day I'll post particularily about Scala. So Lift is based on Scala and with this framework we can easy create a various range of Web 2.0 sites and by that I mean Ajax/COMET, jQuery and other technologies used in Web 2.0.
  • Spring - well, this framework is a widely known one. I wanted to present examples in Jakarta Struts, but I've decided that it will be better for me to learn something new, and particularily Spring. By the way, there are a lot of opinions on the Web that point us to idea that Struts has poorly implemented MVC. And that a better implementation is present in Spring. So I'm gonna give it a try.
May be there are languages and frameworks which I didn't cover yet, but you would like me to cover. If so let me know about it. I'll see what we can do. By the way, any examples of using MVC in desktop applications? So a little bit about the pattern itself. It is an old pattern. MVC was first described in 1979 by Trygve Reenskaug, who came from that interesting programming community of Smalltalk (we know all the other guys too, Kent Beck, Ward Cunningham, Martin Fowler, and others). It was introduced to separate different functional parts of application, and by that we achieve loused coupling of different parts of a system. So changing one part will not head to changing in all the parts. So what is it anyway?
  • Model - which is data, XML, object, whatever we handle and try visualize in our applications, web pages.
  • View - the presentation layer, window on the screen, our web page, UI elements which visualizes the Model. Note that we can have multiple views for a single model. We call it reusing the Model. That means that for having different views of the same data we don't need multiple tables/objects/XML tags/whatever. We can achieve we same effect using multiple Views.
  • Controller - as the name says, it controls the user input, events which happened on the View side and transmits these results to the Model.
How does it work together: Ok, here I'll give an example for you, let's say we have an Agenda - a web application. We have the database (which represents our Model) with the Users Table which contains the users id, user name and its password. And we also have the Events Table which contains event id, user id, the name of the event and the date. 1. User interacts with the View Now a user wants to add an event into his agenda. His view represents a table with a date column and an event column. If nothing is planned for a specific date he can select this date and plan an event on this date.
         Date                                         Event

               23th of March, 2009                   David's birthday
               24th of March, 2009                  
               25th of March, 2009                  
2. Now our Controller class obtains the user input and does the processing of the data, in our case it takes the name of the event and also the date out. 3. Our Controller transmits or notifies the Model about the results of his processing (in our case the name of the event and the date). Model reacts at this results in a way or another. In our case it inserts a new event into the events database. 4. The View reloads using indirectly(!) the updated Model. So, nor the Controller, neither the Model has the direct information about the View. In our case the page reloads with the event already set for that specific date. 5. The system waits for another user interactions which will restart this from the beginning. So this is an introductory post, there are a lot of deep things what I hope we will touch in our future examples. What's it for now. Waiting for your feedback and comments.

No comments:

Post a Comment