I have the following Automapper defintion:
Mapper.CreateMap<IB.BusinessComponents.Data.LocationMaster, IB.Entites.Master.Location>(); Mapper.CreateMap<IB.BusinessComponents.Data.LocationMaster, IB.Entites.Master.Location>() .ForMember(destination => destination.Id, source => source.MapFrom(item => item.LocationMasterID)) .ForMember(destination => destination.ChildLocationList, source => source.Ignore());
This works fine when I map a single object. But I can't seem to pass in Lists of objects. Do I need a different definition when passing in a list, or is it not possible?
How do I use AutoMapper? First, you need both a source and destination type to work with. The destination type's design can be influenced by the layer in which it lives, but AutoMapper works best as long as the names of the members match up to the source type's members.
1. If you use the convention-based mapping and a property is later renamed that becomes a runtime error and a common source of annoying bugs. 2. If you don't use convention-based mapping (ie you explicitly map each property) then you are just using automapper to do your projection, which is unnecessary complexity.
Yes, or you can call CreateMap<ModelClass, ViewModelClass>(). ReverseMap() .
Inside this article, it discusses performance and it indicates that Automapper is 7 times slower than manual mapping. This test was done on 100,000 records and I must say I was shocked.
In your AutoMapper Definition:
CreateMap<MyStuffDTO, MyStuffViewModel>() .ForMember(dto => dto.MyDate, opt => opt.MapFrom(src => src.LastDate)) .ForMember(dto => dto.MyTime, opt => opt.MapFrom(src => src.LastTime)) .ForMember(dto => dto.Category, opt => opt.MapFrom(src => src.Category));
In code:
For Single:
var result = Mapper.Map<MyStuffDTO, MyStuffViewModel>(obj);
For List:
var list = Mapper.Map<IList<MyStuffDTO>, IList<MyStuffViewModel>>(obj);
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