/// <summary> /// Converts Creation Result to Creation Result with tracking errors containing correct source in fail case. /// </summary> /// <typeparam name="TResult">Type of entity to be created.</typeparam> /// <typeparam name="TCommand">Command type.</typeparam> /// <param name="creationResult">Creation Result.</param> /// <param name="getProperty">Property of command to fill as source.</param> /// <returns>Modified creation result.</returns> public static CreationResult <TResult> ForCommand <TResult, TCommand>( this CreationResult <TResult> creationResult, Expression <Func <TCommand, object> > getProperty) where TCommand : ICommand where TResult : class { if (creationResult.Entry != null) { return(creationResult); } var source = SourceBuilder.BuildErrorSource(getProperty); var firstExecutionError = creationResult.Errors.First(); // It's the first error and we should fill correct source path for this error. if (string.IsNullOrEmpty(firstExecutionError.Source)) { var updatedError = new ExecutionError(firstExecutionError.CodeInfo, source, firstExecutionError.Message); return(new CreationResult <TResult>(updatedError)); } // There are error with filled source => that we need to create copy of error with another source var newError = new ExecutionError(firstExecutionError.CodeInfo, source, firstExecutionError.Message); var allErrors = creationResult.Errors.ToList(); allErrors.Add(newError); return(new CreationResult <TResult>(allErrors)); }
/// <summary> /// Extension for Creation Result that fixes path with incorrect indices. /// </summary> /// <typeparam name="TResult">Type of entity to be created.</typeparam> /// <typeparam name="TCommand">Command type.</typeparam> /// <param name="creationResult">Creation Result.</param> /// <param name="getProperty">Property of command to fill as source.</param> /// <returns>Modified creation result.</returns> public static CreationResult <TResult> WithSource <TResult, TCommand>( this CreationResult <TResult> creationResult, Expression <Func <TCommand, object> > getProperty) where TCommand : ICommand where TResult : class { if (creationResult.Entry != null) { return(creationResult); } var newSource = SourceBuilder.BuildErrorSource(getProperty); var allErrors = new List <ExecutionError>(); foreach (var error in creationResult.Errors) { var updatedError = UpdateErrorWithSource(error, newSource); allErrors.Add(updatedError); } return(new CreationResult <TResult>(allErrors)); }
/// <summary> /// Converts Creation Result to Creation Result with tracking errors containing correct source in fail case. /// </summary> /// <typeparam name="TResult">Type of entity to be created.</typeparam> /// <typeparam name="TCommand">Command type.</typeparam> /// <param name="creationResult">Creation Result.</param> /// <returns>Modified creation result.</returns> public static CreationResult <TResult> ForCommand <TResult, TCommand>(this CreationResult <TResult> creationResult) where TCommand : ICommand where TResult : class { return(creationResult.ForCommand <TResult, TCommand>(c => c)); }