2017-12-30 10 views
1

저는 AutoMapper와 Entity Framework를 사용하고 있습니다.오토 맵퍼가 최상의 매핑을 찾습니다.

  • 사람
    • 학생
    • 노동자

각 Business Object는 데이터베이스에있는 개체에 대한지도를 가지고 : 나는 개체의 계층 구조를 가지고있다.

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와 그런 것들을위한 "Type"enum을 가지고 있지만 여전히 틀린 느낌입니다. 어쩌면 당신이 도울 수 있습니다. 당신은이 작업을 수행 할 수

답변

1

AutoMapper는 mapping inheritance입니다. 매핑은 다음과 같아야합니다

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

을 이제 당신이 PersonPersonEntity에, AutoMapper 올바른 기본 유형 또는 하위 유형을 만들 것입니다지도 할 때.

관련 문제