Auto-technician error ()

Okay, I hope I'm just ignoring the obvious somehow. Following is the following situation with the code. For some reason, the SequenceNo property is still displayed even though I call Ignore (). I am using the latter. I also tested it with two different classes in the same project and it seems to work, so what happened to this scenario?

This is a domain object:

 public class CableID 
{
    private string _panelID1;
    public string PanelID1
    {
        get { return _panelID1; }
        private set { _panelID1 = value; }
    }

    private string _panelID2;
    public string PanelID2
    {
        get { return _panelID2; }
        private set { _panelID2 = value; }
    }

    private int _sequenceNo;
    public int SequenceNo
    {
        get { return _sequenceNo; }
        private set { _sequenceNo = value; }
    }

    private DateTime _inService;
    public DateTime InService
    {
        get { return _inService; }
        set { _inService = value; }
    }

    private string _id;
    public string ID
    {
        get { return _id; }
        private set { _id = value; }
    }

    public CableID(string panelID1, string panelID2, int sequenceNo)
    {
        this.PanelID1 = panelID1;
        this.PanelID2 = panelID2;
        this.SequenceNo = sequenceNo;
        this.ID = string.Format("({0}-{1}){2}", this.PanelID1, this.PanelID2, this.SequenceNo);
    }

    public CableID(string id)
    {
        if (string.IsNullOrEmpty(id))
            throw new ArgumentNullException("id");

        this.ID = id;
    }
}

      

And here is the DTO object:

public class CableIDDTO
{
    private string _panelID1;
    public string PanelID1
    {
        get { return _panelID1; }
        set { _panelID1 = value; }
    }

    private string _panelID2;
    public string PanelID2
    {
        get { return _panelID2; }
        set { _panelID2 = value; }
    }

    private int _sequenceNo;
    public int SequenceNo
    {
        get { return _sequenceNo; }
        set { _sequenceNo = value; }
    }

    private string _id;
    public string ID
    {
        get { return _id; }
        set { _id = value; }
    }

    public CableIDDTO()
    { }

    public CableIDDTO(string panelID1, string panelID2, int sequenceNo)
    {
        this.PanelID2 = panelID1;
        this.PanelID1 = panelID2;
        this.SequenceNo = sequenceNo;
        this.ID = string.Format("({0}-{1}){2}", this.PanelID2, this.PanelID1, this.SequenceNo);
    }
}

      

And finally, the AutoMapper use case:

       CableID cableID = new CableID("A1", "B1", 2);
        Mapper.CreateMap<CableID, CableIDDTO>()
              .ForMember(dest => dest.SequenceNo, opt => opt.Ignore());

        CableIDDTO dto = Mapper.Map<CableID, CableIDDTO>(cableID);

      

dto.SequenceNo = 2, since I set Ignore () it should be 0.

+3


source to share


1 answer


This is because AutoMapper finds this constructor CableIDDTO

:

public CableIDDTO(string panelID1, string panelID2, int sequenceNo)

      

and call it by setting sequenceNo

. I'm not entirely sure how or why he does it - I'll keep digging.

You can fix this by calling .ConstructUsing

and telling AutoMapper to use the no-args constructor:



Mapper.CreateMap<CableID, CableIDDTO>()
    .ConstructUsing((Func<CableID, CableIDDTO>)(src => new CableIDDTO()))
    .ForMember(dest => dest.SequenceNo, opt => opt.Ignore());

      

On further investigation, it looks like an AutoMapper function that tries to map source property names to destination constructors. Since your appointment type ( CableIDDTO

) was the designer, which is perfectly matched to several property names in the source ( panelID1

, panelID2

, sequenceNo

), the constructor has been called.

Another way to disable this feature is to call DisableConstructorMapping

:

Mapper.Configuration.DisableConstructorMapping()

      

+5


source







All Articles