What is the correct way of using Automapper? Is it in controller or is it in repository pattern? To me it seems like putting them in controller is not neat and unorganized.
So for example,
(Automapper used in controller):
- StudentController
public IActionResult Get()
{
var query = _context.Students.ToList();
var mapper = AutoMapper.Mapper.Map<List<Student>, List<StudentViewModel>>(query);
return new OkObjectResult(mapper);
}
(Automapper used in repository pattern):
- StudentController
public IActionResult Get()
{
return new OkObjectResult(_studentRepository.GetAllStudents());
}
- StudentRepository
public IEnumerable<StudentViewModel> GetAllStudents()
{
var query = _context.Students.ToList();
var mapper = AutoMapper.Mapper.Map<List<Student>, List<StudentViewModel>>(query);
return mapper;
}
Which returns the same result, but what is actually the best place to put automapper settings? and also would it be better to use .ProjectTo() to avoid using AutoMapper.Mapper.Map?
If you have to do complex mapping behavior, it might be better to avoid using AutoMapper for that scenario. Reverse mapping can get very complicated very quickly, and unless it's very simple, you can have business logic showing up in mapping configuration.
Automapper is considerably faster when mapping a List<T> of objects on . NET Core (It's still slower on full . NET Framework).
AutoMapper in C# is a library used to map data from one object to another. It acts as a mapper between two objects and transforms one object type into another. It converts the input object of one type to the output object of another type until the latter type follows or maintains the conventions of AutoMapper.
Your configuration (e.g. Automapper Profiles) are singletons. That is, they are only ever loaded once when your project runs. This makes sense since your configurations will not change while the application is running.
An easy way to think about it, is in transformations of your models.
In this case you are mapping to view models. This is the reponsibility of the controller. Therefore a logical choice would be to do the mapping in the controller code.
Another way to think about it. Another user of the repository could consume the same models but do something different with it. E.g. convert it to a JSON object for use in a REST api. In that case a different mapping is needed. This mapping does not belong in the repository but in the users of the repository.
According to Mosh Hamedani
Mapping is not the responsibility of the repository. It’s the responsibility of your controllers. Your repositories should return domain objects and the client of the repository can decide if it needs to do the mapping. By mapping the domain objects to view models (or something else) inside a repository, you prevent the client of your repositories from getting access to the underlying domain object
So, for an API controller, it makes more sense to have the mapper in the controller
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With