Пример #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)));
            }
        }
Пример #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)));
            }
        }