Skip to content

main · v3.2 (unreleased)

Create an application

Users · How-to guide · CurrentMarkdownAgent index

Status: Current. 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, which explains how an application extends Core and includes its source.

The starting point is the sample application 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.

1. Create the repository and add the platform

Section titled “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
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 records the details.

Terminal window
cp -R allors/sample/Repository allors/sample/Database allors/sample/Workspace .
ProjectWhat it holds
Repository/DomainThe model: Core’s, which the project includes, and the application’s own, in Custom.
Database/MetaThe metadata, which a generator writes from the model.
Database/ResourcesThe messages of the domain, merged from Core’s.
Database/DomainThe 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/ConfigurationThe services of the database.
Database/GenerateThe generator of the classes of the domain and of the workspace.
Database/CommandsThe command-line tool.
Database/ServerThe 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.TestsThe tests of the domain, the tool and the HTTP API.
Workspace/Meta and Workspace/Meta.LazyThe metadata of the workspace Default, which the generator writes from the model.
Workspace/DomainThe typed classes of the workspace, which the generator writes, and Core’s code for the workspace.
Workspace/ConfigurationThe services of the workspace and of its sessions.
Workspace/TestsThe tests of the workspace, on the Local and the Remote adapter.

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

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

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
printf '[Bb]in/\n[Oo]bj/\n[Gg]enerated/\n' > .gitignore

Generated code is not committed. Three tools write it; run them from the root of the application’s repository:

Terminal window
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.

The command-line tool and the server read their settings from one file below 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
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
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 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.

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; Host a server 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
$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
$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
$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.

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 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 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 and Upgrade a population explain them.
  • The workspace. [Workspace] on the declarations of the model decides which classes, roles and methods a client can use; Use a workspace shows how to connect, pull and push.
  • The tests. Replace the tests of the sample with the application’s.

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:

FilesWhat the application does with them
Database/DatabaseServices.cs, Database/DefaultDatabaseServices.cs, Database/PreparedFetches/PreparedSelects.cs and Transaction/TransactionServices.cs, below Database/Configuration/CustomKeeps them as the platform has them.
The three files in Database/Domain/Virtual, and Database/Domain/Custom/ObjectsBase.csKeeps 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 explains.
Database/PreparedExtents/PreparedExtents.cs and Database/WorkspaceMask/WorkspaceMask.cs, below Database/Configuration/CustomAdds entries of its own; the sample’s are empty.
Database/Meta/Custom/MetaBuilder.csKeeps the method BuildCustom, which the generated metadata calls, and changes the metadata of its domain there when it needs to.
Database/Domain/Rules.csLists Core’s six rules next to its own.
Database/Generate/Program.csHolds 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/CustomKeeps them as the platform has them.

Issue #408 records why an application needs these files.

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

Terminal window
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
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.

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