1

If two aggregates are listening to the same event; then the event will be stored in an event store twice, once for each aggregate.

class ItemLoaded implements Event {
    LocationId location;
    VehicleId vehicle;
    Amount amount;
    Date loadDate;
}

class Location implements Aggregate {
    Amount itemAmount;

    void updateFrom(ItemLoaded event) {
        this.itemAmount.subtract(event.amount);
    }
}


class Vehicle implements Aggregate {
    Amount itemAmount;

    void updateFrom(ItemLoaded event) {
        this.itemAmount.add(event.amount);
    }
}

For instance there will be entries of the ItemLoaded event in the store once for Location and once for Vehicle, in the above case.

This much is expected, as can be seen from the two toy examples below, which you might have already seen if you've been reading about event sourcing:

In one example:

_current = new Dictionary<Guid, List<EventDescriptor>>();

In another example:

CREATE TABLE EventWrappers(
EventId uniqueidentifier NOT NULL,
StreamId nvarchar(50) COLLATE Latin1_General_CI_AS NOT NULL,
Sequence bigint NOT NULL,
TimeStamp datetime NOT NULL,
EventType nvarchar(100) COLLATE Latin1_General_CI_AS NOT NULL,
Body nvarchar](max) COLLATE Latin1_General_CI_AS NOT NULL,

Production systems aren't probably that different. In axon framework event are stored in a table like below, if you choose JPA or JDBC implementation of event store :

create table DomainEventEntry (
    -- .....
    primary key (aggregateIdentifier, sequenceNumber, type)
);

type field above is a string used to differentiate different aggregate types.

Given that events apparently maybe persisted multiple times, if I want to recreate a view from scratch, or add a new view model, how would I replay the events.

If we return to the above example, consider a view model like this:

class ItemsLoadedByYearProjection {
    @Subscribe
    void update(ItemLoaded event) {
        ItemsLoadedByYear row = getRowForYear(event.loadDate.year);
        row.itemAmount.add(itemAmount);
    }
}

class ItemsLoadedByYear {
    int year;
    Amount itemAmount;
}

Not only will the amounts will be counted twice now, if I replay the events naively; if I add a new aggregate in the future, some events will be counted twice, where others will be counted three times.

How do I prevent a projection/denormalizer from double counting the events that was saved to the event store multiple times?

  • 4
    What exactly is your question? – durron597 Mar 6 '15 at 15:44
5

If two aggregates are listening to the same event; then the event will be stored in an event store twice, once for each aggregate.

I would spell that differently. If two aggregates are listening to the same event, then representations of that event will appear in the history of each.

They won't be storing the same event, and the event that they store won't be the event that they are reacting to.

The aggregates will be storing events describing the changes to their own state, not the changes to state elsewhere in the domain that they are reacting to.

When you get that right, recreating the aggregates is easy - each aggregate loads its own history. There's no "double counting" to worry about, because each event in the history represents a change to an aggregate, so they are all different things.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.