AutoMapper从列表到按日期分组的集合

时间:2014-12-16 01:08:41

标签: c# automapper

我有一个父子源,我希望使用AutoMapper将其映射到父日期子目的地(例如,目的地使用SortedSet集合按日期对子数据进行分组。

目的是您可以使用SortedSet获取该日期的所有“孩子”。

我目前正在使用一个IValueResolver循环来源的Children并单独映射,然后将它们添加到SortedSet。从下面的代码可以看出,如果日期不存在,则需要在SortedSet集合中创建一个新项。

是否有更好的配置映射方法?

我知道AutoMapper可以映射列表(而不仅仅是单个列表项),但由于需要按日期对目标项进行分组,我不确定是否有更好的解决方案来解决我目前的问题使用

来源类型

public class ViewModel
{
    public ViewModel()
    {
        Days = new SortedSet<DayViewModel>();
    }

    public SortedSet<DayViewModel> Days { get; set; }
}

public class ModelChild 
{
    public DateTime Started { get; set; }
    public string Description { get; set; }
}

public class ModelParent
{
    public string Name;
    public IList<ModelChild> Children = new List<ModelChild>();
}

目的地类型

public class ChildViewModel 
{
    public DateTime Started { get; set; }
    public string Description { get; set; }
}

public class DayViewModel
{
    public DateTime Date { get; set; }

    public DayViewModel()
    {
        Children = new List<ChildViewModel>();
    }

    public IList<ChildViewModel> Children { get; set; }
}

public class DayComparer : IComparer<DayViewModel>
{
    public int Compare(DayViewModel x, DayViewModel y)
    {
        return x.Date.CompareTo(y.Date);
    }
}

public class ViewModel
{
    public ViewModel()
    {
        Days = new SortedSet<DayViewModel>(new DayComparer());
    }

    public SortedSet<DayViewModel> Days { get; set; }
}

IValueResolver

public class DaysResolver : IValueResolver
{
    public ResolutionResult Resolve(ResolutionResult source)
    {
        var person = (ModelParent)source.Context.SourceValue;
        var vm = (ViewModel)source.Context.DestinationValue;

        foreach (var modelChild in person.Children)
        {
            var file = Mapper.Map<ChildViewModel>(modelChild);
            var day = GetDay(vm, file.Started);
            day.Children.Add(file);
        }
        return source.Ignore();
    }

    private static DayViewModel GetDay(ViewModel model, DateTime key)
    {
        var dayViewModel = model.Days.FirstOrDefault(d => d.Date == key);

        if (dayViewModel == null)
        {
            dayViewModel = new DayViewModel
            {
                Date = key
            };

            model.Days.Add(dayViewModel);
        }
        return dayViewModel;
    }
}

映射

private static void Main(string[] args)
{
    Mapper.CreateMap<ModelChild, ChildViewModel>();

    Mapper.CreateMap<ModelParent, ViewModel>()
        .ForMember(d => d.Days, x => x.ResolveUsing<DaysResolver>());

    Mapper.AssertConfigurationIsValid();
}

1 个答案:

答案 0 :(得分:2)

我提出的一个选项是将DayViewModel查找/创建逻辑移动到ContructUsing方法中,以便在ModelChild和DayViewModel之间进行映射。例如

Mapper.CreateMap<ModelChild, DayViewModel>()
    .ConstructUsing(context =>
    {
        var key = ((ModelChild) context.SourceValue).Started;
        var daysCollection = (SortedSet<DayViewModel>) context.Parent.DestinationValue;

        var dayViewModel = daysCollection.FirstOrDefault(d => d.Date == key);

        if (dayViewModel == null)
        {
            dayViewModel = new DayViewModel
            {
                Date = key
            };

            daysCollection.Add(dayViewModel);
        }

        dayViewModel.Children.Add(Mapper.Map<ChildViewModel>(context.SourceValue));
        return dayViewModel;
    })
    .ForAllMembers(d => d.Ignore());

这仍然明确地再次调用Map,但我对其他建议持开放态度