-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathRepoFactory.cls
81 lines (72 loc) · 2.47 KB
/
RepoFactory.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
public virtual class RepoFactory {
private static final Map<Schema.SObjectType, IHistoryRepository> CACHED_REPOS = new Map<Schema.SObjectType, IHistoryRepository>();
public Facade facade {
get {
if (this.facade == null) {
this.facade = new Facade();
}
return this.facade;
}
protected set;
}
public IAggregateRepository getOppRepo() {
List<Schema.SObjectField> queryFields = new List<Schema.SObjectField>{
Opportunity.IsWon,
Opportunity.StageName
// etc ...
};
IAggregateRepository oppRepo = this.facade.getRepo(Opportunity.SObjectType, queryFields, this);
oppRepo.addParentFields(
new List<Schema.SObjectField>{ Opportunity.AccountId },
new List<Schema.SObjectField>{ Account.Id }
);
return oppRepo;
}
public IAggregateRepository getAccountRepo() {
IAggregateRepository accountRepo = this.facade.getRepo(
Account.SObjectType,
new List<Schema.SObjectField>{ Account.Name },
this
);
accountRepo.addChildFields(Contact.AccountId, new List<Schema.SObjectField>{ Contact.LastName });
return accountRepo;
}
public IHistoryRepository getOppLineItemRepo() {
List<Schema.SObjectField> queryFields = new List<Schema.SObjectField>{
OpportunityLineItem.Description,
OpportunityLineItem.OpportunityId
// etc
};
return this.facade.getRepo(OpportunityLineItem.SObjectType, queryFields, this);
}
public IHistoryRepository getProfileRepo() {
return this.facade.getRepo(Profile.SObjectType, new List<Schema.SObjectField>{ Profile.Name }, this);
}
public IDML getDML() {
return this.facade.getDML();
}
public RepoFactory setFacade(Facade mockFacade) {
if (Test.isRunningTest() == false) {
throw new IllegalArgumentException('Should not call this outside of tests');
}
this.facade = mockFacade;
return this;
}
public virtual class Facade {
public virtual IDML getDML() {
return new DML();
}
public virtual IHistoryRepository getRepo(
Schema.SObjectType repoType,
List<Schema.SObjectField> queryFields,
RepoFactory repoFactory
) {
IHistoryRepository potentiallyCachedInstance = CACHED_REPOS.get(repoType);
if (potentiallyCachedInstance == null) {
potentiallyCachedInstance = new FieldLevelHistoryRepo(repoType, queryFields, repoFactory);
CACHED_REPOS.put(repoType, potentiallyCachedInstance);
}
return potentiallyCachedInstance;
}
}
}