Exemplo n.º 1
0
        public static Result <TAValue, string> FindAndTransformResultValue <TResult, TRValue, TAValue>(this IEnumerable <ResultPair> evalResults,
                                                                                                       Func <TargetItem, bool> filterTargetFunc,
                                                                                                       Func <ValuesItem, bool> selectValuesFunc, Func <TRValue, Result <TAValue, string> > selectResultFunc)
            where TResult : class, ResultItem
            where TRValue : class, ValuesItem
        {
            ResultPack findResult = evalResults.FirstToResultWithValueAndError(filterTargetFunc, ERROR_TEXT_CONTRACT_NOT_FOUND);

            if (findResult.IsFailure)
            {
                return(Result.Fail <TAValue, string>(findResult.Error));
            }
            ResultItem itemResult = findResult.Value;

            TResult typeResult = itemResult as TResult;

            if (typeResult == null)
            {
                return(Result.Fail <TAValue, string>(itemResult.DecoratedError(ERROR_TEXT_RESULTS_CASTING_FAILED)));
            }
            Maybe <TRValue> typeValues = typeResult.ReturnValue <TRValue>(selectValuesFunc);

            if (typeValues.HasNoValue)
            {
                return(Result.Fail <TAValue, string>(typeResult.DecoratedError(ERROR_TEXT_RESULTS_LOOKUP_FAILED)));
            }
            Result <TAValue, string> tranResult = selectResultFunc(typeValues.Value);

            if (tranResult.IsFailure)
            {
                return(Result.Fail <TAValue, string>(typeResult.DecoratedError(ERROR_TEXT_RESULTS_SELECT_FAILED)));
            }
            return(Result.Ok <TAValue, string>(tranResult.Value));
        }
Exemplo n.º 2
0
        public static Result <IEnumerable <TSValue>, string> GetResultValuesInListAndError <TResult, TRValue, TSValue>(this IEnumerable <ResultPair> evalResults,
                                                                                                                       Func <TargetItem, bool> filterTargetFunc, Func <TResult, bool> filterValuesFunc,
                                                                                                                       Func <ValuesItem, bool> selectValuesFunc, Func <TargetItem, TRValue, Result <TSValue, string> > selectResultFunc)
            where TResult : class, ResultItem
            where TRValue : class, ValuesItem
        {
            Func <Result <IEnumerable <TSValue>, string>, ResultPair,
                  Func <TargetItem, bool>, Func <TResult, bool>, Func <ValuesItem, bool>, Func <TargetItem, TRValue, Result <TSValue, string> >,
                  Result <IEnumerable <TSValue>, string> > agrFunc = (bAgr, a, tFilter, vFilter, vSelect, rSelect) => {
                if (bAgr.IsFailure)
                {
                    return(Result.Fail <IEnumerable <TSValue>, string>(bAgr.Error));
                }
                IEnumerable <TSValue>       bAgrList = bAgr.Value;
                Result <ResultItem, string> aResult  = a.Value;
                if (aResult.IsFailure)
                {
                    return(Result.Fail <IEnumerable <TSValue>, string>(aResult.Error));
                }
                TargetItem aParamKey = a.Key;
                if (tFilter(aParamKey) == false)
                {
                    return(Result.Ok <IEnumerable <TSValue>, string>(bAgrList));
                }
                ResultItem aParamRes = aResult.Value;

                TResult aParamVal = aParamRes as TResult;
                if (aParamVal == null)
                {
                    return(Result.Fail <IEnumerable <TSValue>, string>(aParamRes.DecoratedError(ERROR_TEXT_RESULTS_CASTING_FAILED)));
                }
                if (vFilter(aParamVal) == false)
                {
                    return(Result.Ok <IEnumerable <TSValue>, string>(bAgrList));
                }
                Maybe <TRValue> typeValues = aParamVal.ReturnValue <TRValue>(vSelect);
                if (typeValues.HasNoValue)
                {
                    return(Result.Fail <IEnumerable <TSValue>, string>(aParamVal.DecoratedError(ERROR_TEXT_RESULTS_LOOKUP_FAILED)));
                }
                Result <TSValue, string> selResult = rSelect(aParamKey, typeValues.Value);
                if (selResult.IsFailure)
                {
                    return(Result.Fail <IEnumerable <TSValue>, string>(aParamVal.DecoratedError(ERROR_TEXT_RESULTS_SELECT_FAILED)));
                }

                IEnumerable <TSValue> resultList = bAgrList.Merge(selResult.Value);

                return(Result.Ok <IEnumerable <TSValue>, string>(resultList));
            };

            Result <IEnumerable <TSValue>, string> initResult = Result.Ok <IEnumerable <TSValue>, string>(new List <TSValue>());

            return(evalResults.Aggregate(initResult, (agr, x) => agrFunc(agr, x, filterTargetFunc, filterValuesFunc, selectValuesFunc, selectResultFunc)));
        }
Exemplo n.º 3
0
        public static Result <TResult, string> FindTypeResult <TResult>(this IEnumerable <ResultPair> evalResults,
                                                                        Func <TargetItem, bool> filterTargetFunc) where TResult : class, ResultItem
        {
            ResultPack findResult = evalResults.FirstToResultWithValueAndError(filterTargetFunc, ERROR_TEXT_CONTRACT_NOT_FOUND);

            if (findResult.IsFailure)
            {
                return(Result.Fail <TResult, string>(findResult.Error));
            }
            ResultItem itemResult = findResult.Value;

            TResult typeResult = itemResult as TResult;

            if (typeResult == null)
            {
                return(Result.Fail <TResult, string>(itemResult.DecoratedError(ERROR_TEXT_RESULTS_CASTING_FAILED)));
            }
            return(Result.Ok <TResult, string>(typeResult));
        }
Exemplo n.º 4
0
        public static Result <IEnumerable <ResultPair>, string> GetTypedResultsInListAndError <TResult>(this IEnumerable <ResultPair> evalResults,
                                                                                                        Func <TargetItem, bool> filterTargetFunc) where TResult : class, ResultItem
        {
            Func <Result <IEnumerable <ResultPair>, string>, ResultPair,
                  Func <TargetItem, bool>, Result <IEnumerable <ResultPair>, string> > agrFunc = (bAgr, a, tFilter) => {
                if (bAgr.IsFailure)
                {
                    return(Result.Fail <IEnumerable <ResultPair>, string>(bAgr.Error));
                }
                IEnumerable <ResultPair>    bAgrList = bAgr.Value;
                Result <ResultItem, string> aResult  = a.Value;
                if (aResult.IsFailure)
                {
                    return(Result.Fail <IEnumerable <ResultPair>, string>(aResult.Error));
                }
                TargetItem aParamKey = a.Key;
                if (tFilter(aParamKey) == false)
                {
                    return(Result.Ok <IEnumerable <ResultPair>, string>(bAgrList));
                }
                ResultItem itemResult = aResult.Value;

                TResult typeResult = itemResult as TResult;
                if (typeResult == null)
                {
                    return(Result.Fail <IEnumerable <ResultPair>, string>(itemResult.DecoratedError(ERROR_TEXT_RESULTS_CASTING_FAILED)));
                }
                IEnumerable <ResultPair> resultList = bAgrList.Merge(a).OrderBy((x) => (x.Key));

                return(Result.Ok <IEnumerable <ResultPair>, string>(resultList));
            };

            Result <IEnumerable <ResultPair>, string> initResult = Result.Ok <IEnumerable <ResultPair>, string>(new List <ResultPair>());

            return(evalResults.Aggregate(initResult, (agr, x) => agrFunc(agr, x, filterTargetFunc)));
        }