Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoMapper Ignore() Issue

Tags:

c#

automapper

Okay, I'm hoping I am just somehow overlooking the obvious. I have the following code situation below. For some reason, the SequenceNo property is still getting mapped even though I'm calling Ignore(). I'm using the latest. I also had tested it with two different classes in the same project and it seemed to work, so what's wrong with this scenario then?

This is the 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 when since I had set the Ignore() it should be 0.

like image 933
bjhuffine Avatar asked Jul 15 '26 10:07

bjhuffine


1 Answers

This is because AutoMapper is finding this CableIDDTO constructor:

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

and calling it, setting sequenceNo. I'm not exactly sure how or why it's doing that--i'll continue to dig.

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());

Upon further research, this looks like a feature in AutoMapper that tries to match up source property names with destination constructors. Since your destination type (CableIDDTO) had a constructor that perfectly matched up with several property names on the source (panelID1, panelID2, sequenceNo), that constructor was called.

Another way to disable this feature is to call DisableConstructorMapping:

Mapper.Configuration.DisableConstructorMapping()
like image 179
Andrew Whitaker Avatar answered Jul 17 '26 23:07

Andrew Whitaker