【问题标题】:AutoMapping nested models自动映射嵌套模型
【发布时间】:2013-10-24 16:14:30
【问题描述】:

我正在尝试将模型从提供者项目automap 转换为 wcf 数据合同。但是,它们在原始(嵌套)中都有另一个模型/数据合同。例如:我们有一个客户端模型,包含姓名、电话号码、EIN 等信息……但是每个客户端可以有多个联系人(另一个模型)。我将如何使用流利的映射在自动映射器中映射它?以下是课程。


数据合同

客户端数据合约

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

联系数据合同

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

提供者模型

客户端模型

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

联系模型

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

【问题讨论】:

  • 而不是粘贴整个代码,只需使用一些小例子。很容易回答您的问题。
  • 只需映射这两个类,AutoMapper 就会为您映射嵌套列表。

标签: c# wcf automapper


【解决方案1】:

您必须为所有使用的类创建映射,以便将 Client 映射到 ClientDataContract

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

Wiki page 表示创建 Contact-to-ContactDataContract 映射就足够了,所有通用集合都将被映射。

【讨论】:

    猜你喜欢
    • 2021-01-20
    • 2014-09-26
    • 2013-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多