Esempio n. 1
0
        public void Dispatch <TCommand>(TCommand command)
        {
            var handlers = _commandHandlerFactory.GetHandlersForCommand(command);

            foreach (var handler in handlers)
            {
                handler.Handle(command);
            }
        }
        void IBus.Send <TCommand>(TCommand message)
        {
            #region Command Validations
            // Eventual consistency checks
            // ATTENTION: based on domain requisites could be possibile to add constraint to readmodel DB
            //          or to check before persisting. In this case, it's quite rare to have concurrent conflicts
            //          so, this "eventual check" is perfect in most of the domain cases and could be assumed as good default rule
            IValidator <TCommand>[] validators = _commandValidatorFactory.GetValidatorsForCommand <TCommand>(message);
            foreach (var validator in validators)
            {
                validator.ValidateAndThrow(message);
            }
            #endregion

            #region Command Handling
            ICommandHandler <TCommand>[] handlers = _commandHandlerfactory.GetHandlersForCommand <TCommand>(message);
            foreach (var handler in handlers)
            {
                handler.Handle(message);
            }
            #endregion

            #region Aggregates Snapshooting
            #if DEBUG
            foreach (var handler in handlers)
            {
                // If commandHandler is a SnapshotCreator it handles the snapshoot persistance.
                // NOTE: Very unrealistic Snapsooting policy/implementation, but it's just for training purposes
                MethodInfo method = handler.GetType().GetMethod("SaveSnapShot");
                if (method != null)
                {
                    method.Invoke(handler, new object[] { message });
                }
            }
            #endif
            #endregion
        }