Skip to content

main · v3.2 (unreleased)

Getting started

Users · Tutorial · CurrentMarkdownAgent index

Status: Current. Tutorial for users. It works in a clone of the platform’s repository, on its test domain. It does not set up an application of your own.

In this tutorial you run the platform on your own machine and write four small tests. They create objects, relate them, let a rule derive a value and query the result: the things that the code of an application does in the database. You need no database server.

The tutorial uses the test domain that the platform’s own tests use, with its classes Person and Organisation. The projects of an application of your own are a different subject, which Domains introduces. The sample application of the repository shows those projects for the database: a domain that extends Core, with two classes, a rule, its security, a command-line tool and their tests. Create an application starts an application from it.

You need Git and the .NET SDK, version 10.

If your shell sets adapter, the tests read that setting and can then run on a database server. Clear the variable for this tutorial.

Terminal window
git clone https://github.com/allors/allors3.git
cd allors3
export ALLORS_CONFIG_ROOT="$PWD/config/snapshot"
./build.sh Generate

In PowerShell on Windows the last two commands are $env:ALLORS_CONFIG_ROOT = "$PWD\config\snapshot" and .\build.cmd Generate. Make a full clone, not a shallow one: the build computes its version from the history.

ALLORS_CONFIG_ROOT names the configuration root, the folder that holds the settings of the build and of the tests. The build and the tests stop without it. config/snapshot is a root of the repository that needs no database server. Keep the variable set in the terminal that you use for the rest of the tutorial.

Generate writes the typed classes of the model, which are not in the repository. The clone does not compile until it has run. It ends with Build succeeded.

Terminal window
dotnet test dotnet/Core/Database/Domain.Tests --filter "FullyQualifiedName~Tests.DomainDerivationTest"

The first run builds the projects. The run ends with a test summary: one test, and none failed.

The test is one of the domain tests of Core and of its test domain. With the configuration root of step 1 they run on the Memory adapter, a database in memory, so nothing has to be installed or started.

Open dotnet/Core/Repository/Domain/Custom/Database/Special/Organisation.cs. It declares the class Organisation with its relations, among them:

DeclarationMeaning
string NameAn organization has a name.
Person[] Employees, OneToManyAn organization has many employees, and a person is an employee of one organization.
Person Owner, ManyToOneAn organization has one owner, and a person can own many organizations.

Person.cs, next to it, declares FirstName, LastName and DomainFullName. The last one is marked [Derived]: the tests of this tutorial never assign it, because a rule maintains it.

Generate wrote a class for each of them in dotnet/Core/Database/Domain/Generated. Those classes are what the tests use. The model describes the declarations.

Create the file dotnet/Core/Database/Domain.Tests/Domain/GettingStartedTests.cs:

namespace Allors.Database.Domain.Tests
{
using Xunit;
public class GettingStartedTests : DomainTest, IClassFixture<Fixture>
{
public GettingStartedTests(Fixture fixture) : base(fixture) { }
[Fact]
public void AnOrganisationHasEmployees()
{
var acme = new OrganisationBuilder(this.Transaction).WithName("Acme").Build();
var jane = new PersonBuilder(this.Transaction).WithFirstName("Jane").WithLastName("Roe").Build();
acme.AddEmployee(jane);
Assert.Contains(jane, acme.Employees);
Assert.Equal(acme, jane.OrganisationWhereEmployee);
}
}
}

Run it:

Terminal window
dotnet test dotnet/Core/Database/Domain.Tests --filter GettingStartedTests

The test summary reports one test, and none failed.

  • DomainTest gives every test a new database with the population of the test domain, and a transaction, this.Transaction. It rolls the transaction back when the test ends.
  • A builder creates an object in the transaction. Nothing in this test calls new Organisation.
  • AddEmployee writes the role Employees. The test never assigns jane.OrganisationWhereEmployee: that is the association, the same relation read from the other end, and the platform keeps it in step.

Add a second test to the class:

[Fact]
public void APersonHasOneEmployer()
{
var acme = new OrganisationBuilder(this.Transaction).WithName("Acme").Build();
var globex = new OrganisationBuilder(this.Transaction).WithName("Globex").Build();
var jane = new PersonBuilder(this.Transaction).WithFirstName("Jane").WithLastName("Roe").Build();
acme.AddEmployee(jane);
globex.AddEmployee(jane);
Assert.Equal(globex, jane.OrganisationWhereEmployee);
Assert.DoesNotContain(jane, acme.Employees);
}

Run the tests again; two pass. Employees is OneToMany, so a person is an employee of one organization. When Globex takes Jane on, the platform removes her from the employees of Acme. No code of the test did that. Objects and relations describes what each multiplicity maintains.

Add a third test:

[Fact]
public void TheFullNameIsDerived()
{
var jane = new PersonBuilder(this.Transaction).WithFirstName("Jane").WithLastName("Roe").Build();
Assert.False(jane.ExistDomainFullName);
this.Transaction.Derive();
Assert.Equal("Jane Roe", jane.DomainFullName);
jane.LastName = "Doe";
this.Transaction.Derive();
Assert.Equal("Jane Doe", jane.DomainFullName);
}

Run the tests again; three pass. The test never assigns DomainFullName. Derive() looks at what changed in the transaction and runs the rules that depend on it. One of them is PersonFullNameRule, in dotnet/Core/Database/Domain/Custom/Rules: it runs when the first name or the last name of a person changes, and writes the full name. Open the file; it is short. Derivations explains how a derivation works, and Write a rule how to add a rule.

Add a fourth test:

[Fact]
public void AnExtentFindsThePeopleWithoutAnEmployer()
{
var acme = new OrganisationBuilder(this.Transaction).WithName("Acme").Build();
var jane = new PersonBuilder(this.Transaction).WithFirstName("Jane").WithLastName("Roe").Build();
var john = new PersonBuilder(this.Transaction).WithFirstName("John").WithLastName("Roe").Build();
acme.AddEmployee(jane);
var people = this.Transaction.Extent<Person>();
people.Filter.AddEquals(this.M.Person.LastName, "Roe");
people.Filter.AddNot().AddExists(this.M.Person.OrganisationWhereEmployee);
Assert.Equal(new[] { john }, people.ToArray());
}

Run the tests again; four pass. An extent is the set of the objects of a type, narrowed by a filter: here the people with the last name Roe who are not an employee. this.M is the metadata of the model; this.M.Person.LastName names a role without a string. The population of the test domain holds other people as well, which is why the filter names a last name that only this test uses. Extents lists the predicates.

You ran the platform without a database server, and you used its four basic operations in the database: creating objects, writing a role and reading the association, deriving, and querying with an extent.

The file that you wrote is yours to experiment with. Delete it when you are done: it is not part of the platform.

Read on: