Storage For Delphi Applications

Last Updated: 10/14/2005



Sections above here:
Home  »  Development  »  State Management

Sections below here:

    Topics in this section:
    State Management
    Storage For Delphi Applications
    Complex State & Back Button

    Search Documentation:

    Safe Storage in IntraWeb for Delphi

    Form / Datamodule Members

    This section refers to IntraWeb for Delphi only: since IntraWeb forms and datamodules are persistent just like in a normal Delphi application you can store information as member variables and properties. Such members should be used when the form itself needs to store data about its instance or to receive input from another form. 

    User Session

    The user session (covered more in detail in the Session Management section of this manual) contains a .Data property that can hold a reference to an object. When you need to store user specific information you can store it in the .Data property of the session. Data accepts a TObject instance and will destroy the TObject automatically when the session is destroyed. The easiest way is to create an object and add the fields that you wish, and then create your object and store it in the session's Data property when the session is created. The Phonetics demo shows an extended example of this.
     
    When a new IntraWeb project is created a shell user session object is created for you in the ServerController. The default ServerController looks like this:
     

    unit ServerController;
      unit ServerController;
     
    interface
     
    uses
      SysUtils, Classes, IWServerControllerBase,
      IWApplication, IWAppForm;
     
    type
      TIWServerController = class(TIWServerControllerBase)
        procedure IWServerControllerBaseNewSession(ASession: TIWApplication;
          var VMainForm: TIWAppForm);
      private
      public
      end;
     
      TUserSession = class
      public
      end;
     
    // Procs
      function UserSession: TUserSession;
     
    implementation
    {$R *.dfm}
     
    uses
      IWInit;
     
    function UserSession: TUserSession;
    begin
      Result := TUserSession(RWebApplication.Data);
    end;
     
    procedure TIWServerController.IWServerControllerBaseNewSession(
      ASession: TIWApplication; var VMainForm: TIWAppForm);
    begin
      ASession.Data := TUserSession.Create;
    end;
     
    end.

    TUserSession is an empty session object that you can add members, properties and methods to. The code to create the TUserSession for each session is also created in the OnNewSession Event.
     
    A function named UserSession also exists for easy access to the object. So if you changed the TUserSession declaration to the following:

    TUserSession = class
      public
        Username: string;
        Password: string;
      end;
    

    You could access these properties elsewhere in your code simply as shown here:

    UserSession.Username := 'Joe';
    LPassword := UserSession.Password;

    If you do not need a user session you may choose to eliminate it from the code. It is not necessary and is part of the default template simply as a convenience.
     
    The class type of TUserSession can be of any type. For projects that are generated with a datamodule the TUserSession is a descendant of TComponent and not TObject as shown here. TComponent allows the session to own components such as the datamodule and allows for easier cleanup.



    (C) 2002-2009 - Atozed Software Ltd.