4

I am migrating from Eclipse to Android Studio and have a Android App connected to AppEngine. I have split the Server side into two modules (default module for Endpoints and user facing requests) and "admin" module for backend stuff. Now both these modules need to use the Entities. (backend module usually is responsible for saving these entities to DB, while the frontend default module is the one who returns data back to Android using these Entities).

What is the best way to share these Entity classes between these two modules in Android Studio? (also making sure these classes get enhanced etc). I do not wish to have duplicate classes, both in the default module as well as admin. Maybe have a common "java" module shared between the two (but not sure class enhancing would work). Or should the admin module NOT use the Entities and instead use other ways of persistence?

Appreciate your thoughts.

AAP
  • 1,198
  • 11
  • 16

1 Answers1

3

While there may be reasons for not sharing the code, personally I prefer DRY.

I solved the issue in DRY spirit with the Python backend by placing the models definition file in the app dir app/models.yaml and sym-linking it into each of the modules subdirs app/module_blah/models.yaml, thus ensuring all modules see the same models definitions. At deployment time the symlinks are automatically replaced with the actual content of the file being symlinked. From appcfg.py update:

The command follows symlinks and recursively uploads all files to the server. Temporary or source control files, such as foo~, .svn/* are skipped.

Care may be needed to deploy all modules at the same time.

I used the same technique to also share entire libraries with common code across modules, by symlinking app/lib/libX subdirs into the desired app/module_blah/lib/libX as needed.

Not sure if this technique is usable in Java, tho.

Dan Cornilescu
  • 37,297
  • 11
  • 54
  • 89
  • Thanks, thats an interesting idea! Will give it a try..luckily I am on ubuntu – AAP Dec 15 '15 at 16:21
  • I finally ended up making symlinks directly to the model folders (no changes to yaml) and atleast the code edits are shared between the two modules. Thanks – AAP Dec 17 '15 at 06:41
  • Thanks! Any link to the docs detailing that "symlinks are automatically replaced with the actual content of the file being symlinked"? – Alex Oct 15 '16 at 17:16
  • updated the answer with the pointer to the doc and the relevant quote from it. – Dan Cornilescu Oct 15 '16 at 17:34
  • I think it would be interesting if the Datastore supported this feature natively. In Python, you can do something similar with `ndb.Model._lookup_model('Animal')` (see https://cloud.google.com/appengine/docs/standard/python/ndb/modelclass), but this is only supported if the model has already been imported by the application, which sort of defeats the purpose. I wonder if this kind of feature will be supported in the future? – yoonjesung Feb 28 '18 at 16:56
  • Not trivial - a one size fits all solution would have to work with all combinations of service languages and environments which may or may not be developed by the same teams or using the same workflows. Leaving it to the respective teams appears simpler ;) – Dan Cornilescu Feb 28 '18 at 17:24
  • Can you please share your models.yaml file? – Jack tileman Apr 28 '19 at 19:34
  • @Jacktileman The `models.yaml` content itself isn't relevant from one application to another, each app defines and uses its models as needed. What is it that you're looking for? – Dan Cornilescu Apr 29 '19 at 05:26