简体   繁体   中英

AutoMapper AfterMap

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM