How to implement a "remote" Domain?
Asked Answered
N

3

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?
Nyx answered 16/5, 2012 at 7:41 Comment(0)
S
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.

Slone answered 18/5, 2012 at 14:38 Comment(5)
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))Nyx
If you're going with option 1, json-rest-api is a great plugin for setting up!Wording
json-rest-api looks great! At least, that's a solution for one side of my problem :-)Nyx
Links are dead. Ryan's blog post available on new siteMalaise
I'm curious, could you elaborate some of the caveats that make option (2) harder than option (3)? Thanks.Catabolism
A
4

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

Arv answered 25/5, 2012 at 5:42 Comment(1)
have you tries this things in your application?Arv
N
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.

Nyx answered 24/5, 2012 at 7:36 Comment(1)
btw: maybe there are even other databases for which there is a GORM plugin which uses the webservice API in order to connect.Nyx

© 2022 - 2024 — McMap. All rights reserved.