如何使用自动映射器添加两个以上的深度嵌套求和方法?

时间:2019-04-13 22:51:32

标签: asp.net-core entity-framework-core automapper

我正在使用.NET Core 2.2和EF Core编写API,该项目包含一个敏捷项目经理。我正在尝试从数据库实体映射到dto。

问题是,例如,要计算某项任务的预算指标,我需要知道其子任务的预算指标。

config.CreateMap<Task, TaskWithIndicators>()
                .ForMember(dest => dest.Indicators.Completion,
                    opt => opt.MapFrom(src => src.SubTasks.Sum(st => st.Progress)))
                .ForMember(dest => dest.Indicators.UsedBudget,
                    opt => opt.MapFrom(src => src.SubTasks.Sum(st => st.InvestedCash)))
                .ForMember(dest => dest.Indicators.RemainingDays,
                    opt => opt.MapFrom(src => src.SubTasks.Sum(st => st
                        .EstimatedStartDate.AddDays(st.EstimatedDuration)
                        .Subtract(DateTimeOffset.Now)
                        .TotalDays)));

因此,对于活动我必须做同样的事情:

config.CreateMap<Activity, ActivityWithIndicators>()
                .ForMember(dest => dest.Indicators.Completion,
                    opt => opt.MapFrom(src =>
                        src.Tasks.Sum(t => t.SubTasks.Sum(st =>
                            st.Progress))))
                .ForMember(dest => dest.Indicators.UsedBudget,
                    opt => opt.MapFrom(src =>
                        src.Tasks.Sum(t => t.SubTasks.Sum(st => st.InvestedCash))))
                .ForMember(dest => dest.Indicators.RemainingDays,
                    opt => opt.MapFrom(src =>
                        src.Tasks.Sum(t =>
                            t.SubTasks.Sum(st =>
                                st.EstimatedStartDate
                                    .AddDays(t.EstimatedDuration)
                                    .Subtract(DateTimeOffset.Now)
                                    .TotalDays))));

然后为了交付,我有这个:

config.CreateMap<Deliverable, DeliverableWithIndicators>()
                .ForMember(dest => dest.Indicators.Completion,
                    opt => opt.MapFrom(src =>
                        src.Activities.Sum(a => a.Tasks.Sum(t =>
                            t.SubTasks.Sum(st =>
                                st.Progress)))))
                .ForMember(dest => dest.Indicators.UsedBudget,
                    opt => opt.MapFrom(src =>
                        src.Activities.Sum(a =>
                            a.Tasks.Sum(t =>
                                t.SubTasks.Sum(st =>
                                    st.InvestedCash)))))
                .ForMember(dest => dest.Indicators.RemainingDays,
                    opt => opt.MapFrom(src =>
                        src.Activities.Sum(a =>
                            a.Tasks.Sum(t =>
                                t.SubTasks.Sum(st =>
                                    st.EstimatedStartDate
                                        .AddDays(t.EstimatedDuration)
                                        .Subtract(DateTimeOffset.Now)
                                        .TotalDays)))));

那么,这是添加嵌套求和方法的最佳方法吗?

这是层次结构:

  1. 项目(计算指标)
  2. -可交付成果(计算指标)
  3. ---活动(计算指标)
  4. ----任务(计算指标)
  5. -----子任务(实际指标)
  6. ------子任务依赖性(实际指标)

谢谢

0 个答案:

没有答案