THÔNG TIN TÀI LIỆU
www.it-ebooks.info
Download at Boykma.Com
www.it-ebooks.info
Enterprise Rails
Download at Boykma.Com
www.it-ebooks.info
Other resources from O’Reilly
Related titles
Advanced Rails
Ajax and Rails
Learning Rails
Learning Ruby
Rails Cookbook
Rails: Up and Running
Ruby Cookbook
The Ruby Programming
Language
oreilly.com
oreilly.com is more than a complete catalog of O’Reilly books.
You’ll also find links to news, events, articles, weblogs, sample
chapters, and code examples.
oreillynet.com is the essential portal for developers interested in
open and emerging technologies, including new platforms, pro-
gramming languages, and operating systems.
Conferences
O’Reilly brings diverse innovators together to nurture the ideas
that spark revolutionary industries. We specialize in document-
ing the latest tools and systems, translating the innovator’s
knowledge into useful skills for those in the trenches. Visit
conferences.oreilly.com for our upcoming events.
Safari Bookshelf (safari.oreilly.com) is the premier online refer-
ence library for programmers and IT professionals. Conduct
searches across more than 1,000 books. Subscribers can zero in
on answers to time-critical questions in a matter of seconds.
Read the books on your Bookshelf from cover to cover or sim-
ply flip to the page you need. Try it today for free.
,roadmap.14319 Page ii Friday, October 10, 2008 2:21 PM
Download at Boykma.Com
www.it-ebooks.info
Enterprise Rails
Dan Chak
Beijing
•
Cambridge
•
Farnham
•
Köln
•
Sebastopol
•
Taipei
•
Tokyo
Download at Boykma.Com
www.it-ebooks.info
Enterprise Rails
by Dan Chak
Copyright © 2009 Dan Chak. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions
are also available for most titles (http://safari.oreilly.com). For more information, contact our corporate/
institutional sales department: (800) 998-9938 or corporate@oreilly.com.
Editor: Mike Loukides
Production Editor: Loranah Dimant
Copyeditor: Colleen Gorman
Proofreader: Loranah Dimant
Indexer: Joe Wizda
Cover Designer: Karen Montgomery
Interior Designer: David Futato
Illustrator: Robert Romano
Printing History:
October 2008: First Edition.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of
O’Reilly Media, Inc. Enterprise Rails, the image of a sturgeon fish on the cover, and related trade dress
are trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a
trademark claim, the designations have been printed in caps or initial caps.
Java™ is a trademark of Sun Microsystems, Inc.
While every precaution has been taken in the preparation of this book, the publisher and author assume
no responsibility for errors or omissions, or for damages resulting from the use of the information con-
tained herein.
ISBN: 978-0-596-51520-1
[M]
1223662105
Download at Boykma.Com
www.it-ebooks.info
Table of Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
1. The Big Picture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
What Is Enterprise? 1
Growing Slowly 3
Understanding All the Pieces 5
Persistence Layer 5
Application Layer 8
Caching Layer 11
Messaging System 16
Web Server 16
Firewall 16
2. Organizing with Plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Benefits 20
Writing Your Own Plugins 21
Core Enhancements 22
Custom Extensions 26
Deployment 30
svn:externals 30
3. Organizing with Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Files and Directories 32
Module Boundaries for Namespacing 33
ActiveRecord Associations Between Modules 34
Reciprocal Relationships 35
Modules Presage Services 36
Ensuring Proper Load Order 38
Exercises 39
Refactor Steps 39
High-Level Module Refactor 39
Detangling Utility Methods 40
v
Download at Boykma.Com
www.it-ebooks.info
4. Database As a Fortress . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Your Database Is an Application, Too 42
“One Framework to Rule Them All” 42
“No One in My Company Will Ever Write a Bug” 42
“This Rails Application Is the Only Application on Earth” 43
Sit Atop the Shoulders of Giants 43
Choosing the Right RDBMS 44
A Note on Migrations 44
Dispelling Myths 46
Operations and Reporting 47
5. Building a Solid Data Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Theatre Tickets 51
Starting Simple 52
Constraints 54
Mythbusting 63
Referential Integrity 64
Intro to Indexing 70
6. Refactoring to Third Normal Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Third Normal Form 73
Timing Is Key 77
Refactoring: Inheritance and Mixins 78
Exercises 82
Refactor Steps 82
7. Domain Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Dealing with Zip Codes 87
Strategy Pattern with Domain Tables 89
Refactor from Day One 93
8. Composite Keys and Domain Key/Normal Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Why Composite Natural Keys Matter 97
Spotting Composite Keys 99
Atop the Shoulders of… 101
Migrating to Domain Key/Normal Form 102
Using Composite Keys in Rails 105
Deferrable Foreign Key Constraints 109
Understanding the Trade-offs 111
Exercises 113
Refactor Steps 113
Single Column Refactor 114
vi | Table of Contents
Download at Boykma.Com
www.it-ebooks.info
Multiple Column Refactor 115
9. Guaranteeing Complex Relationships with Triggers . . . . . . . . . . . . . . . . . . . . . . . . 117
Constraint Checking with Triggers 117
Anatomy of a PL/pgSQL Function 121
10. Multiple Table Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
The Problem 127
What Is Polymorphism? 129
Inheritance and Persistence 131
Single Table Inheritance 132
Multiple Table Inheritance 133
XOR on Columns 135
Elegant MTI in Rails 137
Factory Classes 143
Exercises 144
Refactor Steps 145
Refactoring STI 145
Refactoring: polymorphic => true 145
11. View-Backed Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Database Views 148
Creating a View 149
Basing a Model on a View 149
Considerations 150
Insert, Update, Delete 151
References and Constraints 152
Indexing 153
Exercises 153
Refactor Steps 154
12. Materialized Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Materialized View Principles 156
A View to Materialize 157
Getting into Form 158
The Target Table 160
Refresh and Invalidation Functions 161
Time Dependency 163
Who Pays the Price? 165
Triggered Refreshes and Invalidations 168
Movie Showtimes 169
Hiding the Implementation with the Reconciler View 174
Periodic Refreshes 177
Table of Contents | vii
Download at Boykma.Com
www.it-ebooks.info
Cache Indexes 178
Results 179
Cascading Caches 180
Exercises 180
13. SOA Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
What Is SOA? 183
Why SOA? 186
Shared Resources 187
Reduce Database Load 190
Scalability II: Caching Is Tricky 196
Reduce Local Complexity 197
In Summary 200
Exercises 200
14. SOA Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Service Considerations 201
Implementation Details Are Hidden 202
API Is Accessible 204
API Design Best Practices 205
Send Everything You Need 205
Limit Round Trips 207
Look for Opportunities for Parallelization 209
Send as Little as Possible 210
REST Versus XML-RPC Versus SOAP 212
XML-RPC 212
SOAP 214
15. An XML-RPC Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
ActionWebService and Rails 2.0 217
Creating an Abstraction Barrier 218
ActiveRecord As the Physical Model Layer 218
The Object Model Layer 220
Defining the API 225
More Testing 230
The Client Plugin 232
Shared Code 234
The Client Singleton 234
Integration Testing 236
16. Refactoring to Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
An Orders Service 239
Integrating with the MoviesService 251
viii | Table of Contents
Download at Boykma.Com
www.it-ebooks.info
[...]... 1, we start out by taking a tour of an ideal enterprise systems layout, highlighting all of the elements that aren’t Rails, as well as noting the various places where Rails can fit in The rest of this book can be divided principally into three major sections The first deals with the Rails framework itself The next few chapters examine Rails itself While Rails gives you a Model-View-Controller (MVC)... Ruby and Rails to tie together all the other xvi | Preface Download at Boykma.Com parts of your application stack: the database, your servers and clients, other services, and users of your application In this book, we begin by putting Rails in the correct context The purpose of the original Rails book, Agile Web Development with Rails by Dave Thomas et al (Pragmatic Bookshelf), was to sell Rails to... leaders in the Rails community itself have advocated not worrying about scaling until you really, really, really need to They say it’s an unnecessary waste of time up front and that Rails scales easily because it’s a share-nothing architecture This, of course, is baloney In the early days, this was good propaganda to get Rails onto developers’ desktops, but today it is simply hurting Rails s image as... If you are new to Rails, the first book to read is Agile Web Development with Rails by Dave Thomas et al (Pragmatic Bookshelf) The book you have in your hands, by contrast, is not a how-to guide for writing your first Rails application This should be the second book you read This book deals in larger concepts, the formulas for how pieces fit together It is not a compendium of the Rails API or a reference... to prove themselves worthy against This doesn’t mean that a Rails application cannot stand up to the challenges imposed by constant traffic and large scale Of course Rails can However, there has been a dearth of public examples, and by the same token, there has not been much public discourse within the Rails community about how to design Rails applications to scale to the same levels that have been... JavaScript, and of course, the language du jour for the application itself But with Ruby on Rails, the bar seems to have dropped almost through the floor You can learn only Rails, and the development of the database, HTML, and JavaScript layers are waved away by the Rails magic It’s an excellent sales pitch for Rails, but is it true? As much as we might like it to be, the sad truth is that if your goal... “architecture” for enterprise web applications, from the ground up What are the topics of web architecture, and why aren’t they found in most books on Rails? In truth, the success or failure of a web application has only partially to do with what is classically called the application layer This may come as a surprise to those who have cut their development teeth on Rails, because the Rails view is that... so definitively that the rest of the stack becomes obsolete? Rails is redefining the landscape of web development Ruby is a wonderful programming language, and the Rails framework does dramatically increase productivity However, databases, legacy systems, and third parties who don’t share our love of Rails are a fact of life Even though Rails does, at times, seem downright magical, it does not make... hardware to achieve those goals? Before diving into the pieces of an enterprise system, or discussing how to build one, a good starting point is to simply define enterprise Unfortunately, that is not an easy task There is no particular set of tools that, if used, will make your architecture qualify as enterprise, even if the word enterprise is in the product names of the tools you use The big companies... their software stack, but they are definitely still enterprise. ” Similarly, there is no single configuration of pieces that together spell enterprise If you looked at the configuration of Google’s servers and compared their it to Amazon’s, the two would look quite different But they are both enterprises nonetheless—it just happens that the two enterprises have different goals, and therefore need different . Boykma.Com
www.it-ebooks.info
Enterprise Rails
Download at Boykma.Com
www.it-ebooks.info
Other resources from O’Reilly
Related titles
Advanced Rails
Ajax and Rails
Learning Rails
Learning. Boykma.Com
www.it-ebooks.info
Enterprise Rails
Dan Chak
Beijing
•
Cambridge
•
Farnham
•
Köln
•
Sebastopol
•
Taipei
•
Tokyo
Download at Boykma.Com
www.it-ebooks.info
Enterprise Rails
by
Ngày đăng: 23/03/2014, 02:20
Xem thêm: Enterprise Rails docx