I am planning to use Automapper with ASP.NET MVC solution and Unity DI. The video posted on automapper on how to use is very old and doesn't show how mapper can be used with dependency injection. Most of the examples on stackoverflow also uses Mapper.CreateMap() method which is now deprecated.
The automapper guide says
Once you have your types you can create a map for the two types using a MapperConfiguration instance and CreateMap. You only need one MapperConfiguration instance typically per AppDomain and should be instantiated during startup.
var config = new MapperConfiguration(cfg => cfg.CreateMap<Order, OrderDto>());
So i am assuming above line of code will go into application startup, like global.asax
To perform a mapping, create an IMapper use the CreateMapper method.
var mapper = config.CreateMapper();
OrderDto dto = mapper.Map<OrderDto>(order);
The above line will go into controller. However i am not understanding where this config
variable coming from? How do i inject IMapper in controller?
IMapper
and the mapper instance. Then you should declare a dependency onIMapper
from the controller (by accepting anIMapper
in the constructor). – Yacoub Massad