2013-08-01 49 views
6

Im在映射回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; } 
} 

視圖模型:

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; } 
} 

控制器

對於從DTO映射要使用下面的代碼視圖模型即時通訊,並且其工作的罰款。

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); 
} 

雖然轉換回im無法做到。下面的代碼我試過了。

. 
. 
. 
ServiceEntity objser = new ServiceEntity(); 

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

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

但我沒有得到任何成功。舉個例子,我提供了幾個屬性。我實時我可能有超過30個財產。 任何建議,將不勝感激......

+0

你有任何錯誤或只是空的屬性? –

回答

1

編輯因此,你可以改變BusinessEntity實現,你可以使用AutoMapper約定的功率和扁平化。下面是修改業務實體:

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; } 
} 

您需要Bk名稱允許的Bk.Name壓扁BkName。現在所有的映射都將生成幾行:

// 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); 

原來的答案因此,你使用的是自定義映射,從服務實體的業務實體,則默認映射也不會爲反向映射工作。您應該手動爲會員提供映射:

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

如果爲所有屬性映射,那麼爲什麼我應該使用AutoMapper,而不是我可以通過創建實例直接綁定。 – Vino

+0

@ user2640897這就是爲什麼我寫*「但我認爲手動映射在這種情況下更好」* –

+0

我發現了一個更多的解決方案。我只是繼承了BUsinessEntity中的ChildBussiness並進行了映射,而不是在Business Entity中使用屬性。它的作品出來了。 – Vino