Reputation: 273
What is the best practice for passing application-scoped DSN value to a component? Is it as follows?
component datasource="#application.dsn#"
I have always thought that it's not a good practice to use application-scoped variables inside components. Would it be better like this?
component{
function init( dsn, arg1, arg2 ){
this.datasource = arguments.dsn;
}
}
Upvotes: 1
Views: 329
Reputation: 1605
@cfvonner has replied in detail and is the correct answer, I believe. ColdFusion most of times allow you to do things as you want, use DSN as session, application, request, hardcoded too. But this flexibility should not an excuse for writing poor code. And as the question itself strive for "Good Practice" and your 2nd block of code does it rightly except the point @Adam Cameron raised. In short
OOPs--->ENCAPSULATION--->Passing through parameter
Upvotes: 0
Reputation: 1149
Best practice is to keep components completely isolated from knowing anything about the outside world (including any variables external to a component, such as the application scope). You should pass in via arguments any data a component needs to fulfill it's intended purpose. So, based on that premise, your second code sample is the preferred approach.
Upvotes: 6