在相同的两种对象类型之间创建两个自动映射器映射
本文关键字:映射 两个 创建 两种 对象 之间 类型 | 更新日期: 2023-09-27 17:52:17
我在WCF服务中使用AutoMapper来返回User
对象。CCD_ 2具有诸如CCD_。所有类都具有AutoMapper映射。
根据调用的WCF OperationContract
,我希望返回不同数量的数据。我希望一个OperationContract
返回User
对象而不填充其AccountTeams
属性(及其子级(,另一个OperationContract
返回User
并填充整个属性链。
有没有一种方法可以在相同的两个对象之间有两个不同的映射,或者我需要执行完整的映射并将我不想从服务返回的属性null
输出?
我想我已经找到了一种使用概要文件的方法:为每个唯一的映射创建一个概要文件,为公共映射创建第三个概要文件。然后创建两个配置,一个用于每个唯一的配置文件,但也将通用配置文件添加到每个配置中。
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<User, User>();
}
}
public class Profile2 : Profile
{
protected override void Configure()
{
base.CreateMap<User, User>().ForMember(dest => dest.Age, opt => opt.Ignore());
}
}
public class CommonProfile : Profile
{
protected override void Configure()
{
base.CreateMap<Vehicle, Vehicle>();
}
}
然后创建配置并映射对象:
[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<CommonProfile>();
cfg.AddProfile<Profile1>();
});
var mapper1 = configuration1.CreateMapper();
var mappedUser1 = mapper1.Map<User, User>(user);//maps both Name and Age
var mappedVehicle1 = mapper1.Map<Vehicle, Vehicle>(vehicle);//Maps both FleetNumber
//and Registration.
var configuration2 = new MapperConfiguration(cfg =>
{
cfg.AddProfile<CommonProfile>();
cfg.AddProfile<Profile2>();
});
var mapper2 = configuration2.CreateMapper();
var mappedUser2 = mapper2.Map<User, User>(user);//maps only Name
var mappedVehicle2 = mapper2.Map<Vehicle, Vehicle>(vehicle);//Same as mappedVehicle1.
}
我试过了,效果很好。
我假设您正在从User
映射到User
(如果不是,则只更改目的地类型(
假设以下示例为此类:
public class User
{
public string Name { get; set; }
public int Age { get; set; }
}
然后,您可以使用单独的AutoMapper.Configuration
来定义两个映射:
[TestMethod]
public void TestMethod()
{
var configuration1 = new Configuration(new TypeMapFactory(), MapperRegistry.AllMappers());
var mapper1 = new MappingEngine(configuration1);
configuration1.CreateMap<User, User>();
var user = new User() { Name = "John", Age = 42 };
var mappedUser1 = mapper1.Map<User, User>(user);//maps both Name and Age
var configuration2 = new Configuration(new TypeMapFactory(), MapperRegistry.AllMappers());
configuration2.CreateMap<User, User>().ForMember(dest => dest.Age, opt => opt.Ignore());
var mapper2 = new MappingEngine(configuration2);
var mappedUser2 = mapper2.Map<User, User>(user);
Assert.AreEqual(0, mappedUser2.Age);//maps only Name
}
为了避免每隔一个类型映射两次,您可以添加一个通用方法,该方法使用Configuration
来映射从User
可以访问的所有内容,并在调用CreateMap
之后在configuration1
和configuration2
上调用该方法。
更新
对于Automapper 4.x,请使用以下内容:
var configuration1 = new MapperConfiguration(cfg =>
{
cfg.CreateMap<User, User>();
});
var mapper1 = configuration1.CreateMapper();
var user = new User() { Name = "John", Age = 42 };
var mappedUser1 = mapper1.Map<User, User>(user);//maps both Name and Age
var configuration2 = new MapperConfiguration(cfg =>
{
cfg.CreateMap<User, User>().ForMember(dest => dest.Age, opt => opt.Ignore());
});
var mapper2 = configuration2.CreateMapper();
var mappedUser2 = mapper2.Map<User, User>(user); //maps only Name
我认为你可以用这里描述的不同配置对象来解决这个问题,你可以在这里找到一个例子