| | |
|
|
|
Project Zero resource model overview
The Zero resource model (ZRM) extension is a layer of abstraction between the REST and persistence. ZRM reduces the amount of boilerplate code that is repetitive for certain types of situational applications and simplifies resource handler development to a simple "model" definition. The typical resource-handler features, such as handlers for CRUD, persistence, and content negotiation are provided. ZRM is not yet complete, but there is enough function to demonstrate ZRM to collect feedback.
Objectives
The objectives of ZRM are to provide:
- A constrained set of APIs that encourage a RESTful application architecture
- A data model that maps well to REST and feeds
- Default top-down data mapping scheme and automatic table creation
- First class support for accessing data in a RESTful fashion both programmatically and using HTTP
- Resources defined in the data model can be accessed via HTTP with no manual coding
- First class support for accessing data as feeds
- Robust frameworks for persistence, validation, and serialization
ZRM implements a significantly constrained REST model to liberate developer attention from mundane persistence details to the application. ZRM is not an attempt to introduce a general purpose ORM. For example, its data model is geared toward REST rather than to an arbitrary POJO model by asserting fundamental persistence assumptions.
Elements of ZRM
The ZRM programming model consists of the following parts:
- Configuring ZRM
- Setting up and configuring resource models
- Resource model declaration
- Declaring the data shape of the resource model
- Programmatic Model API
- Interacting with resource models using the programmatic model API
- HTTP REST API
- Interacting with resource models using the HTTP REST API
Configuring the Project Zero resource model
Configuring ZRM consists of the database configuration, Resource model declaration, and the resource event handler configuration. You can find an example ZRM application in the employee.resource.demo package in the Project Zero samples.
Resource model declaration
The Resource model declaration is a collection of resource definitions. It contains the definitions for fields, constraints, and filtered collections among other optional configuration. The resource definitions are datastore independent and can be shared across applications. They can be created and manipulated either declaratively or programmatically or using HTTP.
// Resource Model declaration
// File: /app/models/employees.groovy
fields = [
name: [type: 'CharField', max_length:50],
birthdate: [type: 'DateField'],
state: [type: 'USStateField'],
phone: [type: 'USPhoneField'],
]
Programmatic model API
The Model API allows accessing resources programmatically using collection-like constructs. In addition to the basic CRUD programming model, ZRM introduces a list-function and therefore we call this LCRUD model. The list function allows for listing and filtering collection members using simple conditions (for example, published_between: ['01-01-2003'..'12-31-2007']). A filter can be applied to a collection to create a new collection that is a subset of the original. The filter conditions can be passed in dynamically, therefore eliminating the need for writing a different service for each subset of the data that the client might need.
import zero.resource.TypeCollection
...
def employees = TypeCollection.retrieve('employees')
def allEmployees = employees.list()
def employee = employees.retrieve(1)
def someEmployees = employees.list(firstname__contains: 'e')
HTTP REST API
The HTTP REST API is symmetrical to the programmatic API. The default result rendering is for JSON but other formats can also be specified, such as Atom, Basic XML, HTML, and microformats.
http://localhost:8081/resources/employees
http://localhost:8081/resources/employees/1
http://localhost:8081/resources/employees?firstname_contains=e
The HTTP access is not enabled by default. It can be enabled by introducing a resource event handler that, in its simplest form, delegates the HTTP requests to a system-provided ZRM handler with only one line of application code.
|
|
r20 - 09 Feb 2008 - 17:56:23 - paynel
|
|
|
| | |