How is a common module handled in asp.net web application

nor 1 Reputation point
2022-08-05T18:34:41.68+00:00

I have a common module that has functions that are used by various web page in my asp.net web app. Does each user get his own copy of that module or is it shared by all users?

Thanks
Nor

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,272 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Albert Kallal 4,651 Reputation points
    2022-08-07T01:33:46.34+00:00

    Well, just like say in VB6 or even access VBA, or vbnet and asp.net web pages?

    You can simple add a module to the project, and dump in your helper and handy dandy code routines.

    Only ONE BIG new issue?

    In those code modules, we often say had variables at the base module (class) level. That you SHOULD NOT do at all, unless those values are to be common to all web users.

    In c#, we don't have this common module, but dropping in (adding) a static class to the project behaves very simular to that module in vb.net.

    But, once again, do NOT share and have class wide variables, since all users are hitting and running the SAME web site?

    Well, for a page load or any other class you use - a NEW instance is created. But for a static public class, a NEW instance is NOT re-created, and any class wide variables ARE IN FACT shared among all users!!!

    But, I have a boatload of general code routines (to get connection strings, to get say a data table etc.

    So, in vb.net, I might have a grid on a form, and my load code will will call/consume/use code from that global module of code routines that all web pages need and want and can freely use.

    So, my markup might be say this GridView

    228825-image.png

    And my code to load can say be this:

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  
      
            If Not IsPostBack Then  
                Dim rstData As DataTable = MyRst("SELECT * FROM Fighters")  
                GridView1.DataSource = rstData  
                GridView1.DataBind()  
            End If  
      
        End Sub  
      
    

    And the results are this:

    228767-image.png

    So, the routine MyRst? That is a global function, placed in my global module that I added to the project (called module 2).

    So that code in module2 is this:

    Module Module2  
      
        Public Function MyRst(strSQL As String, ByVal Optional strCon As String = "") As DataTable  
      
            If strCon = "" Then  
                strCon = My.Settings.TEST4  
            End If  
      
            Dim rstData As New DataTable  
            Using conn As New SqlConnection(strCon)  
                Using cmdSQL As New SqlCommand(strSQL, conn)  
                    conn.Open()  
                    rstData.Load(cmdSQL.ExecuteReader)  
                    rstData.TableName = strSQL  
                End Using  
            End Using  
            Return rstData  
        End Function  
      
    etc. etc. etc.  
    

    Now, as noted, in c# we don't have just "modules" we can add to the project.

    So, above in c# would be this:

            protected void Page_Load(object sender, EventArgs e)  
            {  
                if (!IsPostBack)  
                {  
                    DataTable rstData;  
                    rstData = General.MyRst("SELECT * FROM Fighters");  
                    GridView1.DataSource = rstData;  
                    GridView1.DataBind();  
                }  
            }  
      
    

    Note how I did prefix the routine MyRst with "general". So, I add a static class "General" to the project.

    The code would look like this - all static methods/members in that class I called general:

        public static class General  
        {  
      
            public static DataTable MyRst(string strSQL)  
            {  
                DataTable rstData = new DataTable();  
                using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))  
                {  
                    using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))  
                    {  
                        cmdSQL.Connection.Open();  
                        rstData.Load(cmdSQL.ExecuteReader());  
                    }  
                }  
                return rstData;  
            }  
      
    etc. etc. etc. (more functions )  
      
    

    So, the above gives a rundown for c# and vb.net

    However, ONE BIG new rule:

    Do not create nor use nor have variables that are public/global to the static class - since as noted, they WILL and are actualy shared between all web users. In fact, some attempt to persit and share data/variables between all web users this way - but it is a bad idea, since such values can (and will) often go out of scope.

    So, the above quite much gives a run down as to how you can have a fmailer "code library" of routines.

    Regards,
    Albert D. Kallal (Access MVP 2003-2017)
    Edmonton, Alberta Canada

    0 comments No comments

  2. Lan Huang-MSFT 25,711 Reputation points Microsoft Vendor
    2022-08-08T03:08:27.803+00:00

    Hi @nor ,

    • Access Level. Within a module, you can declare each member with its own access level. Module members default to Public access, except variables and constants, which default to Private access. When a module has more restricted access than one of its members, the specified module access level takes precedence.
    • Scope. A module is in scope throughout its namespace.
      The scope of every module member is the entire module. Notice that all members undergo type promotion, which causes their scope to be promoted to the namespace containing the module. For more information, see Type Promotion.
    • Qualification. You can have multiple modules in a project, and you can declare members with the same name in two or more modules. However, you must qualify any reference to such a member with the appropriate module name if the reference is from outside that module. For more information, see References to Declared Elements.

    https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/module-statement

    Note that a .NET Framework module is not the same as a module in Visual Basic, which is used by a programmers to organize functions and subroutines in an application.

    https://learn.microsoft.com/en-us/dotnet/api/system.reflection.module?view=net-6.0
    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments