自动映射:内置集合列表的嵌套映射

时间:2014-07-25 18:15:55

标签: automapper

我有一组对象需要填充目标类中的集合。我尝试了一些东西,但完全缺少了一些东西。任何帮助都非常感谢。 这是源对象:

public class SourceClass
{
    public Sessions SourceSessions { get; set; }
}
public class Sessions
{
    public List<Session> SessionCollection { get; set; }
}

public class Session
{
    public DateTime SessionInfo { get; set; }
}

目标对象是:

 public class DestinationClass
{
    public List<DateTime> Sessions;
}

我需要使用AutoMapper填充DestinationClass。 我看到唯一可能的映射将发生在Session和DestinationClass之间,其中实际映射需要发生,但是,DestinationClass属性是Session内部的集合,但是为了获得集合,我将其映射到Sessions对象而不是并尝试编写这样的自定义解析器:

public static class AutoMapperConfig
{
    public static void RegisterMappings()
    {
        Mapper.CreateMap<Sessions, DestinationClass>();
    }
}
public class CustomResolver : ValueResolver<Sessions, DestinationClass>
{
    protected override DestinationClass ResolveCore(Sessions source)
    {
        DestinationClass dest = new DestinationClass();
        List<DateTime> lstDates = new List<DateTime>();
        foreach (Session session in source.SessionCollection)
            lstDates.Add(session.SessionInfo);

        dest.Sessions = lstDates;
        return dest;
    }
}

然后像下面那样执行它是行不通的。无法理解它的错误在哪里和哪里。

private void button1_Click(object sender, EventArgs e)
    {
        Session session1 = new Session();
        session1.SessionInfo = DateTime.Now;

        Session session2 = new Session();
        session2.SessionInfo = DateTime.Now;

        Sessions sessions = new Sessions();

        List<Session> lstSessions = new List<Session>();

        lstSessions.Add(session1);
        lstSessions.Add(session2);

        sessions.SessionCollection = lstSessions;

        SourceClass src = new SourceClass();
        src.SourceSessions = sessions;

        DestinationClass dest = new DestinationClass();

        Sessions sessionsCopy = new Sessions();
        sessionsCopy = src.SourceSessions;

        Mapper.Map(sessionsCopy,dest);

        MessageBox.Show(dest.Sessions[0].ToString());

    }

1 个答案:

答案 0 :(得分:1)

有两种可行的方法。第一个是简单地使用LINQ。如果srcSourceClass的实例:

var dest = new DestinationClass
{
    Sessions = src.SourceSessions.SessionCollection.Select(s => s.SessionInfo).ToList()
};

如果你想进入自动传输路径,你可以告诉Automapper在映射对象时使用上面的表达式:

Mapper.CreateMap<SourceClass, DestinationClass>()
    .ForMember(d => d.Sessions, o => o.MapFrom(sess => sess.SourceSessions.SessionCollection.Select(s => s.SessionInfo)));

<强>更新

这是验证映射的单元测试:

var src = new SourceClass
{
    SourceSessions = new Sessions
    {
        SessionCollection = new List<Session>
        {
            new Session
            {
                SessionInfo = new DateTime(2012, 1, 2),
            },
            new Session
            {
                SessionInfo = new DateTime(2014, 5, 6)
            }
        }
    }
};

Mapper.CreateMap<SourceClass, DestinationClass>()
    .ForMember(d => d.Sessions, o => o.MapFrom(sess => sess.SourceSessions.SessionCollection.Select(s => s.SessionInfo)));
Mapper.AssertConfigurationIsValid();

var dest = Mapper.Map<SourceClass, DestinationClass>(src);
Assert.AreEqual(2, dest.Sessions.Count);
Assert.IsTrue(dest.Sessions.Contains(new DateTime(2012, 1, 2)));
Assert.IsTrue(dest.Sessions.Contains(new DateTime(2014, 5, 6)));