예제 #1
0
        public static Outcome <TResult> Reduce <T, TResult>(
            this Outcome <IEnumerable <T> > @this,
            Func <TResult, T, Outcome <TResult> > generator)
        {
            if ([email protected])
            {
                return(Outcome <TResult> .Reject(@this.FailureOrThrow()));
            }

            var     parameters = @this.ResultOrDefault();
            TResult previous   = default;

            foreach (var param in parameters)
            {
                var current = generator(previous, param);
                if (current.IsSuccessful)
                {
                    previous = current.ResultOrDefault();
                }
                else
                {
                    return(Outcome <TResult> .Reject(
                               $"{nameof(generator)} function failed for the parameter value {param}"));
                }
            }
            return(previous);
        }
예제 #2
0
        public static Outcome <ResultType> Map <T, ResultType>(this Outcome <T> @this, Func <T, ValueTuple <ResultType, Failure> > fn)
        {
            if ([email protected])
            {
                return(Outcome <ResultType> .Reject(@this.FailureOrNull()));
            }

            return(Try <ResultType>(() => fn(@this.ResultOrDefault())));
        }
예제 #3
0
        public static Outcome <ResultType> Map <T, ResultType>(this Outcome <T> @this, Func <ResultType> fn)
        {
            if ([email protected])
            {
                return(Outcome <ResultType> .Reject(@this.FailureOrNull()));
            }

            return(Outcome.Of(fn));
        }
        public static Outcome <IEnumerable <ResultType> > ThenForEach <T, ResultType>(this Outcome <IEnumerable <T> > @this, Func <ResultType> fn)
        {
            if ([email protected])
            {
                return(Outcome <IEnumerable <ResultType> > .Reject(@this.FailureOrThrow()));
            }

            return(Outcome.Of(() => @this.ResultOrDefault(Enumerable.Empty <T>()).Select(_ => fn())));
        }
예제 #5
0
        public static Outcome <T> Fail <T>(this Outcome <T> @this, Func <Failure> fn)
        {
            if ([email protected])
            {
                return(@this);
            }

            return(Outcome <T> .Reject(fn()));
        }
예제 #6
0
        public static async Task <Outcome <T> > Fail <T>(this Outcome <T> @this, Func <T, Task <Failure> > fn)
        {
            if ([email protected])
            {
                return(@this);
            }

            return(Outcome <T> .Reject(await fn(@this.ResultOrDefault())));
        }
예제 #7
0
        public static Outcome <ResultType> Then <T, ResultType>(this Outcome <T> @this, Func <T, ResultType> fn)
        {
            if ([email protected])
            {
                return(Outcome <ResultType> .Reject(@this.FailureOrNull()));
            }

            return(Outcome.Of(() => fn(@this.ResultOrDefault())));
        }
예제 #8
0
        public static async Task <Outcome <T> > Fail <T>(this Task <Outcome <T> > promise, Func <Task <Failure> > fn)
        {
            var @this = await promise;

            if ([email protected])
            {
                return(@this);
            }

            return(Outcome <T> .Reject(await fn()));
        }
예제 #9
0
        public static async Task <Outcome <ReturnType> > Then <T, ReturnType>(this Outcome <T> outcome, Func <T, Task <Outcome <ReturnType> > > asyncFunc)
        {
            return(await Try(async() => {
                if (outcome.IsSuccessful)
                {
                    return await asyncFunc(outcome.ResultOrDefault());
                }

                return Outcome <ReturnType> .Reject(outcome.FailureOrNull());
            }));
        }
예제 #10
0
        public static Task <Outcome <ReturnType> > Map <T, ReturnType>(this Outcome <T> outcome, Func <Task <ReturnType> > asyncFunc)
        {
            if (outcome.IsSuccessful)
            {
                return(Outcome.Of(asyncFunc));
            }

            return(Outcome <ReturnType>
                   .Reject(outcome.FailureOrNull())
                   .ForAsync());
        }
예제 #11
0
        public static async Task <Outcome <T> > Then <T>(this Task <Outcome <T> > @this, bool condition, Func <Task <Failure> > fn)
        {
            if (!condition)
            {
                return(await @this);
            }

            var failure = await fn();

            return(Outcome <T> .Reject(failure));
        }
        public static async Task <Outcome <ResultType> > Map <T, ResultType>(this Task <Outcome <T> > asyncPromise, Func <ResultType> func) //where ResultType: class
        {
            return(await Try(async() => {
                var outcome = await asyncPromise;
                if (outcome.IsSuccessful)
                {
                    return Outcome.Of(func);
                }

                return Outcome <ResultType> .Reject(outcome.FailureOrNull());
            }));
        }
        public static async Task <Outcome <ReturnType> > Map <T, ReturnType>(this Task <Outcome <T> > asyncPromise, Func <Task <Outcome <ReturnType> > > aysncFunc)
        {
            return(await Try(async() => {
                var outcome = await asyncPromise;
                if (outcome.IsSuccessful)
                {
                    return await aysncFunc();
                }

                return Outcome <ReturnType> .Reject(outcome.FailureOrNull());
            }));
        }
        public static async Task <Outcome <ResultType> > Map <T, ResultType>(this Task <Outcome <T> > asyncPromise, Func <T, Outcome <ResultType> > func)
        {
            return(await Try(async() => {
                var outcome = await asyncPromise;
                if (outcome.IsSuccessful)
                {
                    return func(outcome.ResultOrDefault());
                }

                return Outcome <ResultType> .Reject(outcome.FailureOrNull());
            }));
        }
        public static async Task <Outcome <ReturnValue> > Then <T, ReturnValue>(this Task <Outcome <T> > asyncPromise, Func <T, Task <Outcome <ReturnValue> > > aysncFunc)
        {
            return(await Try(async() => {
                var outcome = await asyncPromise;
                if (outcome.IsSuccessful)
                {
                    return await aysncFunc(outcome.ResultOrDefault());
                }

                return Outcome <ReturnValue> .Reject(outcome.FailureOrNull());
            }));
        }
예제 #16
0
        public static Task <Outcome <ReturnType> > Map <T, ReturnType>(this Outcome <T> outcome, Func <T, Task <Outcome <ReturnType> > > asyncFunc)
        {
            return(Try(() => {
                if (outcome.IsSuccessful)
                {
                    return asyncFunc(outcome.ResultOrDefault());
                }

                return Outcome <ReturnType>
                .Reject(outcome.FailureOrNull())
                .ForAsync();
            }));
        }
예제 #17
0
        /// <summary>
        /// Applies the given function on all elements of the input enumerable and
        /// returns an enumerable of the results.
        /// </summary>
        /// <typeparam name="TResult">The return type of the generator function.</typeparam>
        /// <typeparam name="TParam">The element type of the input enumerable.</typeparam>
        /// <param name="parameters">The input enumerable.</param>
        /// <param name="generator">The function to be applied.</param>
        /// <returns>The enumerable of results.</returns>
        public static Outcome <IEnumerable <TResult> > MapAll <T, TResult>(
            this Outcome <IEnumerable <T> > @this,
            Func <T, TResult> fn)
        {
            if ([email protected])
            {
                return(Outcome <IEnumerable <TResult> >
                       .Reject(@this.FailureOrThrow()));
            }

            return(Outcome.Of(
                       () => @this
                       .ResultOrDefault(Enumerable.Empty <T>())
                       .Select(r => fn(r))
                       ));
        }
예제 #18
0
        public static async Task <Outcome <T> > Of <T>(Task <T> task)
        {
            _ = task ?? throw new ArgumentNullException(nameof(task));

            try
            {
                var result = await task.ConfigureAwait(false);

                return(new Outcome <T>(result));
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception ex)
            {
                return(Outcome <T> .Reject(ex));
            }
#pragma warning restore CA1031 // Do not catch general exception types
        }
예제 #19
0
        public static Outcome <T> Of <T>(Func <T> func)
        {
            _ = func ?? throw new ArgumentNullException(nameof(func));


            try
            {
                var result = func();
                return(new Outcome <T>(result));
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception e)
            {
                return(Outcome <T> .Reject(e.Message, e));
            }
#pragma warning restore CA1031 // Do not catch general exception types
        }
예제 #20
0
        public static async Task <Outcome <TResult> > Reduce <T, TResult>(
            this Task <Outcome <IEnumerable <T> > > input,
            Func <TResult, T, Task <Outcome <TResult> > > generator)
        {
            Outcome <IEnumerable <T> > @this;

            try
            {
                @this = await input;
            }
            catch (Exception ex)
            {
                return(Outcome <TResult> .Reject(ex));
            }

            if ([email protected])
            {
                return(Outcome <TResult> .Reject(@this.FailureOrThrow()));
            }

            var     parameters = @this.ResultOrDefault();
            TResult previous   = default;

            foreach (var param in parameters)
            {
                var current = await generator(previous, param);

                if (current.IsSuccessful)
                {
                    previous = current.ResultOrDefault();
                }
                else
                {
                    return(Outcome <TResult> .Reject(
                               $"{nameof(generator)} function failed for the parameter value {param}"));
                }
            }
            return(previous);
        }
예제 #21
0
        /// <summary>
        /// Applies the given function on all elements of the input enumerable and
        /// returns an enumerable of the results.
        /// </summary>
        /// <typeparam name="TResult">The return type of the generator function.</typeparam>
        /// <typeparam name="TParam">The element type of the input enumerable.</typeparam>
        /// <param name="parameters">The input enumerable.</param>
        /// <param name="generator">The function to be applied.</param>
        /// <returns>The enumerable of restuls.</returns>
        public static async Task <Outcome <IEnumerable <TResult> > > MapAll <T, TResult>(
            this Task <Outcome <IEnumerable <T> > > asyncOutcome,
            Func <T, TResult> fn)
        {
            return(await Utility.Try(async() => {
                var @this = await asyncOutcome;

                if ([email protected])
                {
                    Outcome <IEnumerable <TResult> >
                    .Reject(@this.FailureOrThrow());
                }

                var source = @this.ResultOrDefault(Enumerable.Empty <T>());
                int count = source is ICollection c ? c.Count : 5;
                var dest = new List <TResult>(count);

                foreach (var item in source)
                {
                    dest.Add(fn(item));
                }
                return new Outcome <IEnumerable <TResult> >(dest);
            }));
        }
예제 #22
0
 internal static Outcome <T> ToKnownFailed <T>(Outcome <T> failed)
 {
     Debug.Assert(!failed.IsSuccessful);
     return(Outcome <T> .Reject(new KnownFailure(failed.FailureOrNull())));
 }
예제 #23
0
 public static Outcome <T> Fail <T>(string reason) => Outcome <T> .Reject(reason);
예제 #24
0
 public static Outcome <T> Then <T>(this Outcome <T> @this, bool condition, Func <Failure> fn) =>
 condition? @this.Then(() => Outcome <T> .Reject(fn())) : @this;
예제 #25
0
 public static Task <Outcome <T> > FailAsync <T>(string reason) =>
 Outcome <T> .Reject(reason).ForAsync();
 public static Outcome <Nop> DiscardResult <T>(this Outcome <T> @this)
 => @this.IsSuccessful ? Outcome.Any() : Outcome.Reject(@this.FailureOrThrow());