Plugin framework for Force.com Apex
Ever wondered if the apex software blocks can be easily installed and plugged in into each other ?
// load QueryAccounts plugit and pipe its output to next plugit and so on
Pluggable resultPlug = plugin('QueryAccounts')
.exec( seedData)
.pipe('ProcessActiveAccounts')
.pipe('NotifyInActiveAccountOwners')
.pass('HandleAccountSuccess')
.fail('SendEmailToAdmin') //invoked if any of the plugit fails
.fail('LogDebugToFile');
if( resultPlug.hasErrors() ){
//access resultPlug.errors();
} else {
Object result = resultPlug.result();
}
Plug
base class
public class Calculator extends Plug {
public override Object execute(Object data) {
Pluggable addService = plugin('AdditionService')
.exec( data )
.pass( 'AdditionServicePassed' )
.fail( 'AdditionServiceFailed' );
Object resultValue = addService.result();
if( resultValue == null ){
System.debug( 'Errors In The Service '+addService.errors());
}
return resultValue;
}
}
public class AdditionService extends Plug {
public override Object execute(Object data) {
Map<String,Decimal> dataMap = (Map<String,Decimal>)data;
try {
no1 = Integer.valueOf(dataMap.get('no1'));
no2 = Integer.valueOf(dataMap.get('no2'));
output('result',no1+no2);
return no1+no2;
} catch (Exception exp){
//sets error state
error('AdditionServiceException',exp);
}
return null;
}
}
public class AdditionServicePassed extends Plug {
public override Object execute(Object data) {
System.debug(' outputResult ='+Decimal.valueOf(data));
return data;
}
}
public class AdditionServiceFailed extends Plug {
public override Object execute(Object data) {
//handle errors errors
for( String errorKey : this.errors().keySet() ) {
System.debug(errorKey +' : '+ ((Exception)errors.get(errorKey)) );
//handle errors here
}
return null;
}
}
Object result = Plug.plugin('CalculatorNanoService')
.exec(new Map<String,Decimal>{
'no1' => 1,
'no2' => 50
});
plugin('CalculatorNanoService')
.exec(new Map<String,Decimal>{
'no1' => 1,
'no2' => null
});