From 8be9adcce0abcd903596aeac060b81e56f23d043 Mon Sep 17 00:00:00 2001 From: Barry Schnell Date: Fri, 22 Jan 2016 17:28:27 -0800 Subject: [PATCH] Add templates for fflib_DomainObjectBuilder Add templates for generating fflib_DomainObjectBuilder Base & Domain specific classes. Update fflib Domain Class template to reflect recent fflib_SObjectDomain.IConstructable2 interface --- ApexClass/DomainClass.cls | 7 +- ApexClass/DomainObjectBuilderBaseClass.cls | 54 ++++++++ ApexClass/DomainObjectBuilderClass.cls | 144 +++++++++++++++++++++ package.json | 41 ++++++ 4 files changed, 245 insertions(+), 1 deletion(-) create mode 100644 ApexClass/DomainObjectBuilderBaseClass.cls create mode 100644 ApexClass/DomainObjectBuilderClass.cls diff --git a/ApexClass/DomainClass.cls b/ApexClass/DomainClass.cls index 44fa304..4f2d610 100644 --- a/ApexClass/DomainClass.cls +++ b/ApexClass/DomainClass.cls @@ -47,11 +47,16 @@ public class {{ api_name }} extends fflib_SObjectDomain } } - public class Constructor implements fflib_SObjectDomain.IConstructable + public class Constructor implements fflib_SObjectDomain.IConstructable2 { public fflib_SObjectDomain construct(List sObjectList) { return new {{ api_name }}(sObjectList); } + + public fflib_SObjectDomain construct(List sObjectList, SObjectType sObjectType) + { + return new {{ api_name }}(sObjectList); + } } } \ No newline at end of file diff --git a/ApexClass/DomainObjectBuilderBaseClass.cls b/ApexClass/DomainObjectBuilderBaseClass.cls new file mode 100644 index 0000000..0e3bdf0 --- /dev/null +++ b/ApexClass/DomainObjectBuilderBaseClass.cls @@ -0,0 +1,54 @@ +/** + * See https://github.com/financialforcedev/fflib-apex-common for more info + * + * Install library via + * https://githubsfdeploy.herokuapp.com/app/githubdeploy/financialforcedev/fflib-apex-common + */ + +/** + * Base class for Domain specific builders implementing the Test Data Builder pattern as described + * by Nate Pryce (http://www.natpryce.com/) and Object Mother Pattern (http://www.c2.com/cgi/wiki?ObjectMother) + * + * For more guidelines and details see + * https://github.com/financialforcedev/fflib-apex-common + * https://github.com/financialforcedev/fflib-apex-common-samplecode + **/ +public abstract class {{ api_name }} extends fflib_DomainObjectBuilder +{ + /** + * @description Constructs the Builder class with the specified SObjectType + * + * @param type The SObject type that the builder will build + **/ + protected {{ api_name }}(SObjectType type) + { + super(type); + } + + /** + * @description Copy Constructor that constructs the Builder class based on the builder specified + * + * @param copyFrom The builder to copy/clone this instance from + **/ + protected {{ api_name }}({{ api_name }} copyFrom) + { + super(copyFrom); + } + + /** + * @description Helper method to obtain the default Unit Of Work for use with persist methods + **/ + protected virtual fflib_ISObjectUnitOfWork createUnitOfWork() + { + /** + * @todo If using Application class concept from the Apex Enterprise Patterns, change the below to + * return from Application.UnitOfWork.newInstance(). + * If not using the Application class concept, add the default SObjectTypes that should be + * passed to Unit Of work construction. This method will be called by derived builders + * during persist() method call. + */ + + //return Application.UnitOfWork.newInstance(); + return new fflib_SObjectUnitOfWork(new List {}); + } +} \ No newline at end of file diff --git a/ApexClass/DomainObjectBuilderClass.cls b/ApexClass/DomainObjectBuilderClass.cls new file mode 100644 index 0000000..252d9b2 --- /dev/null +++ b/ApexClass/DomainObjectBuilderClass.cls @@ -0,0 +1,144 @@ +/** + * See https://github.com/financialforcedev/fflib-apex-common for more info + * + * Install library via + * https://githubsfdeploy.herokuapp.com/app/githubdeploy/financialforcedev/fflib-apex-common + */ + +/** + * Domain specific class implementing the Test Data Builder pattern as described by Nate Pryce + * (http://www.natpryce.com/) and Object Mother Pattern (http://www.c2.com/cgi/wiki?ObjectMother) + * + * For more guidelines and details see + * https://github.com/financialforcedev/fflib-apex-common + * https://github.com/financialforcedev/fflib-apex-common-samplecode + **/ +public class {{ api_name }} extends {{ base_name }} +{ + /** + * @description Default constructor + **/ + private {{ api_name }}() + { + super({{ object_name }}.SObjectType); + } + + /** + * @description Copy Constructor that constructs the Builder class based on the builder specified + * + * @param copyFrom The builder to copy/clone this instance from + **/ + private {{ api_name }}({{ api_name }} copyFrom) + { + super(copyFrom); + } + + /** + * @description Creates an existing SObject without issuing DML + * + * @remarks Wrapper method to base class to allow for casting of specific SObjectType + **/ + public {{ object_name }} build() + { + return ({{ object_name }})build(false); + } + + /** + * @description Creates an New SObject (No Id) without issuing DML + * + * @remarks Wrapper method to base class to allow for casting of specific SObjectType + **/ + public {{ object_name }} buildNew() + { + return ({{ object_name }})build(true); + } + + /** + * @description Persists builder and its related data through Unit Of Work + * + * @remarks Wrapper method to base class to allow for casting of specific SObjectType + **/ + public {{ object_name }} persist(fflib_ISObjectUnitOfWork uow) + { + return ({{ object_name }})persistBuilder(uow); + } + + /** + * @description Persists builder and its related data using default unit of work + * + * @remarks Wrapper method to base class to allow for casting of specific SObjectType + **/ + public {{ object_name }} persist() + { + return persist(createUnitOfWork()); + } + + /** + * @description Registers instance for persistance via persistBuilders + * + * @remarks Wrapper method to base class to allow for casting of specific SObjectType + **/ + public {{ api_name }} register() + { + return ({{ api_name }})registerBuilder(); + } + + /** + * @description Returns Contact SObject associated to this builder + * + * @remarks Wrapper method to base class to allow for casting of specific SObjectType + **/ + public {{ object_name }} Record + { + get { return ({{ object_name }})getRecord(); } + private set; + } + + /** + * @description Returns a Clone of this instance + **/ + public {{ api_name }} but() + { + return new {{ api_name }}(this); + } + + /** + * @description Object Mother method for an empty {{ api_name }} instance + **/ + public static {{ api_name }} {{ mother_name }}() + { + return new {{ api_name }}(); + } + + /* + * @todo Add additional Object Mother methods for commonly used values + * + * For more examples see https://github.com/financialforcedev/fflib-apex-common-samplecode + * + * + public static {{ api_name }} {{ mother_name }}WithRequiredFields() + { + return {{ mother_name }}() + .withName('Test Name') + .withType('My Type'); + } + */ + + /* + * @todo Add methods to set field values + * + * For more examples see https://github.com/financialforcedev/fflib-apex-common-samplecode + * + public {{ api_name }} withName(String value) + { + set({{ object_name }}.Name, value); + return this; + } + + public {{ api_name }} withAccount(Account_t value) + { + setParent({{ object_name }}.AccountId, value); + return this; + } + */ +} \ No newline at end of file diff --git a/package.json b/package.json index ca1b7c1..9812f6a 100644 --- a/package.json +++ b/package.json @@ -109,6 +109,47 @@ } ] }, + { + "name":"DomainObjectBuilder", + "file_name":"DomainObjectBuilderClass.cls", + "description":"Apex Enterprise Patterns Domain Object Builder Class", + "author":"Jon Davis @jondavis9898", + "params":[ + { + "name":"api_name", + "description":"Domain Object Builder class name", + "default":"Invoice_t" + }, + { + "name":"base_name", + "description":"Domain Object Builder Base class name", + "default":"Domain_t" + }, + { + "name":"object_name", + "description":"API name of the Standard or Custom Object", + "default":"Invoice__c" + }, + { + "name":"mother_name", + "description":"Method name for empty Builder Object Mother method", + "default":"anInvoice" + } + ] + }, + { + "name":"DomainObjectBuilderBase", + "file_name":"DomainObjectBuilderBaseClass.cls", + "description":"Apex Enterprise Patterns Domain Object Builder Base Class", + "author":"Jon Davis @jondavis9898", + "params":[ + { + "name":"api_name", + "description":"Domain Object Builder Base class name", + "default":"Domain_t" + } + ] + }, { "name":"Email Service", "file_name":"EmailServiceApexClass.cls",