AutoMapper - 通过接口重用映射

时间:2013-03-04 16:26:03

标签: c# automapper

我有一个基类和各种派生类,我想使用AutoMapper映射到TPH结构中的EF实体。

示例,假设我有以下简单模型:

public abstract class VehicleBase
{
    public readonly VehicleType VehicleType;

    public readonly int NumWheels;

    protected VehicleBase(VehicleType vehicleType, int numWheels)
    {
        this.VehicleType = vehicleType;
        this.NumWheels = numWheels;
    }

    public int Id { get; set; }

    public int PolicyId { get; set; }

    public int Year { get; set; }

    public string Make { get; set; }

    public string Model { get; set; }
}

public class Cycle : VehicleBase
{
    public Cycle()
        : base(VehicleType.Cycle, 2)
    {
    }

    public bool HasJetKit { get; set; }
}

public class Truck : VehicleBase, IHasAntiLockBrakeSystem
{
    public Truck()
        : base(VehicleType.Truck, 4)
    {
    }

    public AntiLockBrakeSystem AntiLockBrakeSystem { get; set; }

    public bool AntiLockBrakeSystemIsStandard { get; set; }

    public bool IsCrewCab { get; set; }
}

public class Car : VehicleBase, IHasAntiLockBrakeSystem
{
    public Car()
        : base(VehicleType.Car, 4)
    {
    }

    public AntiLockBrakeSystem AntiLockBrakeSystem { get; set; }

    public bool AntiLockBrakeSystemIsStandard { get; set; }

    public int NumDoors { get; set; }
}

public class AntiLockBrakeSystem
{
    public int SomeValue { get; set; }
}

public interface IHasAntiLockBrakeSystem
{
    AntiLockBrakeSystem AntiLockBrakeSystem { get; set; }

    bool AntiLockBrakeSystemIsStandard { get; set; }
}

public enum VehicleType
{
    UNDEFINED = 0,
    Car = 1,
    Truck = 2,
    Cycle = 3
}

有没有办法使用像上面的IHasAntiLockBrakeSystem这样的接口,让AutoMapper映射该接口的属性,而不必为实现接口的每个类映射它们?这有意义吗?

我开始走这条路:

AutoMapper.Mapper.CreateMap<DomainModel.VehicleBase, DalModel.Vehicle>()
  .ForMember(dst => dst.VehicleTypeId, opt => opt.MapFrom(src => (byte)src.VehicleType))
  .ForMember(dst => dst.NumWheels, opt => opt.MapFrom(src => (byte)src.NumWheels))
  // include derived mappings
  .Include<DomainModel.Car, DalModel.Vehicle>()
  .Include<DomainModel.Truck, DalModel.Vehicle>()
  .Include<DomainModel.Cycle, DalModel.Vehicle>()

AutoMapper.Mapper.CreateMap<DomainModel.Car, DalModel.Vehicle>()
  .ForMember(dst => dst.NumDoors, opt => opt.MapFrom(src => (byte)src.NumDoors))
  .Include<DomainModel.IHasAntiLockBrakeSystem, DalModel.Vehicle>();

AutoMapper.Mapper.CreateMap<DomainModel.Truck, DalModel.Vehicle>()
  .Include<DomainModel.IHasAntiLockBrakeSystem, DalModel.Vehicle>();

AutoMapper.Mapper.CreateMap<DomainModel.Cycle, DalModel.Vehicle>();

但是我尝试使用映射时出现编译错误:

.Include<DomainModel.IHasAntiLockBrakeSystem, DalModel.Vehicle>();

“AutoMapperTesting.DomainModel.IHasAntiLockBrakeSystem”类型不能用作泛型类型或方法“AutoMapper.IMappingExpression.Include()”中的类型参数“TOtherSource”。从'AutoMapperTesting.DomainModel.IHasAntiLockBrakeSystem'到'AutoMapperTesting.DomainModel.Car'没有隐式引用转换。

EF5生成的实体看起来像:

    public partial class Vehicle
{
    public int Id { get; set; }
    public int PolicyId { get; set; }
    public byte VehicleTypeId { get; set; }
    public byte NumWheels { get; set; }
    public Nullable<bool> HasJetKit { get; set; }
    public Nullable<bool> IsCrewCab { get; set; }
    public Nullable<byte> NumDoors { get; set; }

    public virtual AntiLockBrakeSystem AntiLockBrakeSystem { get; set; }
    public virtual Policy Policy { get; set; }
    public virtual VehicleType VehicleType { get; set; }
}

public partial class AntiLockBrakeSystem
{
    public int VehicleId { get; set; }
    public int SomeValue { get; set; }
    public bool IsStandard { get; set; }

    public virtual Vehicle Vehicle { get; set; }
}

1 个答案:

答案 0 :(得分:0)

我没有看到您所说的“车辆”课程中的内容,但您可以创建一个类似特征的地图,并遗漏任何您不想要的内容。

Mapper.CreateMap<VehicleBase, Vehicle>()
            .ForMember(dest => dest.firstProp, opt => opt.MapFrom(source => source.FirstProp))
            .ForMember(dest => dest.secondProp, opt => opt.MapFrom(source => source.secondProp))

如果省略特定于其他类和接口的属性,它将不会尝试映射它们。