|My ASP.net application uses a common class I added to my App_Code folder which has all of the methods I use more than once throughout the application.
Because this class is used in the masterpage, almost all site aspx pages and multiple usercontrols, I am instantiating the class multiple times in each request.
I am instantiating the class in the page_load of pages and controls and then setting it to nothing on each page_unload.
There is nothing in the class that needs to maintain state between requests.
Is there a better way to do this or is that the normal approach?
It seems so inefficient to me to be creating/destroying the class in every code behind file.
I was wondering if there would be any advantage to putting the class in each users session for re-use for the lifetime of the session.
I don't know if there would be any advantage to that approach or not.
i was thinking of checking (each time I instantiate) if the object is in session and if not add it and if its already in session, return the object.
I guess I could do that in the objects constructor.
Any suggestions much appreciated
Heres what I am doing in every code behind file now:
Dim c As New CommonCode()
Public ReadOnly Property C As CommonCode
If C Is Nothing Then C = New CommonCode
Protected Sub btnPostComment_Click(sender As Object, e As EventArgs)
Dim name As String = IIf(txtName.Text = [String].Empty, "Anonymous", txtName.Text)
If txtComments.Text <> [String].Empty Then
c.PostComment(ID, name, txtComments.Text, RadRating.Value)
Protected Sub Page_Unload(sender As Object, e As System.EventArgs) Handles Me.Unload
c = Nothing