Reputation: 7017
I have some code that pretty much looks like below, see working example at https://dotnetfiddle.net/wuE81t.
public class Program
{
public static void Main()
{
Mapper.CreateMap<Foo, Bar>()
.AfterMap((s, d) => {
var stuff = SomeController.GetStuff(DateTime.Now.Second);
d.Stuff = stuff.Contains(s.Name);
});
var foo = new List<Foo>() {
new Foo() { Name = "joe", Age = 10 },
new Foo() { Name = "jane", Age = 20 },
};
var bar = Mapper.Map<List<Foo>, List<Bar>>(foo);
}
}
public class Foo
{
public string Name { get; set; }
public int Age { get; set; }
}
public class Bar
{
public string Name { get; set; }
public int Age { get; set; }
public bool Stuff { get; set; }
}
public static class SomeController
{
public static List<string> GetStuff(int currentUserId)
{
return new List<string>() { "jane" };
}
}
The problem I have is that GetStuff is called for each of the items in the source list, and it's a quite heavy operation, so I'd like to optimize it by only calling it once. In my actual code, GetStuff uses the currentUserId parameter.
I have currently solved it by moving GetStuff to after Mapper.Map, but since we have quite a few places where this is called, it's much uglier than using AfterMap. There is also a much greater risk that a future developer will forget the required extra call.
public static void Main()
{
Mapper.CreateMap<Foo, Bar>();
var foo = new List<Foo>() {
new Foo() { Name = "joe", Age = 10 },
new Foo() { Name = "jane", Age = 20 },
};
var bar = Mapper.Map<List<Foo>, List<Bar>>(foo);
AddStuff(bar); // Required extra call!
bar.Dump();
}
private static void AddStuff(List<Bar> bar)
{
var stuff = SomeController.GetStuff(DateTime.Now.Second);
foreach(var b in bar)
b.Stuff = stuff.Contains(b.Name);
}
Is there a better solution?
Upvotes: 3
Views: 15366
Reputation: 14274
The problem is that Automapper AfterMap
runs once per mapping. Your mapping configuration is:
Mapper.CreateMap<Foo, Bar>();
So if you attach an AfterMap extension to this mapping it will run it on each mapping between a Foo and a Bar. That is why you are seeing it run more than once.
If you want to run it only once, you should attach it to a List to List mapping configuration and not an item to item configuration. However AutoMapper is not flexible enough to make it easy to use with a List to List configuration.
One way to make it work is to use a ConvertUsing
method and specify explicitly the mapping to use on list items and invoke the after mapping stuff there:
Mapper.CreateMap<Foo, Bar>();
Mapper.CreateMap<List<Foo>, List<Bar>>()
.ConvertUsing(source =>
{
var mapped = source.Select(Mapper.Map<Foo, Bar>).ToList();
// After mapping code;
var stuff = SomeController.GetStuff(DateTime.Now.Second);
return mapped;
});
Upvotes: 10