I'm trying to get my head around DDDD in Greg Young's style.
There is much talk about how to implement DDDD with CQRS+EventSourcing and there are some sample implementations ... and altogether it can be quite confusing...
In Gregs view aggregates don't have getters or setters - just state-changing methods which emit an corresponding event.
Basicly a event decribes a state-transitions that happend in the past. It's data describes what changed.
Some say, that this data can be "enriched" by additional data.
Where can this additional data come from?
i.e. I have User
and Usergroup
- both aggregate roots (can exist independently, have identity). User
has a method called AddToUsergroup
.
public class User : AggregateRoot
{
// ...
public void AddToUsergroup(Usergroup usergroup)
{
// validate state
RaiseEvent(new UserJoinedUsergroup(this.Id, usergroup.Id));
}
// ...
}
public class Usergroup : AggregateRoot
{
private string _displayName;
// ...
public void ChangeDisplayName(string displayName)
{
// validate state
RaiseEvent(new DisplayNameChanged(this.Id, displayName));
}
public void Apply(DisplayNameChanged e)
{
this._displayName = e.DisplayName;
}
// ...
}
If I would like to "enrich" the event with the name of the Usergroup (for debug reasons or something like this), how would I do that?
- getters are nonexistent
- internal state of usergroup is inaccessible
Injecting such things as repositories into User
is not allowed (am I right here?!?), like
- Read-side repository
- Event-store repository
Bottomline questions:
CanShould something like repositories be injected to aggregate roots?- Should a event only use data available through parameters and internal state of the aggregate?
- Should events only contain the minimum data describing the state-change?
And (little off-topic but the sample is here)
- Should
AddToUsergroup
take an Guid instead of an full aggregate root?
Looking forward to your answers!
Lg
warappa