[Coldbox 4.3.0] CF2016 - How to inject a business component in a service?

47 views
Skip to first unread message

Philippe Sambor

unread,
Feb 13, 2018, 10:41:12 PM2/13/18
to ColdBox Platform
I created a few utility components such as randomString, randomPassword etc... which I placed under /includes/helpers/utils directory. I wanted to call the randomString component in a service called UserSVC by injection, like so:

// Dependency Injection

property name="dao" inject="models.rrm.UserDAO";
property name="gw" inject="models.rrm.UsersGW";

property name="rString" inject="includes.helpers.utils.RandomString";
property name="log" inject="logbox:logger:{this}";
property name="populator" inject="wirebox:populator";
property name="wirebox" inject="wirebox";

function init(){
return this;

}

with the method in the UserSVC component like this:

// Return a random string as the default user account screen name

public string function randomString(required numeric length) {

return rString.RandomString(arguments.length);



But I get an error message: method randomString is undefined in UserSVC. 

Question: Should all invoked business components be considered part of the model and therefore be located under the "models" directory instead of any other directory such as "includes" for them to be recognised by ColdBox? Or should I create a mapping in Wirebox? 

Ancient Programmer

unread,
Feb 13, 2018, 11:22:12 PM2/13/18
to ColdBox Platform
If I were you, I would wrap the utility components in a ColdBox module. See this https://www.forgebox.io/view/BCrypt or https://www.forgebox.io/view/cbcommons as an example.

Philippe Sambor

unread,
Feb 14, 2018, 12:30:18 AM2/14/18
to ColdBox Platform
I have moved all the utilities under the models directory and it works now. As for wrapping the utility components in a Coldbox module, I think it is a great suggestion. I am going to try it now. Thanks.
Reply all
Reply to author
Forward
0 new messages