Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1

Building a Builder


[ 272 ]

}

def registerSalesOrder() {
registerFactory("sales_order", new SalesOrderFactory())
}

FactoryBuilderSupport will detect all of these and run them in turn. Which
method you use is a matter of choice. The only issue that you need to be aware of is
that the registration methods will not be called in any predetermined order. If there
are dependencies in your registration code, then it's best to group these into a single
registration method.


To finish, we can drop this modified builder right where we previously used
CustomersBuilder, and it will work in the same way:


given:
def builder = new CustomersFactoryBuilder()

def customers = builder.customers {
fred = customer(firstName:"Fred",lastName:"Flintstone") {
invoice {
sales_order(sku:"productid01", amount:1, price:1.00)
sales_order(sku:"productid02", amount:2, price:1.00)
sales_order(sku:"productid03", amount:3, price:1.00)
}
}
invoice2 = invoice(fred)

sales_order(invoice2, sku:"productid04", amount:1, price:1.00)
sales_order(invoice2, sku:"productid05", amount:1, price:1.00)
sales_order(invoice2, sku:"productid06", amount:1, price:1.00)
}

expect:
CustomerWithInvoices.count() == 1
Invoice.count() == 2
SalesOrder.count() == 6

In terms of management and maintenance, this version is far superior. Adding
capabilities now for new tables will simply involve writing a new Factory class
and registering it.


http://www.ebook3000.com
Free download pdf