Tun
Tun

Reputation: 912

what is the benefits of using mapper (automapper) over linq map function

You may find this question is stupid. But It could help other people who come out with this question in their brain.

What is benefits of using some generic mapper ( AutoMapper) over linq query mapping function ?

EG: Using mapper vs linq mapping

Using Linq mapping function:

var peopleList = _dbContext.People.Select(x => new PersonModel()
                                   { Field1 = x.Field......});

Using generic mapper (EG: AutoMapper)

var peopleList = _dbContext.People.ToList();
var personList = Mapper.Map<IEnumerable<PersonModel>>(peopleList);

Upvotes: 1

Views: 481

Answers (1)

Paweł Bejger
Paweł Bejger

Reputation: 6366

In this case:

var peopleList = _dbContext.People.Select(x => new PersonModel()
                                   { Field1 = x.Field......});

You need to specify the mapping field by field.

In that case:

var peopleList = _dbContext.People.ToList();
var personList = Mapper.Map<IEnumerable<PersonModel>>(peopleList);

All the properties with the same names are automatically mapped. What is more in the latter case you can specify the mapping rules just once (globally) and then use it throughout the application.

Upvotes: 2

Related Questions