Davita
Davita

Reputation: 9124

Best way to map objects differently in different situations (AutoMapper)

I'm using AutoMapper and want to know which way is the best approach to map objects differently in different situations (for example, ignore one field in MethodA, include that field in MethdoB etc.). I can create My own MappingEngine but I was wonderign if there was a better way to achieve that.

Upvotes: 2

Views: 467

Answers (1)

Ed Charbeneau
Ed Charbeneau

Reputation: 4634

You can map one source to multiple destinations with automapper. For example you can have a source object with

Person

  • Id
  • FirstName
  • LastName
  • PhoneNumber

and view models

ContactInfoViewModel

  • FirstName
  • PhoneNumber

BioViewModel

  • FirstName
  • LastName
  • PhoneNumber
Mapper.Map<Person, ContactViewModel>();
Mapper.Map<Person, BioViewModel>();

Upvotes: 1

Related Questions