EventFlow Как использовать IEventUpgrader? - PullRequest
0 голосов
/ 05 ноября 2019

Я следовал документации. EventUpgrade

Мой агрегат emit ExampleEvent.

Я никогда не передаю в UpgradeExampleEventToExampleEventV2.

В тестовом файле:

_resolver = EventFlowOptions.New
    .AddEvents(typeof(ExampleEvent), typeof(ExampleEventV2))
    .AddEventUpgraders(typeof(UpgradeExampleEventToExampleEventV2))
    .AddCommands(typeof(ExampleCommand))
    .AddCommandHandlers(typeof(ExampleCommandHandler))
    .UseInMemoryReadStoreFor<ExampleReadModel>()
    .CreateResolver();

В EventUpgrader:

public class UpgradeExampleEventToExampleEventV2 : IEventUpgrader<ExampleAggregate, ExampleId>
{
    private readonly IDomainEventFactory _domainEventFactory;

    public UpgradeExampleEventToExampleEventV2(IDomainEventFactory domainEventFactory)
    {
        _domainEventFactory = domainEventFactory;
    }

    public IEnumerable<IDomainEvent<ExampleAggregate, ExampleId>> Upgrade(IDomainEvent<ExampleAggregate, ExampleId> domainEvent)
    {
        var exampleEvent = domainEvent as IDomainEvent<ExampleAggregate, ExampleId, ExampleEvent>;
        yield return exampleEvent == null
            ? domainEvent
            : _domainEventFactory.Upgrade<ExampleAggregate, ExampleId>(domainEvent, new ExampleEventV2(exampleEvent.AggregateEvent.MagicNumber));
    }
}

В совокупности:

public void SetMagicNumber(int magicNumber)
{
    if (_magicNumber.HasValue) throw DomainError.With("Magic number already set");

    Emit(new ExampleEvent(magicNumber));
}
...