Nested mappings with instance version of AutoMappe

2019-07-30 01:50发布

问题:

I'm trying to use nested mappings with AutoMapper, using the instance version of the mapper -- but it doesn't seem to be working.

Here's two models I'm using:

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; }
}

And I'm using this code to map them:

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();
    }
}

onto these viewmodels:

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; }
}

The problem is that, a User contains a TaskTime, and a TaskTime contains a User. This cycle needs to be present since there are several different ways of getting to each item, depending on which object you originally asked for (which is why I'm using the instance version of AutoMapper). I'm serializing these to send them through an ASP.NET MVC API app, so the cycle is a big problem.

I've read this example of using nested mappings with AutoMapper, and from what I can tell I'm doing it right. But with the mappings above, I'm getting a self-referencing loop error on User for the path [0].TaskTimes[0].User.TaskTimes. If I comment out the TaskTimes property of UserFull, I don't get an error, so I know the User->UserFull mapping is working -- but for some reason the TaskTime->UserTaskTime is not working.

What can I do?

Edit: I'm mapping like this:

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

回答1:

Assuming that you have a typo, and that UserFull should have a collection of UserTaskTime's rather than a collection of TaskTime's, these quick tests work:

[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)));
    }
}

Note that for the above I converted the mapping back to using the static methods:

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

    Mapper.AssertConfigurationIsValid();
}

I'll take another look if you did intend it to be TaskTime.