13

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?
rdmueller
  • 10,742
  • 10
  • 69
  • 126

3 Answers3

7

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.

Community
  • 1
  • 1
Chris
  • 3,552
  • 2
  • 26
  • 36
  • Great answer, but solutions 2 and 3 still need both apps connected to the database :-( I need a solution more like your solution 1. I am aware that I will loose some functionality, but I guess I can solve this through caching (use a locak domain as cache domain and only remote requests to .get(id)) – rdmueller May 19 '12 at 11:36
  • 1
    If you're going with option 1, json-rest-api is a great plugin for setting up! – gotomanners May 21 '12 at 09:42
  • json-rest-api looks great! At least, that's a solution for one side of my problem :-) – rdmueller May 22 '12 at 14:33
  • Links are dead. Ryan's blog post available [on new site](http://blog.ryangeyer.com/2010/03/09/modularizing-your-grails-application-domain-classes/) – kaskelotti Jul 18 '14 at 12:35
  • I'm curious, could you elaborate some of the caveats that make option (2) harder than option (3)? Thanks. – dexterous May 08 '18 at 21:55
4

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

sanghavi7
  • 758
  • 1
  • 15
  • 38
2

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.

rdmueller
  • 10,742
  • 10
  • 69
  • 126
  • btw: maybe there are even other databases for which there is a GORM plugin which uses the webservice API in order to connect. – rdmueller May 24 '12 at 07:39