ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Dynamic Apex Binding
Eric Leung
Salesforce Solution Architect
eric.leung@fronde.com
@erichpleung
With Design Patterns
Ben Naylor
Senior Salesforce Technical Consultant
ben.naylor@fronde.com
@nzchicken
Australia
New Zealand
Auckland
Wellington
Sydney
Melbourne
8 Years
Salesforce Partnership
5 Locations
Wellington, Auckland, Sydney,
Melbourne, Manila
400+
Customers
23 Years
Experience in IT Solutions
Google
Enterprise Partner of the Year
2012, Australia and New Zealand
Amazon
Advanced Consulting Partner &
Channel Reseller Partner
300+
Hobbits/Roos
Salesforce
Platinum Partner
www.fronde.com
Key Capabilities Offered by Fronde
Agenda
Part 1: Design Patterns
? Factory and Proxy Patterns
Part 2: Custom Settings and Schema Namespace
Part 3: Dynamic Binding
What are Design Patterns?
Object Oriented Software Design
Commonly used in Object Oriented Languages
? Apex, Java, C#, Objective C
Reusable Solution Templates
? Not libraries
3 categories (from GoF)
? Creational ¨C object creation
? Structural ¨C relationships between objects
? Behavioural ¨C communication between objects
Creational
Logic isolation
Centralize object creation logic
? Interface
? Class hierarchy
Factory Pattern
Structural
On behalf of the real object
? Hide details
? Optimization
? House keeping tasks
Proxy Pattern
Combine Proxy and Factory Patterns
Demo 1
Custom Settings
Custom Settings
Thin object
Promotes testingNo need to query
Configurable in real time
Store KVP
Application/Code Settings
List/Map containers
.getAll()
List vs. Hierarchy Custom Settings
User/Profile based
Global setting
One set of results
.getInstance()
List Type Hierarchy Type
Key Benefits
Manage change quickly
Configuration > Customization
Feature Toggles
Code Reuse
Schema Namespace
Runtime metadata query
Useful for Dynamic Apex
Get object configuration information
Special accessor methods
Demo 2
What is Dynamic Binding?
Compile-time vs Runtime
Tightly coupled vs. Loosely coupled
Classes vs. Interfaces
Types vs. Objects
System.Type Class
System.Type.forName(className: String)
Default constructor
Type casting, can be interface
Dependency Injection
public interface Animal {...}
public class Cat implements Animal {
public Cat(){¡­}// default constructor
}
System.Type sType = System.Type.forName('Cat'); // using apex-class name to get aType
Animal animal = (Animal) sType.newInstance(); // instantiate and cast to an interface
Back to the first demo¡­
Boilerplate code
Not easy to add a new version
? New code: service, & implementation classes
? Need to update permissions on new classes
Testing predefined scenarios
? Testing compile time logic
Problems Changes
Using custom settings to provide
implementation details
Using System.Type to instantiate
implementation
Using classes in Schema namespaces to
create and query Salesforce objects
Testing runtime behaviour
? Setup test configuration
Demo 3
Summary
Dynamic Binding using Apex
? Runtime, Interfaces, Loosely Coupled
Custom Settings
? Hierarchical and List
Advance Apex Features
? System.Type and SObject classes
? Schema Namespace
Summary continues
Testing
? Interface driven
? Test classes can be used in custom setting
? Testing new scenarios
Other examples on using dynamic binding
? Fine grain security
? Extends Salesforce OOTB functions/features
? Trigger
References
Books:
? Design Patterns: Elements of Reusable Object-Oriented Software, Addison-Wesley
? Head First Design Patterns, O¡¯Reilly
Links:
? Apex Design Patterns, Salesforce https://developer.salesforce.com/page/Apex_Design_Patterns
? Force.com Apex Code Developer's Guide https://developer.salesforce.com/docs/atlas.en-
us.apexcode.meta/apexcode/
Q & A
Thank you

More Related Content

DF15 - Dynamic Apex Binding with design patterns

  • 1. Dynamic Apex Binding Eric Leung Salesforce Solution Architect eric.leung@fronde.com @erichpleung With Design Patterns Ben Naylor Senior Salesforce Technical Consultant ben.naylor@fronde.com @nzchicken
  • 3. 8 Years Salesforce Partnership 5 Locations Wellington, Auckland, Sydney, Melbourne, Manila 400+ Customers 23 Years Experience in IT Solutions Google Enterprise Partner of the Year 2012, Australia and New Zealand Amazon Advanced Consulting Partner & Channel Reseller Partner 300+ Hobbits/Roos Salesforce Platinum Partner www.fronde.com
  • 5. Agenda Part 1: Design Patterns ? Factory and Proxy Patterns Part 2: Custom Settings and Schema Namespace Part 3: Dynamic Binding
  • 6. What are Design Patterns? Object Oriented Software Design Commonly used in Object Oriented Languages ? Apex, Java, C#, Objective C Reusable Solution Templates ? Not libraries 3 categories (from GoF) ? Creational ¨C object creation ? Structural ¨C relationships between objects ? Behavioural ¨C communication between objects
  • 7. Creational Logic isolation Centralize object creation logic ? Interface ? Class hierarchy Factory Pattern
  • 8. Structural On behalf of the real object ? Hide details ? Optimization ? House keeping tasks Proxy Pattern
  • 9. Combine Proxy and Factory Patterns
  • 11. Custom Settings Custom Settings Thin object Promotes testingNo need to query Configurable in real time
  • 12. Store KVP Application/Code Settings List/Map containers .getAll() List vs. Hierarchy Custom Settings User/Profile based Global setting One set of results .getInstance() List Type Hierarchy Type
  • 13. Key Benefits Manage change quickly Configuration > Customization Feature Toggles Code Reuse
  • 14. Schema Namespace Runtime metadata query Useful for Dynamic Apex Get object configuration information Special accessor methods
  • 16. What is Dynamic Binding? Compile-time vs Runtime Tightly coupled vs. Loosely coupled Classes vs. Interfaces Types vs. Objects
  • 17. System.Type Class System.Type.forName(className: String) Default constructor Type casting, can be interface Dependency Injection public interface Animal {...} public class Cat implements Animal { public Cat(){¡­}// default constructor } System.Type sType = System.Type.forName('Cat'); // using apex-class name to get aType Animal animal = (Animal) sType.newInstance(); // instantiate and cast to an interface
  • 18. Back to the first demo¡­ Boilerplate code Not easy to add a new version ? New code: service, & implementation classes ? Need to update permissions on new classes Testing predefined scenarios ? Testing compile time logic Problems Changes Using custom settings to provide implementation details Using System.Type to instantiate implementation Using classes in Schema namespaces to create and query Salesforce objects Testing runtime behaviour ? Setup test configuration
  • 20. Summary Dynamic Binding using Apex ? Runtime, Interfaces, Loosely Coupled Custom Settings ? Hierarchical and List Advance Apex Features ? System.Type and SObject classes ? Schema Namespace
  • 21. Summary continues Testing ? Interface driven ? Test classes can be used in custom setting ? Testing new scenarios Other examples on using dynamic binding ? Fine grain security ? Extends Salesforce OOTB functions/features ? Trigger
  • 22. References Books: ? Design Patterns: Elements of Reusable Object-Oriented Software, Addison-Wesley ? Head First Design Patterns, O¡¯Reilly Links: ? Apex Design Patterns, Salesforce https://developer.salesforce.com/page/Apex_Design_Patterns ? Force.com Apex Code Developer's Guide https://developer.salesforce.com/docs/atlas.en- us.apexcode.meta/apexcode/
  • 23. Q & A