English 中文(简体)
What s the best way to write engine-specific CFML code?
原标题:

Sometimes it is necessary to write different code for Adobe ColdFusion vs Railo vs OpenBD, due to differences in implementation.

Do people have a specific method that they use for this?

For example, switching on ProductName is one option:

<cfswitch expression="#Server.ColdFusion.ProductName#">
    <cfcase value="ColdFusion Server">
       ...
    </cfcase>
    <cfcase value="Railo">
       ...
    </cfcase>
    <cfcase value="BlueDragon">
       ...
    </cfcase>
</cfswitch>

Is that the best way, or can anyone suggest something better?

问题回答

When you get down to it, that s probably the most reliable way. You might be safer doing feature detection rather than explicit product checks, but CFML doesn t have a lot of introspection features you can use for that kind of thing.

If you use CFCs in your work, then you can also hide some of these alternate implementations using patterns like Strategy and Template Method. But when you re choosing an implementation, you ll probably still come back to ProductName.

i think a better way to handle this would be to create a cfc for each engine and a matching method name in each cfc. then what you could do is invoke that cfc and run the method depending on the engine. you can use your switch statement in the onapplicationstart() event to set an application wide variable to initialize and store the engine specific cfc. a small example would be:

<cfset loc.engine = "adobe">
<cfswitch expression="#Server.ColdFusion.ProductName#">
    <cfcase value="Railo">
        <cfset loc.engine = "railo">
    </cfcase>
    <cfcase value="BlueDragon">
        <cfset loc.engine = "openbd">
    </cfcase>
</cfswitch>

<cfset application.engine = createobject("component", "engines.#loc.engine#").init()>

then in your code all you would have to do:

<cfset myvar = application.engine.somemethod(arguments)>

granted it s still not the prettiest solution, but at least you will be containing all the engine specific code in one place and not littering your codebase with switch logic.





相关问题
JQuery AJAX .load - flash chart doesnt load in IE

An IE issue has me completely stumped. I have a coldfusion page that uses JQuery s AJAX .load function to load in a new flash file that is generated by coldFusion s cfchart tag. This works completely ...

Best Coldfusion Library for OpenID [closed]

I am getting ready to start a project that requires using OpenID within Coldfusion 8. I have found a number of different options and was wondering what has worked the best, get s the most support, ...

Find ColdFusion Generated ID

Is there a way to find the elements generated by ColdFusion s <CFLayout> and <CFLayoutArea> tags? These tags: <cflayout type="tab" name="MyAccount"> <cflayoutarea name="...

ColdFusion COM error

I am upgrading from CF4.5 to CF8. Calls to COM dll s that used to work in version 4.5 now throw a "Complex object types cannot be converted to simple values.." error. The COM object has a few arrays ...

What s the best way to write engine-specific CFML code?

Sometimes it is necessary to write different code for Adobe ColdFusion vs Railo vs OpenBD, due to differences in implementation. Do people have a specific method that they use for this? For example, ...

热门标签