public async Task <MethodResult <TCommand> > ExecuteCommand <TCommand>(TCommand command) where TCommand : Command { MethodResult <TCommand> methodResult; _logger.Information("Executing command: {0}, Id:{1}.", command, command.RequestId); try { _transformationService.PreProcess(command); var businessRules = _ruleManager.ExecuteBusinessRules(command); if (!businessRules.IsSuccessful) { methodResult = new MethodResult <TCommand>(MethodResultStates.UnSuccessful, $"{businessRules}"); _logger.Information("Execution failed for validations {0}. Reasons: {1}", command.RequestId, methodResult); } else { var props = command.GetType() .GetProperties() .Where(prop => Attribute.IsDefined(prop, typeof(GenerateKeyAttribute))); foreach (var prop in props) { var generateIdAttr = prop.GetCustomAttributes(true) .OfType <GenerateKeyAttribute>() .FirstOrDefault(); if (generateIdAttr != null) { if (prop.PropertyType == typeof(string)) { prop.SetValue(command, $"{_generateKey.GetNextStringKey( generateIdAttr.CollectionName)}"); } else if (prop.PropertyType == typeof(long)) { prop.SetValue(command, _generateKey.GetNextNumericalKey(generateIdAttr.CollectionName)); } } } _logger.Information("Sending command for execution {0}.", command.RequestId); await _bus.Send(command); methodResult = new MethodResult <TCommand>(command); } } catch (Exception ex) { _logger.Error(ex, "Error occured @ CommandService: "); methodResult = new MethodResult <TCommand>(MethodResultStates.UnSuccessful, "Error occured executing the command."); } return(methodResult); }
public void Send(long senderId, EmailMessage emailMessage) { var id = _generateKey.GetNextStringKey("Email"); emailMessage.Id = id; emailMessage.Status = MailStatus.Stored; emailMessage.ReceivedOn = DateTime.Now; emailMessage.LastAttemptFailureMessage = ""; emailMessage.SenderId = senderId; _documentStore.Store(emailMessage); }