How to use AutoMapper 9 with static implementation without DI?

Viewed 2705
1 Answers

You can simply build a mapper from the mapper configuration. An example is provided in the AutoMapper docs, which I have reproduced here:

// use cfg to configure AutoMapper
var config = new MapperConfiguration(cfg => cfg.CreateMap<Order, OrderDto>()); 

var mapper = config.CreateMapper();
// or
var mapper = new Mapper(config);
OrderDto dto = mapper.Map<OrderDto>(order);

Then you could simply set a static field/property somewhere in your project to hold mapper.

Related