Table of Contents

Typermedia in one process

An API needn't cross processes. Strictly-local tueries and tommands are executed in the process that declares their handlers, synchronously, on the caller's thread: nothing is serialized and no endpoint is needed. The LocalTessagingEngine of Tessaging in one process executes them.

Strictly-local tueries and tommands

An application's account settings, as a resource with two forms:

public class AccountSettingsLink : StrictlyLocal.Tueries.StrictlyLocalTuery<AccountSettingsLink, AccountSettings>;

public class AccountSettings(string displayName, bool receivesNewsletter)
{
   public string DisplayName { get; } = displayName;
   public bool ReceivesNewsletter { get; } = receivesNewsletter;

   public ChangeDisplayName ChangeDisplayName(string displayName) => new(displayName);
   public ToggleNewsletter ToggleNewsletter() => new();
}

//Returns nothing.
public class ChangeDisplayName(string displayName) : StrictlyLocal.Tommands.StrictlyLocalTommand
{
   public string DisplayName { get; } = displayName;
}

//Returns the settings as they are after the change.
public class ToggleNewsletter : StrictlyLocal.Tommands.StrictlyLocalTommand<AccountSettings>;

A strictly-local tuery names its own type as its first type argument. Strictly-local tueries and tommands never leave their process: the remote navigator doesn't take them. So they need no type identities, and their resources need not be serializable.

Declaring the handlers

Typermedia handlers are declared in the engine's composition block, under a verb of their own beside RegisterTessageBusHandlers:

IContainerBuilder containerBuilder = new MicrosoftContainerBuilder();
containerBuilder.Registrar
                .Register(Singleton.For<SettingsStore>().CreatedBy(() => new SettingsStore()))
                .LocalTessagingEngine(engine => engine
                   .RegisterTypermediaHandlers(handle => handle
                      .ForTuery((AccountSettingsLink _, SettingsStore store) => store.Current)
                      .ForTommand((ChangeDisplayName tommand, SettingsStore store) => store.ChangeDisplayName(tommand.DisplayName))
                      .ForTommand((ToggleNewsletter _, SettingsStore store) => store.ToggleNewsletter())));

await using var container = containerBuilder.Build();

A tuery handler takes up to four more parameters and a tommand handler one, resolved from the scope that executes it. In an endpoint, the same handlers are declared in its declaration's RegisterTueryHandlers and RegisterTypermediaTommandHandlers overrides.

Executing them

Two navigators execute them, as two publishers publish tevents. Which one depends on whether the code runs inside a unit of work.

Outside any unit of work, such as in an application's own classes, execute through IIndependentLocalTypermediaNavigator. It is a singleton: a class takes it as an ordinary constructor dependency. It executes each tommand as a unit of work of its own, committed when the call returns, and each tuery in a scope of its own, with no transaction:

var navigator = container.RootResolver.Resolve<IIndependentLocalTypermediaNavigator>();

var settings = navigator.Execute(new AccountSettingsLink());
navigator.Execute(settings.ChangeDisplayName("Ada"));
settings = navigator.Execute(settings.ToggleNewsletter());

It refuses to execute within an ambient transaction, where its execution would join that transaction instead of standing on its own.

Inside a unit of work, such as in a handler, execute through ILocalTypermediaNavigatorSession, resolved from the unit of work. A tommand executes within the caller's transaction, and commits or rolls back with it; executing one with no transaction fails. A tuery executes in the caller's scope, so what it reads is consistent with what the caller has written:

container.ScopeFactory.ExecuteUnitOfWork(unitOfWork =>
{
   var session = unitOfWork.Resolve<ILocalTypermediaNavigatorSession>();
   var settings = session.Execute(new AccountSettingsLink());
   session.Execute(settings.ChangeDisplayName("Ada Lovelace")); //Commits, or rolls back, with the unit of work.
});

A handler's exception reaches the caller as it was thrown, and the handler isn't run again.

Local or remote is the type's

A tuery or tommand is either strictly local or remotable, and each navigator takes only its own kind: the compiler refuses a remotable tuery on a local navigator, and a strictly-local one on the remote navigator. So moving an API out of its process changes its types, not only where it is deployed, and navigation specifications, which navigate remote navigators only, have no in-process counterpart.