Table of Contents

Taggregate Inheritance

A taggregate class can inherit another, as any class can. Here Cat and Dog inherit Animal, and the tevents of Animal are theirs too: a cat's birth and a dog's birth are both an IAnimalTevent.IBorn.

interface IAnimalTevent : ITaggregateTevent
{
   interface IBorn : IAnimalTevent, ITaggregateCreatedTevent;
}

Telling the publisher apart

A subscriber that cares only about dogs' births can't tell them apart by the birth tevent: there is one IBorn, which every animal publishes. So every taggregate publishes each tevent inside a wrapper tevent of its class: a generic interface that says which class of taggregate published the tevent, and whose type argument is the tevent itself.

interface IAnimalTevent<out T> : ITaggregateTevent<T> where T : IAnimalTevent;
interface ICatTevent<out T> : IAnimalTevent<T> where T : IAnimalTevent;
interface IDogTevent<out T> : IAnimalTevent<T> where T : IAnimalTevent;

A dog's birth is published as an IDogTevent<IAnimalTevent.IBorn>, and a cat's as an ICatTevent<IAnimalTevent.IBorn>. Both wrappers inherit IAnimalTevent<T>, and their type parameter is covariant, out T, so each is also an IAnimalTevent<IAnimalTevent.IBorn>, just as an IEnumerable<string> is an IEnumerable<object>:

IEnumerable<object> objects = [new(), new()];
IEnumerable<string> strings = ["1", "2"];
objects = strings;

Since tevents are routed by type compatibility, each subscriber receives exactly what its type asks for. The births of dogs only:

registrar
  .ForTevent<IDogTevent<IAnimalTevent.IBorn>>(born => WriteLine($"Dog Id:{born.Tevent.TaggregateId} was born!"));

Of cats only:

registrar
  .ForTevent<ICatTevent<IAnimalTevent.IBorn>>(born => WriteLine($"Cat Id:{born.Tevent.TaggregateId} was born!"));

Of every animal, whichever kind published it:

registrar
  .ForTevent<IAnimalTevent.IBorn>(born => WriteLine($"Animal Id:{born.TaggregateId} was born!"));

Of every animal, handling each kind differently:

registrar
  .ForTevent<IAnimalTevent<IAnimalTevent.IBorn>>(born => WriteLine(born switch
   {
      ICatTevent<IAnimalTevent.IBorn> => $"A cat was born: {born.Tevent.TaggregateId}",
      IDogTevent<IAnimalTevent.IBorn> => $"A dog was born: {born.Tevent.TaggregateId}",
      _                               => $"Another animal was born: {born.Tevent.TaggregateId}"
   }));

The taggregates

Each kind of animal publishes its tevents in a wrapper of its own. The tevent classes mirror the interfaces: a class for each tevent, and a generic class for each wrapper.

abstract class AnimalTevent : TaggregateTevent, IAnimalTevent
{
   protected AnimalTevent() {}
   AnimalTevent(TaggregateId taggregateId) : base(taggregateId) {}

   internal class Born(TaggregateId taggregateId) : AnimalTevent(taggregateId), IAnimalTevent.IBorn;
}

class AnimalTevent<T>(T tevent) : TaggregateTevent<T>(tevent), IAnimalTevent<T> where T : IAnimalTevent;
class CatTevent<T>(T tevent) : AnimalTevent<T>(tevent), ICatTevent<T> where T : IAnimalTevent;
class DogTevent<T>(T tevent) : AnimalTevent<T>(tevent), IDogTevent<T> where T : IAnimalTevent;

Animal is a taggregate like any other, written as on the Taggregates page. Its tevent routing has the applier that records when an animal was born.

abstract class Animal :
   Taggregate<Animal, IAnimalTevent, AnimalTevent, IAnimalTevent<IAnimalTevent>, AnimalTevent<AnimalTevent>>
{
   protected static readonly TeventRouting<Animal, IAnimalTevent> AnimalAppliers = DeclareAppliers(it => it
     .For<IAnimalTevent.IBorn>(static (animal, born) => animal.TimeOfBirth = born.UtcTimeStamp));

   public DateTime TimeOfBirth { get; private set; }

   protected void PublishBirth() => Publish(new AnimalTevent.Born(new TaggregateId()));
}

Cat and Dog each extend Animal's routing, so Animal's applier applies their births too, and each names its own wrapper class. That is all it takes for a cat's birth to be published as an ICatTevent<IAnimalTevent.IBorn>, and a dog's as an IDogTevent<IAnimalTevent.IBorn>:

class Cat : Animal
{
   static readonly TeventRouting<Cat, IAnimalTevent> CatAppliers = AnimalAppliers.Extend<Cat>(_ => {});
   protected override TeventRouting TeventAppliers => CatAppliers;

   protected override Type WrapperTeventImplementation => typeof(CatTevent<>);

   Cat() {}

   public static Cat RegisterBirth()
   {
      var cat = new Cat();
      cat.PublishBirth();
      return cat;
   }
}

class Dog : Animal
{
   static readonly TeventRouting<Dog, IAnimalTevent> DogAppliers = AnimalAppliers.Extend<Dog>(_ => {});
   protected override TeventRouting TeventAppliers => DogAppliers;

   protected override Type WrapperTeventImplementation => typeof(DogTevent<>);

   Dog() {}

   public static Dog RegisterBirth()
   {
      var dog = new Dog();
      dog.PublishBirth();
      return dog;
   }
}

A kind of animal with tevents of its own declares their appliers in the lambda it hands Extend. Animal's appliers apply each tevent first.