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