Domain Objects

Overview

Domain objects are generated from the local database schema. This means domain objects always match the schema, much like the Rails/Fowler ActiveRecord pattern.

joist-domain uses the Generation Gap pattern to separate boilerplate getters/setters from business logic. For each table foo, the generated code created both a Foo and FooCodegen class. Foo is never touched again, so you can add domain logic without fear of it being overwritten.

Example

Child.java is a clean-slate for any business logic:

    public class Child extends ChildCodegen {
    }

While ChildCodegen.java has the appropriate boilerplate getter/setter methods, for example:

    public abstract class ChildCodegen extends AbstractDomainObject {

        ...
        private Integer id = null;
        private String name = null;
        private Integer version = null;
        private ForeignKeyHolder<Parent> parent = new ForeignKeyHolder<Parent>(Parent.class);
        ...

        ... Validation rules are added based on the db constraints
        private void addExtraRules() {
            this.addRule(new NotNull<Child>(Shims.name));
            this.addRule(new MaxLength<Child>(Shims.name, 100));
        }

        public Integer getId() {
            return this.id;
        }

        public void setId(Integer id) {
            this.getChanged().record("id", this.id, id);
            this.id = id;
            if (UoW.isOpen()) {
                UoW.getIdentityMap().store(this);
            }
        }

        public String getName() {
            return this.name;
        }

        public void setName(String name) {
            this.getChanged().record("name", this.name, name);
            this.name = name;
        }

        ...
    }