Tentities
A tentity is an entity of a taggregate: a part of it with an identity of its own, of which the taggregate can hold any number, and which is teventive like the taggregate. It changes its state only by applying the tevents it publishes, and those tevents are the taggregate's, as a tomponent's are. What a tentity adds is its id: each of its tevents carries it, so that the taggregate knows which of its tentities the tevent is for.
A tentity belongs to one taggregate. One written once, to be used in many different taggregates, is a shared tentity.
This page gives the User of Taggregates addresses: as many as it likes, each of which can move, and
be removed.
Its tevents
A tentity's tevents are rooted in an interface nested in the taggregate's root tevent interface, as a tomponent's are,
which declares the tentity's id: IUserTevent.IAddress declares AddressId.
interface IUserTevent : ITaggregateTevent
{
interface IPropertyUpdated : IUserTevent
{
interface IEmail : IPropertyUpdated
{
string Email { get; }
}
}
interface IRegistered : IUserTevent, ITaggregateCreatedTevent, IPropertyUpdated.IEmail;
interface IAddress : IUserTevent
{
Guid AddressId { get; }
interface IPropertyUpdated : IAddress
{
interface IStreet : IPropertyUpdated
{
string Street { get; }
}
}
interface IAdded : IAddress, IPropertyUpdated.IStreet;
interface IMoved : IAddress, IPropertyUpdated.IStreet;
interface IRemoved : IAddress;
}
}
interface IUserTevent<out T> : ITaggregateTevent<T> where T : IUserTevent;
The tevents that matter most are those that begin and end a tentity: IAdded, which creates an address, and
IRemoved, which removes it.
The tevent classes mirror the interfaces. The class of the tentity's root tevent also has an id getter-setter, a small class that tells Compze how to read and set the address id of an address tevent:
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 Address : UserTevent, IUserTevent.IAddress
{
public Guid AddressId { get; private set; }
public class IdGetterSetter : IGetSetTaggregateEntityTeventEntityId<Guid, Address, IUserTevent.IAddress>
{
public void SetEntityId(Address tevent, Guid id) => tevent.AddressId = id;
public Guid GetId(IUserTevent.IAddress tevent) => tevent.AddressId;
}
internal class Added : Address, IUserTevent.IAddress.IAdded
{
public Added(Guid addressId, string street)
{
AddressId = addressId;
Street = street;
}
public string Street { get; }
}
internal class Moved(string street) : Address, IUserTevent.IAddress.IMoved
{
public string Street { get; } = street;
}
internal class Removed : Address, IUserTevent.IAddress.IRemoved;
}
}
class UserTevent<T>(T tevent) : TaggregateTevent<T>(tevent), IUserTevent<T> where T : IUserTevent;
Only Added is given the address's id. Every other address tevent gets it from the address that publishes it, through
the id getter-setter.
The tentity
class Address : User.RemovableEntity<Address,
Guid,
IUserTevent.IAddress,
UserTevent.Address,
IUserTevent.IAddress.IAdded,
IUserTevent.IAddress.IRemoved,
UserTevent.Address.IdGetterSetter>
{
static readonly TeventRouting<Address, IUserTevent.IAddress> Appliers = DeclareAppliers(it => it
.For<IUserTevent.IAddress.IPropertyUpdated.IStreet>(static (address, updated) => address.Street = updated.Street));
protected override TeventRouting TeventAppliers => Appliers;
public Address(User user) : base(user) {}
public string Street { get; private set; } = "";
public void MoveTo(string street) => Publish(new UserTevent.Address.Moved(street));
public void Remove() => Publish(new UserTevent.Address.Removed());
}
- It derives from
User.RemovableEntity<...>, a base class every taggregate class has. Its type arguments are the tentity's own class, the type of its id, its root tevent interface and the class implementing it, the tevents that create and remove it, and its id getter-setter. A tentity that is never removed derives fromUser.Entity<...>instead, which takes no removed tevent. - Its constructor takes the user it belongs to. Compze constructs it, when its created tevent is applied.
- Its
Idis the id its created tevent carries. - It declares its tevent routing once, for the class, as a taggregate does.
- It publishes through the user, as a tomponent does:
MoveTopublishes an address tevent, which carries the address's id and the user's.
The taggregate that holds them
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)
.Entities(static user => user._addresses));
protected override TeventRouting TeventAppliers => Appliers;
readonly Address.ICollectionManager _addresses;
User() => _addresses = Address.CreateSelfManagingCollection(this);
public string Email { get; private set; } = "";
public IReadOnlyEntityCollection<Address, Guid> Addresses => _addresses.Entities;
public Address AddAddress(string street) => _addresses.AddByPublishing(new UserTevent.Address.Added(Guid.NewGuid(), street));
public static User Register(string email)
{
var user = new User();
user.Publish(new UserTevent.Registered(new TaggregateId(), email));
return user;
}
}
The user keeps its addresses in a collection that manages itself: CreateSelfManagingCollection makes it, and the
user routes the address tevents to it with Entities. The collection creates an address when an IAdded arrives,
routes every other address tevent to the address whose id it carries, and removes an address when its IRemoved
arrives. Entities is the collection as the user's code reads it: in creation order, or by id.
AddByPublishing publishes a created tevent, and returns the tentity it created.
Using it
var user = User.Register("ada@example.com");
var home = user.AddAddress("12 Marylebone Road");
home.MoveTo("1 St James's Square");
//home.Street is now "1 St James's Square", and user.Addresses[home.Id] is home.
home.Remove();
//user.Addresses is empty again.