public ICommandResult <TOutput> Execute <TInput, TOutput>(ISerialization <TInput> input, ISerialization <TOutput> output, TInput data) { var either = CommandResult <TOutput> .Check <Argument <TInput>, TInput>(input, output, data, CreateExampleArgument); if (either.Error != null) { return(either.Error); } var argument = either.Argument; var eventType = DomainModel.Find(argument.Name); if (eventType == null) { return (CommandResult <TOutput> .Fail( "Couldn't find event type {0}.".With(argument.Name), @"Example argument: " + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output)))); } var aggregateInterface = eventType.GetInterfaces().FirstOrDefault(it => it.IsGenericType && it.GetGenericTypeDefinition() == typeof(IDomainEvent <>)); var aggregateType = aggregateInterface != null?aggregateInterface.GetGenericArguments()[0] : null; if (aggregateType == null) { return (CommandResult <TOutput> .Fail( "{0} does not implement IDomainEvent<TAggregate>.".With(eventType.FullName), @"Example argument: " + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output)))); } if (!Permissions.CanAccess(eventType)) { return (CommandResult <TOutput> .Return( HttpStatusCode.Forbidden, default(TOutput), "You don't have permission to access: {0}.", eventType.FullName)); } if (!Permissions.CanAccess(aggregateType)) { return (CommandResult <TOutput> .Return( HttpStatusCode.Forbidden, default(TOutput), "You don't have permission to access: {0}.", aggregateType.FullName)); } try { var commandType = typeof(SubmitEventCommand <,>).MakeGenericType(eventType, aggregateType); var command = Activator.CreateInstance(commandType) as ISubmitEventCommand; var result = command.Submit(input, output, Locator, EventStore, argument.Uri, argument.Data); return(CommandResult <TOutput> .Return(HttpStatusCode.Created, result, "Event applied")); } catch (ArgumentException ex) { return(CommandResult <TOutput> .Fail( ex.Message, ex.GetDetailedExplanation() + @" Example argument: " + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output)))); } }