These are chat archives for eventflow/EventFlow

19th
Mar 2018
identify1980
@identify1980
Mar 19 08:04
So far I understood that a snapshot basically contains a copy of the aggregate data. My question is then: how to load the snapshot data back into the aggregate, when aggregate states are used. As SnapshotAggregateRoot requires "LoadSnapshotAsync" to be implemented, I don't know how to do this when using aggregate states. Given following example code (sorry for the wall of text):
public class Value1Added : AggregateEvent<MyAggregate, MyAggregateId>
{
    public Value1Added(string value)
    {
        this.Value = value;
    }

    public string Value { get; }
}

public class Value2Added : AggregateEvent<MyAggregate, MyAggregateId>
{
    public Value2Added(string value)
    {
        this.Value = value;
    }

    public string Value { get; }
}

public class MyState : AggregateState<MyAggregate, MyAggregateId, MyState>, IApply<Value1Added>, IApply<Value2Added>
{
    public void Apply(Value1Added @event)
    {
        this.Value1 = @event.Value;
    }

    public void Apply(Value2Added @event)
    {
        this.Value2 = @event.Value;
    }

    public string Value1 { get; private set; }
    public string Value2 { get; private set; }
}

public class MySnapshot : ISnapshot
{
    public MySnapshot(string value1, string value2)
    {
        this.Value1 = value1;
        this.Value2 = value2;
    }

    public string Value1 { get; }
    public string Value2 { get; }
}

public class MyAggregate : SnapshotAggregateRoot<MyAggregate, MyAggregateId, MySnapshot>, IEmit<Value1Added>, IEmit<Value2Added>
{
    private readonly MyState state = new MyState();

    public MyAggregate(MyAggregateId id) : base(id)
    {
        this.Register(this.state);
    }

    public void AddValue1(string value)
    {
        this.Emit(new Value1Added(value));
    }

    public void AddValue2(string value)
    {
        this.Emit(new Value2Added(value))
    }

    protected override Task<MySnapshot> CreateSnapshotAsync(CancellationToken cancellationToken)
    {
        return Task.FromResult(new MySnapshot(this.state.Value1, this.state.Value2));
    }

    protected override Task LoadSnapshotAsync(MySnapshot snapshot, ISnapshotMetadata metadata, CancellationToken cancellationToken)
    {
        // How to apply the data from the snapshot here???
        return Task.FromResult(0);
    }
}