Table of Contents

Tessage types: the type is the contract

Every tessage is an instance of a .NET type, and that type says everything about it: its kind, who may receive it, whether it may leave its process, and what its delivery guarantees. The interfaces it implements, all in the Compze.Tessaging.TessageTypes namespace, are how it says so.

Tevents: interfaces in a hierarchy

A tevent says that something happened. Declare tevents as interfaces that inherit each other, each adding the meaning that sets it apart:

public interface IAccountTevent : ITevent
{
   Guid AccountId { get; }
}

public interface IAccountRegistered : IAccountTevent
{
   string Email { get; }
}

public interface IAccountImported : IAccountRegistered
{
   string ImportedFrom { get; }
}

public interface IAccountEmailChanged : IAccountTevent
{
   string Email { get; }
}

Classes implement them. Only the code that publishes a tevent knows its class:

public class AccountRegistered(Guid accountId, string email) : IAccountRegistered
{
   public Guid AccountId { get; private set; } = accountId;
   public string Email { get; private set; } = email;
}

public class AccountImported(Guid accountId, string email, string importedFrom) : IAccountImported
{
   public Guid AccountId { get; private set; } = accountId;
   public string Email { get; private set; } = email;
   public string ImportedFrom { get; private set; } = importedFrom;
}

public class AccountEmailChanged(Guid accountId, string email) : IAccountEmailChanged
{
   public Guid AccountId { get; private set; } = accountId;
   public string Email { get; private set; } = email;
}

Routing by type

A tevent is delivered to every handler whose subscribed type it is compatible with: the rule is .NET's own type compatibility, so routing is polymorphism. Given these subscriptions:

.ForTevent((IAccountTevent tevent) => Console.WriteLine($"Every account tevent: {tevent.GetType().Name}"))
.ForTevent((IAccountRegistered registered) => Console.WriteLine($"Registrations, imports included: {registered.GetType().Name}"))
.ForTevent((IAccountImported imported) => Console.WriteLine($"Imports only: {imported.GetType().Name}"))
Published IAccountTevent handler IAccountRegistered handler IAccountImported handler
AccountRegistered ✔ ✔
AccountImported ✔ ✔ ✔
AccountEmailChanged ✔

That is what makes a hierarchy of tevents worth designing: a subscriber says precisely what it cares about, and keeps receiving it whatever is added later. A new kind of registration, IAccountRegisteredBySupport : IAccountRegistered, reaches the IAccountRegistered subscribers with no change to them. Routing configuration, and type checks in handlers, have nothing left to do.

Tip

Subscribe to interfaces, never to tevent classes. A subscriber to a class misses every tevent of the classes added later, and the classes are the publisher's implementation detail to refactor as it likes.

Designing a hierarchy of tevents, for an entity that publishes them, is what Teventive is about.

What a tevent's type promises about its delivery

Three interfaces declare how far a tevent travels and what its delivery guarantees. The publisher and the subscribers choose none of it:

//Never leaves the process that publishes it.
public interface IAccountCacheInvalidated : ITevent;

//May cross to other endpoints, best-effort: no store, no deduplication, no retries.
public interface IAccountsOnlineCountChanged : IRemotableTevent
{
   int AccountsOnline { get; }
}

//Crosses to other endpoints exactly once: stored, deduplicated, its handling retried when it fails.
public interface IAccountClosed : IExactlyOnceTevent
{
   Guid AccountId { get; }
}

public class AccountClosed(Guid accountId) : IAccountClosed
{
   public TessageId Id { get; private set; } = new(); //What the receiving endpoint deduplicates on.
   public Guid AccountId { get; private set; } = accountId;
}
The tevent implements It travels Its delivery
ITevent Within the process that publishes it. Its handlers there run within the publisher's transaction.
IRemotableTevent Also to the endpoints that subscribe to it. Best-effort: no store, no deduplication, no retries.
IExactlyOnceTevent Also to the endpoints that subscribe to it. Exactly once: stored in the publisher's transaction, deduplicated by its Id, its handling retried when it fails.

Delivery guarantees says what each guarantee means in full.

Tommands: exactly one handler

A tommand tells its one handler to do something. The tommand of the TessageBus is the exactly-once tommand: it is delivered to its handler exactly once, wherever the handler is, and the sender doesn't wait for it to be handled. The Remotable.ExactlyOnce.Tommand base class gives a tommand the Id its deduplication needs:

public class CloseAccount(Guid accountId) : Remotable.ExactlyOnce.Tommand
{
   public Guid AccountId { get; private set; } = accountId;
}

It is sent through IUnitOfWorkTommandSender or IIndependentTommandSender, which exactly-once endpoints provide: see Endpoints.

Typermedia has tommands of its own, whose callers wait for them to be handled, and may get a result: strictly-local tommands, executed in-process, and at-most-once typermedia tommands, executed by another endpoint: see Typermedia.

Tueries: a question with one answer

A tuery asks its one handler for an answer, and changes nothing. Its type declares the type of its result:

public class AccountDetails(Guid accountId, string email)
{
   public Guid AccountId { get; private set; } = accountId;
   public string Email { get; private set; } = email;
}

public class AccountDetailsTuery(Guid accountId) : Remotable.NonTransactional.Tueries.Tuery<AccountDetails>
{
   public Guid AccountId { get; private set; } = accountId;
}

Tueries are Typermedia's: see Typermedia.

One handler, and declarations that fail loud

A tommand type and a tuery type have exactly one handler each: declaring a second handler for one fails when it is declared. A tevent type has any number.

The framework also refuses a type that contradicts itself, such as one that is both a tommand and a tevent, or both remotable and strictly local. It checks a type the first time the type is sent or subscribed to. To check a whole vocabulary at once, from a test, point TessageTypeDesignRules at the assemblies that declare it:

TessageTypeDesignRules.AssertFulfilledByAllTessageTypesIn(typeof(IAccountTevent).Assembly);

It reports every violating type in one failure.