Skip to content

Commit

Permalink
Improved the startup performance when AppLogic, ChildRecordFinder or …
Browse files Browse the repository at this point in the history
…MessageRenderer configurations exist
  • Loading branch information
rob-baillie-ortoo committed Apr 20, 2022
1 parent 5bae05d commit a82924a
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 27 deletions.
11 changes: 6 additions & 5 deletions framework/default/ortoo-core/default/classes/Application.cls
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,10 @@ public inherited sharing class Application {
}
}


/**
* Provides a default mechanism for defining a 'Class Type Configuration', being a mapping between two Classes (or an interface and a class).
* Provides a default mechanism for defining a 'Raw Type Configuration', being a mapping between two String names
*
* Can be used by a factory to map between Class and Sobject, or Class and Class, for example.
*/
private abstract inherited sharing class RawTypeConfiguration implements TypeConfiguration
{
Expand Down Expand Up @@ -223,7 +224,7 @@ public inherited sharing class Application {
/**
* Provides a mechanism for configuring and registering the App Logic Configuration
*/
private inherited sharing class AppLogicConfiguration extends ClassTypeConfiguration
private inherited sharing class AppLogicConfiguration extends RawTypeConfiguration
{
public void register()
{
Expand All @@ -234,7 +235,7 @@ public inherited sharing class Application {
/**
* Provides a mechanism for configuring and registering the Child Record Finders Configuration
*/
private inherited sharing class ChildRecordFinderConfiguration extends ClassTypeConfiguration
private inherited sharing class ChildRecordFinderConfiguration extends RawTypeConfiguration
{
public void register()
{
Expand All @@ -245,7 +246,7 @@ public inherited sharing class Application {
/**
* Provides a mechanism for configuring and registering the Child Record Finders Configuration
*/
private inherited sharing class MessageRendererConfiguration extends ClassTypeConfiguration
private inherited sharing class MessageRendererConfiguration extends RawTypeConfiguration
{
public void register()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/**
* Provides the ability to create object factories that perform a simple Type to Type mapping with no constructor or configuration required.
*
* Can be defined using either Type to Type or TypeName to TypeName (Strings)
*
* Should not be referenced directly in code, but instead wrapped in another factory that defines the type name and whether the factory
* should error when an unmapped type is passed in.
*
Expand All @@ -10,12 +12,27 @@
*/
public inherited sharing class ortoo_SimpleObjectFactory // NOPMD: specified a mini-namespace to differentiate from fflib versions
{
private Map<Type,Type> implementationByType;
private Map<Type,Type> implementationByType;
private Map<String,String> implementationNameByTypeName;
private Map<Type,Object> mockByType;

private String typeName = 'Unknown';
private Boolean errorOnUnmappedType = true;

/**
* Construct an instance using the given type ame to implementation name mapping
*
* @param Map<String,String> The Type name to Implementation name mapping
*/
public ortoo_SimpleObjectFactory( Map<String,String> implementationNameByTypeName )
{
Contract.requires( implementationNameByTypeName != null, 'ortoo_SimpleObjectFactory instantiated with a null implementationNameByTypeName' );

this.implementationNameByTypeName = implementationNameByTypeName;
this.implementationByType = new Map<Type,Type>();
this.mockByType = new Map<Type,Object>();
}

/**
* Construct an instance using the given type to implementation mapping
*
Expand All @@ -25,6 +42,7 @@ public inherited sharing class ortoo_SimpleObjectFactory // NOPMD: specified
{
Contract.requires( implementationByType != null, 'ortoo_SimpleObjectFactory instantiated with a null implementationByType' );

this.implementationNameByTypeName = new Map<String,String>();
this.implementationByType = implementationByType;
this.mockByType = new Map<Type,Object>();
}
Expand Down Expand Up @@ -75,7 +93,9 @@ public inherited sharing class ortoo_SimpleObjectFactory // NOPMD: specified
return mockByType.get( requestedType );
}

if ( errorOnUnmappedType && ! implementationByType.containsKey( requestedType ) )
String requestedTypeName = requestedType.getName();

if ( errorOnUnmappedType && ! implementationByType.containsKey( requestedType ) && ! implementationNameByTypeName.containsKey( requestedTypeName ) )
{
throw new Exceptions.DeveloperException( 'No implementation registered for ' + typeName + ' ' + requestedType )
.setErrorCode( FrameworkErrorCodes.FACTORY_NO_IMPLEMENTATION_REGISTERED )
Expand All @@ -88,6 +108,18 @@ public inherited sharing class ortoo_SimpleObjectFactory // NOPMD: specified
{
typeToConstruct = implementationByType.get( requestedType );
}
else if ( implementationNameByTypeName.containsKey( requestedTypeName ) )
{
typeToConstruct = Type.forName( implementationNameByTypeName.get( requestedTypeName ) );
if ( typeToConstruct == null )
{
throw new Exceptions.DeveloperException( 'Invalid implementation registered for ' + typeName + ' ' + requestedTypeName + ': The class does not exist, or is not visible' )
.setErrorCode( FrameworkErrorCodes.FACTORY_INVALID_IMPLEMENTATION_REGISTERED )
.addContext( 'typeName', typeName )
.addContext( 'requestedType', requestedType );
}
implementationByType.put( requestedType, typeToConstruct );
}

Object constructedObject;
try
Expand Down
Loading

0 comments on commit a82924a

Please sign in to comment.