Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

manage conditional mapping simply for merging #148

Open
xbrossard opened this issue Feb 16, 2018 · 0 comments
Open

manage conditional mapping simply for merging #148

xbrossard opened this issue Feb 16, 2018 · 0 comments

Comments

@xbrossard
Copy link

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;
          }
      }
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant