ASP Classic Best Practices

L

Lorenzo Bolognini

Hi all,

i am an ASP developer with 1,5+ years experience. After sneaking around the
ASP Classic world for a bit i became a bit dissatisfied of my ASP code
practices so i'd like to have some advice from the advanced/pro users...
some advice that would help me to grow up!

I know that many (if not all) the stuff that was a hassle in ASP 3 has been
corrected in ASP.NET/VS.NET (which i'm studying) but ASP 3 will still be
around for a long time (at least for all the lifetime of Win2k3) so i think
it would still be worth knowing how to program the right way in ASP 3.

So here are my questions
- How do you design multi-tier web applications? What needs to go in the
Data/Business/Presentation layer? Otherwise (if 3 tier is overkill for you,
as it would be for the stuff i write) how do you architect your application?
Where do you put code, ect...?
- How do you access the db? Do you have customized classes for accessing the
data, retrieving recordsets?
- Do you have any datagrid class for displaying/editing/sorting data?
- Do you store db connections as an application variable?
- Some links to useful components (preferably free!)
- Any else?

Thank You very much for paying attention to this post.

Lorenzo
 
L

Lorenzo Bolognini

In my previous post i forgot and important subject: error handling,
especially form processing error handling, user input validation, etc...

Lorenzo
 
A

Aaron Bertrand - MVP

In my previous post i forgot and important subject: error handling,
especially form processing error handling, user input validation, etc...

I do as much validation as I can on the client side. This way, the user
gets feedback right away, instead of waiting for a round-trip to the server.
Also, this means less toll on your web server, particularly if your user
base consists of a large ratio of numbskulls (or if your forms aren't
designed very well :).

However, I think it is important to also validate on the server side (for
format issues, in ASP; for data violations, in SQL Server). There should
never be a way for bad data to get into the system, and a cunning user can
usually find a way to bypass client-side validation.
 
W

WIlliam Morris

Where do you put code, ect...?

- Option Explicit is the first thing after the language declaration
- Pre-processing code - getting the data from the database; updates;
inserts; deletes all go above the <HTML> tag, so redirecting is not an issue
when the db work is done.
- Presentation - that is, everything the user sees after getting the data -
goes between the <HTML>of course</HTML>.
- All javascript validations and instructions are collected into an array
and output as the last thing before the </HTML> tag. I have another post on
this subject, earlier in this group.
- Subs and functions local to the page go after the </HTML> tag.
- Subs and functions used throughout the app go into other files, such as
stringFunctions.asp, sqlFunctions.asp, etc etc etc, and are included as
needed immediately after the Option Explicit line.
- Because we specify IE5.5 as a minimum standard for our application,
behaviors are used whenever possible to limit the amount of code written in
every page.
- How do you access the db? Do you have customized classes for accessing the
data, retrieving recordsets?

Customized classes? No. Wrapper functions? Yes. For instance,

GetRs conn, rs, sSQL, "[Somepage.asp].[Function1] Getting employee data<p>"
& sSQL

- creates the connection if conn.state <> 1
- creates the recordset if rs is not an object
- redirects to an "Oops" page if rs.state <> 1, displaying the error
message shown in the last parameter

CreateConnection objConnection:=conn, UseShape:=false

- accepts a connection object
- gets connection string for shape queries if true, vanilla OLEDB if false
- Do you have any datagrid class for displaying/editing/sorting data?
No.

- Do you store db connections as an application variable?

NO. NEVER. NOT EVER. ON PAIN OF DEATH. OR JUST PAIN. Let me be
completely unambiguous: NO.
- Some links to useful components (preferably free!)

JMail. http://www.dimac.net. Beats the hell out of CDONTS.


- Wm "agressively lazy" Morris
 
A

Aaron Bertrand - MVP

JMail. http://www.dimac.net. Beats the hell out of CDONTS.

I don't think anybody ever suggested that CDONTS was any good. It was
deprecated for a reason (well, dozens of reasons, probably).

If you have access to SQL Server, I prefer xp_smtp_sendmail for sending
alerts, etc. This way, all mailing is controlled from one location, and it
is capable of sending alerts for database errors, ASP errors (500, 404, 403,
etc), and you can also schedule it to send daily, weekly emails to lists of
users, etc. You have a single stored procedure wrapped around this xp, and
you can call it from anywhere (stored procedure, directly from ASP, from a
job, etc).

Of course, you might have to rely on a COM, or CDO.Message object if you are
worried that xp_smtp_sendmail will ever fail... but that's easy enough to
set up. In the procedure that wraps the xp, just have an error trap that
uses sp_OACreate to send mail via jmail, CDO.Message, or some other object
to send the mail. You might also wish to send a NET SEND in this case, in
case it's the SMTP server and not the sending itself, or else have a backup
SMTP server available as well.

In any case, for more info about xp_smtp_sendmail, see www.aspfaq.com/2403
 
B

Brian Staff

After retrieving data into a RecordSet, I used to iterate thru the RS and I
have even issued a GetRows into an array, but I now save it into an XML
structure and iterate thru its Nodes.

Putting data into an array makes maintenance a PITA because you have to
identify the columns using numbers, so visiting the page some days later
causes headaches.

here's what I do now:

set oXML = Server.CreateObject("MSXML2.DomDocument")
oRS.save oXML,1
' now close all ADO objects
set rows = oXML.selectNodes("//z:row")
rowCount = rows.length
for each row in rows
' use row.getAttribute("columnName")
next

Brian
 
D

dlbjr

Why not make a client side cursor recordset and use it as a disconnected
recordset?

Just disconnect right after rs.open.

<%
Set rs = Server.CreateObject("ADODB.Recordset")
rs.CursorLocation = 3
rs.CursorType = adOpenStatic
rs.Open strSQL, ObjConn
Set ObjConn = Nothing
Do While Not rs.EOF
'What Ever
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
%>

iterating an XML is expensive processing time.

-dlbjr

Discerning resolutions for the alms
 
B

Bob Barrows

Brian said:
After retrieving data into a RecordSet, I used to iterate thru the RS
and I have even issued a GetRows into an array, but I now save it
into an XML structure and iterate thru its Nodes.

Putting data into an array makes maintenance a PITA because you have
to identify the columns using numbers, so visiting the page some days
later causes headaches.
You can use constants to save yourself those headaches:

rs.open (select username, userpassword from ..." ...
arResults = rs.GetRows
const cUserName = 0
const cUserPassword = 1

sUserName = arResults(cUserName, 0)
etc.

An array will outperform the XML Parser, so if you need performance, stick
with the array.

HTH,
Bob Barrows
 
B

Bob Barrows

dlbjr said:
Why not make a client side cursor recordset and use it as a
disconnected recordset?

Just disconnect right after rs.open.

iterating an XML is expensive processing time.
It will probably outperform a recordset loop (I've never tested this - it's
just my gut feeling), but an array will beat both handily.

Bob Barrows
 
B

Brian Staff

Don't write it off so quickly. Performance is not an issue for me - it's
plenty fast enough.

Having my data in an XML structure now allows me to separate the tiers quite
easily.

The data tier reads the data and creates an XML structure. It then passes it
off to the business tier who iterates thru it creating a XHTML structure who
then passes it to the GUI tier where it may, depending on the client,
transform it using XSL before sending it to the end user.

It works for me<g>.

Brian
 
B

Bob Barrows

Brian said:
Don't write it off so quickly. Performance is not an issue for me -
it's plenty fast enough.

I didn't mean to recommend that it be "written off". I was merely commenting
on the relative performance.
I use xml documents extensively, mainly for passing structured data between
processes, such as from server-side to client-side code.

Bob Barrows
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top