Salesforce 㧠DI ã§ããªããèãã¦ã¿ã
DI ã£ã¦ï¼
Dependency Injection å¤é¨ä¾å注å
¥ã®ç¥ã
DI ã®èª¬æãè¦æ¨ã§ããªãã®ã§ãä¸è¨ URL åç
§ã
Salesforce 㧠DI ï¼
Salesforce ã®ã¢ããªã±ã¼ã·ã§ã³ãããã±ã¼ã¸ã§å
¬éããã¨ãããã£ã¦ã¿ãã¨ãããã®ã§ããããglobalã修飾ãããªãã¸ã§ã¯ãããå©ç¨è
ã¯ã«ã¹ã¿ã ã§ãã¾ããã
ããã¯å¥ã«è¯ãã®ã§ããããè²ã
ã«ã¹ã¿ãã¤ãºã§ããããã«ããããã¨ãããã global æå®ãã¦ãã¾ãã¨ã以ä¸ã®ã¸ã¬ã³ãã«æ©ã¾ããã¾ãã
global ã¤ã³ã¿ã¼ãã§ã¼ã¹ã¯çµ¶å¯¾ã«å¤æ´ã§ããªãã®ã§ãå ¬éããã°ããã ãå¤æ´ãã§ããªã
ã¨ãããã¨ã«ãªãã¾ãã
ç¹ã«ãç¶æ¿ã許ãã¦ãã¾ãã¨è´å½çã§ããã
å
é¨æåã«ä¾åããã³ã¼ããæ¸ããã¦ãä½ãå¤æ´ããããã®ãªãããã°ã ã´ã«ã¡ï¼ãã¨ã¯ã¬ã¼ã ãé£ã¶ãã¨è«ãåãã§ãã
ããã§ãæ©è½ãåé¢ãã¦ãè¨å®ã§ä¸æ¸ããããç¶æ¿ãããã§ããããã«ã§ããªããï¼
ã¨ãã£ãã¨ãã«
ã¤ã³ã¿ã¼ãã§ã¼ã¹ã ãå ¬éãã¦å¥½ãã«å®è£ ãã
ã¨ãå
é¨æåãåæã«å®è£
ããããã ãã¤ã³ã¿ã¼ãã§ã¼ã¹ã®ä»æ§ã¯å®ããã¨ã
ãããªæãã§ã«ã¹ã¿ã ã§ããæ§ã«ããæ段ã¨ã㦠DI ãèå¯ãã¾ããã
åºæ¬åç
ã¯ã©ã¹åãããããã°ãå¼æ°ãªãã³ã³ã¹ãã©ã¯ã¿çµç±ã§ã¤ã³ã¹ã¿ã³ã¹ãçæã§ãããã¨ãããã£ã¦ããã®ã§ã
- ã«ã¹ã¿ã è¨å®ã«ãã¤ã³ã¿ã¼ãã§ã¼ã¹åï¼å®è£ ã¯ã©ã¹åããå®è£ ããã
- ã³ã³ããããããã¿ã¦ã¤ã³ã¹ã¿ã³ã¹ãçæããï¼æªè¨å®ãªãããã©ã«ãã®è¨å®ã¯ã©ã¹ãã¤ã³ã¹ã¿ã³ã¹åããï¼
- ã¤ã³ã¿ã¼ãã§ã¼ã¹ã«ä¾åããå½¢ã§ã¢ããªã±ã¼ã·ã§ã³ãä½æãã
ã¨ãã¦ããã°ã顧客ã¯ã«ã¹ã¿ã è¨å®ãå¤æ´ããã ãã§ãå®éã®æåãå¤æ´ãããã«ã¹ã¿ã ãããã§ããããã«ãªãã¯ãã ã
ã¨ãããã¨ã§æ¤è¨¼ããã
ããå®è£
ã¾ãã¯æ¤è¨¼å¯¾è±¡ã¯ã©ã¹ãä½æ
å®éã«ã¯ã«ã¼ãã¯ã©ã¹ã¨ãã¤ã³ã¿ã¼ãã§ã¼ã¹ã¯ global ã«ã§ãããã®ã ãããããã§ã¯å²æã
public with sharing class SimpleExample { public interface ExampleInterface { String getMessage(); } public class DefaultImpl implements ExampleInterface { public String getMessage() { return 'Default implements'; } } }
DI ç½®ãæã対象ã®ã¯ã©ã¹ãä½æ
public with sharing class CustomImpl implements SimpleExample.ExampleInterface { public String getMessage() { return 'Custom implements'; } }
ããã¾ã§ã¯å¤§ããé£ãã話ã§ã¯ãªãããï¼
ã«ã¹ã¿ã è¨å®ã追å ãã
è¨å®ã®å 容ã¯ããã¾ã§æ¤è¨¼ç¨ãµã³ãã«ãªã®ã§
- API åç
§å
DISample__c - é
ç®
ExampleInterface__c : String(32)
DI ã³ã³ããããã¼ã¸ã£ã®ä½æ
DI ãå®éã«è¡ã£ã¦ããã¯ã©ã¹ãä½æããã
ãããå®éç¨ã 㨠global ã ã£ããããã¼ã ã¹ãã¼ã¹æå®ã¨ãã«ã¹ã¿ã è¨å®ãåãåãã¨ãè²ã
ããããã ããä»åã¯ããã¾ã§æ¤è¨¼ãªã®ã§å²æãã¦å®è£
ããã
public with sharing class DIManager { public class DISetting { private String interfaceName { get; private set; } private String settingName { get; private set; } private Type defaultObjectType { get; private set; } public DISetting(String interfaceName, String settingName, Type defaultObjectType) { this.interfaceName = interfaceName; this.settingName = settingName; this.defaultObjectType = defaultObjectType; } } private Map<String, DISetting> diSettings; public DIManager(List<DISetting> settings) { this.diSettings = new Map<String, DISetting>(); for (DISetting st : settings) { this.diSettings.put(st.interfaceName, st); } } public Object getObject(String interfaceName) { DISetting setting = this.diSettings.get(interfaceName); if (setting == null) { return null; } // not found DISample__c diSetting = DISample__c.getOrgDefaults(); String targetObjectName = diSetting != null ? (String) diSetting.get(setting.settingName) : null; if (String.isEmpty(targetObjectName)) { return this.getDefaultObject(interfaceName); } else { Type t = Type.forName(targetObjectName); return t.newInstance(); } } private Object getDefaultObject(String interfaceName) { DISetting setting = this.diSettings.get(interfaceName); if (setting == null) { return null; } // not found return setting.defaultObjectType.newInstance(); } }
æ¤è¨¼
è²ã
é¢åãªã®ã§å¿å Apex å®è¡ããã¦ã¿ãã
ã½ã¼ã¹ã¯ä¸è¨
DIManager.DISetting setting = new DIManager.DISetting('ExampleInterface', 'ExampleInterface__c', SimpleExample.DefaultImpl.class); DIManager manager = new DIManager(new List<DIManager.DISetting> { setting }); SimpleExample.ExampleInterface example = (SimpleExample.ExampleInterface) manager.getObject('ExampleInterface'); System.debug('Message : ' + example.getMessage());
ã¾ãã¯ãã«ã¹ã¿ã è¨å®ãå©ç¨ããã«å®è¡ããã
(åç¥) Execute Anonymous: System.debug('Message : ' + example.getMessage()); 17:05:25.25 (25031288)|USER_INFO|[EXTERNAL]|0052w000003PbHm|[email protected]|(GMT+09:00) æ¥æ¬æ¨æºæ (Asia/Tokyo)|GMT+09:00 17:05:25.25 (25094245)|EXECUTION_STARTED 17:05:25.25 (25103087)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex 17:05:25.25 (30135496)|USER_DEBUG|[6]|DEBUG|Message : Default implements â ç¹ã«è¨å®ãåå¨ããªãã®ã§ããã©ã«ãå®è£ ãåä½ãã¦ã 17:05:25.30 (30230158)|CUMULATIVE_LIMIT_USAGE 17:05:25.30 (30230158)|LIMIT_USAGE_FOR_NS|(default)| Number of SOQL queries: 0 out of 100 Number of query rows: 0 out of 50000 (以ä¸ç¥)
ã§ã¯ããã§ãã«ã¹ã¿ã è¨å®ãè¡ã£ã¦ã¿ãã
ãã®ç¶æ ã§å®è¡ããã¨
17:09:13.33 (33341882)|EXECUTION_STARTED 17:09:13.33 (33360008)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex 17:09:13.33 (42787189)|USER_DEBUG|[6]|DEBUG|Message : Custom implements â è¨å®ã«ãã£ã¦å©ç¨ãããã¤ã³ã¹ã¿ã³ã¹ãæ¸ãæãã£ãï¼ 17:09:13.42 (42932602)|CUMULATIVE_LIMIT_USAGE 17:09:13.42 (42932602)|LIMIT_USAGE_FOR_NS|(default)|
ã¨ãããã¨ãã§ããã