at-m42:casestudies:cs05
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
at-m42:casestudies:cs05 [2009/04/28 07:13] – eechris | at-m42:casestudies:cs05 [2009/04/30 18:32] – eechris | ||
---|---|---|---|
Line 3: | Line 3: | ||
In this case study, we re-develop part of the adventure game in the Grails web framework. This will demonstrate the productivity gains possible from a good, well-design application development framework, and further emphasizes the power of the Groovy programming language and the so-called lightweight enterprise frameworks. The presentation has been designed as an // | In this case study, we re-develop part of the adventure game in the Grails web framework. This will demonstrate the productivity gains possible from a good, well-design application development framework, and further emphasizes the power of the Groovy programming language and the so-called lightweight enterprise frameworks. The presentation has been designed as an // | ||
- | The presentation was inspired by case-study presented in Chapter 16 " | + | The presentation was inspired by case-study presented in Chapter 16 " |
===== A Short Introduction to Grails ===== | ===== A Short Introduction to Grails ===== | ||
Line 40: | Line 40: | ||
We shall take the example that we have developed over the previous four case studies, and re-implement //Player// and //Item// administration for an adventure game in Grails. | We shall take the example that we have developed over the previous four case studies, and re-implement //Player// and //Item// administration for an adventure game in Grails. | ||
- | * [[at-m42: | + | * [[at-m42: |
* [[#Step 2: Create project]] | * [[#Step 2: Create project]] | ||
+ | * [[#Step 3: Run Application]] | ||
+ | * [[#Step 4: Create First Domain Class]] | ||
+ | * [[#Step 5: Add some properties and a Controller]] | ||
+ | * Further steps ... to be written! | ||
- | An index to the source code for all the examples in this case study is [[/ | + | An index to the source code for all the examples in this case study is [[/ |
- | ===== Step 2: Create project | + | ==== Step 2: Create project ==== |
The first step in any system development with Grails is the creation of a project. In my case, this will be //game// project. Change to the development directory then issue the command '' | The first step in any system development with Grails is the creation of a project. In my case, this will be //game// project. Change to the development directory then issue the command '' | ||
Line 112: | Line 116: | ||
**Figure 2**: The files generated by '' | **Figure 2**: The files generated by '' | ||
- | ===== Iteration I: Specification and Map Implementation ===== | + | An explanation of what all these files are is shown in Table 1. |
- | | + | *Table 2*: Directory structure |
- | | + | |
- | * Display details | + | |
- | * Display the items being carried by a given player | + | |
- | * Display the number of players who are carrying a particular item | + | |
- | <code groovy> | + | ^ Directory ^ Content ^ |
- | def banquetingRoom = [ | + | | '' |
- | 'bread' | + | | <html>& |
- | 'fork' | + | | < |
- | 'sword' | + | | < |
- | 'magic amulet' | + | | < |
- | 'apple' | + | | < |
- | ] | + | | < |
- | </code> | + | | < |
+ | | < | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | < | ||
+ | | < | ||
+ | | '' | ||
+ | | < | ||
+ | | < | ||
+ | | < | ||
+ | | < | ||
+ | | < | ||
+ | | < | ||
+ | ==== Step 3: Run Application ==== | ||
- | <code groovy> | + | <cli prompt=">"> |
- | def addItem(location, item) { | + | e: |
- | | + | Welcome to Grails 1.1 - http:// |
+ | Licensed under Apache Standard License 2.0 | ||
+ | Grails home is set to: d: | ||
+ | |||
+ | Base Directory: E: | ||
+ | Running script d: | ||
+ | Environment set to development | ||
+ | [mkdir] Created dir: e: | ||
+ | [groovyc] Compiling 6 source files to e: | ||
+ | [mkdir] Created dir: e: | ||
+ | [native2ascii] Converting 11 files from E: | ||
+ | | ||
+ | | ||
+ | Running Grails application.. | ||
+ | Server running. Browse to http:// | ||
+ | </cli> | ||
+ | |||
+ | Now connect to http:// | ||
+ | |||
+ | {{: | ||
+ | |||
+ | | ||
+ | |||
+ | ==== Step 4: Create First Domain Class ==== | ||
+ | |||
+ | Run command '' | ||
+ | <cli prompt=">"> | ||
+ | e: | ||
+ | Welcome to Grails 1.1 - http:// | ||
+ | Licensed under Apache Standard License 2.0 | ||
+ | Grails home is set to: d: | ||
+ | |||
+ | Base Directory: E: | ||
+ | Running script d: | ||
+ | Environment set to development | ||
+ | Created DomainClass for Item | ||
+ | Created Tests for Item | ||
+ | e: | ||
+ | </ | ||
+ | |||
+ | Creates a domain class '' | ||
+ | <code groovy 1|Listing 2: Domain class Item.groovy | ||
+ | class Item { | ||
+ | |||
+ | | ||
+ | } | ||
} | } | ||
- | </ | + | </ |
+ | |||
+ | It also creates a '' | ||
+ | <code groovy 1|Listing 3: Test class for domain class Item.groovy (at-m42/ | ||
+ | import grails.test.* | ||
+ | |||
+ | class ItemTests extends GrailsUnitTestCase { | ||
+ | protected void setUp() { | ||
+ | super.setUp() | ||
+ | } | ||
+ | |||
+ | protected void tearDown() { | ||
+ | super.tearDown() | ||
+ | } | ||
+ | |||
+ | void testSomething() { | ||
- | <code groovy> | + | } |
- | readNumberOfPlayersHoldingItem(location, | + | |
- | | + | |
} | } | ||
- | </ | + | </ |
- | <code groovy 1| A simple adventure game (at-m42/Examples/case-study-02/location1.groovy)> | + | Unit test and integration tests can be run automatically from the command line using '' |
- | extern> http://www.cpjobling.org.uk/~eechris/at-m42/ | + | |
- | </ | + | |
- | ===== Iteration II: Implementation of a Text-based User Interface ===== | + | ==== Step 4: Add some properties and a Controller |
- | <code groovy 1| Text-based user interface | + | Each domain class is simply a Plain-Old-Groovy-Object with one major difference. If we add some properties, these will be automatically persisted to a database. |
- | extern> http:// | + | <code groovy 1|Listing 4: Item class with properties |
- | </ | + | class Item { |
- | ===== Iteration III: Implementation With Closures ===== | + | static constraints |
+ | } | ||
+ | |||
+ | String name | ||
+ | String description | ||
+ | Integer weight | ||
- | < | + | String toString() { return " |
- | extern> | + | } |
+ | </code> | ||
+ | |||
+ | If we now add a controller, we will be able to create some objects via the web interface. | ||
+ | <cli prompt=">"> | ||
+ | e:\dev\at-m42-2009\Case-Studies\case-study-05\game>grails create-controller Item | ||
+ | |||
+ | Welcome to Grails 1.1 - http://grails.org/ | ||
+ | Licensed under Apache Standard License 2.0 | ||
+ | Grails home is set to: d: | ||
+ | |||
+ | Base Directory: E: | ||
+ | Running script d: | ||
+ | Environment set to development | ||
+ | Created Controller for Item | ||
+ | [mkdir] Created dir: E: | ||
+ | Created Tests for Item | ||
+ | e: | ||
+ | </cli> | ||
+ | |||
+ | As you can see this creates a new controller | ||
+ | <code groovy 1|Listing 5: ItemController class (at-m42/Case-Studies/ | ||
+ | class ItemController { | ||
+ | |||
+ | def index = { } | ||
+ | } | ||
</ | </ | ||
+ | It also creates another '' | ||
- | ===== Exercises ===== | + | Before we use this, we'll make Grails generate the necessary views " |
+ | <code groovy 1|Listing 6: ItemController class with dynamic scaffolding (at-m42/ | ||
+ | class ItemController { | ||
+ | |||
+ | def scaffold | ||
+ | } | ||
+ | </ | ||
+ | I found that at this step, I had to stop the running grails application, | ||
+ | After this runs, you will be able to browse to http:// | ||
---- | ---- | ||
[[at-m42: | [[at-m42: |
at-m42/casestudies/cs05.txt · Last modified: 2011/01/14 12:59 by 127.0.0.1