[QueueNews] ORM in Dynamic Languages

QueueNews queuenews at acmqueue.com
Mon Jun 30 08:00:02 PDT 2008


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
. . . . . . . . . . . . . . . . . . . . . . . .
   Queue E-Mail Newsletter
   for the Week of Jun/30/2008
. . . . . . . . . . . . . . . . . . . . . . . .
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-------------------------------------------------
      Sponsored by

      Oracle
      2008 O'Reilly Open Source Convention
      ACM

/----------------------------------------------------------\

--- Embedded Technologies

Oracle's embedded technologies for ISVs and OEMs
http://www.oracle.com/go/?&Src=5951533&Act=13

\----------------------------------------advertisement------/


Latest Articles:


ORM in Dynamic Languages
O/R mapping frameworks for dynamic languages such as Groovy provide a
different flavor of ORM that can greatly simplify application code.
http://acmqueue.com/rd.php?c.539
   (scroll down to read an excerpt from this article)


Bridging the Object-Relational Divide
ORM technologies can simplify data access, but be aware of the
challenges that come with introducing this new layer of abstraction.
http://acmqueue.com/rd.php?c.538

/----------------------------------------------------------\

Discover a Decades Worth of Open Source Innovation at OSCON O'Reilly
Open Source Convention, July 21-25, Portland, OR

OSCON is a vibrant meeting ground for the open source community to
inspire, debate, make deals, motivate, and connect face to face. The
conference features the key players and issues influencing open source
today, and explores the greatest potential for open source tomorrow.
Use
code "os08acm" to save 15% off registration fees.
http://conferences.oreilly.com/oscon

\----------------------------------------advertisement------/

New article on ACM Queue:
ORM in Dynamic Languages
http://acmqueue.com/rd.php?c.539
O/R mapping frameworks for dynamic languages such as Groovy provide a
different flavor of ORM that can greatly simplify application code.
by Chris Richardson, Consultant

>From the Object-Relational Mappers issue, vol. 6, no. 3 - May/June 2008

article excerpt:
A major component of most enterprise applications is the code that
transfers objects in and out of a relational database. The easiest
solution is often to use an ORM (object-relational mapping) framework,
which allows the developer to declaratively define the mapping between
the object model and database schema and express database-access
operations in terms of objects. This high-level approach significantly
reduces the amount of database-access code that needs to be written and
boosts developer productivity.
Several ORM frameworks are in use
today. For example, the Hibernate,^1 TopLink,^2 and
OpenJPA^3 frameworks are popular with Java developers, and
NHibernate^4 is used by many .NET developers. Two newer ORM
frameworks that have recently received a lot of attention from
enterprise developers are Active Record^5 for Ruby^6
and GORM (Grails Object Relational Mapping)^7 for
Groovy.^8 These new frameworks differ from traditional ORM
frameworks in that they are written in dynamic languages that allow new
program elements to be created at runtime. Active Record and GORM use
these dynamic capabilities in ways that can significantly simplify an
application.
This article looks at how GORM works. It compares
and contrasts GORM with Hibernate, focusing on three areas: defining
object-relational mapping; performing basic save, load, and delete
operations on persistent objects; and executing queries. It describes
how GORM leverages the dynamic features of Groovy to provide a
different
flavor of ORM that has some limitations but for many applications is
much easier to use.


 Groovy, Grails, and GORM

GORM is
the persistence component of Grails, which is an open source framework
that aims to simplify Web development. Grails is written in Groovy, a
dynamic, object-oriented language that runs on the JVM (Java Virtual
Machine). Because Groovy interoperates seamlessly with Java, Grails can
leverage several mature Java frameworks. In particular, GORM uses
Hibernate, a popular and robust ORM framework.
GORM, however, is
much more than a simple wrapper around the Hibernate framework.
Instead,
it provides a very different kind of API. GORM is different in two
ways.
First, the dynamic features of the Groovy language enable GORM to do
things that are impossible in a static language. Second, the pervasive
use of CoC (Convention over Configuration) in Grails reduces the amount
of configuration required to use GORM. Let's look at each of these
reasons in more detail.


 Dynamic Groovy

GORM relies
heavily on the dynamic capabilities of the Groovy language. In
particular, it makes extensive use of Groovy's ability to define
methods
and properties at runtime. In a static language such as Java, a
property
access or a method invocation is resolved at compile time. In
comparison, Groovy does not resolve property accesses and method
invocations until runtime. A Groovy application can dynamically define
methods and properties.
Groovy provides a couple of different
ways to add methods and properties to a class at runtime. The simplest
approach is to define propertyMissing() or methodMissing() methods. The
propertyMissing() method is called by the Groovy runtime when the
application attempts to access an undefined property. Similarly, the
methodMissing() method is called when the application calls an
undefined
method. These methods enable an object to behave as if the property or
method existed.
The second and more sophisticated approach is to
use the wonderfully named ExpandoMetaClass. Every Groovy class has a
metaClass property that returns an ExpandoMetaClass. An application can
add methods or properties to a class by manipulating this metaclass.
For
example, figure 1 is a code snippet that adds a method to the String
class that concatenates a string with itself.

[]
src="http://acmqueue.com/figures/issue049/fig1.jpg" width="480"
height="124" />

This code snippet obtains the String metaclass
and assigns to its doubleString property a closure (a kind of anonymous
method) that implements the new method.
Groovy applications often
use methodMissing() and ExpandoMetaClass together. The first time an
undefined method is invoked, missingMethod() defines the method using
the ExpandoMetaClass. The next time around, the newly defined method is
called directly, thereby bypassing the relatively expensive
missingMethod() mechanism.
Later you will see how Grails uses
methodMissing() and ExpandoMetaClass to inject persistence-related
methods and properties into domain classes at runtime, thereby
simplifying application code.
Read the rest of this article at acmqueue.com
http://acmqueue.com/rd.php?c.539

/----------------------------------------------------------\

Join ACM

A Special Offer to Join ACM for Queue Readers
http://www.acm.org/joinacm2
\----------------------------------------advertisement------/

See all the latest articles and audio interviews with Queue's RSS Feeds
http://acmqueue.com/rd.php?s.48.7

To unsubscribe to this newsletter, send an email to
queuenews-request at acmqueue.com
with the words 'unsubscribe' in the subject line.

Change your email address
http://www.acmqueue.com/mailman/options/queuenews

Subscribe to Queue in print
http://www.acmqueue.com/click.php?id=30
About Queue
http://acmqueue.com/rd.php?s.31.5
Contact Us
http://acmqueue.com/rd.php?s.32.10
Privacy policy
http://acmqueue.com/rd.php?s.27.9

For advertising information, contact advertising at acmqueue.com


© 2008 ACM, Inc. All rights reserved.




More information about the QueueNews mailing list