自动映射嵌套模型 [英] AutoMapping nested models

查看:62
本文介绍了自动映射嵌套模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自动映射从提供者项目到wcf数据合同的模型.但是,它们在原始文件(嵌套)中都有另一个模型/数据协定.例如:我们有一个客户模型,保存着诸如姓名,电话号码,EIN等信息.但是每个客户可以有多个联系人(另一个模型).我如何使用流利的映射在automapper中进行映射?下面是类.

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.

数据合同

客户数据合同

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

推荐答案

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

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页面说,足以创建联系人到-ContactDataContract映射和所有常规集合都将被映射.

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

这篇关于自动映射嵌套模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆