[ACCEPTED]-Mapper does not contain a definition for Initialize AutoMapper C#-automapper

Accepted answer
Score: 14

Method Mapper.Initialize is obsolete since version v9.0.0 5 (doc), you need to use MapperConfiguration instead (doc).

var config = new MapperConfiguration(cfg => {
    cfg.AddProfile<AutomapperWebProfile>();
});

var mapper = config.CreateMapper();
// or
var mapper = new Mapper(config);

Initializing 4 a mapper with a static method in a Golbal.asax 3 is not a flexible solution. I would suggest 2 creating a config directly in the custom 1 mapper class.

public interface IFooMapper 
{   
    Foo Map(Bar bar); 
}

public class FooMapper : IFooMapper
{
    private readonly IMapper mapper;

    public FooMapper()
    {
        var config = new MapperConfiguration(cfg =>
        {
            cfg.AddProfile<FooProfile>();
        });

        mapper = config.CreateMapper();
    }

    public Foo Map(Bar bar) => mapper.Map<Foo>(bar);
}

More Related questions