title: AutoMapper - Reuse mapping via Interfaces
body: <p>I have a base class and a variety of derived classes that I want to use AutoMapper to map to EF entities that are in a TPH structure.</p>
  
  <p>Example, let's say I have the following simple model:</p>
  
  <pre><code>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
  }
  </code></pre>
  
  <p>Is there any way to use an interface like IHasAntiLockBrakeSystem above, to have AutoMapper map the properties of that interface without having to map them for every class that implements the interface? Does that make sense?</p>
  
  <p>I started down this road:</p>
  
  <pre><code>AutoMapper.Mapper.CreateMap&lt;DomainModel.VehicleBase, DalModel.Vehicle&gt;()
    .ForMember(dst =&gt; dst.VehicleTypeId, opt =&gt; opt.MapFrom(src =&gt; (byte)src.VehicleType))
    .ForMember(dst =&gt; dst.NumWheels, opt =&gt; opt.MapFrom(src =&gt; (byte)src.NumWheels))
    // include derived mappings
    .Include&lt;DomainModel.Car, DalModel.Vehicle&gt;()
    .Include&lt;DomainModel.Truck, DalModel.Vehicle&gt;()
    .Include&lt;DomainModel.Cycle, DalModel.Vehicle&gt;()
  
  AutoMapper.Mapper.CreateMap&lt;DomainModel.Car, DalModel.Vehicle&gt;()
    .ForMember(dst =&gt; dst.NumDoors, opt =&gt; opt.MapFrom(src =&gt; (byte)src.NumDoors))
    .Include&lt;DomainModel.IHasAntiLockBrakeSystem, DalModel.Vehicle&gt;();
  
  AutoMapper.Mapper.CreateMap&lt;DomainModel.Truck, DalModel.Vehicle&gt;()
    .Include&lt;DomainModel.IHasAntiLockBrakeSystem, DalModel.Vehicle&gt;();
  
  AutoMapper.Mapper.CreateMap&lt;DomainModel.Cycle, DalModel.Vehicle&gt;();
  </code></pre>
  
  <p>But I get a compilation error trying with the mappings:</p>
  
  <pre><code>.Include&lt;DomainModel.IHasAntiLockBrakeSystem, DalModel.Vehicle&gt;();
  </code></pre>
  
  <p><strong>The type 'AutoMapperTesting.DomainModel.IHasAntiLockBrakeSystem' cannot be used as type parameter 'TOtherSource' in the generic type or method 'AutoMapper.IMappingExpression.Include()'. There is no implicit reference conversion from 'AutoMapperTesting.DomainModel.IHasAntiLockBrakeSystem' to 'AutoMapperTesting.DomainModel.Car'.</strong></p>
  
  <p>The EF5 generated entities look like:</p>
  
  <pre><code>    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&lt;bool&gt; HasJetKit { get; set; }
      public Nullable&lt;bool&gt; IsCrewCab { get; set; }
      public Nullable&lt;byte&gt; 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; }
  }
  </code></pre>