2013-08-01 1 views
6

다시 DTO로 매핑하는 데 문제가 있습니다.ViewModel에서 DTO로 다시 매핑 할 때 AutoMapper 문제가 발생했습니다.

DTO :

public class ServiceEntity : BaseChild 
{ 
    public int Id { get; set; } 
} 

public class BaseChild 
{ 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public int Salary { get; set; } 
    public string BkName { get; set; } 
    public int BkPrice { get; set; } 
    public string BkDescription { get; set; } 
} 

뷰 모델 : DTO에서 매핑

public class BusinessEntity 
{ 
    public ChildBussiness Details { get; set; } 
} 

public class ChildBussiness 
{ 
    public string NameFirst { get; set; } 
    public string LastName { get; set; } 
    public Books BookDetails { get; set; } 
    public string Salary { get; set; } 
} 

public class Books 
{ 
    public string BookName { get; set; } 
    public int BookPrice { get; set; } 
    public string BookDescription { get; set; } 
} 

컨트롤러

는 아래의 코드를 사용하여 메신저의 ViewModel하려면, 그 잘 작동합니다.

public ActionResult Index() 
{ 
    ServiceEntity obj = GetData(); 
    Mapper.CreateMap<ServiceEntity, BusinessEntity>() 
     .ForMember(d => d.Details, o => o.MapFrom(x => new ChildBussiness { NameFirst = x.FirstName, LastName = x.LastName, Salary = x.Salary.ToString(), BookDetails = new Books { BookDescription = x.BkDescription, BookName = x.BkName, BookPrice = x.BkPrice }})); 

    BusinessEntity objDetails = Mapper.Map<ServiceEntity, BusinessEntity>(obj); 
} 

다시 변환 할 수 없습니다. 코드 아래 내가 노력했다.

. 
. 
. 
ServiceEntity objser = new ServiceEntity(); 

Mapper.CreateMap<BusinessEntity, ServiceEntity>(); 
Mapper.CreateMap<Books, ServiceEntity>(); 

objser = Mapper.Map<BusinessEntity, ServiceEntity>(model); 
. 
. 
. 

그러나 나는 어떤 성공을 얻지 못했습니다. 예를 들어, 몇 가지 속성을 제공했습니다. 나는 실시간으로 30 개가 넘는 재산을 가질 수 있습니다. 어떤 제안이

+0

오류가 있거나 빈 속성 만 있습니까? –

답변

1

편집 따라서 당신은 당신이 AutoMapper 규칙의 전력 및 병합을 사용할 수 BusinessEntity 구현을 변경할 수 있습니다 ... 감사하겠습니다. 다음은 수정 된 비즈니스 엔티티 :

public class BusinessEntity 
{ 
    public string FirstName { get; set; } // Instead of NameFirst 
    public string LastName { get; set; } 
    public Book Bk { get; set; } // Bk instead of BookDetails 
    public string Salary { get; set; } 
} 

public class Book 
{ 
    public string Name { get; set; } // No prefixes 
    public int Price { get; set; } 
    public string Description { get; set; } 
} 

당신은 BkNameBk.Name의 플래트 닝을 할 수 있도록 Bk 이름이 필요합니다. 이제 모든 매핑이 여러 줄로 생성됩니다.

// For mapping from service entity to book 
Mapper.Initialize(cfg => cfg.RecognizePrefixes("Bk")); 

Mapper.CreateMap<BusinessEntity, ServiceEntity>(); 

// Trick to un-flatten service entity 
// It is mapped both to Book and BusinessEnity 
Mapper.CreateMap<ServiceEntity, Book>(); 
Mapper.CreateMap<ServiceEntity, BusinessEntity>() 
     .ForMember(d => d.Bk, m => m.MapFrom(s => s)); 

그게 전부입니다. 모든 30 개 특성이 규칙에 따라 매핑됩니다

var service = new ServiceEntity { 
    FirstName = "Sergey", 
    LastName = "Berezovskiy", 
    Salary = 5000, 
    BkName = "Laziness in Action", 
    BkDescription = "...", 
    BkPrice = 42 
}; 

var business = Mapper.Map<BusinessEntity>(source); 
var anotherService = Mapper.Map<ServiceEntity>(business); 

ORIGINAL 답변는 따라서 사용자 정의 매핑을 사용하고, 서비스 기업에서 비즈니스 엔티티, 다음 역 매핑에 대해 작동하지 않습니다 또한 매핑 기본. 수동 회원 매핑을 제공해야

Mapper.CreateMap<BusinessEntity, ServiceEntity>() 
    .ForMember(d => d.FirstName, m => m.MapFrom(s => s.Details.NameFirst)) 
    .ForMember(d => d.LastName, m => m.MapFrom(s => s.Details.LastName)) 
    .ForMember(d => d.Salary, m => m.MapFrom(s => s.Details.Salary)) 
    .ForMember(d => d.BkName, m => m.MapFrom(s => s.Details.BookDetails.BookName)) 
    .ForMember(d => d.BkPrice, m => m.MapFrom(s => s.Details.BookDetails.BookPrice)) 
    .ForMember(d => d.BkDescription, m => m.MapFrom(s => s.Details.BookDetails.BookDescription)); 

하지만 수동 매핑이 모든이 개별 속성 맵을 제공하고,이 경우 더 나은 생각합니다.

+0

모든 속성에 대한 매핑이 im 인 경우 AutoMapper를 사용해야하는 이유 대신 인스턴스를 만들어 직접 바인딩 할 수 있습니다. – Vino

+0

@ user2640897 그 이유는 * "하지만 수동 매핑이이 경우에 더 좋습니다"* –

+0

하나 더 많은 해결책을 찾았습니다. Business Entity에서 속성을 사용하는 대신 BUsinessEntity 및 Mapped에서 ChildBussiness를 상속했습니다. 그것의 작품. – Vino