public static ConcurrentBag <ResultMessageModel> ToResultMessageModel(this ConcurrentBag <MessageEntity> MessageEntities) { if (!MessageEntities.AnyWithNullCheck()) { return(null); } var resultMessageModels = new ConcurrentBag <ResultMessageModel>(); foreach (MessageEntity messageEntity in MessageEntities) { resultMessageModels.Add(new ResultMessageModel() { ErrorCode = messageEntity.ErrorCode, ErrorMessage = messageEntity.ErrorMessage }); } return(resultMessageModels); }
public ResultMessageEntity Execute() { var nonParallelExecution = parallelExecutions.Where(x => !x.IsParallel).ToList(); #region non Parallel Execution if (nonParallelExecution.AnyWithNullCheck()) { foreach (var action in nonParallelExecution) { var result = action.Activity.Invoke(action.RequestObject); if (result == null) { return(new ResultMessageEntity { ResultStatus = ResultStatus.Fail }); } if (result.ResultStatus != ResultStatus.Success && action.IsStoppedOnFailureResult) { return(result); } } } #endregion #region Parallel Execution var parallelActions = parallelExecutions.Where(x => x.IsParallel).ToList(); ConcurrentBag <ResultMessageEntity> outPutResultMessageEntities = new ConcurrentBag <ResultMessageEntity>(); Parallel.ForEach(parallelActions, parallelAction => { outPutResultMessageEntities.Add(parallelAction.Activity.Invoke(parallelAction.RequestObject)); }); #endregion #region Process Result if (!outPutResultMessageEntities.AnyWithNullCheck(x => x.ResultStatus == ResultStatus.Fail)) { return(new ResultMessageEntity { ResultStatus = ResultStatus.Success }); } var failedresultMessageEntities = outPutResultMessageEntities.Where(x => x.ResultStatus == ResultStatus.Fail); var resultMessageEntities = new ResultMessageEntity { ResultStatus = ResultStatus.Fail, MessageEntity = new ConcurrentBag <MessageEntity>() }; foreach (var failedresultMessageEntity in failedresultMessageEntities) { if (failedresultMessageEntity == null || !failedresultMessageEntity.MessageEntity.AnyWithNullCheck()) { continue; } foreach (MessageEntity messageEntity in failedresultMessageEntity.MessageEntity) { resultMessageEntities.MessageEntity.Add(messageEntity); } } return(resultMessageEntities); #endregion }