# 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:

{% code title="MainModule.pas (type declaration)" %}

```pascal
type
  TUserInfo = record
    Name, Surname : string;
    UserId : Integer;
  end;
```

{% endcode %}

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

{% code title="MainModule.pas (module class)" %}

```pascal
TUniMainModule = class(TUniGUIMainModule)
private
  { Private declarations }
  FUserInfo: TUserInfo;
public
  { Public declarations }
  property UserInfo: TUserInfo read FUserInfo;
end;
```

{% endcode %}

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

{% code title="MainForm.pas (accessing UserInfo)" %}

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

{% endcode %}

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.

{% hint style="info" %}
Avoid using shared global objects in uniGUI. Use session-scoped modules (MainModule, DataModules) and their public properties for per-session shared data.
{% endhint %}

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

{% code title="MainModule.pas (bad example)" %}

```pascal
unit MainModule;

interface
...

var
  UserInfo: TUserInfo;
```

{% endcode %}

And then access it in MainForm:

{% code title="MainForm.pas (bad example)" %}

```pascal
procedure TMainForm.UniFormCreate(Sender: TObject);
begin
  UniLabel1.Caption := UserInfo.Name + ' ' + UserInfo.Surname;
end;
```

{% endcode %}

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.

{% hint style="warning" %}
Do not place per-session components (for example: database connections, per-user data) on ServerModule. ServerModule is a singleton shared across all sessions.
{% endhint %}

![Application ServerModule](/files/be225d3d236595d674ed19ca4be5cbf95ae5da0d)

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>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://unigui-doc.falconsistemas.com.br/unigui-manual-en-us/developers-guide/application-design-considerations/general-design-concept.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
