110 likes | 400 Vues
Introduction to Models using the Play Framework. Philip Johnson Collaborative Software Development Laboratory Information and Computer Sciences University of Hawaii Honolulu HI 96822. Model-View-Controller in Play. H2. MySQL. Postgres. Twitter Bootstrap. Model - models.* package
E N D
Introduction to Modelsusing the Play Framework Philip Johnson Collaborative Software Development Laboratory Information and Computer Sciences University of Hawaii Honolulu HI 96822
Model-View-Controller in Play H2 MySQL Postgres Twitter Bootstrap Model - models.* package - EBean ORM Controller - routes file - controllers.* package Browser View - views/*.scala.html
What is the Model? • A representation of "domain knowledge" • In Play, implemented as a set of Java class instances. • Think objects and relationships • The model should represent the domain well enough to answer any queries the user might ask of the application. • Two basic problems: • Designing the right model (domain abstraction) • Making it persistent
Persistency approaches • Relational databases • Examples: MySQL, Postgres, Oracle • Well understood, very popular • Object-relational impedance mismatch • Object-oriented databases • Examples: Gemstone, db4o • Solves impedance mismatch problem • Failed to gain traction. • NoSQL persistent stores • Examples: Hadoop, MongoDB, Neo4J • Higher scalability; looser consistency
Addressing the O/R Impedance Mismatch: Object-Relational Mappers (ORMs) • Provides a language allowing the developer to: • Map classes to tables • Map fields to columns • Specify arity (1-1, 1-Many, Many-1, Many-Many) • Provides API calls to: • Take class instances, save as rows in tables • Retrieve row, convert to class instance • Query the db "conveniently" (w/o SQL)
Our ORM: EBean • Open source (LGPL) Java ORM. • Uses JPA annotations (@entity, @OneToOne, etc.) • Support for Postgres, MySQL, and H2 (default) • Supports "autofetch"—automatic query tuning • Can reduce data retrieved from DB based upon recognition that caller does not refer to it. • Default ORM provided by Play
Model Design Process • 1. Create a simple data model diagram • Use the free LucidChart plugin to Google Docs • Determine the entities • Determine relationships between entities (1-1, 1-Many, Many-1, Many-Many) • 2. In the models package: • define Java classes for all of your entities • create fields corresponding to relationships • 3. In the tests package: • create a JUnit test class that verifies that your model can be created, persisted, modified, and queried appropriately.
Heuristics for model development • http://ics613s13.wordpress.com/modules/web-application-development/model-development-hints/
Next step • Watch the demo Model development screencast