Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it better to use Automapper in controller or repository pattern?

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?

like image 200
bbusdriver Avatar asked Dec 29 '16 12:12

bbusdriver


People also ask

When should you not use AutoMapper?

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.

Is AutoMapper faster than manual mapping?

Automapper is considerably faster when mapping a List<T> of objects on . NET Core (It's still slower on full . NET Framework).

What is AutoMapper good for?

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.

Should AutoMapper be Singleton?

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.


2 Answers

An easy way to think about it, is in transformations of your models.

  • The repository transforms database entities into business models.
  • The controller transforms business model into view models.
  • The view transforms view models into html.

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.

like image 176
vuilehaid Avatar answered Oct 21 '22 00:10

vuilehaid


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

like image 2
LRitter Avatar answered Oct 21 '22 01:10

LRitter