AutoMapper - 对象不同的名称空间

时间:2016-06-15 20:28:22

标签: c# automapper

我有一个场景,我们有两个服务引用一个名为SLInterface.dll的对象定义dll。这只是我们对象模型的汇编。这个对象很大,所以我会把它调下来并给出一段代码:

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

namespace SLInterface
{
    [DataContract(Namespace = Utilities.strNamespace)]
public class EntityShipment
{

    public EntityShipment()
    {
        lstEntityResponseStatus = new List<EntityResponseStatus>();
    }
    [DataMember]
    public EntityAddress objShipTo
    {
        get;
        set;
    }
    [DataMember]
    public EntityToolKit ToolKit { get; set; }
    [DataMember]
    public List<EntityResponseStatus> lstEntityResponseStatus
    {
        get;
        set;
    }

 }

}

namespace SLInterface
{
public class EntityAddress
{
    [DataMember]
    public String strCustomerID
    {
        get;
        set;
    }
    [DataMember]
    public String strCompanyName
    {
        get;
        set;
    }
    [DataMember]
    public String strContactName
    {
        get;
        set;
    }
    [DataMember]
    public String strAddressLine1
    {
        get;
        set;
    }
    [DataMember]
    public String strAddressLine2
    {
        get;
        set;
    }
    [DataMember]
    public String strAddressLine3
    {
        get;
        set;
    }
    [DataMember]
    public String strCity
    {
        get;
        set;
    }
    [DataMember]
    public String strState
    {
        get;
        set;
    }
    [DataMember]
    public String strPostalCode
    {
        get;
        set;
    }
    [DataMember]
    public String strCountryCode
    {
        get;
        set;
    }
    [DataMember]
    public String strCountryName
    {
        get;
        set;
    }
    [DataMember]
    public String strPhoneNumber
    {
        get;
        set;
    }
    [DataMember]
    public String strFaxNumber
    {
        get;
        set;
    }
    [DataMember]
    public Boolean blnRBDICheck
    {
        get;
        set;
    }
    [DataMember]
    public Boolean blnIsRBDI
    {
        get;
        set;
    }
    [DataMember]
    public String strPlantID
    {
        get;
        set;
    }
    [DataMember]
    public String strAccountNumber
    {
        get;
        set;
    }
    [DataMember]
    public String strEmailAddress
    {
        get;
        set;
    }
    [DataMember]
    public Boolean blnBlindShip
    {
        get;
        set;
    }
}
}

然后还有另一个工具包和EntityResponseStatus的定义,如果需要,我可以发布。

我们有一个场景,从ServiceA开始,我们需要将此对象传递给ServiceB。我们不能只传递在ServiceA期间填充的这个对象,因为它是一个不同的命名空间。因此,我们尝试使用AutoMapper,因此我们不必单独映射每个属性及其值。

这是我们的AutoMapperCode:

public class ES
{

    public ServiceA.SLInterface.EntityResponseStatus CallES(string strPrinterPath, string strShipmentNumber, string strCompanyCode)
    {

        Mapper.CreateMap<ServiceA.SLInterface.EntityShipment, ServiceB.SLInterface.EntityShipment>();

        ServiceA.SLInterface.EntityResponseStatus objERS = new ServiceA.SLInterface.EntityResponseStatus();
        objERS.StatusType = ServiceA.SLInterface.ResponseStatusType.SUCCESS;

        ServiceA.SLInterface.EntityShipment objES = new ServiceA.SLInterface.EntityShipment();

        objES = FillES(objES, strShipmentNumber, strCompanyCode);

        Sage300 obj300 = new Sage300();
        objES = obj300.Pull(objES);

         ServiceB.SLInterface.EntityShipment objESES = new ServiceB.SLInterface.EntityShipment();
        try
        {

            objESES = Mapper.Map<ServiceB.SLInterface.EntityShipment>(objES);  //This is the line that we blow up on.  The error message is "Missing type map configuration or unsupported mapping."
        }
        catch(AutoMapperConfigurationException amce)
        {
            ServiceA.SLInterface.EntityResponseStatus objERS1 = new ServiceA.SLInterface.EntityResponseStatus();
            objERS1.Message = amce.Message;
        }
        catch (AutoMapperMappingException amme)
        {
            ServiceA.SLInterface.EntityResponseStatus objERS1 = new ServiceA.SLInterface.EntityResponseStatus();
            objERS1.Message = amme.Message;
        }

        objESES = ServiceB.CallESShip(objESES,strPrinterPath);

        return objERS;
    }

try catch中我们收到错误消息 objESES = Mapper.Map<ServiceB.SLInterface.EntityShipment>(objES); //这就是我们爆炸的路线。错误消息为"Missing type map configuration or unsupported mapping."

两个对象模型完全相同。我们是否必须为主对象中的每个对象执行映射。现在,我们正在考虑我们所拥有的代码,它将从根目录映射到完全相同的所有内容,然后传输数据。任何帮助,将不胜感激。

谢谢!

0 个答案:

没有答案