Table of Contents

Shared tentities

A tentity belongs to one taggregate. A shared tentity is to a tentity what a shared tomponent is to a tomponent: written once, with an id of its own, and kept in a collection by any number of different taggregates. It builds on shared tomponents, so read that page first: this one continues its example of products and their prices.

Reviews are an example. Every tevent of a review carries, as ordinary domain data, the id of the review it belongs to: its EntityId.

interface IReviewTevent : ISharedTentityTevent<ReviewId>
{
   interface IWritten : IReviewTevent
   {
      int Stars { get; }
   }

   interface IAnswered : IReviewTevent
   {
      string Answer { get; }
   }
}
abstract class ReviewTevent(ReviewId entityId) : IReviewTevent
{
   public ReviewId EntityId { get; } = entityId;

   internal class Written(ReviewId entityId, int stars) : ReviewTevent(entityId), IReviewTevent.IWritten
   {
      public int Stars { get; } = stars;
   }

   internal class Answered(ReviewId entityId, string answer) : ReviewTevent(entityId), IReviewTevent.IAnswered
   {
      public string Answer { get; } = answer;
   }
}
class Review : SharedTentity<Review, ReviewId, IReviewTevent, IReviewTevent.IWritten>
{
   static readonly TeventRouting<Review, IReviewTevent> Appliers = DeclareAppliers(it => it
     .For<IReviewTevent.IWritten>(static (review, written) => review.Stars = written.Stars)
     .For<IReviewTevent.IAnswered>(static (review, answered) => review.Answer = answered.Answer));

   protected override TeventRouting TeventAppliers => Appliers;

   public Review(SharedTentityCollection<Review, ReviewId, IReviewTevent, IReviewTevent.IWritten> reviews) : base(reviews) {}

   public int Stars { get; private set; }
   public string? Answer { get; private set; }

   public void AnswerWith(string answer) => Publish(new ReviewTevent.Answered(Id, answer));
}

ReviewId is a small value type of the example's own, around a Guid.

  • A review derives from SharedTentity<Review, ReviewId, IReviewTevent, IReviewTevent.IWritten>: its own class, its id, its tevent root, and the tevent that creates one.
  • Its routing starts with an applier of the base class's, which gives a new review the EntityId of its created tevent as its Id. The review's own applier of IWritten comes after it.
  • A review publishes tevents that carry its own Id, and Publish asserts that they do.
  • The owner keeps its reviews in a SharedTentityCollection, which is itself a shared tomponent: it takes one slot, and every review in it publishes through that slot's adopting wrapper tevent. The collection creates a review when a created tevent arrives, and routes every other tevent to the review whose id it carries. A review is added by publishing its created tevent, with AddByPublishing, which returns the new review.

Start to finish: a second owner

A bundle of products has a price too, and reviews. Here is all of it:

interface IBundleTevent : ITaggregateTevent
{
   interface ICreated : IBundleTevent, ITaggregateCreatedTevent
   {
      string Name { get; }
   }
}

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

interface IBundlePriceTevent<out T> : IBundleTevent, IPublisherTevent<T> where T : IPriceTevent;
interface IBundleReviewsTevent<out T> : IBundleTevent, IPublisherTevent<T> where T : IReviewTevent;

abstract class BundleTevent : TaggregateTevent, IBundleTevent
{
   protected BundleTevent() {}
   BundleTevent(TaggregateId taggregateId) : base(taggregateId) {}

   internal class Created(TaggregateId taggregateId, string name) : BundleTevent(taggregateId), IBundleTevent.ICreated
   {
      public string Name { get; } = name;
   }
}

class BundleTevent<T>(T tevent) : TaggregateTevent<T>(tevent), IBundleTevent<T> where T : IBundleTevent;

class BundlePriceTevent<T>(T tevent) : BundleTevent, IBundlePriceTevent<T> where T : IPriceTevent
{
   public T Tevent { get; } = tevent;
}

class BundleReviewsTevent<T>(T tevent) : BundleTevent, IBundleReviewsTevent<T> where T : IReviewTevent
{
   public T Tevent { get; } = tevent;
}

class Bundle : Taggregate<Bundle, IBundleTevent, BundleTevent, IBundleTevent<IBundleTevent>, BundleTevent<BundleTevent>>
{
   static class Slots
   {
      internal static readonly SharedTomponentSlot<Bundle, IBundleTevent, BundleTevent, IPriceTevent, IBundlePriceTevent<IPriceTevent>> Price =
         new(typeof(BundlePriceTevent<>), static bundle => bundle.Price);

      internal static readonly SharedTomponentSlot<Bundle, IBundleTevent, BundleTevent, IReviewTevent, IBundleReviewsTevent<IReviewTevent>> Reviews =
         new(typeof(BundleReviewsTevent<>), static bundle => bundle.Reviews);
   }

   static readonly TeventRouting<Bundle, IBundleTevent> Appliers = DeclareAppliers(it => it
     .For<IBundleTevent.ICreated>(static (bundle, created) => bundle.Name = created.Name)
     .SharedTomponent(Slots.Price)
     .SharedTomponent(Slots.Reviews));

   protected override TeventRouting TeventAppliers => Appliers;

   Bundle()
   {
      Price = new Price(Slots.Price.In(this));
      Reviews = new SharedTentityCollection<Review, ReviewId, IReviewTevent, IReviewTevent.IWritten>(Slots.Reviews.In(this));
   }

   public string Name { get; private set; } = "";
   public Price Price { get; }
   public SharedTentityCollection<Review, ReviewId, IReviewTevent, IReviewTevent.IWritten> Reviews { get; }

   public static Bundle Create(string name)
   {
      var bundle = new Bundle();
      bundle.Publish(new BundleTevent.Created(new TaggregateId(), name));
      return bundle;
   }

   public Review AddReview(int stars) => Reviews.AddByPublishing(new ReviewTevent.Written(ReviewId.New(), stars));
}

The price and the review are written once, each:

  1. Its tevent interfaces, rooted in an interface of its own that implements ITevent, or ISharedTentityTevent<TId> for a shared tentity, and the classes implementing them.
  2. Its class, deriving from SharedTomponent<> or SharedTentity<,,,>, with its tevent routing.

Each owner then writes, for each shared tomponent it holds:

  1. An adopting wrapper tevent: an interface that is a tevent of the owner and an IPublisherTevent<T> of the shared tomponent's tevents, and a class implementing it.
  2. A slot, in its nested Slots class, holding that class and how to get the shared tomponent from an owner.
  3. The slot, entered in its tevent routing: .SharedTomponent(Slots.Price).
  4. The shared tomponent, constructed with the slot in this owner: new Price(Slots.Price.In(this)).