Esempio n. 1
0
        /// <summary>
        /// Tries to merge values.
        /// </summary>
        /// <typeparam name="T">The type of the value to merge.</typeparam>
        /// <param name="first">First value.</param>
        /// <param name="second">Second value.</param>
        /// <param name="valueSelector">Value selector.</param>
        /// <returns>The result of the merge attempt.</returns>
        private static Result <T?> TryMergeValue <T>(
            SongDirectorDataModel first,
            SongDirectorDataModel second,
            Func <SongDirectorDataModel, T?> valueSelector)
        {
            T?firstValue  = valueSelector(first);
            T?secondValue = valueSelector(second);

            if (firstValue == null && secondValue == null)
            {
                return(Result.Ok <T?>(default));
Esempio n. 2
0
        /// <summary>
        /// Tries to merge two data models.
        /// </summary>
        /// <param name="first">First data model.</param>
        /// <param name="second">Second data model.</param>
        /// <returns>The result of the attempt.</returns>
        private static Result <SongDirectorDataModel> TryMerge(
            SongDirectorDataModel first, SongDirectorDataModel second)
        {
            // Return a failure result if the ids of the two data models can't be merged
            if (first.Id != second.Id)
            {
                return(Result.Fail("Song director data models with different ids can't be merged."));
            }

            // Merge values
            Result <string?> mergeFullNameResult = TryMergeValue(first, second, m => m.FullName);

            Result <string?> mergeEmailAddressResult = TryMergeValue(
                first, second, m => m.EmailAddress);

            Result <string?> mergePhoneNumberResult = TryMergeValue(
                first, second, m => m.PhoneNumber);

            Result <string?> mergeRankNameResult = TryMergeValue(first, second, m => m.RankName);

            Result <bool?> mergeIsActiveResult = TryMergeValue(first, second, m => m.IsActive);

            // Validate merges
            Result mergeResult = Result.Merge(
                mergeEmailAddressResult,
                mergeFullNameResult,
                mergeIsActiveResult,
                mergePhoneNumberResult,
                mergeRankNameResult);

            // Return a failure result if any merges failed
            if (mergeResult.IsFailed)
            {
                return(mergeResult);
            }

            // Return a success result if the merges succeeded
            return(Result.Ok(new SongDirectorDataModel(
                                 first.Id,
                                 mergeFullNameResult.Value,
                                 mergeEmailAddressResult.Value,
                                 mergePhoneNumberResult.Value,
                                 mergeRankNameResult.Value,
                                 mergeIsActiveResult.Value)));
        }
Esempio n. 3
0
        /// <summary>
        /// Tries to merge song director data models.
        /// </summary>
        /// <param name="songDirectorDataModels">The song director data models to merge.</param>
        /// <returns>The result of the merge attempt.</returns>
        public static Result <SongDirectorDataModel> TryMerge(
            IEnumerable <SongDirectorDataModel> songDirectorDataModels)
        {
            SongDirectorDataModel[] dataModelArray = songDirectorDataModels.ToArray();
            SongDirectorDataModel   current        = dataModelArray[0];

            for (int i = 1; i < dataModelArray.Length; i++)
            {
                SongDirectorDataModel          next        = dataModelArray[i];
                Result <SongDirectorDataModel> mergeResult = TryMerge(current, next);

                if (mergeResult.IsFailed)
                {
                    return(mergeResult);
                }

                current = mergeResult.Value;
            }

            return(Result.Ok(current));
        }