Ika
Ika

Reputation: 211

Automapper - not mapping

I am making SOAP request and getting response. So in order send a request I am passing ContractLoad which I am mapping from ContractLoadDTO and it works just fine. When reading response I am trying to map response to DTO ContractLoadResult to ContractLoadDTO which is not getting mapped. I created two maps:

CreateMap<ContractLoadResult, ContractLoadResultDTO>(); // does not work
CreateMap<ContractLoadDTO, ContractLoad> (); // works

My classes:

 public class ContractLoadDTO
    {
        public int carrierId { get; set; }
        public int contractId { get; set; }
        public double amount { get; set; }
        public string refNum { get; set; }
    }  

And

 [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.7.3056.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://com.test.carriergroup.service/types")]
    public partial class ContractLoad {

        private int carrierIdField;

        private int contractIdField;

        private double amountField;

        private string refNumField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public int carrierId {
            get {
                return this.carrierIdField;
            }
            set {
                this.carrierIdField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public int contractId {
            get {
                return this.contractIdField;
            }
            set {
                this.contractIdField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public double amount {
            get {
                return this.amountField;
            }
            set {
                this.amountField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
        public string refNum {
            get {
                return this.refNumField;
            }
            set {
                this.refNumField = value;
            }
        }
    }

Those get mapped right and no issue but second one now working: Classes:

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.7.3056.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://com.test.carriergroup.service/types")]
public partial class ContractLoadResult {

    private int carrierIdField;

    private int contractIdField;

    private double balanceField;

    private int errorCodeField;

    private string stackTraceField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public int carrierId {
        get {
            return this.carrierIdField;
        }
        set {
            this.carrierIdField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public int contractId {
        get {
            return this.contractIdField;
        }
        set {
            this.contractIdField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public double balance {
        get {
            return this.balanceField;
        }
        set {
            this.balanceField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public int errorCode {
        get {
            return this.errorCodeField;
        }
        set {
            this.errorCodeField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
    public string stackTrace {
        get {
            return this.stackTraceField;
        }
        set {
            this.stackTraceField = value;
        }
    }
} 

And:

 public class ContractLoadResultDTO
    {
        public int carrierIdField { get; set; }

        public int contractIdField { get; set; }

        public double balanceField { get; set; }

        public int errorCodeField { get; set; }

        public string stackTraceField { get; set; }

    }

When passing values to Soap Requesting:

ContractLoad[] loads = new ContractLoad[ContractInfo.conractLoads.Length];
loads = Mapper.Map<ContractLoadDTO[], ContractLoad[]>(ContractInfo.conractLoads);

loads contain values from ContractInfo.conractLoads array.

While reading response:

ContractLoadResultDTO[] loadResult = new ContractLoadResultDTO[response.loadResults.Length];                        
loadResult = Mapper.Map<ContractLoadResult[], ContractLoadResultDTO[]>(response.loadResults);

Values in loadResult are "0". Am I doing anything wrong?

Upvotes: 1

Views: 139

Answers (1)

Josh Mein
Josh Mein

Reputation: 28625

AutoMapper expects the members within a class to have the exact same name as those in the class they are being mapped from by default. In your example that is not working, your DTO class has field added on to each member so it does not match the other class. Therefore, your DTO class should look like this unless you are going to manually map the variables:

public class ContractLoadResultDTO
{
    public int carrierId { get; set; }
    public int contractId { get; set; }
    public double balance { get; set; }
    public int errorCode { get; set; }
    public string stackTrace { get; set; }
}

Just an additional note to add context to those who have not used AutoMapper, although private variables exist here with the same name, AutoMapper cannot see them so it cannot copy their value.

Upvotes: 1

Related Questions