Last active
April 20, 2018 13:42
-
-
Save christophervigliotti/ee332ccf14fa63a617a95b9f03d7137c to your computer and use it in GitHub Desktop.
A brief meditation on encapsulation in ColdFusion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--- | |
encapsulation is good stuff https://en.wikipedia.org/wiki/Encapsulation_(computer_programming) | |
---> | |
<cfcomponent> | |
<cfset variables.dsn = '' /> | |
<cffunction name="init"> | |
<cfargument name="dsn" type="string" required="true" /> | |
<cfset variables.dsn = arguments.dsn /> | |
<cfreturn this /> | |
</cffunction> | |
<cffunction name="sad_stuff"> | |
<!--- referring to the request scope inside of a cfc breaks encapsulation ---> | |
<cfquery name="aQuery" datasource="#request.dsn#"> | |
... | |
</cffunction> | |
<cffunction name="good_stuff"> | |
<!--- getting the datasource name from the object's properties respects encapsulation ---> | |
<cfquery name="aQuery" datasource="#variables.dsn#"> | |
... | |
</cffunction> | |
<cffunction name="also_good_stuff"> | |
<!--- getting the datasource name from the arguments scope also respects encapsulation, but is less efficient ---> | |
<cfargument name="dsn" type="string" required="true" /> | |
<cfquery name="aQuery" datasource="#arguments.dsn#"> | |
... | |
</cffunction> | |
</cfcomponent> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment