2013-08-01 7 views
6

Im avendo un problema nel mapping al DTO.Problema di AutoMapper quando si esegue il mapping su DTO da ViewModel

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

ViewModel:

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

controller

per la mappatura da DTO Per ViewModel im usando il codice seguente, e il suo bel lavoro.

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

Durante la conversione non riesco a fare. Sotto il codice che ho provato.

. 
. 
. 
ServiceEntity objser = new ServiceEntity(); 

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

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

Ma non ho avuto alcun successo. Per un esempio ho fornito poche proprietà. In tempo reale potrei avere più di 30 proprietà. Ogni suggerimento sarebbe apprezzato ...

+0

Hai errori o solo proprietà vuote? –

risposta

1

EDIT Così è possibile modificare BusinessEntity implementazione, è possibile utilizzare il potere di convenzioni automapper e appiattimento. Qui viene modificato entità aziendale:

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

devi Bk nome per consentire appiattimento della Bk.Name-BkName. Ora tutti i mapping verranno generati in diverse righe:

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

Questo è tutto. Tutti i 30 immobili verranno mappati per convenzione:

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

ORIGINALE RISPOSTA Quindi si utilizza la mappatura personalizzata, da entità servizio per entità di business, quindi di default la mappatura, inoltre, non funziona per la mappatura all'indietro. È necessario fornire manualmente i mapping per i membri:

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

Ma penso mappatura manuale è meglio in questo caso, quindi fornendo tutto questo le mappe per le singole proprietà.

+0

Se im mapping per tutte le proprietà, quindi perché dovrei usare AutoMapper, invece che posso direttamente associare creando istanza. – Vino

+0

@ user2640897 è per questo che ho scritto * "Ma penso che la mappatura manuale sia migliore in questo caso" * –

+0

Ho trovato un'altra soluzione. Ho appena ereditato ChildBussiness in BUsinessEntity e mappato di conseguenza anziché utilizzare la proprietà in Business Entity. Funziona. – Vino