Justin
Justin

Reputation: 398

Managing multiple CFCs together

I am having an application which includes multiple CFCs (more than 100 CFCs). I would like to initiate these CFCs when the application get started so that I can use its instances anywhere in the system. Can I know what is best way to initialize these CFCs together? Any option available to initialize the all CFCs dynamically?

Upvotes: 0

Views: 94

Answers (1)

Alex
Alex

Reputation: 7833

I assume you are talking about static components, i.e. singletons.

To initialize a component once, use the onApplicationStart event in your Application.cfc and store the component in the persistent APPLICATION scope. This event function is executed (thread-safe) on the first request. You will then refer to the components stored in the APPLICATION scope in your templates.

You can initialize components dynamically, e.g.

componentPath = "yourPath.toThe.Component";
componentName = listLast(componentPath, ".");
APPLICATION[componentName] = createObject("component", componentPath).init();

You could retrieve a list of all .cfc files in a given directory and then loop over them with the code above.

Upvotes: 1

Related Questions