在相同的两个对象类型之间创建两个Automapper映射
我在WCF服务中使用AutoMapper来返回User
对象。 User
具有诸如AccountTeams
属性,其本身具有子对象。 所有类都有AutoMapper映射。
根据调用的WCF OperationContract
,我想返回不同数量的数据。 我想要一个OperationContract
返回User
对象,而不填充其AccountTeams
属性(及其子代),另一个OperationContract
返回User
,并填写整个属性链。
有没有办法在同一个两个对象之间有两个不同的映射,或者我是否需要执行完整映射并null
我不想从服务返回的属性?
Kevin Kalitowski对wal的回答提出了一个很好的观点:如果我们需要两个配置来处理需要不同的映射,那么我们不必复制所有其他常见的映射吗?
我想我已经找到了解决这个问题的方法:使用每个唯一映射的一个配置文件,以及公共映射的第三个配置文件。 然后创建两个配置,每个配置对应一个唯一的配置文件,同时也为每个配置添加公共配置文件。
例如,在AutoMapper 4.2中:
要映射的类:用户和车辆:
public class User { public string Name { get; set; } public int Age { get; set; } } public class Vehicle { public int FleetNumber { get; set; } public string Registration { get; set; } }
简介:
public class Profile1 : Profile { protected override void Configure() { base.CreateMap(); } } public class Profile2 : Profile { protected override void Configure() { base.CreateMap().ForMember(dest => dest.Age, opt => opt.Ignore()); } } public class CommonProfile : Profile { protected override void Configure() { base.CreateMap(); } }
然后创建配置并映射对象:
[TestMethod] public void TestMethod() { var user = new User() { Name = "John", Age = 42 }; var vehicle = new Vehicle {FleetNumber = 36, Registration = "XYZ123"}; var configuration1 = new MapperConfiguration(cfg => { cfg.AddProfile(); cfg.AddProfile(); }); var mapper1 = configuration1.CreateMapper(); var mappedUser1 = mapper1.Map(user);//maps both Name and Age var mappedVehicle1 = mapper1.Map(vehicle);//Maps both FleetNumber //and Registration. var configuration2 = new MapperConfiguration(cfg => { cfg.AddProfile(); cfg.AddProfile(); }); var mapper2 = configuration2.CreateMapper(); var mappedUser2 = mapper2.Map(user);//maps only Name var mappedVehicle2 = mapper2.Map(vehicle);//Same as mappedVehicle1. }
我尝试了这个,它的确有效。
我假设您正在从User
映射到User
(如果不是,则只需更改目标类型)
为以下示例假设此类:
public class User { public string Name { get; set; } public int Age { get; set; } }
然后,您可以使用单独的AutoMapper.Configuration
来定义2个映射:
[TestMethod] public void TestMethod() { var configuration1 = new Configuration(new TypeMapFactory(), MapperRegistry.AllMappers()); var mapper1 = new MappingEngine(configuration1); configuration1.CreateMap(); var user = new User() { Name = "John", Age = 42 }; var mappedUser1 = mapper1.Map(user);//maps both Name and Age var configuration2 = new Configuration(new TypeMapFactory(), MapperRegistry.AllMappers()); configuration2.CreateMap().ForMember(dest => dest.Age, opt => opt.Ignore()); var mapper2 = new MappingEngine(configuration2); var mappedUser2 = mapper2.Map(user); Assert.AreEqual(0, mappedUser2.Age);//maps only Name }
为了避免每隔两次Type映射两次,你可以添加一个公共方法,它接受一个Configuration
,它映射User
可以访问的所有内容,并在调用CreateMap
后在configuration1
和configuration2
上调用它。
更新
对于Automapper 4.x,请使用以下命令:
var configuration1 = new MapperConfiguration(cfg => { cfg.CreateMap(); }); var mapper1 = configuration1.CreateMapper(); var user = new User() { Name = "John", Age = 42 }; var mappedUser1 = mapper1.Map(user);//maps both Name and Age var configuration2 = new MapperConfiguration(cfg => { cfg.CreateMap().ForMember(dest => dest.Age, opt => opt.Ignore()); }); var mapper2 = configuration2.CreateMapper(); var mappedUser2 = mapper2.Map(user); //maps only Name
我认为您可以使用此处所述的不同Configuration对象解决此问题,您可以在此处找到此示例