Пример #1
0
        public static Result <domain.Day> create_day
            (IEnumerable <DataAttribute> attributes)
        {
            var errors  = new List <ResultError>();
            var summary = string.Empty;

            time.Date date  = null;
            var       diary = Enumerable.Empty <domain.DiaryEntry>();

            var create_day = (Func <domain.Day>)(() => new domain.Day(
                                                     summary
                                                     , date
                                                     , diary
                                                     ));

            attributes
            .value_for(
                "summary"
                , error: () => new SummarySerialisationError()
                )
            .match(
                success: value => summary = value
                , error: errs => errors.AddRange(errs)
                )
            ;

            attributes
            .collection_for(
                "date"
                , error: () => new DateSerialisationError()
                )
            .bind(collection => time.serialisation.Date.create_date(collection))
            .match(
                success: d => date = d
                , error: errs => errors.AddRange(errs)
                );

            attributes
            .collection_for(
                "diary"
                , error: () => new DiarySerialisationError()
                )
            // I. This does not handled failures in create_diary entry this has multiple otions and will need to be dealt with.
            .bind(EEa =>



                  EEa
                  .Select((Ea, i) => {
                return
                (DiaryEntry.create_diary_entry(



                     Ea
                     .match(s => default(IEnumerable <DataAttribute>),
                            ca => ca)



                     ));
            })
                  .Aggregate(
                      new AggregatedDiaryEntryResults()
                      , (acc, Re) => acc.add_result(Re)
                      )
                  .fmap(acc => acc.result())


                  )
            .match(
                success: diary_entries => diary = diary_entries
                , error: errs => errors.AddRange(errs)
                );

            /*
             *
             *      public static TSource Aggregate<TSource>(
             *            this IEnumerable<TSource> source
             *           ,Func<TSource, TSource, TSource> func
             *      );
             *      public static TAccumulate Aggregate<TSource, TAccumulate>(
             *           this IEnumerable<TSource> source
             *          ,TAccumulate seed
             *          ,Func<TAccumulate, TSource, TAccumulate> func
             *      );
             *      public static TResult Aggregate<TSource, TAccumulate, TResult>(
             *           this IEnumerable<TSource> source
             *          ,TAccumulate seed
             *          ,Func<TAccumulate, TSource, TAccumulate> func
             *          ,Func<TAccumulate, TResult> resultSelector
             *      );
             *
             */

            return(!errors.Any()
                 ? Result <domain.Day> .success(create_day())
                 : Result <domain.Day> .error(errors)
                   );
        }
Пример #2
0
 public static IEnumerable <DataAttribute> create_memento
     (domain.Day day)
 {
     return(new DataAttribute[] {
         DataAttribute.create_value("summary", day.summary),
         DataAttribute.create_collection("date", time.serialisation.Date.create_attribute_memento(day.date)),
         DataAttribute.create_collection("diary", day.diary.Select((de, i) => DataAttribute.create_collection(i.ToString(), DiaryEntry.create_memento(de))))
     });
 }