Creating an entity builder

To create a new entity builder of a particular type, use the following syntax:

new TypeOfBuilder()

This creates a new builder of the specified type, with the Builder class setting various default properties on the builder entity. Each entity builder provides different default property values depending on its particular implementation. For example, to create (or build) a default address, use the following:

var address = new AddressBuilder()

To set specific properties to specific values, use the property configuration methods. The following are the types of property configuration methods, each which serves a different purpose as indicated by the method’s initial word:

Initial word

Indicates

on

A link to a parent. For example, PolicyPeriod is on an Account, so the method is onAccount(Account account).

as

A property that holds only a single state. For example, asSmallBusiness or asAgencyBill.

with

The single element or property to be set. For example, the following sets a FirstName property:

    withFirstName("Joe")

Use a DataBuilder.with(...) configuration method to add a single property or value to a builder object. For example, the following Gosu code creates a new Address object and uses a number of with(...) methods to initialize properties on the new object. It then uses an asType(...) method to set the address type.

var address = new AddressBuilder()
  .withAddressLine1( streetNumber + " Main St." )
  .withAddressLine2( "Suite " + suiteNumber)
  .withCity( "San Mateo" )
  .withState( "CA" )
  .withPostalCode( postalCode )
  .asType(typeStr)
  ...

After you create a builder entity, you are responsible for writing that entity to the database as part of a transaction bundle. In most cases, you must use one of the builder create methods to add the entity to a bundle. Which create method one you choose depends on your purpose.

To complete the previous example, add a create method at the end.

var address = new AddressBuilder()
  .withAddressLine1( streetNumber + " Main St." )
  ...
  .create()