Taggregates
A taggregate is an aggregate whose state changes only by applying the tevents it publishes. It never sets its own state directly: it publishes a tevent that says what happened, and that tevent's applier changes the state. Every tevent it publishes carries its id.
This page writes one: a User, which publishes user tevents like those of the previous pages.
Its tevents
A taggregate's tevents are rooted in an interface of its own, which inherits ITaggregateTevent, the interface every
taggregate's tevents implement. They are nested as on the tevent naming page:
interface IUserTevent : ITaggregateTevent
{
interface IPropertyUpdated : IUserTevent
{
interface IEmail : IPropertyUpdated
{
string Email { get; }
}
}
interface IRegistered : IUserTevent, ITaggregateCreatedTevent, IPropertyUpdated.IEmail;
interface IChangedEmail : IUserTevent, IPropertyUpdated.IEmail;
}
IRegistered is also an ITaggregateCreatedTevent: the tevent that creates a user, and gives it its id. Both
IRegistered and IChangedEmail are IPropertyUpdated.IEmail, as on the property updated tevents
page.
Its wrapper tevent
A taggregate publishes each of its tevents inside a wrapper tevent, which says which class of taggregate published it. Every taggregate declares the interface of its wrapper; taggregate inheritance shows what it makes possible.
interface IUserTevent<out T> : ITaggregateTevent<T> where T : IUserTevent;
The tevent classes
Something must implement the interfaces. The classes mirror them: a class for each tevent, deriving from
TaggregateTevent, and a generic class for the wrapper.
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 class ChangedEmail(string email) : UserTevent, IUserTevent.IChangedEmail
{
public string Email { get; } = email;
}
}
class UserTevent<T>(T tevent) : TaggregateTevent<T>(tevent), IUserTevent<T> where T : IUserTevent;
Only the tevent that creates a user is given its id. Every other tevent gets it from the user that publishes it.
The taggregate
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));
protected override TeventRouting TeventAppliers => Appliers;
User() {}
public string Email { get; private set; } = "";
public static User Register(string email)
{
var user = new User();
user.Publish(new UserTevent.Registered(new TaggregateId(), email));
return user;
}
public void ChangeEmail(string email) => Publish(new UserTevent.ChangedEmail(email));
}
- It derives from
Taggregate<...>. The five type arguments are the taggregate's own class, its root tevent interface and the class implementing it, and its wrapper tevent interface and the class implementing that. - It declares its tevent routing once, for the class, with
DeclareAppliers, keeps it in a static field, and returns it from its override of the protectedTeventAppliersproperty. Every user shares it. For<T>routes every tevent assignable toTto an applier. Here registering and changing the email both reach the applier ofIPropertyUpdated.IEmail. The applier is astaticlambda that gets the user as its first parameter, since the routing is shared and holds no state of its own.- Its state changes only in its appliers.
ChangeEmaildoesn't setEmail: it publishes a tevent, and the applier sets it. - No public setters, and no public fields, in the taggregate or in its tevents. Compze refuses such a class the first time it is used: a taggregate whose state can change other than by its tevents no longer matches them.
- A tevent that reaches no applier is an error. A taggregate's routing can let some go with
IgnoreUnhandled.
Using it
var user = User.Register("ada@example.com");
user.ChangeEmail("ada.lovelace@example.com");
//user.Email is now "ada.lovelace@example.com", and user.Version is 2.
Publishing a tevent applies it at once. The user's Version counts its changes: 1 when it is created, and one more for
each tevent it publishes.
Where its tevents go
Taggregates are kept by a taggregate store. A taggregate that a store keeps publishes its tevents to the handlers of Tessaging right after each publish, inside the unit of work that changes it, as the user tevents on the introduction page are handled.
A taggregate can hold parts of its own: tomponents, and tentities.