13
votes

Imagine two Grails applications which share a domain class. Maybe a Book domain class.

One application is identified as the owner of the data, one will have to access the domain data. Something like amazon and the amazon web services.

I guess it is trivial that the owning application will use a normal domain class and will expose the data through web services - no problem in grails.

But what would be best practice to implement the domain in the other application?

  • use a service to access the remote domain and not implement a local domain class at all?
  • implement a local domain class, overwrite the get()-method in order to fetch the remote data and use the local database as cache?
  • what other solution comes to your mind?
3

3 Answers

7
votes

Ryan Geyer has a very interesting article Modularizing your Grails application domain classes which lists 3 solutions to this problem:

  1. As a RESTful JSON Service - easy to get this setup in Grails but then you lose the automatic GORM functionality.

  2. Separate out the domain classes into a library JAR file and reference that library in both of my other applications. This is not as easy as it first sounds

  3. Create a Grails plugin. Put the domain object in the plugin. Each of your applications can then import this plugin. You can then create different controllers with different functionality as required. The sample code for this is available at:

    git clone git://ec2.nslms.com/grails/blog_example_modular

Ted Naleid gives a great tip later in the post and recommends...

"create a grails-app/conf/BuildConfig.groovy file and put the plugin name and path to the source in it. ... If you do this, your applications will see the live changes to your domain/controller/service/etc classes as if they were actually in current app and there isn't any need to repackage and reinstall the plugin when you make changes."

Using memcache should enable both applications to have a consistent view of the data and would avoid each individual application having it's own inconsistent cache.

4
votes

I think you can make JAR file of your domain classes and add reference to other grails application .

2
votes

Found another interesting solution:

Riak is a key/value database with a first class REST API. There is a grails plugin for riak which maps most of the GORM functionality (relations, dynamic finders etc) to the REST API of riak: http://grails.org/plugin/riak

Now comes the part which I haven't tested yet: if you make use of the DataSources feature of grails 2.0, it should be possible to only connect those "remote" domains to a riak database.

As a result, there would be a domain stored in a riak database and several applications would be able to access it via a clean REST API without effort.

OK. This also show how silly my question is - it would be the same if you connect several apps through the same SQL-database. But sometimes people want to have something more funky like webservices.