user2943775
user2943775

Reputation: 273

How to pass DSN to cfcomponent datasource property

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

Answers (2)

CFML_Developer
CFML_Developer

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

Carl Von Stetten
Carl Von Stetten

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

Related Questions