Search code examples
c#automapperautomapper-6

AutoMapper tiny class to large class with validation


I have a large class with 30+ properties, and I need to map to it from a tiny class with about 6 properties that should map automatically (same name, same Type). I do not want to have to maintain a list of 24+ .Ignore()s in the mapping config, but I do want to be able to run AutoMapper's validation routine against all the rest of my mappings; I don't particularly care if this one mapping is validated, though.

I have tried ReverseMap and some of the Ignore* methods to see what might work. I thought ReverseMap would be the trick, but either I'm using it wrong or it doesn't do what I understand it to do. It does not seem to be well-documented.

For clarity:

public class LargeClass {
    // 30+ properties here
}

public class TinyClass {
    // 6 properties here that map perfectly to LargeClass
    // 4-8 properties that do not map to LargeClass, by design
}

CreateMap<TinyClass, LargeClass>(); // Will not validate, 24+ unmapped properties on Destination :(

Thank you!


Solution

  • ReverseMap is when you want to reverse a map from a CreateMap call.

    It sounds like you need to pass in the member list you want to validate against:

    CreateMap<TinyClass, LargeClass>(MemberList.Source);
    

    This validates against the source members.

    And if you need more documentation, check out the wiki!