예제 #1
0
        public static async Task <MethodResult <T> > OperateWhenAsync <T>(
            this Task <T> @this,
            bool predicate,
            Func <T, MethodResult <T> > function)
        {
            var t = await @this;

            return(predicate ? function(t) : MethodResult <T> .Ok(t));
        }
예제 #2
0
 public static async Task <MethodResult> OperateWhenAsync(
     bool predicate,
     Func <Task <MethodResult> > function)
 {
     if (predicate)
     {
         return(await function());
     }
     return(MethodResult.Ok());
 }
 public static MethodResult Try(Action action)
 {
     try {
         action();
         return(MethodResult.Ok());
     }
     catch (Exception e) {
         return(MethodResult.Fail(new ExceptionError(e, message: e.Message)));
     }
 }
 public static MethodResult <TResult> Try <TResult>(
     Func <TResult> function)
 {
     try {
         return(MethodResult <TResult> .Ok(function()));
     }
     catch (Exception e) {
         return(MethodResult <TResult> .Fail(new ExceptionError(e, message : e.Message)));
     }
 }
예제 #5
0
 public static MethodResult OperateWhen(
     bool predicate,
     Action action)
 {
     if (predicate)
     {
         action();
     }
     return(MethodResult.Ok());
 }
예제 #6
0
 public static MethodResult <T> OperateWhen <T>(
     this T @this,
     bool predicate,
     Action <T> action)
 {
     if (predicate)
     {
         action(@this);
     }
     return(MethodResult <T> .Ok(@this));
 }
예제 #7
0
 public static async Task <MethodResult <T> > OperateWhenAsync <T>(
     this T @this,
     bool predicate,
     Func <T, Task <MethodResult <T> > > function)
 {
     if (predicate)
     {
         return(await function(@this));
     }
     return(MethodResult <T> .Ok(@this));
 }
예제 #8
0
 public static async Task <MethodResult <TSource> > TeeOperateWhenAsync <TSource, TResult>(
     this TSource @this,
     bool predicate,
     Func <TSource, Task <TResult> > function)
 {
     if (predicate)
     {
         await function(@this);
     }
     return(MethodResult <TSource> .Ok(@this));
 }
 public static MethodResult Try <T>(
     this T @this,
     Action <T> action)
 {
     try {
         action(@this);
         return(MethodResult.Ok());
     }
     catch (Exception e) {
         return(MethodResult.Fail(new ExceptionError(e)));
     }
 }
예제 #10
0
        public static async Task <MethodResult <T> > OperateWhenAsync <T>(
            this Task <T> @this,
            Func <T, bool> predicate,
            Func <Task <MethodResult <T> > > function)
        {
            var t = await @this;

            if (predicate(t))
            {
                return(await function());
            }
            return(MethodResult <T> .Ok(t));
        }
        public static async Task <MethodResult <T> > TryAsync <T>(
            Func <Task <T> > onSuccessFunction
            )
        {
            try {
                var result = await onSuccessFunction();

                return(MethodResult <T> .Ok(result));
            }
            catch (Exception e) {
                return(MethodResult <T> .Fail(new ExceptionError(e, e.Message)));
            }
        }
        public static async Task <MethodResult <TResult> > TryMapAsync <TSource, TResult>(
            this TSource _,
            Func <Task <TResult> > onSuccessFunction
            )
        {
            try {
                var result = await onSuccessFunction();

                return(MethodResult <TResult> .Ok(result));
            }
            catch (Exception e) {
                return(MethodResult <TResult> .Fail(new ExceptionError(e, e.Message)));
            }
        }
        public static async Task <MethodResult> TryAsync <T>(
            this T @this,
            Func <T, Task> onSuccessFunction
            )
        {
            try {
                await onSuccessFunction(@this);

                return(MethodResult.Ok());
            }
            catch (Exception e) {
                return(MethodResult.Fail(new ExceptionError(e, e.Message)));
            }
        }
 public static async Task <MethodResult> TryAsync(
     this Task @this,
     Action onSuccessFunction
     )
 {
     try {
         await @this;
         onSuccessFunction();
         return(MethodResult.Ok());
     }
     catch (Exception e) {
         return(MethodResult.Fail(new ExceptionError(e, e.Message)));
     }
 }
 public static async Task <MethodResult> TryAsync <TSource>(
     this Task <TSource> @this,
     Action <TSource> onSuccessFunction
     )
 {
     try {
         var source = await @this;
         onSuccessFunction(source);
         return(MethodResult.Ok());
     }
     catch (Exception e) {
         return(MethodResult.Fail(new ExceptionError(e, e.Message)));
     }
 }
예제 #16
0
        public static MethodResult ForEachUntilIsSuccess <TSource, TResult>(
            this IEnumerable <TSource> @this,
            Func <TSource, MethodResult <TResult> > function)
        {
            foreach (var item in @this)
            {
                var result = function(item);
                if (!result.IsSuccess)
                {
                    return(MethodResult.Fail(result.Detail));
                }
            }

            return(MethodResult.Ok());
        }
예제 #17
0
        public static MethodResult ForEachUntilIsSuccess <T>(
            this IEnumerable <T> @this,
            Func <T, MethodResult> function)
        {
            foreach (var item in @this)
            {
                var result = function(item);
                if (!result.IsSuccess)
                {
                    return(result);
                }
            }

            return(MethodResult.Ok());
        }
 public static async Task <MethodResult> TryMapAsync <TSource>(
     this Task <TSource> @this,
     Action <TSource> onSuccessFunction,
     Func <MethodResult> onFailFunction
     )
 {
     try {
         var source = await @this;
         onSuccessFunction(source);
         return(MethodResult.Ok());
     }
     catch (Exception) {
         return(onFailFunction());
     }
 }
 public static async Task <MethodResult> TryMapAsync <TSource>(
     this Task <TSource> @this,
     Action onSuccessFunction,
     Func <ResultDetail, MethodResult> onFailFunction
     )
 {
     try {
         await @this;
         onSuccessFunction();
         return(MethodResult.Ok());
     }
     catch (Exception e) {
         return(onFailFunction(new ExceptionError(e, e.Message)));
     }
 }
 public static async Task <MethodResult <TResult> > TryMapAsync <TResult>(
     this Task @this,
     Func <TResult> onSuccessFunction,
     Func <ResultDetail, MethodResult <TResult> > onFailFunction
     )
 {
     try {
         await @this;
         var   result = onSuccessFunction();
         return(MethodResult <TResult> .Ok(result));
     }
     catch (Exception e) {
         return(onFailFunction(new ExceptionError(e, e.Message)));
     }
 }
 public static async Task <MethodResult <TResult> > TryMapAsync <TResult>(
     this Task @this,
     Func <TResult> onSuccessFunction,
     Func <MethodResult <TResult> > onFailFunction
     )
 {
     try {
         await @this;
         var   result = onSuccessFunction();
         return(MethodResult <TResult> .Ok(result));
     }
     catch (Exception) {
         return(onFailFunction());
     }
 }
 public static async Task <MethodResult> TryMapAsync(
     this Task @this,
     Action onSuccessFunction,
     Func <MethodResult> onFailFunction
     )
 {
     try {
         await @this;
         onSuccessFunction();
         return(MethodResult.Ok());
     }
     catch (Exception) {
         return(onFailFunction());
     }
 }
예제 #23
0
        public static Task <MethodResult> ForEachUntilIsSuccessAsync <TSource, TResult>(
            this Task <IEnumerable <TSource> > @this,
            Func <TSource, Task <MethodResult <TResult> > > function) =>
        TryExtensions.TryAsync(() => @this)
        .OnSuccessAsync(async items => {
            foreach (var item in items)
            {
                var result = await function(item);
                if (!result.IsSuccess)
                {
                    return(MethodResult.Fail(result.Detail));
                }
            }

            return(MethodResult.Ok());
        });
예제 #24
0
        public static async Task <MethodResult> ForEachUntilIsSuccessAsync <T>(
            this IEnumerable <T> @this,
            Func <T, Task <MethodResult> > function)
        {
            foreach (var item in @this)
            {
                var result = await function(item);

                if (!result.IsSuccess)
                {
                    return(result);
                }
            }

            return(MethodResult.Ok());
        }
예제 #25
0
 private static MethodResult ValidateInputs(string fileName, params string[] validTypes)
 {
     if (string.IsNullOrEmpty(fileName))
     {
         return(MethodResult.Fail(new BadRequestError(message: $"{nameof(fileName)} is null or empty.")));
     }
     if (!File.Exists(fileName))
     {
         return(MethodResult.Fail(new NotFoundError(message: $"{nameof(fileName)} is not exist.")));
     }
     if (validTypes.IsNullOrEmpty())
     {
         return(MethodResult.Fail(new BadRequestError(message: $"{nameof(validTypes)} is null or empty.")));
     }
     return(MethodResult.Ok());
 }
예제 #26
0
        public static MethodResult IsNotNullOrEmpty(
            this IEnumerable? @this,
            ErrorDetail?errorDetail       = null,
            bool showDefaultMessageToUser = true)
        {
            var error = errorDetail ?? new ErrorDetail(StatusCodes.Status400BadRequest,
                                                       title: "IsNullOrEmptyError",
                                                       message: "object is not null or empty.",
                                                       showDefaultMessageToUser: showDefaultMessageToUser);

            if (@this is null || @this.IsNullOrEmpty())
            {
                return(MethodResult.Fail(error));
            }
            return(MethodResult.Ok());
        }
예제 #27
0
        public static Task <MethodResult> ForEachUntilIsSuccessAsync <T>(
            this Task <IEnumerable <T> > @this,
            Func <T, Task> action) => TryExtensions.TryAsync(() => @this)
        .OnSuccessAsync(async items => {
            var list = items.ToList();
            foreach (var item in list)
            {
                try {
                    await action(item);
                }
                catch (Exception e) {
                    return(MethodResult.Fail(new ExceptionError(e,
                                                                moreDetails: new { thisObj = list, targetItem = item })));
                }
            }

            return(MethodResult.Ok());
        });
예제 #28
0
        public static MethodResult ForEachUntilIsSuccess <T>(
            this IEnumerable <T> @this,
            Action <T> action)
        {
            var list = @this.ToList();

            foreach (var item in list)
            {
                try {
                    action(item);
                }
                catch (Exception e) {
                    return(MethodResult.Fail(new ExceptionError(e,
                                                                moreDetails: new { thisObj = list, targetItem = item })));
                }
            }

            return(MethodResult.Ok());
        }
예제 #29
0
        public static async Task <MethodResult <List <TResult> > > SelectResultsAsync <TSource, TResult>(
            this Task <IEnumerable <TSource> > @this,
            Func <TSource, MethodResult <TResult> > function)
        {
            var thisList       = (await @this).ToList();
            var selectedResult = new List <TResult>(thisList.Count);

            foreach (var item in thisList)
            {
                var result = function(item);
                if (!result.IsSuccess)
                {
                    result.Detail.AddDetail(new { thisObj = @this, targetItem = item });
                    return(MethodResult <List <TResult> > .Fail(result.Detail));
                }

                selectedResult.Add(result.Value);
            }

            return(MethodResult <List <TResult> > .Ok(selectedResult));
        }
예제 #30
0
        public static async Task <MethodResult> ForEachUntilIsSuccessAsync <TSource, TResult>(
            this IEnumerable <MethodResult <TSource> > @this,
            Func <TSource, Task <MethodResult <TResult> > > function)
        {
            foreach (var item in @this)
            {
                if (!item.IsSuccess)
                {
                    return(item.MapMethodResult());
                }

                var result = await function(item.Value);

                if (!result.IsSuccess)
                {
                    return(MethodResult.Fail(result.Detail));
                }
            }

            return(MethodResult.Ok());
        }