2017-12-30 26 views
1

私はEntity FrameworkでAutoMapperを使用しています。Automapperが最適なマッピングを見つける

    • 学生
    • ワーカー

各ビジネス・オブジェクトは、データベース内のエンティティへのマップを持っている:私は、エンティティの階層を持っています。

public PersonEntity MapPerson(Person person) 
    { 
     switch (person.Type) 
     { 
      case PersonType.Unknown: 
       return Mapper.Map<PersonEntity>(person); 
      case PersonType.Student: 
       return Mapper.Map<StudentEntity>(person); 
      case PersonType.Worker: 
       return Mapper.Map<WorkerEntity>(person); 

      default: 
       throw new ArgumentOutOfRangeException(); 
     } 
    } 
:私は「最高」のマッピングを見つけるためのより良い方法があるかどうだろうか、私は本当にコードでこのような何かを持っている必要があります私はAutoMapperのV6.2.2 を使用していたエンティティへのビジネス・オブジェクトを変換するには

良いことは、私は既にdiscriminatorとそのようなもののための "タイプ" enumを持っていますが、それでもまだ間違っていると感じています。多分あなたが助けることができます。あなたがこれを行うことができます

答えて

1

AutoMapperはmapping inheritanceです。マッピングは次のようになります。あなたはPersonEntityPersonをマッピングする際

class PersonMapperProfile : AutoMapper.Profile 
{ 
    public PersonMapperProfile() 
    { 
     this.CreateMap<Student, StudentEntity>(); 
     this.CreateMap<Worker, WorkerEntity>(); 
     this.CreateMap<Person, PersonEntity>() 
      .Include<Student, StudentEntity>() 
      .Include<Worker, WorkerEntity>(); 
    } 
} 

を今、AutoMapperは正しい基本タイプまたはサブタイプを作成します。

関連する問題