我有一个基类和各种派生类,我想使用 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; }
}