General Design Concept

Each uniGUI application is created with a MainForm, a MainModule, and a ServerModule. As described before, MainModule and MainForm are instantiated per session, while ServerModule is a singleton. In addition to MainModule, you can add extra DataModules to your application. A LoginForm can also be added if needed.

MainModule is automatically created with your application and it performs many important tasks. MainModule is the best place to place shared resources for a session. These resources are shared among a session's forms and DataModules (or units called by them). A database connection is a good example for shared resources. We can consider each session as a separate copy of the web application and, like a regular application, you need a separate database connection for each application instance. In uniGUI this can be achieved by placing the connection component on a DataModule, so that a connection will be created when a new session is started and destroyed when the session is terminated. This guarantees that each session will use its private copy of the database connection.

Another example of shared resources are shared data structures. For instance, each session may need to keep a shared record which holds information about the current user. All other forms and DataModules in the application should be able to access this structure when needed. Consider the following data structure defined in unit MainModule.pas:

MainModule.pas (type declaration)
type
  TUserInfo = record
    Name, Surname : string;
    UserId : Integer;
  end;

This structure needs to be accessible from other forms in the session, so we must define it as a public property in TUniMainModule:

MainModule.pas (module class)
TUniMainModule = class(TUniGUIMainModule)
private
  { Private declarations }
  FUserInfo: TUserInfo;
public
  { Public declarations }
  property UserInfo: TUserInfo read FUserInfo;
end;

The structure above will be populated with real user data when the user logs in. MainForm can access UserInfo like this:

MainForm.pas (accessing UserInfo)
procedure TMainForm.UniFormCreate(Sender: TObject);
begin
  UniLabel1.Caption :=
    UniMainModule.UserInfo.Name +
    ' ' +
    UniMainModule.UserInfo.Surname;
end;

You can see that it can be easily done by accessing the property UserInfo of the modified UniMainModule object. Since each session has its private copy of UniMainModule, each form will access the correct instance of the UserInfo record.

circle-info

Avoid using shared global objects in uniGUI. Use session-scoped modules (MainModule, DataModules) and their public properties for per-session shared data.

For example, one might define TUserInfo as a global variable in MainModule:

And then access it in MainForm:

Although the above code may appear to work when only a single session is active, it will break under multiple concurrent sessions: the global variable will be shared across sessions, producing incorrect behavior that can be hard to diagnose.

Such design flaws may remain undetected when only a few sessions are active, but with increased load, side effects of the initial design flaw will become more visible and it may not be easy to spot the source of the problem. Because of this, it is important to stick with a correct design pattern from the beginning.

Likewise, putting components on ServerModule must be avoided. As described before, ServerModule is a singleton and it will not be created for each session, so for example, putting a database Connection component on ServerModule may work when there are few sessions, but with more sessions you will get unexpected errors which are difficult to detect and debug.

circle-exclamation
Application ServerModule

Application ServerModule

Links in this document:

  • MainForm: https://unigui.com/doc/online_help/main_form.htm

  • MainModule: https://unigui.com/doc/online_help/main_module.htm

  • ServerModule: https://unigui.com/doc/online_help/server_module.htm

  • DataModules: https://unigui.com/doc/online_help/data_modules.htm

  • LoginForm: https://unigui.com/doc/online_help/login-form.htm