User Session Overview

<< Click to Display Table of Contents >>

Navigation:  Getting Started >

User Session Overview

Atozed link

 

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.

 

3.interface

 

4.

 

5.uses

 

6. IWUserSessionBase, SysUtils, Classes, DataModule;

 

7.

 

8.type

 

9. TIWUserSession = class(TIWUserSessionBase)

 

10. procedure IWUserSessionBaseDestroy(Sender: TObject);

 

11. private

 

12. FDM: TDM;

 

13. function GetDM: TDM;

 

14. public

 

15. property DM: TDM read GetDM;

 

16. end;

 

17.

 

18.implementation

 

19.

 

20.{$R *.dfm}

 

21.

 

22.{ TIWUserSession }

 

23.

 

24.function TIWUserSession.GetDM: TDM;

 

25.begin

 

26. if not Assigned(fDM) then begin

 

27. fDM := TDM.Create(Nil);

 

28. end;

 

29. Result := fDM;

 

30.end;

 

31.

 

32.procedure TIWUserSession.IWUserSessionBaseDestroy(Sender: TObject);

 

33.begin

 

34. if Assigned(fDM) then begin

 

35. FreeAndNil(fDM);

 

36. end;

 

37.end;

 

38.

 

39.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.

 

3.interface

 

4.

 

5.uses

 

6. Classes, SysUtils, IWAppForm, IWApplication, IWColor, IWTypes, Data.DB,

 

7. IWVCLBaseControl, IWBaseControl, IWBaseHTMLControl, IWControl, IWCompLabel,

 

8. IWDBStdCtrls, Vcl.Controls, Vcl.Forms, IWVCLBaseContainer, IWContainer,

 

9. IWHTMLContainer, IWHTML40Container, IWRegion, IWCompGrids, IWDBGrids,

 

10. IWHTMLControls, IWCompButton;

 

11.

 

12.type

 

13. TfrmCustomers = class(TIWAppForm)

 

14. dtsCustomer: TDataSource;

 

15. IWRegion1: TIWRegion;

 

16. IWDBLabel1: TIWDBLabel;

 

17. IWDBLabel2: TIWDBLabel;

 

18. IWDBLabel3: TIWDBLabel;

 

19. IWDBLabel4: TIWDBLabel;

 

20. GrOrderIDs: TIWDBGrid;

 

21. dtsCustomerOrders: TDataSource;

 

22. IWDBGrid1: TIWDBGrid;

 

23. dtsOrderDetail: TDataSource;

 

24. IWLink1: TIWLink;

 

25. IWButton1: TIWButton;

 

26. procedure IWAppFormCreate(Sender: TObject);

 

27. procedure IWAppFormDestroy(Sender: TObject);

 

28. procedure GrOrderIDsColumns0Click(ASender: TObject; const AValue: string);

 

29. procedure IWDBGrid1Columns0Click(ASender: TObject; const AValue: string);

 

30. procedure IWLink1Click(Sender: TObject);

 

31. procedure IWButton1Click(Sender: TObject);

 

32. private

 

33. fCustomerID: Integer;

 

34. procedure OpenOrderDetail(OrderID: Integer);

 

35. public

 

36. end;

 

37.

 

38.implementation

 

39.

 

40.{$R *.dfm}

 

41.

 

42.uses IWURLMap, ServerController, IWGlobal;

 

43.

 

44.procedure TfrmCustomers.OpenOrderDetail(OrderID: Integer);

 

45.begin

 

46. UserSession.DM.OrderDetail.Close;

 

47. UserSession.DM.OrderDetail.Open(OrderID);

 

48. dtsOrderDetail.DataSet := UserSession.DM.OrderDetail.Dataset;

 

49.end;

 

50.

 

51.procedure TfrmCustomers.GrOrderIDsColumns0Click(ASender: TObject; const AValue: string);

 

52.begin

 

53. UserSession.DM.CustomerOrders.Dataset.Locate('OrderNo', aValue, []);

 

54. OpenOrderDetail(UserSession.DM.CustomerOrders.Dataset.FieldByName('OrderNo').AsInteger);

 

55.end;

 

56.

 

57.procedure TfrmCustomers.IWAppFormCreate(Sender: TObject);

 

58.begin

 

59. if WebApplication.RunParams.IndexOfName('CustomerID') = -1 then begin

 

60. WebApplication.Terminate('You need to supply the CustomerID');

 

61. end else begin

 

62. fCustomerID := StrToInt(WebApplication.RunParams.Values['CustomerID']);

 

63. // read Customer data

 

64. UserSession.DM.Customers.Close;

 

65. UserSession.DM.Customers.Open(fCustomerID);

 

66. dtsCustomer.DataSet := UserSession.DM.Customers.Dataset;

 

67. // read Customer Order's

 

68. UserSession.DM.CustomerOrders.Close;

 

69. UserSession.DM.CustomerOrders.Open(fCustomerID);

 

70. dtsCustomerOrders.DataSet := UserSession.DM.CustomerOrders.Dataset;

 

71. // Open 1ts Customer Order

 

72. OpenOrderDetail(UserSession.DM.CustomerOrders.Dataset.FieldByName('OrderNo').AsInteger);

 

73. end;

 

74.end;

 

75.

 

76.procedure TfrmCustomers.IWAppFormDestroy(Sender: TObject);

 

77.begin

 

78. UserSession.DM.Customers.Close;

 

79.end;

 

80.

 

81.

 

82.procedure TfrmCustomers.IWDBGrid1Columns0Click(ASender: TObject;

 

83. const AValue: string);

 

84.begin

 

85. UserSession.DM.OrderDetail.Dataset.Locate('ItemNo', AValue, []);

 

86.end;

 

87.

 

88.procedure TfrmCustomers.IWLink1Click(Sender: TObject);

 

89.var

 

90. xReport: TStringList;

 

91. xNomeArquivo: string;

 

92.begin

 

93. xReport := UserSession.DM.ReportCustomerOrders(fCustomerID);

 

94. try

 

95. xNomeArquivo := IWServerController.UserCacheDir + UserSession.DM.RandomName + '.txt';

 

96. xReport.SaveToFile(xNomeArquivo);

 

97. WebApplication.SendFile(xNomeArquivo, True, '', 'Customer Report ' +

 

98. UserSession.DM.Customers.Dataset.FieldByName('CustNo').AsString + '.txt');

 

99. finally

 

100. FreeAndNil(xReport);

 

101. end;

 

102.end;

 

103.

 

104.initialization

 

105. TIWURLMap.Add('/Customer/', 'index.html', TfrmCustomers);

 

106.

 

107.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.