Dark theme | Light theme

September 28, 2009

Groovy Goodness: Building Object Graphs

With Groovy we can build a graph of objects just like with Java, but we can also use the ObjectGraphBuilder. With the builder we can construct a graph of objects that follow the Java Beans rules. The builder follows some standard rules to determine parent/child relations and use of classes. We can override this default behaviour by adding specific resolver classes to the builder object.

class School {
    String name
    List<Course> courses = []

class Course {
    String name
    Teacher teacher
    List<Student> students = []

class Teacher { String name }

class Student { String name }

def builder = new ObjectGraphBuilder(classLoader: getClass().classLoader) // Need classLoader when in Groovy console.
def firstSchool = 'First School') {
    course(name: 'Math') {
        teacher(name: 'Matt') 
        student(name: 'Mary', id: 'Mary')  // id attribute allows us to reference it later in the builder.
        student(name: 'John', id: 'John')
        student(name: 'Rose', id: 'Rose')
    course(name: 'English') {
        teacher(name: 'Emily', id: 'Emily')
        student(refId: 'Mary')  // refId refers to id attribute we set earlier.
        student(name: 'Alex')
        student(refId: 'Rose')
    course(name: 'Java') {
        teacher(refId: 'Emily')
        student(name: 'mrhaki')
        student(refId: 'John')
        student(refId: 'Mary')

assert 'First School' ==
assert 3 ==
assert 'Math' ==[0].name
assert 'Matt' ==[0]
assert 3 ==[0].students.size()
assert ['English', 'Java'] =={ == 'Emily' }.name
assert 'mrhaki' ==[2].students.find { == 'mrhaki' }.name
assert 'Mary' =={ == 'Java' }.students.find{ == 'Mary' }.name