Skip to content

manage conditional mapping simply for merging #148

@xbrossard

Description

@xbrossard

I know that expressMapper can manage conditional mapping executed when calling map, but I think that current implementation doesn't manage a true conditional copy in a simple way.
My goal is very simple: i want to manage a merge between two objects of the same type, simply copy the source fields in destination fields only is it's equal to null (or default field value). I try to use different methods, but nothing work as i want:

  • the Function operation cannot test source and destination field to compare

  • the Before operation cannot conditonnally change comportement of mapping (just make initialisation process)

  • the registerCustom need to define one custom mapping class for each type combinaison; I can't define a generic CustomMapper class which doesn't inherit from a statically generic class ICustomMapper<T1,T2>. Furthermore, you have to define one CustomMapper class for all connected types If you have a connected graph of type, and you have to explicit call mapping for each connected object, like in this example:
    for a Address which have a Town as a field object, you have to define 2 Mappers classes like this one

      public class MergeMapperAdress : ExpressMapper.ICustomTypeMapper<Adress, Adress>
      {
          public Adress Map(IMappingContext<Adress, Adress> context)
          {
          if (context.Destination == default(Adress))
              context.Destination = new Adress();
    
         // copy all primitive fields to destination only if it's null
          context.Source.Merge(context.Destination);
    
          if (context.Destination.town== null)
              context.Destination.town= ExpressMapper.Mapper.Map(context.Source.town, typeof(Town), typeof(Town)) as Town;
          else
              ExpressMapper.Mapper.Map(context.Source.town, context.Destination.ville, typeof(Town), typeof(Town));
    
          return context.Destination;
          }
     }
    
      public class MergeMapperTown : ExpressMapper.ICustomTypeMapper<Town, Town>
      {
          public Ville Map(IMappingContext<Town, Town> context)
          {
          if (context.Destination == default(Town))
              context.Destination = new Town();
    
         // copy all primitive fields to destination only if it's null
          context.Source.Merge(context.Destination);
    
          return context.Destination;
          }
      }
    

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions