Life After ASP
Table of Contents
ColdFusion vs. ASP.NET
Thus far it has been demonstrated that ColdFusion can do everything ASP can do and a lot more, is architecturally not that different from ASP.NET, and that ASP development (the page based model, how code is broken up, how scripts are used) is quite similar to that of ColdFusion. This all means that ASP developers will be comfortable and productive using ColdFusion in a very short time. But what about ASP.NET? Why should an ASP developer choose ColdFusion over the ASP.NET, the perceived migration path to ASP.NET?
You use both ASP.NET and CFML at an application's presentation layer, where productivity is a primary goal. As already stated, the incredible productivity using ColdFusion comes from the CFML language and all the integrated runtime services. This next section provides side-by-side comparisons of common programming tasks in both CFML and ASP.NET (VB.NET).
Parameterized Queries
Both ColdFusion and ASP.NET recommends using parameterized queries instead of embedding SQL. This is both faster and more secure. The following compares the use of parameterized queries.
ASP.NET
Dim sales_cutoff As Integer = 1000
Dim myConnection As SqlConnection = New SqlConnection("connection_string")
Dim myCommand As SqlCommand = myConnection.CreateCommand()
myCommand.CommandText = "SELECT title_id, title, ytd_sales FROM
titles WHERE ytd_sales >= @sales ORDER BY ytd_sales DESC"
myCommand.Parameters.Add("@sales", SqlDbType.Int).Value
= sales_cutoff
Dim SqlDataAdapter myAdapter = New SqlDataAdapter(myCommand)
Dim topSellers As DataSet = New DataSet()
myAdapter.Fill(topSellers, "titles")CFML
<CFSET sales_cutoff = 1000>
<CFQUERY NAME="topSellers"
DATASOURCE="dsn">
SELECT title_id, title, ytd_sales
FROM titles
WHERE ytd_sales >=
<CFQUERYPARAM VALUE="#sales_cutoff#" CFSQLType="CF_SQL_INTEGER">
ORDER BY ytd_sales DESC
</CFQUERY>Generating E-Mail
Developers often need to programmatically generate e-mail messages. ASP provided
no built in SMTP support and required that you use COM objects, but
ASP.NET corrects this deficiency. ColdFusion comes with a powerful mail
delivery engine, and CFML provides sophisticated SMTP support through
the CFMAIL tag. The following snippets compare generating
a simple e-mail message.
ASP.NET
Dim myMail As MailMessage = New MailMessage() myMail.From = "foo@bar.com" myMail.To = "bar@foo.com" myMail.Subect = "Test Message" myMail.Body = "This is a test message." SmtpMail.Send(myMail)
CFML
<CFMAIL TO="foo@bar.com"
FROM="bar@foo.com"
SUBJECT="Test Message">
This is a test message
</CFMAIL>The CFMAIL tag supports multiple MIME types in a single message,
secure SMTP login, and more.
But the real power of the CFMAIL tag comes from performing complex
operations, like sending personalized e-mail to a mailing list which
involves retrieving a list of names and e-mail addresses from a database,
and then generating an SMTP message for each user. The following code
examples compare this process. As you can see, the ASP.NET code is more
than just convoluted; it requires complex low-level plumbing code to
perform what ColdFusion does with just two tags.
ASP.NET
Dim connString as String
connString = "..."
Dim objConnection as OleDbConnection
objConnection = New OleDbConnection(connString)
objConnection.Open()
Dim strSQL as String = "SELECT firstName, email FROM users"
Dim objCommand as OleDbCommand
objCommand = New OleDbCommand(strSQL, objConnection)
Dim objDataReader as OleDbDataReader
objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection)
Dim myMail As MailMessage = New MailMessage()
myMail.From = "foo@bar.com"
myMail.To = objDataReader("eMail")
myMail.Subect = "Test Message"
myMail.Body = "Dear" & objDataReader("firstName") & " ..."
SmtpMail.Send(myMail)
objDataReader.Close()CFML
<CFQUERY DATASOURCE="dsn"
NAME="users">
SELECT firstName, email FROM users
</CFQUERY>
<CFMAIL QUEREY="users"
TO="#email#"
FROM="bar@foo.com"
SUBJECT="Test Message">
Dear #firstname#, ...
</CFMAIL>ColdFusion Enterprise features a high performance multi-threaded mail delivery engine capable of delivering over 1,000,000 messages an hour and redundant SMTP servers; mass mailing is not just easy, it is also highly scalable. Read more about mail improvements in Making the Most of E-Mail with the cfmail Tag Enhancements in ColdFusion MX 6.1 .
Server-Side HTTP
Server-side HTTP support is a core feature of CFML exposed by the CFHTTP
tag.
ASP.NET
Dim myWebClient As System.Net.WebClient
myWebClient = New System.Net.WebClient()
Dim buffer As Byte()
buffer = myWebClient.DownloadData("http://www.macromedia.com/")
Dim content As String
content = System.Text.Encoding.UTF8.GetString(buffer)
Response.Write(Server.HtmlEncode(content))CFML
<CFHTTP URL="http://www.macromedia.com/" METHOD="GET"> <CFOUTPUT>#CFHTTP.FileContent#</CFOUTPUT>
XML Parsing
ColdFusion MX features a powerful integrated XML engine. Parsing an XML document is as simple as using the XMLParse() function. This comparison parses a simple block of XML and displays the value of the first child node.
ASP.NET
<% Dim xml as String xml = "<book ISBN='1-861001-57-5'> <title>Pride And Prejudice</title> <price>19.95</price> </book>" Dim doc As New XmlDocument() doc.LoadXml(xml) Dim root As XmlNode = doc.FirstChild =root.FirstChild.OuterXml %>
CFML
<CFSET xml="<book ISBN='1-861001-57-5'> <title>Pride And Prejudice</title> <price>19.95</price> </book>"> <CFSET doc=XMLParse(xml)> <CFOUTPUT> #doc.book.XmlChildren[1].XmlText# </CFOUTPUT>
The XMLParse() function returns a structure containing
the entire XML document that you may access directly (as just seen)
or manipulate using standard CFML functions. You may apply XSL transformations
using the XMLTransform() function, and you can execute
XPath searches using the XMLSearch() function.
Creating Web Services
ColdFusion components (CFCs) are objects that you create and expose using CFML tags. CFCs form the basis of structured tiered applications and are designed to facilitate code reuse and separate presentation from content.
You can also expose CFCs as web services by simply setting the attribute
ACCESS="remote". WSDL generation is automatic
and all SOAP manipulation is transparent.
The following is complete code for two web services, one to convert Celsius to Fahrenheit and the other Fahrenheit to Celsius.
ASP.NET
<WebMethod()> _ Public Function Celsius2Fahrenheit( _ ByVal Temp As Double) As Double Return Temp * 9 / 5 + 32 End Function <WebMethod()> _ Public Function Fahrenheit2Celsius( _ ByVal Temp As Double) As Double Return (Temp - 32) * 5 / 9 End Function
CFML
<CFCOMPONENT>
<CFFUNCTION NAME="Celsius2Fahrenheit"
RETURNTYPE="numeric"
ACCESS="remote">
<CFARGUMENT NAME="temp"
REQUIRED="yes"
DATATYPE="numeric">
<CFRETURN temp * 9 / 5 + 32>
</CFFUNCTION>
<CFFUNCTION NAME="Fahrenheit2Celsius"
RETURNTYPE="numeric"
ACCESS="remote">
<CFARGUMENT NAME="temp"
REQUIRED="yes"
DATATYPE="numeric">
<CFRETURN (temp – 32) * 5 / 9>
</CFFUNCTION>
</CFCOMPONENT>Invoking Web Services
Creating a CFML web service is quite similar to that of ASP.NET. But compare the way you invoke the web service below.
ASP.NET
Simple web service invocation is not really supported. ASP.Net requires that you first generate a proxy and then write code to connect to the web service through the proxy.
CFML
<!--- Start with 38 degrees Celsius --->
<CFSET tempc=38>
<!--- Invoke web service --->
<CFINVOKEWEBSERVICE="http://host/tempconf.cfc?wsdl"
METHOD="Celsius2Fahrenheit"
TEMP="#tempc#"
RETURNVARIABLE="tempf>
<!--- Display temp in Fahrenheit --->
<CFOUTPUT>
#tempc# Celsius = #tempf# Fahrenheit
</CFOUTPUT>It is somewhat ironic you can invoke web services so much easier with ColdFusion than with ASP.NET, a language built for .NET and web services. You can use the CFML code above to invoke any web services, including both the CFML and VB.NET examples seen previously.

