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 5 3141
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
> 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
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
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]
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
...
|
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...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
| |