When using the provider: injection is there a way to specify constructor/init arguments to the resulting object?
For example:
property name="gatewayResult" inject="provider:GatewayResult";
// Get a new instance of the GatewayResult object
private GatewayResult function getGatewayResult() {
return gatewayResult.$get();
}
Let’s now assume the GatewayResult needs a specific constructor argument, is there a way to pass that when instantiating it via the $get() method?
This currently does not work:
gatewayResult.$get( name="foo" );
You might be wondering why I’m calling $get() in the first place. Well, sometimes you may need to get a new instance of the object, but not call any methods on it, right away. For example:
var result = getGatewayResult();
if ( success ) {
return result.success( ... );
}
return result.error( ... );
I took a look at the code in /coldbox/system/ioc/Provider.cfc and don’t see a way to add constructor arguments.
Workaround 1:
Don’t use constructor arguments, and instead explicitly call methods after instantiation like this:
private GatewayResult function getGatewayResult() {
var result = gatewayResult.$get();
result.setName( "foo" );
return result;
}
Workaround 2
Don’t use the provider: namespace and instead use Wirebox’s getInstance()
return wirebox.getInstance( name=“GatewayResult”, initArguments={ name="foo" } )