# Members of an entity — methods, consts, nested types

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

<!-- id: entity-members · area: entity · stability: stable · html: https://osysharp.com/reference/entity/members/ -->

## Summary        {#summary}
Code that is about an entity can live in the entity, next to its columns:

```osy title="an entity with its own constant and helper" test app=entity-members
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      {#signature}
```osy syntax
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    {#description}

### Reached on the type name   {#reach}
`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   {#visibility}
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   {#package}
An app that `use`s 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   {#instance}
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.

```osy title="a method called on a row" test app=entity-members-instance
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   {#names}
- **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`   {#parts}
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), `const`s and nested types — merged into the entity before anything
resolves, so `Job.Clamp(…)` is one function whichever part wrote it:

```osy title="a part holding an entity's code" test app=entity-members-parts
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](https://osysharp.com/reference/entity/extension-members/).)

### What stays as it is   {#not}
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](https://osysharp.com/reference/class/static/).

### Actors and hooks   {#actors-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](https://osysharp.com/reference/function/on-start/), [[On] — hear an event, after the commit that raised it](https://osysharp.com/reference/function/on/);
an instance member is refused there, since the platform calls a hook with no row.

### Policies   {#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       {#see-also}
- [`static` methods, `static readonly`, `static class` and extension methods](https://osysharp.com/reference/class/static/) — the same members on a class, and `partial class`
- [[ui-reactivity#server-call]] — a member as a page's live value
- [[Actor] — a principal that code may run as](https://osysharp.com/reference/security/declared-actor/) — an actor nested in its entity
- [[On] — hear an event, after the commit that raised it](https://osysharp.com/reference/function/on/) and [[OnStart] — work the app does when it is loaded](https://osysharp.com/reference/function/on-start/) — hooks on static members
- [project layout](https://osysharp.com/reference/project/layout/) — one type per file, the `partial` parts, the lock, and `osy restructure --plan`
- [Extension members](https://osysharp.com/reference/entity/extension-members/) — a `partial entity` over another package's entity
- [entity](https://osysharp.com/reference/entity/declaration/) — the entity itself
- [type visibility (public / internal)](https://osysharp.com/reference/types/visibility/) — `public`, `internal` and `private`
- [policy](https://osysharp.com/reference/security/naming-a-policy/) — `policy`, and a policy as a member
