项目作者: swapnilshrikhande

项目描述 :
Plugin framework for Force.com Apex
高级语言: Apex
项目地址: git://github.com/swapnilshrikhande/plugit-core-apex.git
创建时间: 2019-06-12T11:12:26Z
项目社区:https://github.com/swapnilshrikhande/plugit-core-apex

开源协议:MIT License

下载


Plugit : A Module Framework For Apex

Ever wondered if the apex software blocks can be easily installed and plugged in into each other ?

  • Plugit tries to solve above problem by providing a base framework to design services which can be combined together using a keyword driven approach.
  • Implements Fluent Interface

Installation

Key Features

  • Develop modular and interoperable apex services called as plugits
  • Supports extensible service factory to add custom service factory
  • Service factory can be extended to hide implementation details of the service / plugit , i.e. the name of the service can be mapped to a local class or a remote service through the factory.
  • Without changing the consumer code we can swap the service implementation from local class to an external service. (eg AWS Lambda)
  • All default methods can be overriden to extend the framework
  • Use pipe keyword to easily chain multiple plugits like below

typical usage example

  1. // load QueryAccounts plugit and pipe its output to next plugit and so on
  2. Pluggable resultPlug = plugin('QueryAccounts')
  3. .exec( seedData)
  4. .pipe('ProcessActiveAccounts')
  5. .pipe('NotifyInActiveAccountOwners')
  6. .pass('HandleAccountSuccess')
  7. .fail('SendEmailToAdmin') //invoked if any of the plugit fails
  8. .fail('LogDebugToFile');
  9. if( resultPlug.hasErrors() ){
  10. //access resultPlug.errors();
  11. } else {
  12. Object result = resultPlug.result();
  13. }

Complete Sample Usage

Extend Plug base class

  1. public class Calculator extends Plug {
  2. public override Object execute(Object data) {
  3. Pluggable addService = plugin('AdditionService')
  4. .exec( data )
  5. .pass( 'AdditionServicePassed' )
  6. .fail( 'AdditionServiceFailed' );
  7. Object resultValue = addService.result();
  8. if( resultValue == null ){
  9. System.debug( 'Errors In The Service '+addService.errors());
  10. }
  11. return resultValue;
  12. }
  13. }
  14. public class AdditionService extends Plug {
  15. public override Object execute(Object data) {
  16. Map<String,Decimal> dataMap = (Map<String,Decimal>)data;
  17. try {
  18. no1 = Integer.valueOf(dataMap.get('no1'));
  19. no2 = Integer.valueOf(dataMap.get('no2'));
  20. output('result',no1+no2);
  21. return no1+no2;
  22. } catch (Exception exp){
  23. //sets error state
  24. error('AdditionServiceException',exp);
  25. }
  26. return null;
  27. }
  28. }
  29. public class AdditionServicePassed extends Plug {
  30. public override Object execute(Object data) {
  31. System.debug(' outputResult ='+Decimal.valueOf(data));
  32. return data;
  33. }
  34. }
  35. public class AdditionServiceFailed extends Plug {
  36. public override Object execute(Object data) {
  37. //handle errors errors
  38. for( String errorKey : this.errors().keySet() ) {
  39. System.debug(errorKey +' : '+ ((Exception)errors.get(errorKey)) );
  40. //handle errors here
  41. }
  42. return null;
  43. }
  44. }

Invoke from a normal Apex class or from another Plugit

Normal Class :

  1. Object result = Plug.plugin('CalculatorNanoService')
  2. .exec(new Map<String,Decimal>{
  3. 'no1' => 1,
  4. 'no2' => 50
  5. });

Another Plugit :

  1. plugin('CalculatorNanoService')
  2. .exec(new Map<String,Decimal>{
  3. 'no1' => 1,
  4. 'no2' => null
  5. });

References