コード例 #1
0
        public TAggregate ExecuteCommand <TAggregate>(ICommand command)
            where TAggregate : IAggregate, new()
        {
            var commandType = command.GetType();

            if (!_commandHandlers.ContainsKey(commandType))
            {
                throw new ApplicationException($"Missing handler for command {commandType.Name} on aggregate {typeof(TAggregate)}");
            }

            IAggregate aggregate = _aggregateRepository.GetAggregateById <TAggregate>(command.AggregateId);

            aggregate = (_commandHandlers[commandType] as Func <IAggregate, ICommand, IAggregate>)(aggregate, command);

            foreach (var evt in aggregate.UncommittedEvents)
            {
                evt.Metadata.CausationId   = command.Metadata.CommandId;
                evt.Metadata.CorrelationId = command.Metadata.CorrelationId;
                evt.Metadata.ProcessId     = command.Metadata.ProcessId;
            }

            _aggregateRepository.SaveAggregate((TAggregate)aggregate);

            return((TAggregate)aggregate);
        }