User Session Overview

User Session Overview

User Session Overview

The TIWUserSession base class (TIWUserSessionBase) is inherited from TDataModule and you can use this class for exposing user data and business rules for your application. In general you have only one class definition for your User Session.

The sample code below show how you can use your User Session to expose an already existing Data Module.

  1. unit UserSessionUnit;
  2. interface
  3. uses
  4.   IWUserSessionBase, SysUtils, Classes, DataModule;
  5. type
  6.   TIWUserSession = class(TIWUserSessionBase)
  7.     procedure IWUserSessionBaseDestroy(Sender: TObject);
  8.   private
  9.     FDM: TDM;
  10.     function GetDM: TDM;
  11.   public
  12.     property DM: TDM read GetDM;
  13.   end;
  14. implementation
  15. {$R *.dfm}
  16. { TIWUserSession }
  17. function TIWUserSession.GetDM: TDM;
  18. begin
  19.   if not Assigned(fDM) then begin
  20.     fDM := TDM.Create(Nil);
  21.   end;
  22.   Result := fDM;
  23. end;
  24. procedure TIWUserSession.IWUserSessionBaseDestroy(Sender: TObject);
  25. begin
  26.   if Assigned(fDM) then begin
  27.     FreeAndNil(fDM);
  28.   end;
  29. end;
  30. end.

Where and how is the User Session created?

Everytime a new user access your application, a User Session instance is created by the Server Controller class. This is done in the Server Controller method IWServerControllerBaseNewSession.This code is created automatically by the IntraWeb Wizard and in most of the cases you don’t need to change it.

  1. procedure TIWServerController.IWServerControllerBaseNewSession(
  2.   ASession: TIWApplication; var VMainForm: TIWBaseForm);
  3. begin
  4.   ASession.Data := TIWUserSession.Create(nil);
  5. end;

How to access the User Session instance?

The easiest way to access the User Session instance is using a function available in the Server Controller unit. You need to include the unit ServerController in the uses clause of the unit you want to access the User Session instance, as for example, your IWForms.

  1. function UserSession: TIWUserSession;
  2. begin
  3.   Result := TIWUserSession(WebApplication.Data);
  4. end;

How to use the User Session instance in your code?

After you have added the ServerController unit to your uses clause, you simply access the User Session instance using the UserSession function.

  1. unit Unit1;
  2. interface
  3. uses
  4.   Classes, SysUtils, IWAppForm, IWApplication, IWColor, IWTypes, Data.DB,
  5.   IWVCLBaseControl, IWBaseControl, IWBaseHTMLControl, IWControl, IWCompLabel,
  6.   IWDBStdCtrls, Vcl.Controls, Vcl.Forms, IWVCLBaseContainer, IWContainer,
  7.   IWHTMLContainer, IWHTML40Container, IWRegion, IWCompGrids, IWDBGrids,
  8.   IWHTMLControls, IWCompButton;
  9. type
  10.   TfrmCustomers = class(TIWAppForm)
  11.     dtsCustomer: TDataSource;
  12.     IWRegion1: TIWRegion;
  13.     IWDBLabel1: TIWDBLabel;
  14.     IWDBLabel2: TIWDBLabel;
  15.     IWDBLabel3: TIWDBLabel;
  16.     IWDBLabel4: TIWDBLabel;
  17.     GrOrderIDs: TIWDBGrid;
  18.     dtsCustomerOrders: TDataSource;
  19.     IWDBGrid1: TIWDBGrid;
  20.     dtsOrderDetail: TDataSource;
  21.     IWLink1: TIWLink;
  22.     IWButton1: TIWButton;
  23.     procedure IWAppFormCreate(Sender: TObject);
  24.     procedure IWAppFormDestroy(Sender: TObject);
  25.     procedure GrOrderIDsColumns0Click(ASender: TObject; const AValue: string);
  26.     procedure IWDBGrid1Columns0Click(ASender: TObject; const AValue: string);
  27.     procedure IWLink1Click(Sender: TObject);
  28.     procedure IWButton1Click(Sender: TObject);
  29.   private
  30.     fCustomerID: Integer;
  31.     procedure OpenOrderDetail(OrderID: Integer);
  32.   public
  33.   end;
  34. implementation
  35. {$R *.dfm}
  36. uses IWURLMap, ServerController, IWGlobal;
  37. procedure TfrmCustomers.OpenOrderDetail(OrderID: Integer);
  38. begin
  39.   UserSession.DM.OrderDetail.Close;
  40.   UserSession.DM.OrderDetail.Open(OrderID);
  41.   dtsOrderDetail.DataSet := UserSession.DM.OrderDetail.Dataset;
  42. end;
  43. procedure TfrmCustomers.GrOrderIDsColumns0Click(ASender: TObject; const AValue: string);
  44. begin
  45.   UserSession.DM.CustomerOrders.Dataset.Locate(‘OrderNo’, aValue, []);
  46.   OpenOrderDetail(UserSession.DM.CustomerOrders.Dataset.FieldByName(‘OrderNo’).AsInteger);
  47. end;
  48. procedure TfrmCustomers.IWAppFormCreate(Sender: TObject);
  49. begin
  50.   if WebApplication.RunParams.IndexOfName(‘CustomerID’) = -1 then begin
  51.     WebApplication.Terminate(‘You need to supply the CustomerID’);
  52.   end else begin
  53.     fCustomerID := StrToInt(WebApplication.RunParams.Values[‘CustomerID’]);
  54.    
  55.     UserSession.DM.Customers.Close;
  56.     UserSession.DM.Customers.Open(fCustomerID);
  57.     dtsCustomer.DataSet := UserSession.DM.Customers.Dataset;
  58.     // read Customer Order’s
  59.     UserSession.DM.CustomerOrders.Close;
  60.     UserSession.DM.CustomerOrders.Open(fCustomerID);
  61.     dtsCustomerOrders.DataSet := UserSession.DM.CustomerOrders.Dataset;
  62.    
  63.     OpenOrderDetail(UserSession.DM.CustomerOrders.Dataset.FieldByName(‘OrderNo’).AsInteger);
  64.   end;
  65. end;
  66. procedure TfrmCustomers.IWAppFormDestroy(Sender: TObject);
  67. begin
  68.   UserSession.DM.Customers.Close;
  69. end;
  70. procedure TfrmCustomers.IWDBGrid1Columns0Click(ASender: TObject;
  71.   const AValue: string);
  72. begin
  73.   UserSession.DM.OrderDetail.Dataset.Locate(‘ItemNo’, AValue, []);
  74. end;
  75. procedure TfrmCustomers.IWLink1Click(Sender: TObject);
  76. var
  77.   xReport: TStringList;
  78.   xNomeArquivo: string;
  79. begin
  80.   xReport := UserSession.DM.ReportCustomerOrders(fCustomerID);
  81.   try
  82.     xNomeArquivo := IWServerController.UserCacheDir + UserSession.DM.RandomName + ‘.txt’;
  83.     xReport.SaveToFile(xNomeArquivo);
  84.     WebApplication.SendFile(xNomeArquivo, True, ”, ‘Customer Report ‘ +
  85.       UserSession.DM.Customers.Dataset.FieldByName(‘CustNo’).AsString + ‘.txt’);
  86.   finally
  87.     FreeAndNil(xReport);
  88.   end;
  89. end;
  90. initialization
  91.   TIWURLMap.Add(‘/Customer/’, ‘index.html’, TfrmCustomers);
  92. end.

How the User Session instance is destroyed?

While your user is interacting with your application, his User Session instance is keept in memory. If the user (or the browser) does not interact with the application for a certain period of time (the default is 20 minutes, check the SessionTimeout property on the Server Controller class), ie, the User Session is idle, the IntraWeb Session Manager takes care of destroying the User Session instance.

You need to ensure all your objects are destroyed properly on the destroy method of your User Session class, otherwise you may have memory leaks in your application, which causes server degradation and consequently slower application performance, as gradually there will be less RAM memory availalble in your server.