DISCLAIMER: si tratta di una copia incolla da un vecchio post stackoverflow che non è più disponibile, ma ho lo stesso problema, quindi mi è sembrato opportuno ripubblicarlo come non ha mai risposto.Come si usa automapper per mappare un set di dati con più tabelle
Ho una procedura memorizzata che restituirà 4 set di risultati (contatti, indirizzi, e-mail, telefoni) che vengono popolati in un set di dati. Vorrei utilizzare AutoMapper per compilare un oggetto complesso.
public class Contact
{
public Guid ContactId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public List<Address> Addresses { get; set; }
public List<Phone> Phones { get; set; }
public List<Email> Emails { get; set; }
}
public partial class Address:BaseClass
{
public Guid ContactId { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Address3 { get; set; }
public string City { get; set; }
public string StateProvince { get; set; }
public string PostalCode { get; set; }
public string CountryCode { get; set; }
}
public class Email
{
public Guid EmailId { get; set; }
public Guid ContactId { get; set; }
public string EmailAddress { get; set; }
}
public class Phone
{
public Guid PhoneId { get; set; }
public Guid ContactId { get; set; }
public string Number { get; set; }
public string Extension { get; set; }
}
Ho un metodo che otterrà dati e restituirà un elenco di contatti. Dopo che il DataSet è popolato, definisco le relazioni tra le tabelle.
Ho trovato molti esempi in cui si converte il DataSet (o tabella) per un lettore utilizzando il metodo CreateDataReader e questo è quello che sto facendo qui. In effetti, il metodo analizzerà la prima tabella nell'oggetto, ma non eseguirà l'enumerazione attraverso le tabelle correlate.
public List<Contact> GetContacts()
{
List<Contact> theList = null;
// Get the data
Database _db = DatabaseFactory.CreateDatabase();
DataSet ds = db.ExecuteDataSet(CommandType.StoredProcedure, "GetContacts");
//The dataset should contain 4 tables
if (ds.Tables.Count == 4)
{
//Create the maps
Mapper.CreateMap<IDataReader, Contact>(); // I think I'm missing something here
Mapper.CreateMap<IDataReader, Address>();
Mapper.CreateMap<IDataReader, Email>();
Mapper.CreateMap<IDataReader, Phone>();
//Define the relationships
ds.Relations.Add("ContactAddresses", ds.Tables[0].Columns["ContactId"], ds.Tables[1].Columns["ContactId"]);
ds.Relations.Add("ContactEmails", ds.Tables[0].Columns["ContactId"], ds.Tables[2].Columns["ContactId"]);
ds.Relations.Add("ContactPhones", ds.Tables[0].Columns["ContactId"], ds.Tables[3].Columns["ContactId"]);
IDataReader dr = ds.CreateDataReader();
theList = Mapper.Map<List<Contact>>(dr);
}
return (theList);
}
Mi sento come se mi manca qualcosa nella mappatura per l'oggetto contatto, ma non riesco proprio a trovare un buon esempio da seguire.
Se io popolo manualmente l'oggetto contatto e poi passo è quello di mio controller, sarà correttamente caricare l'oggetto ContactModel utilizzando una mappatura diretta
public ActionResult Index()
{
//From the ContactController
Mapper.CreateMap<Contact, Models.ContactModel>();
Mapper.CreateMap<Address, Models.AddressModel>();
List<Models.ContactModel> theList = Mapper.Map<List<Contact>, List<Models.ContactModel>>(contacts);
return View(theList);
}
è quello che voglio fare anche possibile?
Temevo che questa sarebbe stata la risposta, grazie per l'esempio! –