1

我正在尝试将嵌套映射与 AutoMapper 一起使用,使用映射器的实例版本——但它似乎不起作用。

这是我正在使用的两个模型:

public class User
{
    [Key]
    public string Email { get; set; }
    public string Hash { get; set; }
    public string Salt { get; set; }
    public string Name { get; set; }

    public virtual ICollection<TaskTime> TaskTimes { get; set; }
    public virtual ICollection<Role> Roles { get; set; }
    public virtual ICollection<HistoricalEstimation> HistoricalEstimations { get; set; }
}

public class TaskTime
{
    public int Id { get; set; }
    public User User { get; set; }
    public Task Task { get; set; }
    public TimeSpan Duration { get; set; }
    public DateTime Date { get; set; }
}

我正在使用这段代码来映射它们:

public static class UserViewConfiguration
{
    private static ConfigurationStore configuration;
    public static MappingEngine Engine { get; private set; }

    static UserViewConfiguration()
    {
        configuration = new ConfigurationStore(new TypeMapFactory(), MapperRegistry.AllMappers());
        Engine = new MappingEngine(configuration);

        configuration.CreateMap<User, UserFull>();
        configuration.CreateMap<TaskTime, UserTaskTime>();
        /* snip... */

        configuration.AssertConfigurationIsValid();
    }
}

在这些视图模型上:

public class UserFull
{
    public string Email { get; set; }
    public string Name { get; set; }

    public virtual ICollection<TaskTime> TaskTimes { get; set; }
}

public class UserTaskTime
{
    public int Id { get; set; }
    public Task Task { get; set; }
    public TimeSpan Duration { get; set; }
    public DateTime Date { get; set; }
}

问题是, aUser包含 a TaskTime,而 aTaskTime包含 a User。这个循环需要存在,因为有几种不同的方式可以访问每个项目,具体取决于您最初要求的对象(这就是我使用 AutoMapper 实例版本的原因)。我正在序列化这些以通过 ASP.NET MVC API 应用程序发送它们,因此周期是一个大问题。

我已经阅读了使用 AutoMapper 使用嵌套映射的示例,并且据我所知,我做得对。但是使用上面的映射,我在Userpath上得到一个自引用循环错误[0].TaskTimes[0].User.TaskTimes。如果我注释掉 的TaskTimes属性UserFull,我不会收到错误,所以我知道User->UserFull映射正在工作——但由于某种原因,TaskTime->UserTaskTime它不起作用。

我能做些什么?

编辑:我的映射是这样的:

// GET api/Users
public IEnumerable<UserFull> GetUsers()
{
    //var query = SelectUsers(db.Users.ToList());
    return UserViewConfiguration.Engine.Map<IEnumerable<UserFull>>(db.Users);
}
4

1 回答 1

3

假设您有一个错字,并且 UserFull 应该有一个 UserTaskTime 的集合而不是 TaskTime 的集合,这些快速测试工作:

[TestFixture]
public class MappingTests2
{
    [Test]
    public void AutoMapper_Configuration_IsValid()
    {
        UserViewConfiguration.Configure();
        Mapper.AssertConfigurationIsValid();
    }

    [Test]
    public void AutoMapper_MapsAsExpected()
    {
        UserViewConfiguration.Configure();
        Mapper.AssertConfigurationIsValid();

        var user = new User
            {
                Email = "user1@email.com",
                Hash = "1234Hash",
                Name = "user1",
                Salt = "1234Salt",
                TaskTimes =
                    new Collection<TaskTime>
                        {
                            new TaskTime
                                { Date = new DateTime(2012, 11, 01), Duration = new TimeSpan(0, 20, 1), Id = 1 },
                            new TaskTime
                                { Date = new DateTime(2012, 11, 02), Duration = new TimeSpan(0, 20, 2), Id = 2 }
                        }
            };

        foreach (var taskTime in user.TaskTimes)
        {
            taskTime.User = user;
        }

        var userView = Mapper.Map<User, UserFull>(user);

        Assert.That(userView, Is.Not.Null);
        Assert.That(userView.Email, Is.EqualTo("user1@email.com"));
        Assert.That(userView.Name, Is.EqualTo("user1"));
        Assert.That(userView.TaskTimes, Is.Not.Null);
        Assert.That(userView.TaskTimes.Count, Is.EqualTo(2));
        var tt = userView.TaskTimes.FirstOrDefault(x => x.Id == 1);
        Assert.That(tt, Is.Not.Null);
        Assert.That(tt.Id, Is.EqualTo(1));
        Assert.That(tt.Date, Is.EqualTo(new DateTime(2012, 11, 01)));
        Assert.That(tt.Duration, Is.EqualTo(new TimeSpan(0, 20, 1)));
    }
}

请注意,对于上述内容,我将映射转换回使用静态方法:

public static void Configure()
{
    Mapper.CreateMap<User, UserFull>();
    Mapper.CreateMap<TaskTime, UserTaskTime>();

    Mapper.AssertConfigurationIsValid();
}

如果您确实打算将其用作TaskTime,我会再看一下。

于 2012-11-12T02:19:10.037 回答