Status: Current. Where this is implemented names the code and the tests.
The platform owns the objects of an application and the relations between them. Code never
allocates an object of the model with new, and never keeps a relation in a field of its own: it
asks the platform, which stores the change and keeps the other end of the relation in step. This
page describes that behaviour for code that runs in the database, such as rules, methods and the
setup of a population. The model lists what the declarations behind it can say, and
The workspace explains how a client works with the objects.
The examples use Person and Organisation of the platform’s test domain, and a transaction
of its database. Getting started shows how to run such code.
A transaction holds the work
Section titled “A transaction holds the work”Every read and every change happens in a transaction, an ITransaction that the database creates
with CreateTransaction(). A transaction sees its own changes at once.
Commit()makes the changes permanent andRollback()discards them. After either one the same transaction continues: the objects that it holds stay usable, and an object that a rolled-back transaction created is deleted.- Disposing a transaction rolls back what it did not commit.
- A commit stores the changes as they are. It runs no rules: call
Derive()first, as Derivations explains. - A transaction does not see the uncommitted work of another one. The Snapshot adapter takes its view of the database when the transaction is created and after each commit or rollback. The SqlClient and Npgsql adapters take theirs at the transaction’s first database operation after creation, commit or rollback; it can include changes committed by another transaction in the meantime. Once the view is taken, later commits by other transactions remain invisible until this transaction commits or rolls back.
- On those adapters a commit fails with a
DbExceptionwhen another transaction committed a change to an object that this transaction changed as well. The transaction then stores nothing more until it is rolled back. Roll back and do the work again. Where the adapters differ names other concurrent transactions, which end otherwise on each adapter. - The Memory adapter has one transaction:
CreateTransaction()returns the same one untilInit()or a load replaces it.
Objects
Section titled “Objects”An object is an instance of a class of the model. Its class is fixed when it is created, and the
database gives it an Id, a positive number.
For every class the generator writes a typed class, a builder and an objects class, here
Organisation, OrganisationBuilder and Organisations. The objects class carries the plural
name of the class and gives access to all its objects; an interface has one as well, such as
Users for User. A builder is the usual way to create an object:
var acme = new OrganisationBuilder(transaction).WithName("Acme").Build();var jane = new PersonBuilder(transaction).WithFirstName("Jane").WithLastName("Doe").Build();Build() creates the object, gives it the values of the builder and then calls the methods
OnBuild and OnPostBuild of the object. The part of OnPostBuild that Core has for every
object gives a default value to every required unit role that has none:
false for a boolean, zero for a number, the current time for a date and a new GUID for a unique.
A required string or binary gets no default.
transaction.Create<Organisation>() creates an object without calling those methods, so its
required roles stay empty. transaction.Build<Organisation>(v => v.Name = "Acme") does call
them, like a builder.
transaction.Instantiate(id) returns the object with that id, or null when the database has
none. Two instances that stand for the same object are equal.
Roles and associations
Section titled “Roles and associations”A relation has two ends. The role is the end that code writes, on the
object that declares the relation. The association is the same relation read from the other end.
These are the relations between Organisation and Person that the examples use:
Role, on Organisation | Association, on Person | Multiplicity |
|---|---|---|
Employees | OrganisationWhereEmployee | OneToMany |
Manager | OrganisationWhereManager | OneToOne |
Owner | OrganisationsWhereOwner | ManyToOne |
Shareholders | OrganisationsWhereShareholder | ManyToMany |
Writing a role changes the association in the same statement:
acme.AddEmployee(jane);
var employer = jane.OrganisationWhereEmployee; // acmeAn association has no setter. To change it, write the role at the other end.
The generated members
Section titled “The generated members”| Declaration | Members of the typed class |
|---|---|
A unit role, Name | Name, ExistName, RemoveName() |
A role that holds one object, Manager | Manager, ExistManager, RemoveManager() |
A role that holds several objects, Employees | Employees, AddEmployee(…), RemoveEmployee(…), ExistEmployees, RemoveEmployees() |
| An association to one object | OrganisationWhereManager, ExistOrganisationWhereManager |
| An association to several objects | OrganisationsWhereOwner, ExistOrganisationsWhereOwner |
A method, JustDoIt | JustDoIt() |
Each typed class also has Meta, the metadata of its class, and M, the metadata of the whole
model. M.Organisation.Name is the role type of Name: the value that extents, rules and
prefetch policies take to name a relation. The typed members call the Strategy of the object,
which reads and writes any role by its role type.
Unit roles
Section titled “Unit roles”A unit role holds a value or nothing. ExistName says which; a role without a value reads as
null, and assigning null removes the value, as RemoveName() does. An empty string is a
value.
The typed property of a number, a boolean, a date or a unique is nullable, such as int? for
Person.Age, unless the role is required. The property of a required role is not nullable, and
reading it while the role is empty throws: Build() gives such a role its default, and
Exist… says whether it has a value.
A value has to fit the size, precision and scale of its role. Setting a
string or a binary that is longer than the size, or a decimal with more digits or more decimals
than the role allows, throws an ArgumentException; a decimal is not rounded.
A date is kept in UTC, to the millisecond. A local DateTime is converted, and one of the kind
Unspecified is refused, apart from DateTime.MinValue and DateTime.MaxValue.
What a multiplicity maintains
Section titled “What a multiplicity maintains”The multiplicity of a relation is a promise that the platform keeps on every write.
| Multiplicity | Statement | What else changes |
|---|---|---|
OneToMany | globex.AddEmployee(jane) | Jane leaves the employees of the organization that had her. |
OneToOne | globex.Manager = jane | Jane stops being the manager of the organization that had her, and the manager that globex had manages nothing. |
ManyToOne | globex.Owner = jane | Nothing: Jane owns one organization more. The owner that globex had loses it. |
ManyToMany | globex.AddShareholder(jane) | Nothing. |
acme.AddEmployee(jane);globex.AddEmployee(jane);
var employer = jane.OrganisationWhereEmployee; // globexvar left = acme.ExistEmployees; // falseA role that holds several objects holds each of them once: adding an object again changes nothing.
Deleting
Section titled “Deleting”A class that implements Core’s interface Deletable has the method Delete(). It runs the parts
that the domains wrote for the method and then deletes the object. A domain uses such a part to
delete what must not outlive the object. The test domain’s Cascader deletes its Cascaded this
way; its part is shortened here to that statement:
public partial class Cascader{ public void CustomDelete(DeletableDelete method) => this.Cascaded?.Delete();}Strategy.Delete() deletes any object and runs no parts.
When an object is deleted, the platform removes it from every role and every association, so no
other object refers to it. Strategy.IsDeleted is then true, and reading a role or an
association of the deleted object throws; for one that holds several objects, it throws when the
result is enumerated. A rollback brings the object and its relations back.
Change sets
Section titled “Change sets”A transaction records what changes. Checkpoint() returns the record since the previous
checkpoint as a change set, an IChangeSet, and starts a new one.
| Member | Holds |
|---|---|
Created | The objects that were created. |
Deleted | The strategies of the objects that were deleted. |
Associations | The objects of which a role changed, without the deleted ones. |
Roles | The objects for which an association changed, without the deleted ones. |
RoleTypesByAssociation, AssociationTypesByRole | For each of those objects, the role types or the association types that changed. |
AssociationsByRoleType, RolesByAssociationType | The same changes, by role type and by association type. |
For roles and associations a change set holds differences, not statements. Assigning the value
that a role already has is no change, and neither is a change that is undone before the
checkpoint. A binary role is the exception on the Memory adapter and on the SQL adapters, which
compare the array and not its bytes: assigning another array with the same bytes is a change
there. An object that is created and deleted between two checkpoints is in both Created and
Deleted. A commit and a rollback start a new change set as well.
Derivations are built on change sets: each cycle of a derivation takes a checkpoint and matches the changes against the rules.
Where this is implemented
Section titled “Where this is implemented”IDatabase,ITransaction,IObject,IStrategyandIChangeSetare indotnet/System/Database/Allors.Database. The adapters underdotnet/System/Database/Adaptersimplement the database, the transaction, the strategy and the change set; the typed classes implementIObject.RoleTypeExtensions, inAllors.Database.Adaptersamong the adapters, checks the values of unit roles.- A transaction takes its view of the database in two places.
TransactionofAllors.Database.Adapters.Snapshotkeeps the snapshot that is current when it is created, commits or rolls back.ConnectionofAllors.Database.Adapters.Sql.SqlClientand ofAllors.Database.Adapters.Sql.Npgsqlopens its connection and the snapshot transaction of the server for its first command, and closes both on a commit and on a rollback. - The template
dotnet/Core/Database/Templates/domain.cs.stgwrites the typed classes, the builders and the objects classes.ObjectBuilder, indotnet/Core/Database/Domain/Core, andITransactionExtensions, in its folderExtensions, holdBuild;ObjectExtensions, in its folderCommon, holds the defaults ofOnPostBuild; andDeletableDelete, next to it, holdsDelete. - The shared adapter tests, in
dotnet/System/Database/Adapters/Tests.Static/Static, run on every database adapter.One2OneTest,One2ManyTest,Many2OneTestandMany2ManyTestcheck what a multiplicity maintains,UnitTestthe unit roles, their limits and the dates,LifeCycleTestcreation, deletion, commit and rollback,ChangesTestthe change sets, andConcurrencyTestwhat transactions see of each other and the commit that fails. In that test the reader reads before the writer commits, so it does not check when each adapter takes the view. The Memory adapter takes no part inConcurrencyTest. RequiredTestandBuilderTest, indotnet/Core/Database/Domain.Tests, check the defaults ofOnPostBuildand the builders.DeletingTest, next to them, runsDelete()on aCascader; it does not assert that theCascadedis deleted.