Upload
cory-foy
View
476
Download
0
Tags:
Embed Size (px)
DESCRIPTION
In this talk from a Tampa 8th Light University, Senior Craftsman Cory Foy details the design patterns used in Rails, and shows their use and implementation while reference Fowler's PoEAA and Alexander's Timeless Way of Building
Citation preview
Patterns in Rails
Cory Foy@cory_foy
Friday, June 21, 13
Patterns
Friday, June 21, 13
Friday, June 21, 13
“The specific patterns out of which a building or a town is made may be alive or dead. To the extent they are
alive, they let our inner forces loose, and set us free; but when they are dead, they keep us locked in inner
conflict.”Christopher Alexander - “The Timeless Way of Building”
Friday, June 21, 13
“And it turns out...behind all processes which allow us to make buildings live, there is a single common process.
But though this method is precise, it cannot be used mechanically.
Indeed it turns out, in the end, that what this method does is simply free us from all method.”
Christopher Alexander - “The Timeless Way of Building”
Friday, June 21, 13
“...we have so far beset ourselves with rules, and concepts, and ideas of what must be done to make a
building or a town alive, that we have become afraid of what will happen naturally, and convinced that we must
work within a “system” and with “methods” since without them our surroundings will come tumbling down
in chaos.”Christopher Alexander - “The Timeless Way of Building”
Friday, June 21, 13
Naming something the name of a pattern does not make it that pattern
Friday, June 21, 13
Friday, June 21, 13
Pattern Name
Intent
Motivation
Consequences
Implementation
Elements of a pattern
Friday, June 21, 13
Rails
Friday, June 21, 13
Friday, June 21, 13
“From the beginning, the Rails framework turned web development on its head with the insight that the vast majority of time
spent on projects amounted to meaningless sit-ups.
Instead of having the time to think your domain-specific code, you’d spend the first
few weeks of a project deciding meaningless details.”
- Yahuda Katz, The Rails 3 Way
Friday, June 21, 13
“[Rails is] not a blank slate equally tolerant of every kind of expression. On the
contrary, it trades that flexibility for the convenience of ‘what most people need
most of the time to do most things’”
- David Heinmeier Hansson - The Rails 3 Way
Friday, June 21, 13
Not to be confused with Ruby (programming language).
- Wikipedia
Friday, June 21, 13
Ruby on Rails, often simply Rails, is an open source web application framework which
runs on the Ruby programming language. It is a full-stack framework: it allows creating
pages and applications that gather information from the web server, talk to or query the database, and render templates out of the box. As a result, Rails features a routing system that is independent of the
web server.
Friday, June 21, 13
Ruby on Rails emphasizes the use of well-known software engineering patterns and principles, such as active record pattern,
convention over configuration (CoC), don't repeat yourself (DRY), and model-view-
controller (MVC).
Friday, June 21, 13
RESTRouting
ValidationsControllersAction View
Action MailerActive RecordAuthentication
Session ManagementBackground Processing
Caching and PerformanceExtending Rails with Plugins
Friday, June 21, 13
Model-View Controller
Active Record
Template View
Front Controller
Single Table Inheritance
Friday, June 21, 13
Friday, June 21, 13
Pattern Name
Intent
Motivation
Consequences
Implementation
Elements of a pattern
Friday, June 21, 13
Name Model-View Controller
IntentSeparate the presentation of the model from the view, and separate the wiring
of the model to the view
MotivationA web application which has many
models that need to be interacted with by end users
ImplementationView, Model and Controller should be in separate classes. View should depend
on model, but not vice versa
ConsequencesMay be overkill for simple systems
where the model has very little behavior
Friday, June 21, 13
Friday, June 21, 13
Name Front Controller
IntentConsolidate request handling by
channeling requests through a single handler object
MotivationCentralize the logic and handling for the many things that need to happen for a
complex web site
ImplementationCreate a class to handle all calls for a web site, and determines the correct
classes to dispatch the request to
ConsequencesMore complicated than a Page
Controller. Can turn into a God class
Friday, June 21, 13
/rails/actionpack/lib/action_dispatch/routing/route_set.rbFriday, June 21, 13
Name Mapper
IntentSet up communications between two subsystems that need to stay ignorant
of each other
MotivationAllow two independent systems to be able to talk to each other without them being dependent on each other or the mapper
ImplementationInitialize the mapper (maybe using a Front Controller) and configure the communication between systems
ConsequencesMay be difficult to initialize and configure the mapper since the subsystems can’t know about it
Friday, June 21, 13
/rails/actionpack/lib/action_dispatch/routing/route_set.rbFriday, June 21, 13
Name Active Record
IntentAn object that wraps a row in a
database table, encapsulates the access, and adds domain logic on that data
MotivationSimplify the need to persist data by putting the data access logic in the
domain object
ImplementationEach class is responsible for saving and
loading to the database, and for any domain logic that acts on the data
ConsequencesBest when domain logic isn’t too complex,
and when model corresponds directly to the database table. Couples system to DB
Friday, June 21, 13
Friday, June 21, 13
Friday, June 21, 13
Friday, June 21, 13
Name Single Table Inheritance
IntentRepresent an inheritance hierarchy of classes
as a single table that has columns for all of the fields of the classes
MotivationMinimize the number of joins when
representing an inheritance structure in a relational database
ImplementationA single table contains all of the fields for the subclasses. Classes may or may
not use all of the fields.
ConsequencesSince not all columns are used, may be confusing. May cause large tables. Only
have a single namespace for fields.
Friday, June 21, 13
Friday, June 21, 13
Name Template View
IntentRender information into HTML by
embedding markers in an HTML page
MotivationAllow HTML pages to be created as if
they were static, but still be able to insert dynamic data
ImplementationEmbed placeholders into a static HTML
page when it’s written. Replace placeholders with content at runtime.
ConsequencesEasy to put complicated logic in the view. Harder to test since generally
designed to work within a web server
Friday, June 21, 13
Friday, June 21, 13