Skip to content

Commit 413fbcc

Browse files
wimvelzeboerWim Velzeboerjohn-storey-devops
authored
New domain structure (#31)
* New Domain structure - minimal upgrade Minimal upgrade with only fixing the compile issues. * New Domain structure - Interface upgrade Only changed the interface structure to the new fflib_IDomain and split the opportunities domain from the trigger handler * Separate domain specific logic * Remove file from commit * Update Application.cls Insignificant change to trigger workflow run Co-authored-by: Wim Velzeboer <[email protected]> Co-authored-by: John Storey <[email protected]>
1 parent 7e939dd commit 413fbcc

24 files changed

+522
-156
lines changed

sfdx-source/apex-common-samplecode/main/classes/Application.cls

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright (c) 2014-2016, FinancialForce.com, inc
2+
* Copyright (c) 2014-2016, FinancialForce.com, Inc
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without modification,
@@ -29,7 +29,8 @@ public class Application
2929
// Configure and create the UnitOfWorkFactory for this Application
3030
public static final fflib_Application.UnitOfWorkFactory UnitOfWork =
3131
new fflib_Application.UnitOfWorkFactory(
32-
new List<SObjectType> {
32+
new List<SObjectType> {
33+
Account.SObjectType,
3334
Invoice__c.SObjectType,
3435
InvoiceLine__c.SObjectType,
3536
Opportunity.SObjectType,
@@ -45,6 +46,7 @@ public class Application
4546
public static final fflib_Application.ServiceFactory Service =
4647
new fflib_Application.ServiceFactory(
4748
new Map<Type, Type> {
49+
IAccountsService.class => AccountsServiceImpl.class,
4850
IOpportunitiesService.class => OpportunitiesServiceImpl.class,
4951
IInvoicingService.class => InvoicingServiceImpl.class });
5052

@@ -69,4 +71,4 @@ public class Application
6971
OpportunityLineItem.SObjectType => OpportunityLineItems.Constructor.class,
7072
Account.SObjectType => Accounts.Constructor.class,
7173
DeveloperWorkItem__c.SObjectType => DeveloperWorkItems.class });
72-
}
74+
}

sfdx-source/apex-common-samplecode/main/classes/domains/Accounts.cls

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2525
**/
2626

27-
public with sharing class Accounts extends fflib_SObjectDomain
27+
public with sharing class Accounts extends fflib_SObjects
2828
implements IAccounts
2929
{
30+
public static final String OPPORTUNITY_RAISED_DESCRIPTION = 'Last Opportunity Raised';
31+
3032
public static IAccounts newInstance(List<Account> recordList)
3133
{
3234
return (IAccounts) Application.Domain.newInstance(recordList);
@@ -39,23 +41,32 @@ public with sharing class Accounts extends fflib_SObjectDomain
3941

4042
public Accounts(List<Account> sObjectList)
4143
{
42-
super(sObjectList);
44+
super(sObjectList, Schema.Account.SObjectType);
4345
}
44-
45-
public void updateOpportunityActivity(fflib_ISObjectUnitOfWork uow)
46+
47+
public List<Account> getAccounts()
4648
{
47-
for(Account account : (List<Account>) this.records)
49+
return (List<Account>) getRecords();
50+
}
51+
52+
public void setDescription(String description)
53+
{
54+
for (Account account : getAccounts())
4855
{
49-
account.Description = 'Last Opportunity Raised ' + System.today();
50-
uow.registerDirty(account);
56+
account.Description = description;
5157
}
5258
}
5359

54-
public class Constructor implements fflib_SObjectDomain.IConstructable
60+
public void updateOpportunityActivity()
61+
{
62+
setDescription(OPPORTUNITY_RAISED_DESCRIPTION + ' ' + System.today());
63+
}
64+
65+
public class Constructor implements fflib_IDomainConstructor
5566
{
56-
public fflib_SObjectDomain construct(List<SObject> sObjectList)
67+
public fflib_SObjects construct(List<Object> objectList)
5768
{
58-
return new Accounts(sObjectList);
69+
return new Accounts((List<SObject>) objectList);
5970
}
6071
}
6172
}

sfdx-source/apex-common-samplecode/main/classes/domains/IAccounts.cls

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2525
**/
2626

27-
public interface IAccounts extends fflib_ISObjectDomain
27+
public interface IAccounts extends fflib_ISObjects
2828
{
29-
void updateOpportunityActivity(fflib_ISObjectUnitOfWork uow);
29+
List<Account> getAccounts();
30+
void setDescription(String description);
31+
void updateOpportunityActivity();
3032
}

sfdx-source/apex-common-samplecode/main/classes/domains/IOpportunities.cls

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2525
**/
2626

27-
public interface IOpportunities extends fflib_ISObjectDomain
27+
public interface IOpportunities extends fflib_ISObjects
2828
{
2929
void applyDiscount(Decimal discountPercentage, fflib_ISObjectUnitOfWork uow);
30+
Set<Id> getAccountIds();
3031
}

sfdx-source/apex-common-samplecode/main/classes/domains/Opportunities.cls

Lines changed: 23 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2525
**/
2626

27-
public class Opportunities extends fflib_SObjectDomain
27+
public class Opportunities extends fflib_SObjects
2828
implements IOpportunities, InvoicingService.ISupportInvoicing
2929
{
3030
public static IOpportunities newInstance(List<Opportunity> recordList)
@@ -46,7 +46,7 @@ public class Opportunities extends fflib_SObjectDomain
4646
public void generate(InvoicingService.InvoiceFactory invoiceFactory)
4747
{
4848
// Utilise InvoiceFactory to create invoices from Opportunity details
49-
for(Opportunity opportunity : (List<Opportunity>) this.records)
49+
for(Opportunity opportunity : (List<Opportunity>) getRecords())
5050
{
5151
InvoicingService.Invoice invoice = new InvoicingService.Invoice();
5252
invoice.Account = opportunity.AccountId;
@@ -65,66 +65,7 @@ public class Opportunities extends fflib_SObjectDomain
6565
invoiceFactory.add(invoice);
6666
}
6767
}
68-
69-
public override void onApplyDefaults()
70-
{
71-
// Apply defaults to Opportunities
72-
for(Opportunity opportunity : (List<Opportunity>) this.records)
73-
{
74-
opportunity.DiscountType__c = OpportunitySettings__c.getInstance().DiscountType__c;
75-
}
76-
}
7768

78-
public override void onValidate()
79-
{
80-
// Validate Opportunities
81-
for(Opportunity opp : (List<Opportunity>) this.records)
82-
{
83-
if(opp.Type!=null && opp.Type.startsWith('Existing') && opp.AccountId == null)
84-
{
85-
opp.AccountId.addError( error('You must provide an Account for Opportunities for existing Customers.', opp, Opportunity.AccountId) );
86-
}
87-
}
88-
}
89-
90-
public override void onValidate(Map<Id,SObject> existingRecords)
91-
{
92-
// Validate changes to Opportunities
93-
for(Opportunity opp : (List<Opportunity>) this.records)
94-
{
95-
Opportunity existingOpp = (Opportunity) existingRecords.get(opp.Id);
96-
if(opp.Type != existingOpp.Type)
97-
{
98-
opp.Type.addError( error('You cannot change the Opportunity type once it has been created.', opp, Opportunity.Type) );
99-
}
100-
}
101-
}
102-
103-
public override void onAfterInsert()
104-
{
105-
// Related Accounts
106-
Set<Id> accountIds = new Set<Id>();
107-
for(Opportunity opp : (List<Opportunity>) this.records)
108-
{
109-
if( opp.AccountId != null)
110-
{
111-
accountIds.add(opp.AccountId);
112-
}
113-
}
114-
115-
if ( ! accountIds.isEmpty() )
116-
{
117-
// Update last Opportunity activity on the related Accounts (via the Accounts domain class)
118-
fflib_ISObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(new Schema.SObjectType[] { Account.SObjectType });
119-
120-
IAccounts accounts = (IAccounts)Application.Domain.newInstance( accountIds );
121-
122-
accounts.updateOpportunityActivity(uow);
123-
124-
uow.commitWork();
125-
}
126-
}
127-
12869
public void applyDiscount(Decimal discountPercentage, fflib_ISObjectUnitOfWork uow)
12970
{
13071
// Calculate discount factor
@@ -134,7 +75,7 @@ public class Opportunities extends fflib_SObjectDomain
13475
List<OpportunityLineItem> linesToApplyDiscount = new List<OpportunityLineItem>();
13576

13677
// Apply discount
137-
for(Opportunity opportunity : (List<Opportunity>) this.records)
78+
for(Opportunity opportunity : (List<Opportunity>) getRecords())
13879
{
13980
// Apply to the Opportunity Amount?
14081
if(opportunity.OpportunityLineItems == null
@@ -153,23 +94,36 @@ public class Opportunities extends fflib_SObjectDomain
15394

15495
// Apply discount to lines
15596
IOpportunityLineItems lineItems = (IOpportunityLineItems) Application.Domain.newInstance(linesToApplyDiscount);
156-
97+
15798
lineItems.applyDiscount(this, discountPercentage, uow);
15899
}
159-
100+
160101
public static Decimal calculateDiscountFactor(Decimal discountPercentage)
161102
{
162103
// Calculate discount factor
163104
Decimal discountProportion = discountPercentage==null ? 0 : discountPercentage / 100;
164105
Decimal factor = 1 - discountProportion;
165-
return factor;
106+
return factor;
166107
}
167-
168-
public class Constructor implements fflib_SObjectDomain.IConstructable
108+
109+
public Set<Id> getAccountIds()
110+
{
111+
Set<Id> result = new Set<Id>();
112+
for (Opportunity record : (List<Opportunity>) getRecords())
113+
{
114+
if (record.AccountId != null)
115+
{
116+
result.add(record.AccountId);
117+
}
118+
}
119+
return result;
120+
}
121+
122+
public class Constructor implements fflib_IDomainConstructor
169123
{
170-
public fflib_SObjectDomain construct(List<SObject> sObjectList)
124+
public fflib_SObjects construct(List<Object> objectList)
171125
{
172-
return new Opportunities(sObjectList);
126+
return new Opportunities((List<SObject>) objectList);
173127
}
174128
}
175129
}
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
11
/**
2-
* Copyright (c) 2013, FinancialForce.com, inc
2+
* Copyright (c) 2012-2014, FinancialForce.com, inc
33
* All rights reserved.
44
*
5-
* Redistribution and use in source and binary forms, with or without modification,
5+
* Redistribution and use in source and binary forms, with or without modification,
66
* are permitted provided that the following conditions are met:
77
*
8-
* - Redistributions of source code must retain the above copyright notice,
8+
* - Redistributions of source code must retain the above copyright notice,
99
* this list of conditions and the following disclaimer.
10-
* - Redistributions in binary form must reproduce the above copyright notice,
11-
* this list of conditions and the following disclaimer in the documentation
10+
* - Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
1212
* and/or other materials provided with the distribution.
13-
* - Neither the name of the FinancialForce.com, inc nor the names of its contributors
14-
* may be used to endorse or promote products derived from this software without
13+
* - Neither the name of the FinancialForce.com, inc nor the names of its contributors
14+
* may be used to endorse or promote products derived from this software without
1515
* specific prior written permission.
1616
*
17-
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18-
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19-
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
20-
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
17+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
20+
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2121
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2222
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
2323
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2424
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2525
**/
2626

27-
trigger OpportunityLineItems on OpportunityLineItem (
28-
after delete, after insert, after update, before delete, before insert, before update)
27+
public with sharing class AccountsService
2928
{
30-
// Creates Domain class instance and calls apprpoprite overideable methods according to Trigger state
31-
fflib_SObjectDomain.triggerHandler(OpportunityLineItems.class);
29+
public static void updateOpportunityActivity(Set<Id> accountIds)
30+
{
31+
service().updateOpportunityActivity(accountIds);
32+
}
33+
34+
private static IAccountsService service()
35+
{
36+
return (IAccountsService) Application.Service.newInstance(IAccountsService.class);
37+
}
3238
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<ApexTrigger xmlns="http://soap.sforce.com/2006/04/metadata">
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
33
<apiVersion>51.0</apiVersion>
44
<status>Active</status>
5-
</ApexTrigger>
5+
</ApexClass>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Copyright (c) 2012-2014, FinancialForce.com, inc
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without modification,
6+
* are permitted provided that the following conditions are met:
7+
*
8+
* - Redistributions of source code must retain the above copyright notice,
9+
* this list of conditions and the following disclaimer.
10+
* - Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
13+
* - Neither the name of the FinancialForce.com, inc nor the names of its contributors
14+
* may be used to endorse or promote products derived from this software without
15+
* specific prior written permission.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
20+
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23+
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
**/
26+
public with sharing class AccountsServiceImpl implements IAccountsService
27+
{
28+
/**
29+
* Update last Opportunity activity on the related Accounts
30+
*
31+
* @param accountIds The account Ids of the records to update
32+
*/
33+
public void updateOpportunityActivity(Set<Id> accountIds)
34+
{
35+
fflib_ISObjectUnitOfWork uow = Application.UnitOfWork.newInstance();
36+
37+
IAccounts accounts = (IAccounts) Application.Domain.newInstance( accountIds );
38+
39+
accounts.updateOpportunityActivity();
40+
41+
uow.registerDirty(accounts.getRecords());
42+
uow.commitWork();
43+
}
44+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>51.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright (c) 2012-2014, FinancialForce.com, inc
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without modification,
6+
* are permitted provided that the following conditions are met:
7+
*
8+
* - Redistributions of source code must retain the above copyright notice,
9+
* this list of conditions and the following disclaimer.
10+
* - Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
13+
* - Neither the name of the FinancialForce.com, inc nor the names of its contributors
14+
* may be used to endorse or promote products derived from this software without
15+
* specific prior written permission.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
20+
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23+
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
**/
26+
public interface IAccountsService
27+
{
28+
void updateOpportunityActivity(Set<Id> accountIds);
29+
}

0 commit comments

Comments
 (0)