Skip to content

Instantly share code, notes, and snippets.

@ahgittin
Last active December 15, 2015 04:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahgittin/5204241 to your computer and use it in GitHub Desktop.
Save ahgittin/5204241 to your computer and use it in GitHub Desktop.
simplifying entity/app construction API
// current syntax
MySqlNode mysql = (MySqlNode) addChild(
getEntityManager().createEntity(
BasicEntitySpec.newInstance(MySqlNode.class)
.configure(MySqlNode.CREATION_SCRIPT_URL, getConfig(DB_SETUP_SQL_URL))) );
// step 1 (uncontroversial?): allow addChild to take a spec directly
MySqlNode mysql1 = (MySqlNode) addChild(
BasicEntitySpec.newInstance(MySqlNode.class)
.configure(MySqlNode.CREATION_SCRIPT_URL, getConfig(DB_SETUP_SQL_URL)) );
// step 2, option 1: nicer syntax for a spec (PREFERRED)
MySqlNode mysql2 = (MySqlNode) addChild(Specs.spec(MySqlNode.class)
.configure(MySqlNode.CREATION_SCRIPT_URL, getConfig(DB_SETUP_SQL_URL)) );
// step 2, option 2: new API which takes a configuration object (allowing multiple configures)
MySqlNode mysql3 = (MySqlNode) addChild(MySqlNode.class,
Specs.configure(MySqlNode.CREATION_SCRIPT_URL, getConfig(DB_SETUP_SQL_URL)) );

Currently when an Entity wants to populate its children entities and policies, this is done in a method such as:

postConstruct() {
    addChild(MySqlNode.class);  //for example, like above
}

(Externally, e.g. as parent, you can add children to a child using the spec, but I'm talking when you are implementing an Entity or the root Application, e.g. by subclassing Abstract{Entity,Application}.)

The concern is that the name postConstruct is obscure. It has a deliberate relationship with @javax.annotation.PostConstruct -- the same semantics, namely it is made available internally for finishing construction of the object before making it available externally (leaking it). But it is not @PostConstruct nor is it a friendly and obvious method for where someone implementing an Entity is going to do a large chunk of their work.

We have a few suggestions so far:

  1. Rename it construct() -- this is what we are doing, in a meta sense: constructing the Entity, its children, policies, etc. however there is confusion with object construction as quite clearly this isn't a constructor!

  2. Rename it populate() -- AFAIK this isn't an overloaded term, and slightly less obscure

  3. Use @PostConstruct -- familiar to EE users, but we (so far) have not used annotations, as they quickly become confusing especially when you are trying to enforce an order, or trying to step through code using and IDE (note that 3 could be used with 1 or 2 as a default)

Thoughts? Better ideas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment