/// <summary>
        /// This changes an Optional ResultOrError into a ResultOrError by setting a Missing value to an error ResultOrError, populating it with a provided error message (if the ResultOrError
        /// is already populated, whether as a Result or an Error) then the instance will be returned unaltered
        /// </summary>
        public static ResultOrError <T> SetErrorIfNoValue <T>(this Optional <ResultOrError <T> > source, NonBlankTrimmedString errorMessage)
        {
            if (errorMessage == null)
            {
                throw new ArgumentNullException(nameof(errorMessage));
            }

            return(source.GetValueOrDefault(ResultOrError <T> .FromError(errorMessage)));
        }
コード例 #2
0
        /// <summary>
        /// Given a ResultOrError of Optional T, return a ResultOrError of T by replacing a Missing T value with an Error ResultOrError of T
        /// </summary>
        public static ResultOrError <T> ReplaceMissingResultWithError <T>(this ResultOrError <Optional <T> > source, NonBlankTrimmedString errorMessageForMissingValue)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (errorMessageForMissingValue == null)
            {
                throw new ArgumentNullException(nameof(errorMessageForMissingValue));
            }

            return(source.Match(
                       handleResult: resultIfAvailable => resultIfAvailable
                       .Map(result => ResultOrError.FromResult(result))
                       .GetValueOrDefault(ResultOrError <T> .FromError(errorMessageForMissingValue)),
                       handleError: errorMessage => ResultOrError <T> .FromError(errorMessage)
                       ));
        }