df15 - dynamic apex binding with design patterns
TRANSCRIPT
Dynamic Apex Binding
Eric Leung Salesforce Solution Architect [email protected] @erichpleung
With Design Patterns
Ben Naylor Senior Salesforce Technical Consultant [email protected] @nzchicken
Australia
New Zealand
Auckland
Wellington
Sydney
Melbourne
8 YearsSalesforce Partnership
5 LocationsWellington, Auckland, Sydney, Melbourne, Manila
400+Customers
23 YearsExperience in IT Solutions
GoogleEnterprise Partner of the Year 2012, Australia and New Zealand
AmazonAdvanced Consulting Partner & Channel Reseller Partner
300+Hobbits/Roos
SalesforcePlatinum 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 – object creation
• Structural – relationships between objects
• Behavioural – 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 aTypeAnimal 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