How to ignore properties based on their type

I use AutoMapper to copy an entity infrastructure object to another identical database. The problem is that he is trying to copy the lookup tables.

I tried to exclude them using AddGlobalIgnore and ShouldMapProperty , but it does not work. AutoMapper is still trying to copy these properties.

Here is my code. I would like to ignore properties starting with "LU"

  dynamic newObject= new NewObject(); MapperConfiguration config = new MapperConfiguration(cfg => { cfg.CreateMissingTypeMaps = true; cfg.AddGlobalIgnore("LU"); cfg.ShouldMapProperty = p => !p.GetType().ToString().StartsWith("LU"); cfg.ShouldMapField = p => !p.GetType().ToString().StartsWith("LU"); }); IMapper mapper = config.CreateMapper(); newObject = mapper.Map(objectToCopy, objectToCopy.GetType(), newObject.GetType()); 

I also tried

 MapperConfiguration config = new MapperConfiguration(cfg => { cfg.CreateMissingTypeMaps = true; cfg.AddGlobalIgnore("LU"); cfg.ShouldMapProperty = p => !p.PropertyType.Name.StartsWith("LU"); cfg.ShouldMapField = p => !p.FieldType.Name.StartsWith("LU"); }); 

and

 MapperConfiguration config = new MapperConfiguration(cfg => { cfg.CreateMissingTypeMaps = true; cfg.AddGlobalIgnore("LU"); cfg.ShouldMapProperty = p => !p.Name.StartsWith("LU"); cfg.ShouldMapField = p => !p.Name.StartsWith("LU"); }); 
+6
source share
2 answers

Create your configuration as a separate profile, then add this profile to the mapping configuration.

 class Program { static void Main(string[] args) { dynamic newObject = new NewObject(); var objectToCopy = new ObjectToCopy(); var config = new MapperConfiguration(cfg => { cfg.AddProfile<MyProfile>(); }); var mapper = config.CreateMapper(); mapper.Map(objectToCopy, newObject); // newObject.LU_Ignore = "Original value" // newObject.DoNotIgnore = "New value" } } class MyProfile : Profile { protected override void Configure() { CreateMissingTypeMaps = true; ShouldMapProperty = p => !p.Name.StartsWith("LU"); // this is the correct way to get the property name } } class ObjectToCopy { public string LU_Ignore { get; set; } = "New value"; public string DoNotIgnore { get; set; } = "New value"; } class NewObject { public string LU_Ignore { get; set; } = "Original value"; public string DoNotIgnore { get; set; } = "Original value"; } 

Something seems dumb about how configurations apply to the Mapper created call form mapper.CreateMapper . I look through it to find out if I can find out more information and update this answer if I find anything.

+3
source

I will not say that this is the best (experienced or design) approach, but it works:

 public static class AutoExtensions { public static IMappingExpression Ignore(this IMappingExpression expression, Func<PropertyInfo, bool> filter) { foreach (var propertyName in expression .TypeMap .SourceType .GetProperties() .Where(filter) .Select(x=>x.Name)) { expression.ForMember(propertyName, behaviour => behaviour.Ignore()); } return expression; } } 

You can configure your mapper this way (for these patterns):

 public class Client { public string LUName { get; set; } public string Dno { get; set; } } public class ClientDTO { public string LUName { get; set; } public string Dno { get; set; } } 

and check it as follows:

 private static void ConfigAndTestMapper() { var config = new MapperConfiguration(cfg =>{ cfg.CreateMap(typeof (Client), typeof (ClientDTO)) .Ignore(x => x.Name.StartsWith("LU")); }); var mapper = config.CreateMapper(); var result = mapper.Map<ClientDTO>(new Client() {LUName = "Name", Dno = "Dno"}); var isIgnored = result.LUName == null; } 

PS: it’s also quite β€œhacker”, because it is trying to display all kinds of properties (readonly / non-public, etc.), so take it with salt.

0
source

All Articles