Osy#betaRun it on Osyrin: osyrin.com

Reference / Entity

Members of an entity — methods, consts, nested types

entity <Name> { public const <Type> <Const> = <value>; public static <Return> <Method>(<params>) { … } public <Return> <Method>(<params>) { … } // called on a row public static readonly <Type> <Name> = <value>; public class|enum <Nested> { … } }

An entity may declare the code that belongs to it beside its columns: static methods, methods called on a row (`work.End(reason)`, with `this` the row), `const`s, immutable statics and nested classes and enums. Statics are reached on the entity's name — `Job.Clamp(h)`, `Job.HoursAtMost`, `Job.Stage.Open` — and every member is stored and served exactly as the same function written at the top level. They add no column. A member may not take the name of a query verb (`Count`, `Where`, …) or of a field.

stable3 examples compiled by CIentitymembersstaticconst

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, override and abstract are 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 required id ([[config-mcp-server#member-tools]]), a REST endpoint names it in its path as {id} ([[api-rest#row-member-endpoints]]), and osy 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 input id. 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 called id.
  • A page's live var asks 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, so Count, Where, Any, First, Include, OrderBy, Take, Sum, Insert, Update, Delete and the rest of the query verbs, plus Traverse, Changes and IsOpen, are reserved on an entity. Name it for what it does: CountLive, not Count.
  • A field — MEMBER_NAMES_A_FIELD. An entity with a field Until cannot have a member Until.

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 — "Clamp is 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. (A partial entity over another package's entity still adds optional columns and security, 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#

Related

entity

Declares a persisted type — a table of rows the app stores, queries and secures. Every entity gets an Id from the…

entity members

The typed members an entity holds — text, numbers, dates, booleans, Guids, enums and references. A member's type…

`static` methods, `static readonly`, `static class` and extension methods

A `static` member belongs to the type rather than to any instance and is reached on the type name — `Money.Round(x)`…

class methods

Behaviour attached to a class — a method with a receiver, called as value.Method(). Classes are in-memory values, so a…

type visibility (public / internal)

A top-level declaration carries a public or internal visibility that decides whether code outside its package can name…

[Actor] — a principal that code may run as

A class marked `[Actor]` declares a principal that code may run as: a robot, a nightly job, an integration, a digital…

[On] — hear an event, after the commit that raised it

`[On]` on a function makes it a HANDLER of the event its one parameter is. Every handler of an event — in the app and…

[OnStart] — work the app does when it is loaded

`[OnStart]` marks a function the platform runs, as the app, whenever the app version is loaded — on every node that…

project layout

The shape of an Osy# project — a manifest at the root, and folders for model, seed, migrations and tests. What you put…

Querying data

How you read data in Osy#. You write C# LINQ; it becomes one SQL statement. The rules that follow from that are the…

Functions (the unit of work)

A function is where your app's logic lives — a top-level unit of work, written like a C# method, that runs on the…