MVC Demystified 1
MVC Demystified
Webvisions 2007Michael P. Jones
Understanding the Essence of Ruby on Rails
MVC Demystified 2
About Session
• Web applications• MVC• Pix Patisserie• MVC Another Look• Rails Essence• More Rails• Rails Testing• Other MVC
MVC Demystified 3
About Speaker
• Past– Educational and Reservation software. (Java)– Founded Code in Motion. http://codeinmotion.com/
• Present– Creating custom Ruby on Rails applications.
• Recovery and prevention.• Screening and Interview• PDF generation of applications• Widgets
– Tailoring CMS Solutions• Radiant, Rails based CMS with extensions for business needs of the
client.
MVC Demystified 4
Traditional Applications
• Browser directly accesses page.– Does not centralize control– No content/style separation– Easy and fast to produce– Difficult to maintain
MVC Demystified 5
MVC Applications
• Browser accesses a “controller”– Centralizes control– Clean separation of content/style– More involved to produce– Easier to maintain and expand
MVC Demystified 6
MVC
• MVC is an Architectural Design Pattern• Separates a web application into three different
modules.
MVC Demystified 7
Design Pattern
• A pattern that has been developed to help programmers cope with common problems.
• Blueprints on how to construct something.
MVC Demystified 8
MVC Means
• Most Vexing Conundrum (Amy Hoy)• Model | View | Controller
MVC Demystified 9
Trip to Pix
• A Patisserie in Portland
http://qwendy.typepad.com/
MVC Demystified 10
Typical Bakery Interaction
• Request a tasty treat from the baker
http://www.pixpatisserie.com/
MVC Demystified 11
Baker Gathers Ingredients
• Baker gathers raw ingredients to fulfill the request.
• Some requests utilize same ingredients.
http://www.flickr.com/photos/moria/92792777/
MVC Demystified 12
Baker Select Pan
• The pan dictates what the response looks like.
http://www.flickr.com/photos/tracyhunter/113563802/
MVC Demystified 13
Baker responds with your treat
http://www.pixpatisserie.com/
MVC Demystified 14
Pix Flow
MVC Demystified 15
MVC Diagram
MVC Demystified 16
Controller (Baker)
• Dispatches Requests and controls flow.• Centralizes access.• Interacts with Model and View.
MVC Demystified 17
Model (Ingredients)
• Data representation and business logic.• Can be database/xml/etc• Business Logic• Examples:
– User– Bike– Question
MVC Demystified 18
View (Pan)
• Data presentation and user input.• Renders the Model in to a View.• Can be HTML/PDF/WML/Javascript• No computations, very little logic, display logic i.e. loops
MVC Demystified 19
MVC Diagram
MVC Demystified 20
MVC Advantages
• Separation of interests.– Model centralizes business logic.– View centralizes display logic.– Controller centralizes application flow.
• Clean separation of content/style.• Improved decoupling.• Easier testing.• Allow multiple people to work on different parts.
MVC Demystified 21
Rails and MVC
• Rails in an MVC framework• ”…Rails is such a framework that tries to remove the
complexity and drudgery of MVC, while still allowing you to realize all the benefits.” - DHH (David Heinemeier Hansson)
MVC Demystified 22
Model: ActiveRecord
• ActiveRecord is a design pattern. (Martin Fowler)• Object wraps a row in the database.• Encapsulates data access.• Contains business logic.
• Handles relationships.• Handles validation.
MVC Demystified 23
ActiveRecord Code
MVC Demystified 24
View: ActionView
• Renders the view.• Both .rhtml and .rxml files.• Provides master layouts.• Uses ERb (embedded ruby) for templating and control.
MVC Demystified 25
ActionView Code
MVC Demystified 26
Controller: ActionController
• Controls application flow.• Controls which view to use.• Makes data available as instance variables @var• Contains several actions/defs• Controls rendering and redirection.
MVC Demystified 27
ActionController Code
MVC Demystified 28
All Together Now
http://wiki.rubyonrails.org/
MVC Demystified 29
How it works
• your_domain.com/controller/action/id• Controller has actions.• View directory has directory for each
controller.• Same action name as .rhtml file• your_domain.com/bikes/edit/2
– Controller: bikes– Action: edit– Id: 2
MVC Demystified 30
Rails Concepts
• DRY (Don’t Repeat Yourself)• Convention of configuration• Generators script/generate generator_name • :symbols or ‘symbols’
– represents names and strings
• YAML machine parsable human readable.
MVC Demystified 31
More ActiveRecord
• Automatically maps to a table– No XML configs– Each row is an Object
• Several databases supported.• Provides
– CRUD: Create, Read, Update, Destroy– Getters/Setters– Validation: validates_presence_of, validates_format_of– Callbacks: hooks into lifecycle, trigger events before/after– Finding: Person.find(:all, :conditions => [ "category IN (?)",
categories], :limit => 50)
MVC Demystified 32
More ActiveRecord Code
• Callbacks• Relations• Validation
MVC Demystified 33
More ActionView
• Layouts– app/view/layouts/application.rhtml
• Partials– _partial_view.rhtml – Reuse of common view elements
• Helpers– Helper module per Controller– Contains Ruby code
MVC Demystified 34
More ActionView Code
MVC Demystified 35
More ActionController
• Filters– before, after and around processing hooks
• Routes– Replaces apache mod_rewrite– config/routes.rb
• Caching– Has caching to improve performance
• page• action• fragment
MVC Demystified 36
More ActionController Image
MVC Demystified 37
Pitfalls: Excess
• Model: God Object (anti-pattern)– Too much logic in a Model, one Model too powerful.
• View: Too much logic in View• Controller: Anemic Domain Model (anti-pattern)
– Too much business logic in Controller.
• Cargo cult programming – (over-)applying a design principle blindly without
understanding the reasons. - (wikipedia)
MVC Demystified 38
Testing Rails
• Rails has great testing framework ‘baked in’ the framework– Test stubs created when code is ‘generated’.
• Unit to test models. – test/unit
• Functional to test views/controllers– test/functional
• Fixtures– Supply test data (yaml)
MVC Demystified 39
Unit Testing
• Separate ‘test’ database is used.
• Fixtures supply data• Variety of ‘assert’
statements.
MVC Demystified 40
Functional Testing
• Fixture• Setup• Tests
MVC Demystified 41
Rake
• Rake is your friend.– Database tasks– Deployment– Documentation– Testing– Cleanup
MVC Demystified 42
MVC Around The Web
• Java– Struts, Spring
• .NET– MonoRail (inspired by Rails)
• Perl– Catalyst, MayPole
• Php– CakePHP, Code Igniter
• Python– Django, TurboGears
• Ruby– Nitro, Wee
MVC Demystified 43
Questions?
• MVC• Rails• Pix
Top Related