Summary#
Code that is about an entity can live in the entity, next to its columns:
entity Job {
[Required, MaxLength(40)] string Title;
int Hours;
public const int HoursAtMost = 8; // a constant: no column, inlined where it is used
public static int Clamp(int hours) { // a static method: no row, called on the type
return hours > HoursAtMost ? HoursAtMost : hours; // inside the entity a member is named bare
}
security { allow read, create when IsAnonymous || IsAuthenticated; }
}
[AllowAnonymous] int Plan(int asked) {
return Job.Clamp(asked); // outside it is `Job.Clamp(…)`
}A member is the same function as a top-level one with an owner: the same stored row, the same refusals, the same
[Authorize] handling. Moving a function into its entity changes where it is written, never what it may reach.
Signature#
entity <Name> {
public const <Type> <Const> = <value>; // no column
public static <Return> <Method>(<params>) { … }
public <Return> <Method>(<params>) { … } // called on a row: `this` is the row
public static readonly <Type> <Name> = <value>; // immutable static, evaluated once
public static <Type> <Name> => <value>; // getter-only static property
public enum <Nested> { … } // nested: Job.Nested
public class <Nested> { … }
}Description#
Reached on the type name#
Job.Clamp(9), Job.HoursAtMost, Job.Stage.Open and new Job.Tag { … }. Inside the entity — in a static member, or in
its security { } — the member is named bare. Elsewhere a bare Clamp(9) is refused with BARE_MEMBER_OUTSIDE_TYPE:
"Clamp is a member of Job: write Job.Clamp(…) here."
Private unless you say so#
As on a class, a member is private by default. internal opens it to the package, public to everyone who imports the
namespace. An entity's fields take no visibility: who may read or write them is the security { } block.
A package's entity#
An app that uses a package, and the package's own tests app, call the public members of the package's entities exactly
as they call their own: Box.Plus(2, 5) or Acme.Shelf.Box.Plus(2, 5) for a static member, b.Doubled() on a row for an
instance member. A member keeps everything it has inside its package. A private or internal one is refused by name.
An instance member runs on the caller's row, read under its read rule; its writes are judged by the row's update rule and
the package's deny … except via. A member policy (Box.Large(Size)) may decide the app's own rules. A query verb stays
a query: Box.Count(…) on a type with no Count member is the query it always was, and so is Crate.Plus(…) when only
Box declares Plus.
Methods called on a row#
A method without static is called on a row: work.End(reason). Inside it, this is the row, and a field is read and
written as it is on any row you hold — Reason, this.Reason, Reason = reason — with the same rules: the read is the
caller's secured read, and a write is a write to that row, judged by its update rule when the unit of work commits. Inside
the entity another instance member is called bare (IsLong()). A row handed across a call is checked as an argument is:
called on a row the caller cannot read (one held from before a runas, say), the method is refused as not found before
its body runs, exactly as End(work, reason) would be.
entity Work {
[Required, MaxLength(200)] string Title;
[MaxLength(200)] string? Reason;
int Hours;
public const int LongAfter = 8;
public bool IsLong() => Hours > LongAfter; // `this` is the row: `Hours` is its field
public string End(string reason) {
Reason = reason; // a write to this row, under its update rule
return Title + (IsLong() ? " (long)" : ""); // another member of the row, named bare
}
security { allow read, create, update when IsAnonymous || IsAuthenticated; }
}
[AllowAnonymous] string Finish(string title) {
var w = new Work { Title = title, Hours = 9 };
UnitOfWork.Commit();
return w.End("done"); // called on the row
}- Its body is the one its type declares. A method of an entity is not dispatched, so
virtual,overrideandabstractare refused on it. - Called from a page, a
[Server]method runs on the row the page names, read again on the server through the row's read rule, as the type the method is declared on: a row the caller cannot read is "No such Work, or you cannot read it.", and the method never runs without its row. The same holds when it is published: an MCP tool takes the row as a requiredid([[config-mcp-server#member-tools]]), a REST endpoint names it in its path as{id}([[api-rest#row-member-endpoints]]), andosy run Work.End --this <id>names it on the command line ([[local-command-line-entry-point#the-function]]). A page that calls one directly by its function sends the row as the inputid. An external entity's instance member ([DataSource(…)]) is not published: its rows live in a data source. - It may not take the name of a field (
MEMBER_NAMES_A_FIELD), and a published one may not take a parameter calledid. - A page's
live varasks it as it asks a function.live var summary = bell.Summary();is answered on the server as the viewer, on the row the page holds, bound the same way, and asked again when data it read is written; so is a static member,live var light = Bell.Clamp(3);([[ui-reactivity#server-call]]).
Names a member cannot take#
- A query verb —
MEMBER_NAMES_A_QUERY_VERB.Job.Count(j => …)is a query on every entity, and a member of that name would replace it, soCount,Where,Any,First,Include,OrderBy,Take,Sum,Insert,Update,Deleteand the rest of the query verbs, plusTraverse,ChangesandIsOpen, are reserved on an entity. Name it for what it does:CountLive, notCount. - A field —
MEMBER_NAMES_A_FIELD. An entity with a fieldUntilcannot have a memberUntil.
One entity in several files: partial entity#
A big entity can keep its columns in one declaration and its code in parts, as in C#. A partial entity Job { … } over an
entity of your own package adds methods (static or called on a row), consts and nested types — merged into the entity before anything
resolves, so Job.Clamp(…) is one function whichever part wrote it:
entity Job {
[Required, MaxLength(40)] string Title;
int Hours;
security { allow read, create when IsAnonymous || IsAuthenticated; }
}
partial entity Job {
public const int HoursAtMost = 8;
public static int Clamp(int hours) { return hours > HoursAtMost ? HoursAtMost : hours; }
}
[AllowAnonymous] int Plan(int asked) { return Job.Clamp(asked); }- A field in a part is refused. Columns live in the entity's own declaration, so the table is read in one place.
- The same member in two places is refused, naming both:
PARTIAL_MEMBER_TWICE— "Clampis declared in Job.Rules.osy:12 and Job.osy:40." Overloads (different parameter types) are fine. - A part over another package's entity adds no member.
PARTIAL_OVER_OTHER_PACKAGE_MEMBER— behaviour on a type you do not own is an extension method in a static class of yours. (Apartial entityover another package's entity still adds optional columns andsecurity, see Extension members.)
What stays as it is#
Pages stay one per file, a workflow stays its own type, and a top-level function stays legal. An entity cannot nest an
entity (an entity is a table). partial class is the same idea for a class: see static methods, static readonly, static class and extension methods.
Actors and hooks#
An [Actor] class may nest in the entity whose work it does — [[security-declared-actor#nested]], with rename actor
and remove actor when it moves. [OnStart] and [On] go on a static member — [OnStart] — work the app does when it is loaded, [On] — hear an event, after the commit that raised it;
an instance member is refused there, since the platform calls a hook with no row.
Policies#
An entity (or a class) may declare the policies that are about it: public policy Stands(Guid work) => …;, named bare
in its own security { } and Job.Stands(id) elsewhere. A member policy is always static and compiles to the same SQL
as the top-level one — see [[security-naming-a-policy#member]].
See also#
staticmethods,static readonly,static classand extension methods — the same members on a class, andpartial class- [[ui-reactivity#server-call]] — a member as a page's live value
- [Actor] — a principal that code may run as — an actor nested in its entity
- [On] — hear an event, after the commit that raised it and [OnStart] — work the app does when it is loaded — hooks on static members
- project layout — one type per file, the
partialparts, the lock, andosy restructure --plan - Extension members — a
partial entityover another package's entity - entity — the entity itself
- type visibility (public / internal) —
public,internalandprivate - policy —
policy, and a policy as a member