public void Send <T>(T command) where T : Command { // 获得对应的CommandHandle来对命令进行处理 var handlers = _commandHandlerFactory.GetHandlers <T>(); foreach (var handler in handlers) { // 处理命令 handler.Execute(command); } }
public void Send <T>(T command) where T : Command { var handler = _commandHandlerFactory.GetHandlers <T>(); if (handler != null) { handler.Execute(command); } else { throw new UnregisteredDomainCommandException("No handler was registered."); } }
public Task Send <T>(T command) where T : Command { var handlers = _commandHandlers.GetHandlers <T>().ToList(); if (!handlers.Any()) { throw new InvalidOperationException($"no command handler registered for {typeof(T)}"); } foreach (var h in handlers) { h.Handle(command); } return(Task.FromResult(0)); }
/// <summary> /// Processes the specified command. /// </summary> /// <typeparam name="TCommand">The type of the command.</typeparam> /// <param name="command">The command.</param> /// <exception cref="CommandHandlerNotFoundException"></exception> public void Process <TCommand>(TCommand command) where TCommand : ICommand { Validator.ValidateObject(command, new ValidationContext(command, null, null), true); //var handlers = ServiceLocator.Current.GetAllInstances<ICommandHandler<TCommand>>(); var handlers = _factory.GetHandlers <TCommand>(); if (handlers == null || !handlers.Any()) { throw new CommandHandlerNotFoundException(typeof(TCommand)); } foreach (var handler in handlers) { try { handler.Handle(command); } finally { _factory.ReleaseHandler(handler); } } }