473,320 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Best Practices Question - Where do I put common code?

Hi all,

I have a few common methods that I need to use at different points in
my web application. I'm wondering where the best place would be to
put these? I think that I have three options.

1. I can create a common module like common.vb in my project and put
all the functions in there.

2. Create a utility class and create the common functions as shared
methods

3. Create a page base class that contains the common methods and then
inherit all my pages from that base class.

What's the accepted way to make a method available to all pages in an
application?

Thanks

Ren
Nov 19 '05 #1
5 3166
Hi,

If these methods are part of a page in a logical sense, then I would define
a BasePage class which will have these methods.

On the other hand, if these are generic (meaning not related to Pages), then
I would create a seperate project (or a common class) which will host this
utility class which will have static (shared) methods (only shared methods -
no shared class fields though).

I usually create a separate project for hosting common classes because as
the project moves forward, I will always have many common things which I
place in this assembly.

Personall, I would stay away from modules - I feel that is too VB specific.

--
HTH,
Rakesh Rajan
MVP, MCSD
http://www.rakeshrajan.com/
"wrecker" wrote:
Hi all,

I have a few common methods that I need to use at different points in
my web application. I'm wondering where the best place would be to
put these? I think that I have three options.

1. I can create a common module like common.vb in my project and put
all the functions in there.

2. Create a utility class and create the common functions as shared
methods

3. Create a page base class that contains the common methods and then
inherit all my pages from that base class.

What's the accepted way to make a method available to all pages in an
application?

Thanks

Ren

Nov 19 '05 #2
> 1. I can create a common module like common.vb in my project and put
all the functions in there.
Modules are bad, m'kay? Everything in a Module is static (Shared) and not
only globally available, but not ThreadSafe. Modules are lip-service to VB6
developers, but ASP.Net is not VB6, as much as it looks like it.
2. Create a utility class and create the common functions as shared
methods
A much better idea. However, again, you should use caution with static
(Shared) methods and properties. Encapsulation is a beautiful thing, and
while it is certainly appropriate to create static methods and properties,
there are situations where it is most certainly NOT. For example, creating a
static method that is simply a process that takes parameters and uses
function-scoped variables is an excellent way to re-use code. But a static
Collection is going to bite you in the arse eventually. As it is not
ThreadSafe, and ASP.Net is multi-threaded, you may have more than one
process trying to do more than one thing with the Collection at a time. One
process might be removing an item from the Collection while another is
iterating through it. OOPs. The same goes for static methods that operate on
static fields or properties that are scoped outside of those methods.

So, yes, use it, but no, don't abuse it. Be aware of the issues, and work
with them.
3. Create a page base class that contains the common methods and then
inherit all my pages from that base class.
Inheritance is a wonderful thing. Here's where we start thinking seriously
about Architecture. For example, would any of the common methods come in
handy in another app? You might want to put them into a business class that
the inherited Page makes use of, rather than defining them in the Page class
itself. You may see a use for more than one layer of inheritance as well.
This depends on your present requirements, taking possible future projects
into consideration at the same time. How much of this functionality will you
always want to inherit? How much will be optional? What sort of
functionality or methodology would you expect to share between this and
future projects? If you take your time and think these sorts of questions
through, you should be able to come up with a solid extensible model to go
by.
What's the accepted way to make a method available to all pages in an
application?
It all depends on what the method is, what the requirements of the
application are, and how the design and placement of the method will affect
future development. There is no single one-size-fits-all correct way to do
this. The above information should provide you with some of the
considerations that you should take into account in the process of making
this decision.

A solid understanding of the "pillars" of OOP, abstraction, inheritance,
encapsulation, and polymorphism, will help you immensely in your design
process.

Your questions show a good degree of thoughtfulness. This implies (to me at
least) that you will probably do well in your design efforst.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

<wrecker> wrote in message
news:kn********************************@4ax.com... Hi all,

I have a few common methods that I need to use at different points in
my web application. I'm wondering where the best place would be to
put these? I think that I have three options.

1. I can create a common module like common.vb in my project and put
all the functions in there.

2. Create a utility class and create the common functions as shared
methods

3. Create a page base class that contains the common methods and then
inherit all my pages from that base class.

What's the accepted way to make a method available to all pages in an
application?

Thanks

Ren

Nov 19 '05 #3
Hi,

Just in case you want to know more about statics and threads, check out the
two part series Scott has written.
http://odetocode.com/Articles/313.aspx

--
HTH,
Rakesh Rajan
MVP, MCSD
http://www.rakeshrajan.com/
"wrecker" wrote:
Hi all,

I have a few common methods that I need to use at different points in
my web application. I'm wondering where the best place would be to
put these? I think that I have three options.

1. I can create a common module like common.vb in my project and put
all the functions in there.

2. Create a utility class and create the common functions as shared
methods

3. Create a page base class that contains the common methods and then
inherit all my pages from that base class.

What's the accepted way to make a method available to all pages in an
application?

Thanks

Ren

Nov 19 '05 #4
Kevin Spencer wrote:
1. I can create a common module like common.vb in my project and put
all the functions in there.


Modules are bad, m'kay? Everything in a Module is static (Shared) and not
only globally available, but not ThreadSafe. Modules are lip-service to VB6
developers, but ASP.Net is not VB6, as much as it looks like it.
2. Create a utility class and create the common functions as shared
methods


A much better idea. However, again, you should use caution with static
(Shared) methods and properties. Encapsulation is a beautiful thing, and
while it is certainly appropriate to create static methods and properties,
there are situations where it is most certainly NOT. For example, creating a
static method that is simply a process that takes parameters and uses
function-scoped variables is an excellent way to re-use code. But a static
Collection is going to bite you in the arse eventually. As it is not
ThreadSafe, and ASP.Net is multi-threaded, you may have more than one
process trying to do more than one thing with the Collection at a time. One
process might be removing an item from the Collection while another is
iterating through it. OOPs. The same goes for static methods that operate on
static fields or properties that are scoped outside of those methods.

So, yes, use it, but no, don't abuse it. Be aware of the issues, and work
with them.
3. Create a page base class that contains the common methods and then
inherit all my pages from that base class.


Inheritance is a wonderful thing. Here's where we start thinking seriously
about Architecture. For example, would any of the common methods come in
handy in another app? You might want to put them into a business class that
the inherited Page makes use of, rather than defining them in the Page class
itself. You may see a use for more than one layer of inheritance as well.
This depends on your present requirements, taking possible future projects
into consideration at the same time. How much of this functionality will you
always want to inherit? How much will be optional? What sort of
functionality or methodology would you expect to share between this and
future projects? If you take your time and think these sorts of questions
through, you should be able to come up with a solid extensible model to go
by.

Just to add here:

You may find that some functionality naturally lives within a page,
especially if you need access to the HttpContext (e.g. Page, Request,
Response, Session), but for more general purpose functionality, I would
tend to place that in seperate utility classes/projects (depending upon
how likely it is to be reused in seperate solutions).

Some examples:
Image manipulation functions - seperate project. Gonna be reusable in
different situations, even non-ASP.Net solutions.

Password Management and Client Logon - seperate class. Specific to the
authentication mechanism being used.

Navigation checks (ensuring valid navigations are occuring) - Base
Class, since we need access to the Request and the Session.

Damien

[snip rest]

Nov 19 '05 #5
Kevin Spencer wrote:
1. I can create a common module like common.vb in my project and put
all the functions in there.


Modules are bad, m'kay? Everything in a Module is static (Shared) and
not only globally available, but not ThreadSafe. Modules are
lip-service to VB6 developers, but ASP.Net is not VB6, as much as it
looks like it.


That's a good point that many people overlook. If you're going to use
Shared objects in VB.NET, you should use the Monitor class to control access
to them. In C#, you use the lock keyword.

--
Jim Cheshire
JIMCO Software
http://www.jimcosoftware.com

FrontPage add-ins for FrontPage 2000 - 2003


Nov 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Epson Barnett | last post by:
Hi, What techniques are other people using for common code libraries? I have some classes I've written that will be useful in all my projects. I'd like to have one copy of these common classes...
16
by: D Witherspoon | last post by:
I am developing a Windows Forms application in VB.NET that will use .NET remoting to access the data tier classes. A very simple way I have come up with is by creating typed (.xsd) datasets. For...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
0
by: Anonieko Ramos | last post by:
ASP.NET Forms Authentication Best Practices Dr. Dobb's Journal February 2004 Protecting user information is critical By Douglas Reilly Douglas is the author of Designing Microsoft ASP.NET...
10
by: jojobar | last post by:
Hello, I am trying to use vs.net 2005 to migrate a project originally in vs.net 2003. I started with creation of a "web site", and then created folders for each component of the site. I read...
8
by: SStory | last post by:
When I right a class, I am wondering what are the best practices for error handling? Do I try..catch and trap the error and if so what do I do with it? Because most likely the class user will...
2
by: steve | last post by:
Can someone point me to some information on namespace best practices? Questions I have are as follows: 1) Should I use .NET namespaces or URL, URI and URN's? Ie mycompany.mydivision.myapp vs...
1
by: D Witherspoon | last post by:
Coming up with a scenario here. For example there is the standard .NET MailMessage class. I am creating a project (let's call it CommonBase) that has the following 2 classes ...
52
by: burgermeister01 | last post by:
First, let me say that this question is a rather general programming question, but the context is PHP, so I figured this group would have the most relevant insight. Anyways, this is also more of...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.