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

235 views Asked by At

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?

2

There are 2 answers

0
Sixten Otto On

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.

0
rip747 On

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.