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.
- unit UserSessionUnit;
- interface
- uses
- IWUserSessionBase, SysUtils, Classes, DataModule;
- type
- TIWUserSession = class(TIWUserSessionBase)
- procedure IWUserSessionBaseDestroy(Sender: TObject);
- private
- FDM: TDM;
- function GetDM: TDM;
- public
- property DM: TDM read GetDM;
- end;
- implementation
- {$R *.dfm}
- { TIWUserSession }
- function TIWUserSession.GetDM: TDM;
- begin
- if not Assigned(fDM) then begin
- fDM := TDM.Create(Nil);
- end;
- Result := fDM;
- end;
- procedure TIWUserSession.IWUserSessionBaseDestroy(Sender: TObject);
- begin
- if Assigned(fDM) then begin
- FreeAndNil(fDM);
- end;
- end;
- 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.
- procedure TIWServerController.IWServerControllerBaseNewSession(
- ASession: TIWApplication; var VMainForm: TIWBaseForm);
- begin
- ASession.Data := TIWUserSession.Create(nil);
- 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.
- function UserSession: TIWUserSession;
- begin
- Result := TIWUserSession(WebApplication.Data);
- 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.
- unit Unit1;
- interface
- uses
- Classes, SysUtils, IWAppForm, IWApplication, IWColor, IWTypes, Data.DB,
- IWVCLBaseControl, IWBaseControl, IWBaseHTMLControl, IWControl, IWCompLabel,
- IWDBStdCtrls, Vcl.Controls, Vcl.Forms, IWVCLBaseContainer, IWContainer,
- IWHTMLContainer, IWHTML40Container, IWRegion, IWCompGrids, IWDBGrids,
- IWHTMLControls, IWCompButton;
- type
- TfrmCustomers = class(TIWAppForm)
- dtsCustomer: TDataSource;
- IWRegion1: TIWRegion;
- IWDBLabel1: TIWDBLabel;
- IWDBLabel2: TIWDBLabel;
- IWDBLabel3: TIWDBLabel;
- IWDBLabel4: TIWDBLabel;
- GrOrderIDs: TIWDBGrid;
- dtsCustomerOrders: TDataSource;
- IWDBGrid1: TIWDBGrid;
- dtsOrderDetail: TDataSource;
- IWLink1: TIWLink;
- IWButton1: TIWButton;
- procedure IWAppFormCreate(Sender: TObject);
- procedure IWAppFormDestroy(Sender: TObject);
- procedure GrOrderIDsColumns0Click(ASender: TObject; const AValue: string);
- procedure IWDBGrid1Columns0Click(ASender: TObject; const AValue: string);
- procedure IWLink1Click(Sender: TObject);
- procedure IWButton1Click(Sender: TObject);
- private
- fCustomerID: Integer;
- procedure OpenOrderDetail(OrderID: Integer);
- public
- end;
- implementation
- {$R *.dfm}
- uses IWURLMap, ServerController, IWGlobal;
- procedure TfrmCustomers.OpenOrderDetail(OrderID: Integer);
- begin
- UserSession.DM.OrderDetail.Close;
- UserSession.DM.OrderDetail.Open(OrderID);
- dtsOrderDetail.DataSet := UserSession.DM.OrderDetail.Dataset;
- end;
- procedure TfrmCustomers.GrOrderIDsColumns0Click(ASender: TObject; const AValue: string);
- begin
- UserSession.DM.CustomerOrders.Dataset.Locate(‘OrderNo’, aValue, []);
- OpenOrderDetail(UserSession.DM.CustomerOrders.Dataset.FieldByName(‘OrderNo’).AsInteger);
- end;
- procedure TfrmCustomers.IWAppFormCreate(Sender: TObject);
- begin
- if WebApplication.RunParams.IndexOfName(‘CustomerID’) = -1 then begin
- WebApplication.Terminate(‘You need to supply the CustomerID’);
- end else begin
- fCustomerID := StrToInt(WebApplication.RunParams.Values[‘CustomerID’]);
- UserSession.DM.Customers.Close;
- UserSession.DM.Customers.Open(fCustomerID);
- dtsCustomer.DataSet := UserSession.DM.Customers.Dataset;
- // read Customer Order’s
- UserSession.DM.CustomerOrders.Close;
- UserSession.DM.CustomerOrders.Open(fCustomerID);
- dtsCustomerOrders.DataSet := UserSession.DM.CustomerOrders.Dataset;
- OpenOrderDetail(UserSession.DM.CustomerOrders.Dataset.FieldByName(‘OrderNo’).AsInteger);
- end;
- end;
- procedure TfrmCustomers.IWAppFormDestroy(Sender: TObject);
- begin
- UserSession.DM.Customers.Close;
- end;
- procedure TfrmCustomers.IWDBGrid1Columns0Click(ASender: TObject;
- const AValue: string);
- begin
- UserSession.DM.OrderDetail.Dataset.Locate(‘ItemNo’, AValue, []);
- end;
- procedure TfrmCustomers.IWLink1Click(Sender: TObject);
- var
- xReport: TStringList;
- xNomeArquivo: string;
- begin
- xReport := UserSession.DM.ReportCustomerOrders(fCustomerID);
- try
- xNomeArquivo := IWServerController.UserCacheDir + UserSession.DM.RandomName + ‘.txt’;
- xReport.SaveToFile(xNomeArquivo);
- WebApplication.SendFile(xNomeArquivo, True, ”, ‘Customer Report ‘ +
- UserSession.DM.Customers.Dataset.FieldByName(‘CustNo’).AsString + ‘.txt’);
- finally
- FreeAndNil(xReport);
- end;
- end;
- initialization
- TIWURLMap.Add(‘/Customer/’, ‘index.html’, TfrmCustomers);
- 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.