?
Solved

Best way to implement WebApplication-wide functions/variables?

Posted on 2005-04-01
7
Medium Priority
?
181 Views
Last Modified: 2010-04-07
Say I want a particular set of functions and variables to be available to all my webforms. Let's not worry about session state - they just require state for the current request of the current page.

I don't want to do this:

<code>
Public Class WebForm2
    Inherits System.Web.UI.Page
    Private MyUtilitiesClass as new UtilitiesClass()
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Response.Write (MyUtilitiesClass.BlahBlah())
    End Sub
End Class
</code>

Pretty sure making BLahBLah() a shared member won't work either.


The way I figured out seems pretty sneaky really, and maybe I'm missing a better way. I sorta cheated:

<code>
**FMS5.vb
Public Class FMS5
    Inherits System.Web.UI.Page
    Public Function db() As SqlClient.SqlConnection
        .
    End Function
    Public Function ExecuteReader(ByVal sql As String) As SqlClient.SqlDataReader
        .
    End Function
End Class

**WebForm2.aspx.vb
Public Class WebForm2
    Inherits FMS5

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        db.Open() ' db is declared in FMS5.vb
    End Sub
End Class

**WebForm3.aspx.vb
Public Class WebForm3
    Inherits FMS5

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        set r = ExecuteReader("select...")
    End Sub
End Class
</code>

IOW - I made my own "base" class, inheriting from the Page, and hte actual pages now inherit from my own base class. So they get my functions, and the get teh Page.* functions too.


Is this considered wrong, and is there another way to do it?
0
Comment
Question by:blowfly
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 23

Expert Comment

by:b1xml2
ID: 13686814
You can encapsulate what you want to do into a class and expose that as a property in your base class.

Public NotInheritable Class MyFunctions

    Public Shared Function GetItem(ByVal bag As System.Web.UI.StateBag) As MyFunctions
        Return New MyFunctions(bag)
    End Function


    Private ReadOnly Property Page() As System.Web.UI.Page
        Get
            Return DirectCast(System.Web.HttpContext.Current.Handler, System.Web.UI.Page)
        End Get
    End Property

    Private ReadOnly bag As System.Web.UI.StateBag

    Private Sub New(ByVal bag As System.Web.UI.StateBag)
        Me.bag = bag
    End Sub

    Default Property Items(ByVal name As String) As Object
        Get
            Return bag(name)
        End Get
        Set(ByVal Value As Object)
            bag(name) = Value
        End Set
    End Property
    Public ReadOnly Property UserID() As String
        Get
            Return CStr(Page.Session("UserID"))
        End Get
    End Property


    Public ReadOnly Property Data() As Integer
        Get
            'using the view state
            Return CInt(IIf(Me("Data") Is Nothing, 0, Me("Data")))
        End Get
    End Property

End Class

Base page example:

Public Class BasePage
      Inherits System.Web.UI.Page
      
      Private f As MyFunctions
      
      Private Sub BasePage_Init(ByVal sender As Object,ByVal e As EventArgs) Handles MyBase.Init
            f = MyFunction.GetItem(Me.ViewState)
      End Sub
      
      Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            'e.g.
            Response.Write(MySpecialFunctions.UserID)
      End Sub
      
      Public ReadOnly Property MySpecialFunctions() As MyFunctions
            Get
                  Return f
            End Get
      End Property
End Class
0
 
LVL 9

Expert Comment

by:zaghaghi
ID: 13686862
you can write all of your funxctions in global.asax.
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13686875
I am not a fan of writing stuff to the Global.asax as this becomes available not just to all webforms but to all user controls...

by using the approach mentioned:

1. CUSTOM CLASS
2. BASE PAGE has CUSTOM CLASS as a PROPERTY

This way, you can use different base pages to provide common functionality. You can also write common utility classes that only have shared members and for which you can access anytime. There is really no need to play around with the Global class
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:blowfly
ID: 13686993
Sorry, I should have made it clearer. I said:

==================
I don't want to do this:
<code>
Public Class WebForm2
    Inherits System.Web.UI.Page
    Private MyUtilitiesClass as new UtilitiesClass()
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Response.Write (MyUtilitiesClass.BlahBlah())
    End Sub
End Class
</code>
==================

Reason being that i don't wan to prefix each function by "MyUtilitiesClass." all the time. Every now and then it's OK, but for heavily used functions it's a major nuisance. (Hence my attempt at fiddling with the webform's inheritance)

Coming from a heavily ASP/VB6 background, maybe I just need to adjust to the new way of doing things, but I could simply add a function to a VB6 module, and it was instantly accessible to everywhere does. I know that ain't object oriented, but it sure was nifty.

I'll try the global.asax thing, thanks.
0
 

Author Comment

by:blowfly
ID: 13687242
OK - if I add "Public MyPublicMember As String" to the GLobal class in Global.asax, how do I reference it in WebForm1? (Tried a few different things, no luck)
0
 
LVL 2

Accepted Solution

by:
neil_richards earned 750 total points
ID: 13687268
What you're doing is one of the 'best practice' ways of doing it.  Alternatively you could create static methods within the UtilitiesClass, but then you'd have to go UtilitiesClass.BlahBlah() with the prefix (which you said you didn't want.  Otherwise, what you're doing is fine, creating a parent class which shares methods among it's children is classic Object Oriented design.

Just make sure to declare the methods and variables properly (ie. protected vs. private) so they're accessible from the children.

Cheers
0
 

Author Comment

by:blowfly
ID: 13687980
Thanks - as long as it's "recommended" and not some dodgy hack, I'm cool. Just wanted confirmation on that.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question