Global Objects

Discussion in 'ASP .Net' started by JustinCarmony, Jan 16, 2007.

  1. Hi,

    I'm a PHP programmer who has been hired by a company to do web
    applications, but they want me to use ASP .NET. For the most part, the
    transition has been relatively smooth. However, I have a question:

    In PHP, I would create a class that I called DBContol that would be the
    intermediary for all communications between my web application and
    Database. I would execute a command something like this:

    <?
    // This
    $GLOBALS["mainDB"] = new DBControl("DatabaseName");

    // or I would store it in the session
    $_SESSION["mainDB"] = new DBControl("DatabaseName);

    // Then I would be able to execute commands something like this:

    $_SESSION["mainDB"]->query($queryString);
    ?>

    Now, in ASP .NET I know there is a Global.asax file that you create
    that is the "root" of the application and is the very first executed,
    depending on what EventHandler you use. You also use an <object> tag to
    declare objects used across the application.

    The problem is, I can't really get this to work. So here is my
    question:

    How can I create an instance of a class in my application that can
    access that object where ever needed in the code, how do I access it
    and run it's methods, and make sure that its a sound and secure way of
    doing this. I've looked for tutorials, and they all talk about storing
    variables globally, but I need to access this instance of a class no
    matter where in the code.

    Any suggestions? Thanks in advance for any replies.
     
    JustinCarmony, Jan 16, 2007
    #1
    1. Advertising

  2. Also, some of the errors I experiences while trying this.

    I would set the <object> tag in the Global.asax, and later in the
    program I tried to do this in the Global.asax's Application_Start():

    void Application_Start(object sender, EventArgs e)
    {
    mainDB = new DBControl("mainDB");
    }

    and it told me that mainDB was read only.

    I'm still going to play with it and keep checking back and posting
    progress.
     
    JustinCarmony, Jan 16, 2007
    #2
    1. Advertising

  3. JustinCarmony

    Aidy Guest

    To answer your question, in Application_Start you would;

    Application["myDB"] = new DBControl("mainDB");

    Then when you need to use it on a page;

    DBControl mainDB = (DBControl) Application["myDB"];
    mainDB.SomeMethod();

    However you are better making sure the methods on your DBControl class are
    all static and don't persists any data in the class itself across method
    calls. That way you can simply use this code;

    DBControl.SomeMethod();

    anywhere in your code without the need to store anything globally.

    You can combine the two techniques using the "singleton pattern" (google
    it).

    However do look to using static methods on your class and get away ftom the
    whole "global variable" methodology.

    "JustinCarmony" <> wrote in message
    news:...
    > Hi,
    >
    > I'm a PHP programmer who has been hired by a company to do web
    > applications, but they want me to use ASP .NET. For the most part, the
    > transition has been relatively smooth. However, I have a question:
    >
    > In PHP, I would create a class that I called DBContol that would be the
    > intermediary for all communications between my web application and
    > Database. I would execute a command something like this:
    >
    > <?
    > // This
    > $GLOBALS["mainDB"] = new DBControl("DatabaseName");
    >
    > // or I would store it in the session
    > $_SESSION["mainDB"] = new DBControl("DatabaseName);
    >
    > // Then I would be able to execute commands something like this:
    >
    > $_SESSION["mainDB"]->query($queryString);
    > ?>
    >
    > Now, in ASP .NET I know there is a Global.asax file that you create
    > that is the "root" of the application and is the very first executed,
    > depending on what EventHandler you use. You also use an <object> tag to
    > declare objects used across the application.
    >
    > The problem is, I can't really get this to work. So here is my
    > question:
    >
    > How can I create an instance of a class in my application that can
    > access that object where ever needed in the code, how do I access it
    > and run it's methods, and make sure that its a sound and secure way of
    > doing this. I've looked for tutorials, and they all talk about storing
    > variables globally, but I need to access this instance of a class no
    > matter where in the code.
    >
    > Any suggestions? Thanks in advance for any replies.
    >
     
    Aidy, Jan 16, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    1
    Views:
    792
    Alvin Bruney
    Jul 29, 2003
  2. Wayne
    Replies:
    2
    Views:
    476
    Wayne
    Nov 11, 2003
  3. Daniel Bass
    Replies:
    2
    Views:
    6,885
    Daniel Bass
    Dec 31, 2003
  4. 2obvious

    no global constants in global.asax?

    2obvious, Sep 4, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,832
  5. 7stud
    Replies:
    11
    Views:
    700
    Dennis Lee Bieber
    Mar 20, 2007
Loading...

Share This Page