AutoMapping nested models

2019-09-13 07:05发布

问题:

I'm attempting to automap a model from a provider project to a wcf data contract. However they both have another model/data contract inside the original (Nested). For example: We have a client model, holds information such as name, phone number, EIN, etc... However each client can have multiple contacts (another model). How would I map this in automapper using the fluent mapping? below are the classes.


DataContracts

Client data contract

using System.Collections.Generic;
using System.Runtime.Serialization;

namespace DSP.NET.WholeSale.Service.DataContracts
{
    [DataContract]
    public class ClientDataContract
    {
        [DataMember]
        public int? Id { get; set; }

        [DataMember]
        public string FirstName { get; set; }
        [DataMember]
        public string LastName { get; set; }
        [DataMember]
        public string Organization { get; set; }
        [DataMember]
        public string Email { get; set; }

        [DataMember]
        public string UserName { get; set; }
        [DataMember]
        public string Password { get; set; }

        [DataMember]
        public List<ContactDataContract> Contacts { get; set; }

        [DataMember]
        public string WorkPhone { get; set; }
        [DataMember]
        public string HomePhone { get; set; }
        [DataMember]
        public string MobilePhone { get; set; }
        [DataMember]
        public string FaxNumber { get; set; }
        [DataMember]
        public string Language { get; set; }
        [DataMember]
        public string CurrencyCode { get; set; }
        [DataMember]
        public string Notes { get; set; }


        [DataMember]
        public AddressDataContract PrimaryAddress { get; set; }
        //public Address MailingAddress { get { return this.PrimaryAddress; } set { this.PrimaryAddress = value; } }
        //public Address PostalAddress { get { return this.PrimaryAddress; } set { this.PrimaryAddress = value; } }

        [DataMember]
        public AddressDataContract SecondaryAddress { get; set; }
        //public Address BillingAddress { get { return this.SecondaryAddress; } set { this.SecondaryAddress = value; } }

        [DataMember]
        public string VATName { get; set; }
        [DataMember]
        public int? VATNumber { get; set; }
    }
}

Contact data contract

using System.Runtime.Serialization;

namespace DSP.NET.WholeSale.Service.DataContracts
{
    [DataContract]
    public class ContactDataContract
    {
        [DataMember]
        public int Id { get; set; }

        [DataMember]
        public string FirstName { get; set; }
        [DataMember]
        public string LastName { get; set; }
        [DataMember]
        public string Organization { get; set; }

        [DataMember]
        public string Email { get; set; }
        [DataMember]
        public string UserName { get; set; }
        [DataMember]
        public string Password { get; set; }
        [DataMember]
        public string WorkPhone { get; set; }
        [DataMember]
        public string HomePhone { get; set; }
        [DataMember]
        public string MobilePhone { get; set; }
        [DataMember]
        public string FaxNumber { get; set; }
        // TODO: Language Code also
        // TODO: Currency Code
        [DataMember]
        public string Notes { get; set; }


        [DataMember]
        public AddressDataContract PrimaryAddress { get; set; }
        //public Address MailingAddress { get { return this.PrimaryAddress; } set { this.PrimaryAddress = value; } }
        //public Address PostalAddress { get { return this.PrimaryAddress; } set { this.PrimaryAddress = value; } }

        [DataMember]
        public AddressDataContract SecondaryAddress { get; set; }
        //public Address BillingAddress { get { return this.SecondaryAddress; } set { this.SecondaryAddress = value; } }

        [DataMember]
        public string VATName { get; set; }
        [DataMember]
        public int? VATNumber { get; set; }
    }
}

Provider Models

Client Model

public class Client
{
    public int? Id { get; set; }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Organization { get; set; }
    public string Email { get; set; }

    public string UserName { get; set; }
    public string Password { get; set; }

    public List<Contact> Contacts { get; set; }

    public string WorkPhone { get; set; }
    public string HomePhone { get; set; }
    public string MobilePhone { get; set; }
    public string FaxNumber { get; set; }
    public string Language { get; set; }
    public string CurrencyCode { get; set; }
    public string Notes { get; set; }


    public Address PrimaryAddress { get; set; }
    public Address MailingAddress { get { return this.PrimaryAddress; } set { this.PrimaryAddress = value; } }
    public Address PostalAddress { get { return this.PrimaryAddress; } set { this.PrimaryAddress = value; } }

    public Address SecondaryAddress { get; set; }
    public Address BillingAddress { get { return this.SecondaryAddress; } set { this.SecondaryAddress = value; } }

    public string VATName { get; set; }
    public int? VATNumber { get; set; }
}

Contact Model

public class Contact
{
    public int Id { get; set; }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Organization { get; set; }

    public string Email { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public string WorkPhone { get; set; }
    public string HomePhone { get; set; }
    public string MobilePhone { get; set; }
    public string FaxNumber { get; set; }
    // TODO: Language Code also
    // TODO: Currency Code
    public string Notes { get; set; }


    public Address PrimaryAddress { get; set; }
    public Address MailingAddress { get { return this.PrimaryAddress; } set { this.PrimaryAddress = value; } }
    public Address PostalAddress { get { return this.PrimaryAddress; } set { this.PrimaryAddress = value; } }

    public Address SecondaryAddress { get; set; }
    public Address BillingAddress { get { return this.SecondaryAddress; } set { this.SecondaryAddress = value; } }

    public string VATName { get; set; }
    public int? VATNumber { get; set; }
}

回答1:

You have to create mappings for all classes that are used in order to map Client into ClientDataContract

Mapper.CreateMap<Client, ClientDataContract>(); 
Mapper.CreateMap<Contact, ContactDataContract>();
Mapper.CreateMap<Address, AddressDataContract>();

Wiki page says that it is enough to create Contact-to-ContactDataContract mapping and all generic collections will be mapped.