Mapping Lists using Automapper

Mapper.CreateMap<Person, PersonViewModel>(); peopleVM = Mapper.Map<List<Person>, List<PersonViewModel>>(people); Mapper.AssertConfigurationIsValid(); From Getting Started: 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 … Read more

AutoMapper Migrating from static API

Instead of: Mapper.CreateMap<AbcEditViewModel, Abc>(); The new syntax is: var config = new MapperConfiguration(cfg => { cfg.CreateMap<AbcEditViewModel, Abc>(); }); Then: IMapper mapper = config.CreateMapper(); var source = new AbcEditViewModel(); var dest = mapper.Map<AbcEditViewModel, Abct>(source); (Source with more examples)

Allow mapping of dynamic types using AutoMapper or similar?

AutoMapper 4.2.0 now supports Dynamic/expando/dictionary mapping With this feature you can map to your expando objects to static types: dynamic CurUser = _users.GetSingleUser(UserID); var config = new MapperConfiguration(cfg => { }); var mapper = config.CreateMapper(); var retUser = mapper.Map<UserModel>(CurUser); Old versions of AutoMapper do not support this (Massive internally uses ExpandoObject which doesn’t provide which … Read more

Pass Objects to AutoMapper Mapping

AutoMapper handles this key-value pair scenario out of the box. Mapper.CreateMap<Source, Dest>() .ForMember(d => d.Foo, opt => opt.ResolveUsing(res => res.Context.Options.Items[“Foo”])); Then at runtime: Mapper.Map<Source, Dest>(src, opt => opt.Items[“Foo”] = “Bar”); A bit verbose to dig into the context items but there you go.

Skip null values with custom resolver

UPDATE: IsSourceValueNull is not available starting from V5. If you want all source properties with null values to be ignored you could use: Mapper.CreateMap<SourceType, DestinationType>() .ForAllMembers(opt => opt.Condition(srs => !srs.IsSourceValueNull)); Otherwise, you can do something similar for each member. Read this.

Automapper: Ignore on condition of

The Ignore() feature is strictly for members you never map, as these members are also skipped in configuration validation. I checked a couple of options, but it doesn’t look like things like a custom value resolver will do the trick. Use the Condition() feature to map the member when the condition is true: Mapper.CreateMap<CarViewModel, Car>() … Read more