Beispiel #1
0
        public static IResult <TResult> ChangeModel <T, TResult>(
            this IResult <T> result,
            Func <T, TResult> selector,
            string source)
        {
            const string thisPath = "ResultHelper.ChangeModel<T, TResult>(result, selector, source)";

            if (result == null)
            {
                return(Runtime.ArgNotSetResult <TResult>(thisPath, nameof(result)));
            }
            if (selector == null)
            {
                return(Runtime.ArgNotSetResult <TResult>(thisPath, nameof(selector)));
            }
            if (string.IsNullOrWhiteSpace(source))
            {
                return(Runtime.ArgNotSetResult <TResult>(thisPath, nameof(source)));
            }
            try
            {
                return(Result <TResult> .Success(selector(result.Model), source));
            }
            catch (Exception e)
            {
                var ex = new Exception(e.Message, e);
                return(Result <TResult> .Failed(thisPath, PredefinedResultError.Exception(ex)));
            }
        }
Beispiel #2
0
        public static IResult <IPaged <TResult> > ChangeItems <T, TResult>(
            this IResult <IPaged <T> > sourceResult,
            Func <T, TResult> selector,
            string source)
            where TResult : T
        {
            const string thisPath = "ResultHelper.ChangeItems<T, TResult>(model, selector, source)";

            if (sourceResult == null)
            {
                return(Runtime.ArgNotSetResult <IPaged <TResult> >(thisPath, nameof(sourceResult)));
            }
            if (!sourceResult.Succeeded)
            {
                return(Result <IPaged <TResult> > .Failed(thisPath, sourceResult.Errors.ToArray()));
            }
            if (selector == null)
            {
                return(Runtime.ArgNotSetResult <IPaged <TResult> >(thisPath, nameof(selector)));
            }
            if (string.IsNullOrWhiteSpace(source))
            {
                return(Runtime.ArgNotSetResult <IPaged <TResult> >(thisPath, nameof(source)));
            }
            var paged = sourceResult.Model;

            try
            {
                return(Result <IPaged <TResult> > .Success(
                           new Paged <TResult>(
                               paged.Items.Select(selector),
                               ObjectDictionaryHelpers.GetPublicPropertyNames <TResult>(),
                               paged.Keys,
                               paged.TotalItemsCount,
                               paged.PageIndex,
                               paged.PageSize,
                               paged.IndexFrom,
                               true),
                           source));
            }
            catch (Exception e)
            {
                var ex = new Exception(e.Message, e);
                return(Result <IPaged <TResult> > .Failed(thisPath, PredefinedResultError.Exception(ex)));
            }
        }
Beispiel #3
0
        public static IResult <T> ArgNotSetResult <T>(this string source, string path, string argName)
        {
            const string thisPath = "ResultHelper.ArgNotSetResult<T>(source, path, argName)";

            if (string.IsNullOrWhiteSpace(source))
            {
                return(Result <T> .Failed(Runtime, PredefinedResultError.ArgNotSet(thisPath, nameof(source))));
            }

            if (string.IsNullOrWhiteSpace(path))
            {
                return(Result <T> .Failed(Runtime, PredefinedResultError.ArgNotSet(thisPath, nameof(path))));
            }

            if (string.IsNullOrWhiteSpace(argName))
            {
                return(Result <T> .Failed(Runtime, PredefinedResultError.ArgNotSet(thisPath, nameof(argName))));
            }

            return(Result <T> .Failed(source, PredefinedResultError.ArgNotSet(path, argName)));
        }