public Task Command <TArguments>(TArguments arguments) { var handler = commandFactory.Resolve <TArguments>(); try { return(handler.Execute(arguments)); } finally { commandFactory.Release(handler); } }
public async Task Command <TArguments>(TArguments arguments) where TArguments : ICommand { var handler = commandHandlerFactory.Resolve <TArguments>(); try { await handler.Handle(arguments); } finally { commandHandlerFactory.Release(handler); } }
/// <summary> /// Executes a command handler based on the command type. /// </summary> /// <typeparam name="TCommand">A command type</typeparam> /// <param name="command">An instance of a command with data</param> public void Execute <TCommand>(TCommand command) where TCommand : ICommand { var commandHandler = _commandHandlerFactory.Create <TCommand>(); try { commandHandler.CommandExecuted += CommandExecuted; commandHandler.Execute(command); } finally { _commandHandlerFactory.Release(commandHandler); } }
public void Execute <TCommand>(TCommand command) where TCommand : ICommand { ICommandHandler <TCommand> handler = _HandlerFactory.Create <TCommand>(); try { handler.Execute(command); } finally { _HandlerFactory.Release(handler); } }
/// <summary> /// Helper method to execute any ICommandHandler instance in a generic /// way. /// </summary> /// <param name="handlerTypeTemplate"> /// Generic Handler Type template that we fill in (with the handler's /// TCommand and TResult) and use to resolve an instance of our desired /// <see cref="ICommandHandler" /> using the /// <see cref="ICommandHandlerFactory" />. /// </param> /// <param name="executeMethodTemplate"> /// The pre-reflected template method used to call the actual generic /// Execute method of the <see cref="ICommandHandler" /> instance of /// unknown exact type. /// </param> /// <param name="commandInstance"> /// The <see cref="ICommand" /> to be /// handled by the <see cref="ICommandHandler" />.</param> /// <param name="genericTemplateArgs"> /// An array of Types to be used as generic type parameters when /// when filling in the <see paramRef="handlerTypeTemplate" /> and /// <see paramRef="executeMethodTemplate" />. /// </param> /// <returns> /// The results of executing the <see cref="ICommandHandler" />. /// </returns> private object ExecuteCommand( Type handlerTypeTemplate, MethodInfo executeMethodTemplate, object commandInstance, params Type[] genericTemplateArgs ) { // Fill in handlerTypeTemplate with genericTemplateArgs to get the // generic handlerType we want to resolve var handlerType = handlerTypeTemplate.MakeGenericType(genericTemplateArgs); // Resolve the handlerInstance var handlerInstance = handlerFactory.Create(handlerType); // Fill in the handler's executeMethodTemplate with the same // genericTemplateArgs to get a method instance we can execute // against the resolved handlerInstance var executeMethod = executeMethodTemplate.MakeGenericMethod(genericTemplateArgs); try { return (executeMethod.Invoke( null, new[] { handlerInstance, commandInstance } )); } catch (TargetInvocationException ex) { throw new CommandExecutionException(commandInstance, ex.InnerException); } finally { handlerFactory.Release(handlerInstance); } }
public void Handle(TCommand command) { ICommandHandler <TCommand> handler = null; try { ICommandHandler commandHandler = _factory.CreateByName(command.GetType().FullName); handler = (ICommandHandler <TCommand>)commandHandler; handler.Handle(command); } finally { if (handler != null) { _factory.Release(handler); } } }