ASP classes, code included multiple times - how to avoid?

N

Noozer

I'm writing an ASP application and have a noob question...

I have a class that access an MS SQL database. I have another class also
accesses an MS SQL database and this second class uses objects from the
first class. I have a third class using the DB and objects of the second
class.

Each of these classes contain all the code needed to access the database and
this means much duplicated code. What I'd like to know is if there is a way
to avoid the duplicated code?

I know I could write the code once, then do an #include to include the code
into the class, but that still means multiple occurances of the code.

???
 
M

Mark J. McGinty

Noozer said:
I'm writing an ASP application and have a noob question...

I have a class that access an MS SQL database. I have another class also
accesses an MS SQL database and this second class uses objects from the
first class. I have a third class using the DB and objects of the second
class.

Each of these classes contain all the code needed to access the database
and this means much duplicated code. What I'd like to know is if there is
a way to avoid the duplicated code?

I know I could write the code once, then do an #include to include the
code into the class, but that still means multiple occurances of the code.

Have you considered redesigning your set of classes so that none of them
duplicate each other's functionality? Code a common low-level class that
the others can call to do their dirty work. If that's impractical, put the
redundant code in regular functions and call them from the classes.

Also try to avoid any unnecessary depth in your object dependency trees,
e.g., ClassC depends on ClassB which depends on ClassA, etc... I'm not
saying never to do this, just to keep it to the minimum that's needed.

If you want to post the classes you're using (or perhaps abbreviated
versions if possible) we may be able to offer more specific suggestions.


-Mark
 
C

Chris Hohmann

Noozer said:
I'm writing an ASP application and have a noob question...

I have a class that access an MS SQL database. I have another class also
accesses an MS SQL database and this second class uses objects from the
first class. I have a third class using the DB and objects of the second
class.

Each of these classes contain all the code needed to access the database
and this means much duplicated code. What I'd like to know is if there is
a way to avoid the duplicated code?

I know I could write the code once, then do an #include to include the
code into the class, but that still means multiple occurances of the code.

???

Unfortunately, VBScript does not allow for the redefinition of classes. You
could do it in JScript. Your options would be to rewrite the classes in
JScript or create JScript wrapper classes/functions for your existing
VBScript classes.
 
N

Noozer

Mark J. McGinty said:
Have you considered redesigning your set of classes so that none of them
duplicate each other's functionality? Code a common low-level class that
the others can call to do their dirty work. If that's impractical, put
the redundant code in regular functions and call them from the classes.

Also try to avoid any unnecessary depth in your object dependency trees,
e.g., ClassC depends on ClassB which depends on ClassA, etc... I'm not
saying never to do this, just to keep it to the minimum that's needed.

If you want to post the classes you're using (or perhaps abbreviated
versions if possible) we may be able to offer more specific suggestions.

Thanks all!

Basically, I'm trying to design some very generic classes that I will use
often in multple projects. Because of this, I was building each class to be
self sustaining, containing any code needed to converse with databases,
generate output etc. With this, I'd have a lot of duplicated code if I was
using several of the classes at the same time.

Reading the replies here and planning a bit more, it makes much more sense
to build even more basic classes (like a database class) for anything that
I'll be using that often, and being sure to include them in the PROJECT (ie,
ASP) page and NOT within the classes themselves. Such as:

<!-- #include file="dbclass.inc" --> JUST DB handler code
<!-- #include file="pageclass.inc" --> JUST code about pages
<!-- #include file="bookclass.inc" --> JUST code about books

....instead of...

<!-- #include file="bookclass.inc" --> Code about books, DB handler for book
related info, and has an #include to import the "pageclass.inc" file, which
has it's own DB handler for page related info.
 
P

Paxton

Noozer said:
Thanks all!

Basically, I'm trying to design some very generic classes that I will use
often in multple projects. Because of this, I was building each class to be
self sustaining, containing any code needed to converse with databases,
generate output etc. With this, I'd have a lot of duplicated code if I was
using several of the classes at the same time.

Reading the replies here and planning a bit more, it makes much more sense
to build even more basic classes (like a database class) for anything that
I'll be using that often, and being sure to include them in the PROJECT (ie,
ASP) page and NOT within the classes themselves. Such as:

<!-- #include file="dbclass.inc" --> JUST DB handler code
<!-- #include file="pageclass.inc" --> JUST code about pages
<!-- #include file="bookclass.inc" --> JUST code about books

...instead of...

<!-- #include file="bookclass.inc" --> Code about books, DB handler for book
related info, and has an #include to import the "pageclass.inc" file, which
has it's own DB handler for page related info.

You might want to look at this post wrt include file extensions:

http://groups.google.co.uk/group/mi...cd940/71c14a5ae7975140?hl=en#71c14a5ae7975140

/P.
 
M

Mike Brind

Dave said:
There is no reason to go back. You can simply tell IIS to parse .inc
requests with asp.dll, and there will be no functional difference between
the extensions.

Assuming Noozer has access to the IIS server, or an obliging web
hosting company....

;-)
 

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

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top