Table of Contents

Tomponents

A tomponent is a component of a taggregate: a part of it, with state of its own, that is teventive like the taggregate. It changes its state only by applying the tevents it publishes, and those tevents are the taggregate's: they are part of its tevent hierarchy, carry its id, and reach subscribers as the taggregate's tevents.

A tomponent belongs to one taggregate. One written once, to be used in many different taggregates, is a shared tomponent.

This page gives the User of Taggregates a profile, which holds the user's name.

Its tevents

A tomponent's tevents are rooted in an interface nested in the taggregate's root tevent interface, and inheriting it: IUserTevent.IProfile, as the tevent naming page has it.

interface IUserTevent : ITaggregateTevent
{
   interface IPropertyUpdated : IUserTevent
   {
      interface IEmail : IPropertyUpdated
      {
         string Email { get; }
      }
   }

   interface IRegistered : IUserTevent, ITaggregateCreatedTevent, IPropertyUpdated.IEmail;

   interface IProfile : IUserTevent
   {
      interface IPropertyUpdated : IProfile
      {
         interface IName : IPropertyUpdated
         {
            string Name { get; }
         }
      }

      interface IRenamed : IProfile, IPropertyUpdated.IName;
   }
}

interface IUserTevent<out T> : ITaggregateTevent<T> where T : IUserTevent;

The tevent classes mirror the interfaces, nested the same way:

abstract class UserTevent : TaggregateTevent, IUserTevent
{
   protected UserTevent() {}
   UserTevent(TaggregateId userId) : base(userId) {}

   internal class Registered(TaggregateId userId, string email) : UserTevent(userId), IUserTevent.IRegistered
   {
      public string Email { get; } = email;
   }

   internal abstract class Profile : UserTevent, IUserTevent.IProfile
   {
      internal class Renamed(string name) : Profile, IUserTevent.IProfile.IRenamed
      {
         public string Name { get; } = name;
      }
   }
}

class UserTevent<T>(T tevent) : TaggregateTevent<T>(tevent), IUserTevent<T> where T : IUserTevent;

The tomponent

class Profile : User.Component<Profile, IUserTevent.IProfile, UserTevent.Profile>
{
   static readonly TeventRouting<Profile, IUserTevent.IProfile> Appliers = DeclareAppliers(it => it
     .For<IUserTevent.IProfile.IPropertyUpdated.IName>(static (profile, updated) => profile.Name = updated.Name));

   protected override TeventRouting TeventAppliers => Appliers;

   public Profile(User user) : base(user) {}

   public string Name { get; private set; } = "";

   public void Rename(string name) => Publish(new UserTevent.Profile.Renamed(name));
}
  • It derives from User.Component<...>, a base class every taggregate class has: its three type arguments are the tomponent's own class, its root tevent interface and the class implementing that.
  • Its constructor takes the user it belongs to.
  • It declares its tevent routing once, for the class, with DeclareAppliers, as a taggregate does.
  • It publishes through the user. Rename publishes a profile tevent, which the user publishes as its own: it gets the user's id, counts as one of the user's changes, and is wrapped in the user's wrapper tevent.

The taggregate that holds it

class User : Taggregate<User, IUserTevent, UserTevent, IUserTevent<IUserTevent>, UserTevent<UserTevent>>
{
   static readonly TeventRouting<User, IUserTevent> Appliers = DeclareAppliers(it => it
     .For<IUserTevent.IPropertyUpdated.IEmail>(static (user, updated) => user.Email = updated.Email)
     .Component(static user => user.Profile));

   protected override TeventRouting TeventAppliers => Appliers;

   User() => Profile = new Profile(this);

   public string Email { get; private set; } = "";
   public Profile Profile { get; }

   public static User Register(string email)
   {
      var user = new User();
      user.Publish(new UserTevent.Registered(new TaggregateId(), email));
      return user;
   }
}

The user constructs its profile with itself, and routes the profile's tevents to it with Component: every tevent assignable to the profile's root tevent interface reaches the profile, which applies it through its own routing.

Using it

var user = User.Register("ada@example.com");
user.Profile.Rename("Ada Lovelace");
//user.Profile.Name is now "Ada Lovelace", and user.Version is 2: the profile's tevent is the user's.

A tomponent can hold tomponents and tentities of its own, declared and routed the same way.