Osy#betaRun it on Osyrin: osyrin.com

Reference / Class

Writing a kit with classes

class JobRules : Subject<Job> { public override List<Default> Defaults() => [ … Test(j => j.Stage, Is, …) … ]; }

The shapes a reusable kit is written in — a generic base an app subclasses, a virtual `Defaults()` the subclass overrides, a `Test<V>(Func<T, V> field, …)` helper over a selector, a `Func` member with a default, and `f is TField` over the app's own types. Each answers what C# answers, on the server and in the browser alike.

preview4 examples compiled by CIclasstypesauthoringkits

Summary#

A kit hands the app a generic base; the app closes it over its own entity and overrides what it needs:

enum Op { Is, IsNot }
enum Stage { New, Ready }
entity Job { Stage Stage = Stage.New; }

class Spec { public Op Op; public string Value = ""; }
class Default { public string Key = ""; public List<Spec> If = []; }

class SubjectBase { public virtual List<Default> Defaults() { return []; } }
class Subject<T> : SubjectBase {
  public Spec Test<V>(Func<T, V> field, Op op, V value) { return new Spec { Op = op, Value = $"{value}" }; }
  public Op Is => Op.Is;
}

class JobRules : Subject<Job> {
  public override List<Default> Defaults() {
    return [new Default { Key = "rush", If = [Test(j => j.Stage, Is, Stage.Ready)] }];
  }
}

string FirstRule() {
  var d = new JobRules().Defaults().First();
  return $"{d.Key}:{d.If.First().Op}:{d.If.First().Value}";   // "rush:Is:Ready"
}

JobRules inherits Test<V> from Subject<T>, with T bound to Job, so j => j.Stage is checked against Job and V is inferred as Stage. Called from a page's action, the same code runs in the browser.

Signature#

class Subject<T> : SubjectBase { public R Test<V>(Func<T, V> field, …, V value) { … } }   // a selector helper
class AppRules : Subject<AppEntity> { public override … Defaults() { … Test(x => x.Field, …) … } }
[AppSetting("Key")] class Setup { public Func<int, string> Format = n => $"{n} kr"; }  // a Func member with a default
class Holder<TField> where TField : BaseEntity { public bool Holds(BaseEntity f) => f is TField; }

Description#

An inherited method is the base's method#

A call to a method a base class declares runs the base's body, whatever class the call is written in. The same is true of a generic base: Test(…) inside JobRules is Subject<Job>.Test. A page action that builds new JobRules().Defaults() evaluates the call in the browser, just as a server function does.

A Func member's default is a function#

A Func<…> field may have an initializer, as in C#. The lambda is typed by the field, so its parameters need no types of their own, and every new instance starts with it:

[AppSetting("Fmt")]
public class FmtSetup {
  public Func<int, string> Price = n => $"{n} kr";
  public int Tax = 25;
}

string Shown() {
  // The app has never set Fmt: the setting answers its declared defaults, the Func member included.
  return App.Setting<FmtSetup>().Price(5);       // "5 kr"
}

This works the same way on a plain class (new Plain().Price(5)). An initializer can be a lambda or any other expression of the field's type. The interpolated string inside it keeps its formats and conditionals ($"{n:D3} {(n > 1 ? "items" : "item")}").

f is TField over the app's own types#

A kit that is generic over "which kind of row" tests a value against its type parameter. TField is whatever the app bound it to, and the test reads the value's real type — for an entity, the type the row was created as:

entity Field { [MaxLength(60)] string Name = ""; }
entity GuestField : Field { }
entity RoomField : Field { }

class Holder<TField> where TField : Field {
  public bool Holds(Field f) => f is TField;
}

bool Recognises() {
  var holder = new Holder<GuestField>();
  return holder.Holds(new GuestField()) && !holder.Holds(new RoomField());   // true
}

A row made on the page and not yet saved already has its type. new GuestField() in an action is a GuestField before the commit, in the browser as on the server.

Examples#

enum Stage { New, Ready }
entity Job { Stage Stage = Stage.New; security { allow read when IsAnonymous || IsAuthenticated; } }

class Subject<T> {
  public string Test<V>(Func<T, V> field, V value) { return $"{value}"; }
}
class JobRules : Subject<Job> {
  public string Ready() { return Test(j => j.Stage, Stage.Ready); }
}

[Route("/rules")] [AllowAnonymous]
component RulesPage() {
  string shown = "";
  action Go() { shown = new JobRules().Ready(); }     // runs in the browser: "Ready"
  render { Text(shown); Button("go", onPress: Go); }
}

Errors#

MessageCauseFix
cannot infer 'V'Neither an argument nor the selector's body says what V is.Pass a value of the field's type, or write the type: Test<Stage>(j => j.Stage, …).
a lambda needs a target type to bind its parameter fromA lambda was written where nothing declares its type — a var, or a member typed object.Declare the member as the Func<…> it holds.
'X' and 'Y' are not in the same hierarchy, so no row can be bothf is TField where the constraint's base and f's type are unrelated.Constrain TField to the base f is declared as (where TField : Field).

See also#

Related

Classes

A class is an in-memory shape — data plus the behaviour that belongs to it — and it never touches the database. That is…

Generic classes

A class can declare type parameters, so one shape serves every type it is used with instead of being copied per entity…

Class inheritance

A class can derive from another class with `class Circle : Shape`, inheriting its fields and its methods to any depth…

Testing which class a value is

`s is Circle` asks which type a value actually is at run time, answering by the value's own type rather than the type…

App.Implementations<T>()

One new instance of every class in the app's composition — its own and every package's — that implements the interface…

class properties

A class member that reads and writes like a field but runs a body on access — a computed value, a validating setter, or…

class methods

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