---
title: "Create an application"
description: "Start an application from the sample, generate and test its projects, sign in to the server, and make the domain your own."
audience: "Users"
kind: "How-to guide"
status: "Current"
version: "main · v3.2 (unreleased)"
slug: "create-an-application"
url: "https://docs.allors.com/allors3/create-an-application/"
---

# Create an application

> **Status: Current.** [Where this is implemented](#where-this-is-implemented) names the code and the build targets.

Use this guide to start an application in a repository of its own. It assumes [Domains](https://docs.allors.com/allors3/domains.md), which explains how an application extends Core and includes its source.

The starting point is the [sample application](https://github.com/allors/allors3/tree/main/sample) in the platform’s repository. It includes the model, the database layers, a command-line tool, a server, a .NET workspace and their tests. The platform’s CI generates, builds and tests the sample for every pull request that changes more than the documentation, so the sample follows the platform.

To copy, generate and test the application, you need Git, the .NET 10 SDK and a POSIX shell with Perl. The optional sign-in walkthrough uses PowerShell and SQL Server LocalDB on Windows.

<a id="1-create-the-repository-and-add-the-platform"></a>

## 1\. Create the repository and add the platform

An application compiles the source of the platform, so it keeps a copy of the platform’s repository next to its own projects. A Git submodule holds that copy:

Terminal window

```sh
mkdir my-app
cd my-app
git init
git submodule add https://github.com/allors/allors3.git allors
```

The build computes the version of the platform’s projects from the history of that copy, and stops in a shallow one. Give CI the full history as well: with GitHub’s checkout action, set `fetch-depth: 0` next to `submodules: true`. Issue [#409](https://github.com/allors/allors3/issues/409) records the details.

<a id="2-copy-the-samples-projects"></a>

## 2\. Copy the sample’s projects

Terminal window

```sh
cp -R allors/sample/Repository allors/sample/Database allors/sample/Workspace .
```

| Project | What it holds |
| --- | --- |
| `Repository/Domain` | The [model](https://docs.allors.com/allors3/model.md): Core’s, which the project includes, and the application’s own, in `Custom`. |
| `Database/Meta` | The metadata, which a generator writes from the model. |
| `Database/Resources` | The messages of the domain, merged from Core’s. |
| `Database/Domain` | The domain: Core’s code, the calls to the hooks in `Virtual`, the application’s code in `Custom`, and the list of rules in `Rules.cs`. |
| `Database/Configuration` | The services of the database. |
| `Database/Generate` | The generator of the classes of the domain and of the workspace. |
| `Database/Commands` | The [command-line tool](https://docs.allors.com/allors3/command-line-tool.md). |
| `Database/Server` | The HTTP server that hosts Core’s sign-in endpoint and JSON API for the application’s domain. |
| `Database/Domain.Tests`, `Database/Commands.Tests` and `Database/Server.Tests` | The tests of the domain, the tool and the HTTP API. |
| `Workspace/Meta` and `Workspace/Meta.Lazy` | The metadata of the [workspace](https://docs.allors.com/allors3/workspace.md) `Default`, which the generator writes from the model. |
| `Workspace/Domain` | The typed classes of the workspace, which the generator writes, and Core’s code for the workspace. |
| `Workspace/Configuration` | The services of the workspace and of its sessions. |
| `Workspace/Tests` | The tests of the workspace, on the Local and the Remote adapter. |

<a id="3-point-the-projects-at-the-submodule"></a>

## 3\. Point the projects at the submodule

In the platform’s repository the projects of the sample reach the platform’s source with paths that start with `..\..\..\dotnet\`. Next to the submodule those paths start with `..\..\allors\dotnet\`. The generator in `Database/Generate` also names its templates with paths that start with `../dotnet/`, relative to the folder from which it runs. These commands change both:

Terminal window

```sh
grep -rl --include='*.csproj' '\.\.\\\.\.\\\.\.\\dotnet\\' Repository Database Workspace | xargs perl -pi -e 's/\.\.\\\.\.\\\.\.\\dotnet\\/..\\..\\allors\\dotnet\\/g'
perl -pi -e 's#"\.\./dotnet/#"allors/dotnet/#' Database/Generate/Program.cs
```

The projects then include Core with lines such as this one, from `Database/Domain/Domain.csproj`:

```xml
<Compile Include="..\..\allors\dotnet\Core\Database\Domain\Core*\**\*.cs" />
```

<a id="4-leave-the-builds-output-out-of-git"></a>

## 4\. Leave the build’s output out of Git

The build and the generators write to folders named `bin`, `obj` and `Generated`, which the platform’s repository ignores. Ignore them in the application’s repository too:

Terminal window

```sh
printf '[Bb]in/\n[Oo]bj/\n[Gg]enerated/\n' > .gitignore
```

<a id="5-generate"></a>

## 5\. Generate

[Generated code](https://docs.allors.com/allors3/domains.md#generated-code) is not committed. Three tools write it; run them from the root of the application’s repository:

Terminal window

```sh
mkdir -p Database/Resources/Generated
dotnet run --no-launch-profile --project allors/dotnet/Core/Database/Merge -- allors/dotnet/Core/Database/Resources/Core Database/Resources/Generated
dotnet run --no-launch-profile --project allors/dotnet/System/Repository/Generate -- Repository/Domain/Repository.csproj allors/dotnet/System/Repository/Templates/meta.cs.stg Database/Meta/Generated
dotnet run --project Database/Generate
```

`--no-launch-profile` keeps `dotnet run` from reading the launch settings that the two projects of the platform hold for their own development.

1.  **Merge** writes Core’s messages to `Database/Resources/Generated`. The folder has to exist: without it, Merge exits with code 1 and prints nothing. An application with messages of its own passes their folder as well: every argument but the last is a folder to read.
2.  **The repository generator** reads the model and writes the metadata to `Database/Meta/Generated`.
3.  **The application’s generator** writes the classes of the domain to `Database/Domain/Generated`, and the metadata and the typed classes of the workspace `Default` to `Workspace/Meta/Generated`, `Workspace/Meta.Lazy/Generated` and `Workspace/Domain/Generated`.

Generate again after every change to the model. In the platform’s repository the targets `SampleMerge` and `SampleGenerate` of the build run the same tools on the sample.

<a id="6-build-and-test"></a>

## 6\. Build and test

The command-line tool and the server read their settings from one file below the [configuration root](https://docs.allors.com/allors3/configuration.md#the-configuration-root), and stop without it; so do the tests that run them. Create a configuration root for development in the application’s repository, on the Snapshot adapter, which needs no database server, and point `ALLORS_CONFIG_ROOT` at it:

Terminal window

```sh
mkdir -p config/custom
printf '{\n  "adapter": "snapshot"\n}\n' > config/custom/appsettings.json
export ALLORS_CONFIG_ROOT="$PWD/config"
```

`custom` is the name that the sample’s tool and server pass to `AddAllorsConfiguration`. Then build and run the tests:

Terminal window

```sh
dotnet test Database/Domain.Tests
dotnet test Database/Commands.Tests
dotnet test Database/Server.Tests
dotnet test Workspace/Tests
```

The domain tests use the Memory adapter, unless `ALLORS_TEST_ADAPTER` names another: [Test on PostgreSQL or SQL Server](https://docs.allors.com/allors3/test-on-postgresql-or-sql-server.md) runs them on a database server. The command-line and server tests use Snapshot. The server tests start the application in the test process and send HTTP requests to it. The workspace tests run each test on the Local and on the Remote adapter, with the database on Snapshot and, for the Remote adapter, the server in the test process. By default, all four suites run without a database server.

<a id="run-the-server-and-sign-in"></a>

### Run the server and sign in

The tool that creates the user and the server that signs the user in must share a database. This example uses PowerShell 7.2 or later and SQL Server LocalDB on Windows. Run it from the application root after generating the model. For SQL Server or PostgreSQL elsewhere, set the adapter, application connection and admin connection for that server as described in [Configuration](https://docs.allors.com/allors3/configuration.md#admin-connections); [Host a server](https://docs.allors.com/allors3/host-a-server.md) gives the steps from bash.

Choose a fresh database for this walkthrough. `init` drops and recreates the named database, and `populate` replaces its contents. The following commands name the configuration root of this step for the terminal, override its adapter and connection with environment variables, choose a new database name, populate the sample’s user groups and create a member of `Workers`. Enter a password when the tool prompts.

Terminal window

```powershell
$env:ALLORS_CONFIG_ROOT = "$PWD\config"
$database = "allors_sample_" + [guid]::NewGuid().ToString("N")
$instance = 'Server=(localdb)\MSSQLLocalDB;Integrated Security=true;TrustServerCertificate=true'
$env:adapter = 'sqlclient'
$env:ALLORS_SQLCLIENT = "$instance;Database=master"
$env:ConnectionStrings__DefaultConnection = "$instance;Database=$database"


dotnet run --project Database/Commands -- init
dotnet run --project Database/Commands -- populate
dotnet run --project Database/Commands -- user --name worker@example.com --group Workers
```

In the same terminal, generate a signing key for this local run and start the server. Leave it running while you use the second terminal.

Terminal window

```powershell
$env:JwtToken__Key = [Convert]::ToHexString([Security.Cryptography.RandomNumberGenerator]::GetBytes(32))
dotnet run --no-launch-profile --project Database/Server -- --urls http://localhost:5080
```

Open a second PowerShell terminal. Sign in with the password you entered for the worker, then use the returned bearer token to sync the worker’s own `Person`. The sign-in response holds the id of the user in `u` and the token in `t`. The sync request takes a list of numeric object ids.

Terminal window

```powershell
$api = 'http://localhost:5080/allors'
$credentials = @{
    l = 'worker@example.com'
    p = Read-Host 'Worker password' -MaskInput
}
$login = Invoke-RestMethod -Method Post -Uri "$api/Authentication/Token" `
    -ContentType 'application/json' -Body ($credentials | ConvertTo-Json)
if (-not $login.a) { throw 'Sign-in failed. Check the worker name and password.' }


$headers = @{ Authorization = "Bearer $($login.t)" }
$body = @{ o = @([long]$login.u) } | ConvertTo-Json
$sync = Invoke-RestMethod -Method Post -Uri "$api/sync" -Headers $headers `
    -ContentType 'application/json' -Body $body
$sync.o | Select-Object -ExpandProperty i
```

The printed id should match `$login.u`. A missing or invalid bearer token on `/allors/sync` returns HTTP 401. Stop the server with Ctrl+C in its terminal. To accept another host name, include it in the server’s [`AllowedHosts` setting](https://docs.allors.com/allors3/configuration.md#settings).

<a id="7-make-the-domain-your-own"></a>

## 7\. Make the domain your own

Change the sample into the application one part at a time, and generate, build and test after each change:

-   **The domain.** Give the domain an id of its own: the `[Domain]` attribute of the struct `Custom`, in `Repository/Domain/Custom/Custom.cs`, holds the sample’s.
-   **The model.** Replace `WorkItem`, and the roles that the sample adds to `Person`, with the classes of the application, each with ids of its own; [The model](https://docs.allors.com/allors3/model.md) describes the declarations. The users of an application are objects of a class that implements Core’s interface `User`, as `Person` does.
-   **The rules.** `Database/Domain/Rules.cs` lists Core’s six rules and the application’s own; [Write a rule](https://docs.allors.com/allors3/write-a-rule.md) shows how to add one.
-   **The setup and the security.** The folder `Database/Domain/Custom` holds the sample’s setup, security and upgrade. [Grant access](https://docs.allors.com/allors3/grant-access.md) and [Upgrade a population](https://docs.allors.com/allors3/upgrade-a-population.md) explain them.
-   **The workspace.** `[Workspace]` on the declarations of the model decides which classes, roles and methods a client can use; [Use a workspace](https://docs.allors.com/allors3/use-a-workspace.md) shows how to connect, pull and push.
-   **The tests.** Replace the tests of the sample with the application’s.

<a id="files-that-follow-the-platform"></a>

## Files that follow the platform

Core’s code needs classes and members that the platform keeps outside its folders `Core`, most of them in the folders of its test domain, so an application does not include them. The sample has its own versions of them, and an application keeps these:

| Files | What the application does with them |
| --- | --- |
| `Database/DatabaseServices.cs`, `Database/DefaultDatabaseServices.cs`, `Database/PreparedFetches/PreparedSelects.cs` and `Transaction/TransactionServices.cs`, below `Database/Configuration/Custom` | Keeps them as the platform has them. |
| The three files in `Database/Domain/Virtual`, and `Database/Domain/Custom/ObjectsBase.cs` | Keeps them as the platform has them while its domain extends Core directly. With domains in between, the files in `Virtual` call the part of each domain, as [Domains](https://docs.allors.com/allors3/domains.md#setup-and-security) explains. |
| `Database/PreparedExtents/PreparedExtents.cs` and `Database/WorkspaceMask/WorkspaceMask.cs`, below `Database/Configuration/Custom` | Adds entries of its own; the sample’s are empty. |
| `Database/Meta/Custom/MetaBuilder.cs` | Keeps the method `BuildCustom`, which the generated metadata calls, and changes the metadata of its domain there when it needs to. |
| `Database/Domain/Rules.cs` | Lists Core’s six rules next to its own. |
| `Database/Generate/Program.cs` | Holds the paths of the templates and of their output, for the domain and for the workspace, relative to the folder from which the generator runs; step 3 points the templates at the submodule. |
| `Workspace/WorkspaceServices.cs` and `Session/SessionServices.cs`, below `Workspace/Configuration/Custom` | Keeps them as the platform has them. |

Issue [#408](https://github.com/allors/allors3/issues/408) records why an application needs these files.

<a id="update-the-platform"></a>

## Update the platform

Move the submodule to the newest commit of the platform, then generate, build and test again:

Terminal window

```sh
git submodule update --remote allors
```

The sample changes together with the platform, so compare the files that the application keeps as the platform has them with the sample’s, for example:

Terminal window

```sh
git diff --no-index allors/sample/Database/Domain/Virtual Database/Domain/Virtual
```

Commit the new commit of the submodule together with the changes that it asks for.

<a id="where-this-is-implemented"></a>

## Where this is implemented

-   The sample application is in `sample/`. The targets `SampleMerge` and `SampleGenerate` of the Nuke build, in `build/Sample/Build.cs`, run the platform’s tools on it, and `SampleTest` runs its tests.
-   Merge is in `dotnet/Core/Database/Merge`, the repository generator in `dotnet/System/Repository/Generate`, with its template `dotnet/System/Repository/Templates/meta.cs.stg`, and the generator of Core’s domain, which the sample’s generator includes, in `dotnet/Core/Database/Generate`, with the template `dotnet/Core/Database/Templates/domain.cs.stg`. The sample’s generator also renders the templates `meta.cs.stg`, `meta.lazy.cs.stg` and `domain.cs.stg` of `dotnet/Core/Workspace/Templates`.
-   The projects of the workspace include `dotnet/Core/Workspace/Domain/Core*` and `dotnet/Core/Workspace/Configuration/Core*`. `SampleTest` runs the tests of `sample/Workspace/Tests` as well.
