Skip to content

Instantly share code, notes, and snippets.

@wags
Last active November 10, 2017 13:29
Show Gist options
  • Save wags/54c11378eee210285f93b4c0f8eb7fb7 to your computer and use it in GitHub Desktop.
Save wags/54c11378eee210285f93b4c0f8eb7fb7 to your computer and use it in GitHub Desktop.
Automapper Snippets

Define the child mapping for collection properties on the contact

CreateMap<ApSupplier, ApSupplierDto>();
CreateMap<PoSupplier, PoSupplierDto>();
CreateMap<Customer, CustomerDto>();
CreateMap<Salesperson, SalespersonDto>();

Map from UniVerse mv.NET Entity to DTO

CreateMap<Contact, ContactDto>()
    .ForMember(dest => dest.RoleId, opt => opt.MapFrom(src => src.RoleData.Id))
    .ForMember(dest => dest.RoleDescription, opt => opt.MapFrom(src => src.RoleData.Label))
    .ForMember(dest => dest.AssociatedCustomers, opt => opt.MapFrom(src => src.AssociatedCustomersData))
    .ForMember(dest => dest.AssociatedSalespeople, opt => opt.MapFrom(src => src.AssociatedSalespeopleData))
    .ForMember(dest => dest.AssociatedApSuppliers, opt => opt.MapFrom(src => src.AssociatedApSuppliersData))
    .ForMember(dest => dest.AssociatedPoSuppliers, opt => opt.MapFrom(src => src.AssociatedPoSuppliersData));

// Map from DTO UniVerse mv.NET Entity
// Tell AutoMapper to ignore the special MV fields and wire up a custom map
CreateMap<ContactToBeValidatedDto, Contact>()
    .ForMember(dest => dest.Role, opt => opt.MapFrom(src => src.RoleId));
.ForMember(dest => dest.Email, opt => opt.Condition(src => (src.Email != null)));
.ForAllOtherMembers(opt => opt.Condition((src, dest, srcMember) => srcMember != null));
//.ForMember(dest => dest.ApSupplierNumbers, opt => opt.Ignore())
//.ForMember(dest => dest.CustomerNumbers, opt => opt.Ignore())
//.ForMember(dest => dest.VendorNumbers, opt => opt.Ignore())
//.ForMember(dest => dest.SalesmanNumbers, opt => opt.Ignore())
//.AfterMap((src, dest) =>
//{
//    dest.ApSupplierNumbers.SetFromString(string.Join(_vm, src.ApSupplierNumbers));
//    dest.CustomerNumbers.SetFromString(string.Join(_vm, src.CustomerNumbers));
//    dest.VendorNumbers.SetFromString(string.Join(_vm, src.VendorNumbers));
//    dest.SalesmanNumbers.SetFromString(string.Join(_vm, src.SalesmanNumbers));
//});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment