创建两个相同的对象类型之间的两个Automapper地图(Create two Automapper

2019-07-18 01:59发布

我在一个WCF服务中使用AutoMapper返回User对象。 User拥有如属性AccountTeams其本身具有子对象。 所有的类都有AutoMapper地图。

根据WCF OperationContract那个叫,我想返回不同的数据量。 我想一个OperationContract返回User对象,而其AccountTeams填充属性(及其子女),另一个OperationContract返回User与填写属性的整个链条。

有没有办法有两个相同的物体或是否需要进行完整的映射之间两个不同的地图null出的属性我不想从服务回报?

Answer 1:

凯文Kalitowski提出了关于沃尔玛的回答好点:如果我们需要两个配置来处理,需要不同的映射,那么就不要我们必须复制所有常见的其他映射?

我想我已经找到使用配置文件解决的办法:对每一个独特的映射一个配置文件,并为共同映射第三轮廓。 然后创建两种配置,每一个独特的个人资料,但也增加了通用配置文件到每个配置为好。

例如,在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.
}

我尝试了这一点,它的工作原理。



Answer 2:

我假设你是从映射UserUser (如果没有则只是更改了目标类型)

假设这个类下面的例子:

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<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 ,并把这个两个configuration1configuration2来电后CreateMap

更新

对于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


Answer 3:

我想你可以描述为解决这个问题,不同的配置对象在这里 ,你可以找到这样的例子在这里



文章来源: Create two Automapper maps between the same two object types